Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Adventure 도메인에 해당하는 네트워크 작업 코루틴으로 변경 #315

Merged
merged 4 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.now.naaga.data.remote.dto.AdventureStatusDto
import com.now.naaga.data.remote.dto.CoordinateDto
import com.now.naaga.data.remote.dto.FinishGameDto
import com.now.naaga.data.remote.dto.HintDto
import retrofit2.Call
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.PATCH
Expand All @@ -16,49 +16,49 @@ import retrofit2.http.Query

interface AdventureService {
@GET("/games")
fun getMyGames(): Call<List<AdventureDto>>
suspend fun getMyGames(): Response<List<AdventureDto>>

@GET("/games/{gameId}")
fun getGame(
suspend fun getGame(
@Path("gameId") gameId: Long,
): Call<AdventureDto>
): Response<AdventureDto>

@GET("/games")
fun getGamesByStatus(
suspend fun getGamesByStatus(
@Query("status") status: String,
): Call<List<AdventureDto>>
): Response<List<AdventureDto>>

@POST("/games")
fun beginGame(
suspend fun beginGame(
@Body coordinateDto: CoordinateDto,
): Call<AdventureDto>
): Response<AdventureDto>

@PATCH("/games/{gameId}")
fun endGame(
suspend fun endGame(
@Path("gameId") gameId: Long,
@Body finishGameDto: FinishGameDto,
): Call<AdventureStatusDto>
): Response<AdventureStatusDto>

@GET("/games/{gameId}/result")
fun getGameResult(
suspend fun getGameResult(
@Path("gameId") gameId: Long,
): Call<AdventureResultDto>
): Response<AdventureResultDto>

@GET("/games/results")
fun getMyGameResults(
suspend fun getMyGameResults(
@Query("sort-by") sortBy: String,
@Query("order") order: String,
): Call<List<AdventureResultDto>>
): Response<List<AdventureResultDto>>

@GET("/games/{gameId}/hints/{hintId}")
fun getHint(
suspend fun getHint(
@Path("gameId") gameId: Long,
@Path("hintId") hingId: Long,
): Call<HintDto>
): Response<HintDto>

@POST("/games/{gameId}/hints")
fun requestHint(
suspend fun requestHint(
@Path("gameId") gameId: Long,
@Body coordinateDto: CoordinateDto,
): Call<HintDto>
): Response<HintDto>
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,127 +11,61 @@ import com.now.domain.model.SortType
import com.now.domain.repository.AdventureRepository
import com.now.naaga.data.mapper.toDomain
import com.now.naaga.data.mapper.toDto
import com.now.naaga.data.remote.dto.AdventureDto
import com.now.naaga.data.remote.dto.FinishGameDto
import com.now.naaga.data.remote.retrofit.ServicePool
import com.now.naaga.data.remote.retrofit.fetchResponse
import com.now.naaga.util.getValueOrThrow

class DefaultAdventureRepository : AdventureRepository {
override fun fetchMyAdventures(callback: (Result<List<Adventure>>) -> Unit) {
val call = ServicePool.adventureService.getMyGames()
call.fetchResponse(
onSuccess = { adventureDtos: List<AdventureDto> ->
callback(Result.success(adventureDtos.map { it.toDomain() }))
},
onFailure = {
callback(Result.failure(it))
},
)
override suspend fun fetchMyAdventures(): List<Adventure> {
val response = ServicePool.adventureService.getMyGames().getValueOrThrow()
return response.map { it.toDomain() }
}

override fun fetchAdventure(adventureId: Long, callback: (Result<Adventure>) -> Unit) {
val call = ServicePool.adventureService.getGame(adventureId)
call.fetchResponse(
onSuccess = { adventureDto ->
callback(Result.success(adventureDto.toDomain()))
},
onFailure = {
callback(Result.failure(it))
},
)
override suspend fun fetchAdventure(adventureId: Long): Adventure {
val response = ServicePool.adventureService.getGame(adventureId).getValueOrThrow()
return response.toDomain()
}

override fun fetchAdventureByStatus(status: AdventureStatus, callback: (Result<List<Adventure>>) -> Unit) {
val call = ServicePool.adventureService.getGamesByStatus(status.name)
call.fetchResponse(
onSuccess = { adventureDtos: List<AdventureDto> ->
callback(Result.success(adventureDtos.map { it.toDomain() }))
},
onFailure = {
callback(Result.failure(it))
},
)
override suspend fun fetchAdventureByStatus(status: AdventureStatus): List<Adventure> {
val response = ServicePool.adventureService.getGamesByStatus(status.name).getValueOrThrow()
return response.map { it.toDomain() }
}

override fun beginAdventure(coordinate: Coordinate, callback: (Result<Adventure>) -> Unit) {
val call = ServicePool.adventureService.beginGame(coordinate.toDto())
call.fetchResponse(
onSuccess = { adventureDto ->
callback(Result.success(adventureDto.toDomain()))
},
onFailure = {
callback(Result.failure(it))
},
)
override suspend fun beginAdventure(coordinate: Coordinate): Adventure {
val response = ServicePool.adventureService.beginGame(coordinate.toDto()).getValueOrThrow()
return response.toDomain()
}

override fun endGame(
override suspend fun endGame(
adventureId: Long,
endType: AdventureEndType,
coordinate: Coordinate,
callback: (Result<AdventureStatus>) -> Unit,
) {
): AdventureStatus {
val finishGameDto = FinishGameDto(endType.name, coordinate.toDto())
val call = ServicePool.adventureService.endGame(adventureId, finishGameDto)
call.fetchResponse(
onSuccess = { adventureStatusDto ->
callback(Result.success(AdventureStatus.getStatus(adventureStatusDto.gameStatus)))
},
onFailure = {
callback(Result.failure(it))
},
)
val response = ServicePool.adventureService.endGame(adventureId, finishGameDto).getValueOrThrow()
return AdventureStatus.getStatus(response.gameStatus)
}

override fun fetchAdventureResult(adventureId: Long, callback: (Result<AdventureResult>) -> Unit) {
val call = ServicePool.adventureService.getGameResult(adventureId)
call.fetchResponse(
onSuccess = { adventureResultDto ->
callback(Result.success(adventureResultDto.toDomain()))
},
onFailure = {
callback(Result.failure(it))
},
)
override suspend fun fetchAdventureResult(adventureId: Long): AdventureResult {
val response = ServicePool.adventureService.getGameResult(adventureId).getValueOrThrow()
return response.toDomain()
}

override fun fetchMyAdventureResults(
override suspend fun fetchMyAdventureResults(
sortBy: SortType,
order: OrderType,
callback: (Result<List<AdventureResult>>) -> Unit,
) {
val call = ServicePool.adventureService.getMyGameResults(sortBy.name, order.name)
call.fetchResponse(
onSuccess = { adventureResultDtos ->
callback(Result.success(adventureResultDtos.map { it.toDomain() }))
},
onFailure = {
callback(Result.failure(it))
},
)
): List<AdventureResult> {
val response = ServicePool.adventureService.getMyGameResults(sortBy.name, order.name).getValueOrThrow()
return response.map { it.toDomain() }
}

override fun fetchHint(adventureId: Long, hintId: Long, callback: (Result<Hint>) -> Unit) {
val call = ServicePool.adventureService.getHint(adventureId, hintId)
call.fetchResponse(
onSuccess = { hintDto ->
callback(Result.success(hintDto.toDomain()))
},
onFailure = {
callback(Result.failure(it))
},
)
override suspend fun fetchHint(adventureId: Long, hintId: Long): Hint {
val response = ServicePool.adventureService.getHint(adventureId, hintId).getValueOrThrow()
return response.toDomain()
}

override fun makeHint(adventureId: Long, coordinate: Coordinate, callback: (Result<Hint>) -> Unit) {
val call = ServicePool.adventureService.requestHint(adventureId, coordinate.toDto())
call.fetchResponse(
onSuccess = { hintDto ->
callback(Result.success(hintDto.toDomain()))
},
onFailure = {
callback(Result.failure(it))
},
)
override suspend fun makeHint(adventureId: Long, coordinate: Coordinate): Hint {
val response = ServicePool.adventureService.requestHint(adventureId, coordinate.toDto()).getValueOrThrow()
return response.toDomain()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import com.now.domain.model.AdventureResult
import com.now.domain.model.OrderType
import com.now.domain.model.SortType
import com.now.domain.repository.AdventureRepository
import com.now.naaga.data.repository.DefaultAdventureRepository
import com.now.naaga.data.throwable.DataThrowable
import com.now.naaga.data.throwable.DataThrowable.PlayerThrowable
import kotlinx.coroutines.launch

class AdventureHistoryViewModel(private val adventureRepository: AdventureRepository) : ViewModel() {
private val _adventureResults = MutableLiveData<List<AdventureResult>>()
Expand All @@ -20,16 +22,23 @@ class AdventureHistoryViewModel(private val adventureRepository: AdventureReposi
val errorMessage: LiveData<String> = _errorMessage

fun fetchHistories() {
adventureRepository.fetchMyAdventureResults(SortType.TIME, OrderType.DESCENDING) { result ->
result
.onSuccess { _adventureResults.value = it }
.onFailure { setErrorMessage(it as DataThrowable) }
viewModelScope.launch {
runCatching {
adventureRepository.fetchMyAdventureResults(SortType.TIME, OrderType.DESCENDING)
}.onSuccess { results: List<AdventureResult> ->
_adventureResults.value = results
}.onFailure {
setErrorMessage(it as DataThrowable)
}
}
}

private fun setErrorMessage(throwable: DataThrowable) {
when (throwable) {
is PlayerThrowable -> { _errorMessage.value = throwable.message }
is PlayerThrowable -> {
_errorMessage.value = throwable.message
}

else -> {}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ class AdventureResultViewModel(
val throwable: LiveData<DataThrowable> = _throwable

fun fetchGameResult(adventureId: Long) {
adventureRepository.fetchAdventureResult(
adventureId,
callback = { result ->
result
.onSuccess { adventureResult -> _adventureResult.value = adventureResult }
.onFailure { setErrorMessage(it as DataThrowable) }
},
)
viewModelScope.launch {
runCatching {
adventureRepository.fetchAdventureResult(adventureId)
}.onSuccess { adventureResult ->
_adventureResult.value = adventureResult
}.onFailure {
setErrorMessage(it as DataThrowable)
}
}
}

fun fetchMyRank() {
Expand All @@ -53,7 +54,10 @@ class AdventureResultViewModel(

private fun setErrorMessage(throwable: DataThrowable) {
when (throwable) {
is GameThrowable -> { _throwable.value = throwable }
is GameThrowable -> {
_throwable.value = throwable
}

else -> {}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import com.now.domain.model.Adventure
import com.now.domain.model.AdventureStatus
import com.now.domain.repository.AdventureRepository
import com.now.naaga.data.repository.DefaultAdventureRepository
import com.now.naaga.data.throwable.DataThrowable
import kotlinx.coroutines.launch

class BeginAdventureViewModel(private val adventureRepository: AdventureRepository) : ViewModel() {
private val _adventure = MutableLiveData<Adventure>()
Expand All @@ -22,15 +24,15 @@ class BeginAdventureViewModel(private val adventureRepository: AdventureReposito

fun fetchAdventure(adventureStatus: AdventureStatus) {
_loading.value = true
adventureRepository.fetchAdventureByStatus(adventureStatus) { result ->
_loading.value = false
result
.onSuccess {
_adventure.value = it.firstOrNull()
}
.onFailure {
_error.value = it as DataThrowable
}
viewModelScope.launch {
runCatching {
adventureRepository.fetchAdventureByStatus(adventureStatus)
}.onSuccess {
_loading.value = false
_adventure.value = it.firstOrNull()
}.onFailure {
_error.value = it as DataThrowable
}
}
}

Expand Down
Loading
Loading