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

[AN] feat: 모험이 끝났을 때 서버에게 모험이 종료 됐음을 알려주는 기능 구현 #53

Merged
merged 7 commits into from
Jul 20, 2023
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ package com.now.naaga.data.mapper
import com.now.domain.model.Adventure
import com.now.domain.model.AdventureStatus
import com.now.naaga.data.remote.dto.AdventureDto
import com.now.naaga.data.remote.dto.EndedAdventureDto

fun AdventureDto.toDomain(): Adventure {
return Adventure(
@@ -11,3 +12,7 @@ fun AdventureDto.toDomain(): Adventure {
adventureStatus = AdventureStatus.getStatus(adventureStatus),
)
}

fun EndedAdventureDto.toDomain(): AdventureStatus {
return AdventureStatus.getStatus(adventureStatus)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.now.naaga.data.remote.dto

import kotlinx.serialization.SerialName

data class EndedAdventureDto(
val id: Long,
@SerialName("gameStatus")
val adventureStatus: String,
)
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ package com.now.naaga.data.remote.retrofit.service

import com.now.naaga.data.remote.dto.AdventureDto
import com.now.naaga.data.remote.dto.CoordinateDto
import com.now.naaga.data.remote.dto.EndedAdventureDto
import retrofit2.Call
import retrofit2.http.Body
import retrofit2.http.GET
@@ -24,5 +25,5 @@ interface AdventureService {
fun endGame(
@Path("gameId") gameId: Long,
@Body coordinateDto: CoordinateDto,
): Call<Unit>
): Call<EndedAdventureDto>
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.now.naaga.data.repository

import com.now.domain.model.Adventure
import com.now.domain.model.AdventureStatus
import com.now.domain.model.Coordinate
import com.now.domain.repository.AdventureRepository
import com.now.naaga.data.NaagaThrowable
import com.now.naaga.data.mapper.toDomain
import com.now.naaga.data.mapper.toDto
import com.now.naaga.data.remote.dto.EndedAdventureDto
import com.now.naaga.data.remote.retrofit.ERROR_500
import com.now.naaga.data.remote.retrofit.ERROR_NOT_400_500
import com.now.naaga.data.remote.retrofit.ServicePool
import com.now.naaga.data.remote.retrofit.ServicePool.adventureService
import com.now.naaga.data.remote.retrofit.fetchNaagaResponse
import com.now.naaga.data.remote.retrofit.getFailureDto
import com.now.naaga.data.remote.retrofit.isFailure400
import com.now.naaga.data.remote.retrofit.isFailure500
@@ -54,8 +59,16 @@ class DefaultAdventureRepository : AdventureRepository {
override fun endAdventure(
adventureId: Long,
coordinate: Coordinate,
callback: (Result<Unit>) -> Unit,
callback: (Result<AdventureStatus>) -> Unit,
) {
// TODO("Not yet implemented")
val call: Call<EndedAdventureDto> = adventureService.endGame(adventureId, coordinate.toDto())
call.fetchNaagaResponse(
{ AdventureEndDto ->
if (AdventureEndDto != null) {
callback(Result.success(AdventureEndDto.toDomain()))
}
},
{ callback(Result.failure(NaagaThrowable.ServerConnectFailure())) },
)
}
}
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ package com.now.naaga.data.repository

import com.now.domain.model.Adventure
import com.now.domain.model.AdventureStatus
import com.now.domain.model.AdventureStatus.DONE
import com.now.domain.model.Coordinate
import com.now.domain.model.Destination
import com.now.domain.repository.AdventureRepository
@@ -22,9 +23,9 @@ class MockAdventureRepository : AdventureRepository {
override fun endAdventure(
adventureId: Long,
coordinate: Coordinate,
callback: (Result<Unit>) -> Unit,
callback: (Result<AdventureStatus>) -> Unit,
) {
TODO("Not yet implemented")
callback(Result.success(DONE))
}

companion object {
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.now.naaga.presentation.onadventure

import android.content.Intent
import android.location.Location
import android.os.Bundle
import android.widget.Toast
@@ -19,6 +20,7 @@ import com.now.domain.model.Coordinate
import com.now.naaga.R
import com.now.naaga.data.repository.DefaultAdventureRepository
import com.now.naaga.databinding.ActivityOnAdventureBinding
import com.now.naaga.presentation.beginadventure.BeginAdventureActivity
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
@@ -82,6 +84,7 @@ class OnAdventureActivity : AppCompatActivity(), OnMapReadyCallback {
naverMap.addOnLocationChangeListener { location ->
viewModel.calculateDistance(Coordinate(location.latitude, location.longitude))
viewModel.checkArrived(Coordinate(location.latitude, location.longitude))
setBinding(Coordinate(location.latitude, location.longitude))
}
}

@@ -95,12 +98,17 @@ class OnAdventureActivity : AppCompatActivity(), OnMapReadyCallback {
naverMap.locationTrackingMode = LocationTrackingMode.Follow
}

private fun setBinding(coordinate: Coordinate) {
binding.currentMyCoordinate = coordinate
}

private fun startObserving() {
viewModel.destination.observe(this) { destination ->
addMarker(destination.coordinate)
}
viewModel.status.observe(this) { status ->
stopAdventure(status)
onStatusChanged(status)
}
viewModel.adventureId.observe(this) { adventureId ->
viewModel.fetchDestination(adventureId)
@@ -143,6 +151,20 @@ class OnAdventureActivity : AppCompatActivity(), OnMapReadyCallback {
}
}

private fun onStatusChanged(status: AdventureStatus) {
when (status) {
AdventureStatus.DONE -> {
Toast.makeText(this, getString(R.string.onAdventure_adventure_success), Toast.LENGTH_LONG).show()
startActivity(Intent(this, BeginAdventureActivity::class.java))
finish()
}
AdventureStatus.IN_PROGRESS -> {
Toast.makeText(this, getString(R.string.onAdventure_retry), Toast.LENGTH_LONG).show()
}
AdventureStatus.ERROR -> { stopAdventure(status) }
}
}

companion object {
private const val LOCATION_PERMISSION_REQUEST_CODE = 1000
private const val DESTINATION_PHOTO = "DESTINATION_PHOTO"
Original file line number Diff line number Diff line change
@@ -76,4 +76,29 @@ class OnAdventureViewModel(
}
}
}

fun endAdventure(adventureId: Long, coordinate: Coordinate) {
adventureRepository.endAdventure(adventureId, coordinate, callback = { result ->
result
.onSuccess { adventureStatus -> _status.value = adventureStatus }
.onFailure { throwable ->
when (throwable) {
is NaagaThrowable.AuthenticationError ->
_errorMessage.value =
throwable.userMessage

is NaagaThrowable.UserError -> _errorMessage.value = throwable.userMessage
is NaagaThrowable.PlaceError -> _errorMessage.value = throwable.userMessage
is NaagaThrowable.GameError -> _errorMessage.value = throwable.userMessage
is NaagaThrowable.ServerConnectFailure ->
_errorMessage.value =
throwable.userMessage

is NaagaThrowable.NaagaUnknownError ->
_errorMessage.value =
throwable.userMessage
}
}
})
}
}
5 changes: 5 additions & 0 deletions android/app/src/main/res/layout/activity_on_adventure.xml
Original file line number Diff line number Diff line change
@@ -8,6 +8,10 @@
<variable
name="viewModel"
type="com.now.naaga.presentation.onadventure.OnAdventureViewModel" />

<variable
name="CurrentMyCoordinate"
type="com.now.domain.model.Coordinate" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
@@ -34,6 +38,7 @@
android:background="@drawable/rect_radius_small"
android:backgroundTint="@color/selector_main_gray_purple"
android:enabled="@{viewModel.isArrived}"
android:onClick="@{() -> viewModel.endAdventure(viewModel.adventureId, CurrentMyCoordinate)}"
android:fontFamily="@font/pretendard_bold"
android:paddingHorizontal="28dp"
android:paddingVertical="@dimen/space_default_medium"
2 changes: 2 additions & 0 deletions android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -12,4 +12,6 @@
<string name="on_adventure">모험 중</string>
<string name="end_adventure">모험 종료</string>
<string name="polaroid_description">이곳은 어디일까요?</string>
<string name="onAdventure_retry">아직 도착하지 못했어요.. 다시 도전해봐요!</string>
<string name="onAdventure_adventure_success">목적지를 찾는데 성공했어요!!</string>
</resources>
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.now.domain.repository

import com.now.domain.model.Adventure
import com.now.domain.model.AdventureStatus
import com.now.domain.model.Coordinate

interface AdventureRepository {
fun beginAdventure(coordinate: Coordinate, callback: (Result<Long>) -> Unit)

fun getAdventure(adventureId: Long, callback: (Result<Adventure>) -> Unit)

fun endAdventure(adventureId: Long, coordinate: Coordinate, callback: (Result<Unit>) -> Unit)
fun endAdventure(
adventureId: Long,
coordinate: Coordinate,
callback: (Result<AdventureStatus>) -> Unit,
)
}