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] 놀이터 기능 데모데이에서 발생한 오류 수정 #724

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -68,6 +68,7 @@ import com.happy.friendogly.presentation.ui.playground.action.PlaygroundMapActio
import com.happy.friendogly.presentation.ui.playground.action.PlaygroundMapAction.RegisterMyPlayground
import com.happy.friendogly.presentation.ui.playground.action.PlaygroundMapAction.ShowRegisteringPlaygroundScreen
import com.happy.friendogly.presentation.ui.playground.action.PlaygroundMapAction.StartLocationService
import com.happy.friendogly.presentation.ui.playground.action.PlaygroundMapAction.UpdateLocationService
import com.happy.friendogly.presentation.ui.playground.action.PlaygroundNavigateAction.NavigateToOtherProfile
import com.happy.friendogly.presentation.ui.playground.action.PlaygroundNavigateAction.NavigateToPetImage
import com.happy.friendogly.presentation.ui.playground.action.PlaygroundNavigateAction.NavigateToStateMessage
Expand All @@ -77,6 +78,7 @@ import com.happy.friendogly.presentation.ui.playground.model.Playground
import com.happy.friendogly.presentation.ui.playground.service.PlaygroundLocationReceiver
import com.happy.friendogly.presentation.ui.playground.service.PlaygroundLocationService
import com.happy.friendogly.presentation.ui.playground.service.PlaygroundLocationService.Companion.ACTION_START
import com.happy.friendogly.presentation.ui.playground.service.PlaygroundLocationService.Companion.ACTION_UPDATE
import com.happy.friendogly.presentation.ui.playground.state.PlaygroundUiState
import com.happy.friendogly.presentation.ui.playground.uimodel.PlaygroundUiModel
import com.happy.friendogly.presentation.ui.playground.util.ANIMATE_DURATION_MILLIS
Expand Down Expand Up @@ -124,7 +126,7 @@ class PlaygroundFragment : Fragment(), OnMapReadyCallback {
private lateinit var latLng: LatLng
private lateinit var locationPermission: LocationPermission
private lateinit var onBackPressedCallback: OnBackPressedCallback
private lateinit var playgroundReceiver: PlaygroundLocationReceiver
private lateinit var locationReceiver: PlaygroundLocationReceiver
private lateinit var activityResultLauncher: ActivityResultLauncher<Intent>

private val mapView: MapView by lazy { binding.mapView }
Expand Down Expand Up @@ -233,9 +235,8 @@ class PlaygroundFragment : Fragment(), OnMapReadyCallback {

override fun onDestroy() {
super.onDestroy()
viewModel.leavePlayground()
stopLocationService()
requireContext().unregisterReceiver(playgroundReceiver)
requireContext().unregisterReceiver(locationReceiver)
}

override fun onDetach() {
Expand Down Expand Up @@ -344,7 +345,6 @@ class PlaygroundFragment : Fragment(), OnMapReadyCallback {
viewModel.playgroundInfo.observe(viewLifecycleOwner) { playgroundInfo ->
if (playgroundInfo != null) {
petDetailAdapter.submitList(playgroundInfo.petDetails)
// changeBottomSheetBehavior()
}
}

Expand All @@ -355,9 +355,10 @@ class PlaygroundFragment : Fragment(), OnMapReadyCallback {
}

viewModel.myPlayground.observe(viewLifecycleOwner) { myPlayground ->
stopLocationService()
if (myPlayground != null) {
viewModel.updatePlaygroundArrival(latLng)
} else {
stopLocationService()
}
}

Expand Down Expand Up @@ -401,6 +402,8 @@ class PlaygroundFragment : Fragment(), OnMapReadyCallback {
}

is StartLocationService -> startLocationService()

is UpdateLocationService -> updateLocationService()
}
}

Expand Down Expand Up @@ -454,20 +457,20 @@ class PlaygroundFragment : Fragment(), OnMapReadyCallback {
}

private fun setupBroadCastReceiver() {
playgroundReceiver = PlaygroundLocationReceiver(::updateLocation, ::leavePlayground)
locationReceiver = PlaygroundLocationReceiver(::updateLocation, ::leavePlayground)
val intentFilter =
IntentFilter().apply {
addAction(PlaygroundLocationReceiver.ACTION_UPDATE_LOCATION)
addAction(PlaygroundLocationReceiver.ACTION_LEAVE_PLAYGROUND)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
requireContext().registerReceiver(
playgroundReceiver,
locationReceiver,
intentFilter,
RECEIVER_EXPORTED,
)
} else {
requireContext().registerReceiver(playgroundReceiver, intentFilter)
requireContext().registerReceiver(locationReceiver, intentFilter)
}
}

Expand Down Expand Up @@ -681,6 +684,15 @@ class PlaygroundFragment : Fragment(), OnMapReadyCallback {
requireContext().startForegroundService(intent)
}

private fun updateLocationService() {
val myPlayStatus = viewModel.myPlayStatus.value ?: return
val intent =
PlaygroundLocationService.getIntent(requireContext(), myPlayStatus).apply {
action = ACTION_UPDATE
}
requireContext().startForegroundService(intent)
}

private fun updateLocation(location: Location) {
latLng = LatLng(location.latitude, location.longitude)
viewModel.monitorDistanceAndManagePlayStatus(latLng)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ sealed interface PlaygroundMapAction {
data object ChangeTrackingMode : PlaygroundMapAction

data object StartLocationService : PlaygroundMapAction

data object UpdateLocationService : PlaygroundMapAction
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@ class PlaygroundLocationService : Service() {
flags: Int,
startId: Int,
): Int {
val playStatus = intent?.getSerializableExtra(EXTRA_PLAY_STATUS) as? PlayStatus
val playStatusTitle = convertPlayStatusToTitle(playStatus)
when (intent?.action) {
ACTION_START -> {
val playStatus = intent.getSerializableExtra(EXTRA_PLAY_STATUS) as? PlayStatus
val playStatusTitle = convertPlayStatusToTitle(playStatus)
startForegroundService(playStatusTitle)
}

ACTION_UPDATE -> {
updateNotification(playStatusTitle)
}

ACTION_STOP -> {
sendLeavePlaygroundBroadcast()
stopSelf()
Expand Down Expand Up @@ -125,6 +129,13 @@ class PlaygroundLocationService : Service() {
.build()
}

private fun updateNotification(playStatusTitle: String) {
val notification = createNotification(playStatusTitle)
val notificationManager =
getSystemService(NotificationManager::class.java) as NotificationManager
notificationManager.notify(SERVICE_ID, notification)
}

private fun sendLocationUpdateBroadcast() {
val intent =
Intent(ACTION_UPDATE_LOCATION).apply {
Expand Down Expand Up @@ -153,6 +164,7 @@ class PlaygroundLocationService : Service() {
private const val SERVICE_ID = 1

const val ACTION_START = "PLAY"
const val ACTION_UPDATE = "UPDATE"
const val ACTION_STOP = "STOP"

fun getIntent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import com.happy.friendogly.presentation.ui.playground.action.PlaygroundMapActio
import com.happy.friendogly.presentation.ui.playground.action.PlaygroundMapAction.RegisterMyPlayground
import com.happy.friendogly.presentation.ui.playground.action.PlaygroundMapAction.ShowRegisteringPlaygroundScreen
import com.happy.friendogly.presentation.ui.playground.action.PlaygroundMapAction.StartLocationService
import com.happy.friendogly.presentation.ui.playground.action.PlaygroundMapAction.UpdateLocationService
import com.happy.friendogly.presentation.ui.playground.action.PlaygroundNavigateAction
import com.happy.friendogly.presentation.ui.playground.action.PlaygroundNavigateAction.NavigateToOtherProfile
import com.happy.friendogly.presentation.ui.playground.action.PlaygroundNavigateAction.NavigateToPetImage
Expand Down Expand Up @@ -539,6 +540,11 @@ class PlaygroundViewModel
}

fun handlePlaygroundInfo(id: Long) {
val currentState = uiState.value ?: return
if (currentState is PlaygroundUiState.RegisteringPlayground) {
currentState.circleOverlay.map = null
}

if (id == myPlayground.value?.id) {
loadPlaygroundInfo(id = id)
} else {
Expand Down Expand Up @@ -568,6 +574,7 @@ class PlaygroundViewModel
if (previousPlayStatus != currentPlayStatus) {
val myPlayground = myPlayground.value ?: return@launch
loadPlaygroundInfo(myPlayground.id)
_mapAction.value = UpdateLocationService
}

if (previousPlayStatus == PlayStatus.NO_PLAYGROUND) {
Expand Down
Loading