Skip to content

Commit

Permalink
fix: 스타카토 생성, 삭제 후 지도 화면의 마커가 갱신되지 않는 오류 해결 #326 (#330)
Browse files Browse the repository at this point in the history
* refactor: 지도 화면 onResume에서 스타카토 목록 load

* refactor: 스타카토 목록 로드 메서드명 변경

- 이전: loadMoments
- 이후: loadStaccatos

* feat: 공유 view model에 스타카토 목록 업데이트 상태 추가

* fix: 스타카토 생성, 삭제 후 지도 화면의 마커가 갱신되지 않는 오류 해결

* feat: 지도 로드 시 현위치로 이동하도록 구현
  • Loading branch information
hxeyexn authored Aug 22, 2024
1 parent 4365dd5 commit a1a00f7
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ class SharedViewModel : ViewModel() {
val isTimelineUpdated: SingleLiveData<Boolean>
get() = _isTimelineUpdated

private val _isStaccatosUpdated = MutableSingleLiveData(false)
val isStaccatosUpdated: SingleLiveData<Boolean> get() = _isStaccatosUpdated

fun setTimelineHasUpdated() {
_isTimelineUpdated.setValue(true)
}

fun setStaccatosHasUpdated() {
_isStaccatosUpdated.setValue(true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.woowacourse.staccato.presentation.maps
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.location.Location
import android.net.Uri
import android.os.Bundle
import android.provider.Settings
Expand All @@ -13,9 +14,13 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.navigation.NavOptions
import androidx.navigation.fragment.findNavController
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationRequest
import com.google.android.gms.location.LocationServices
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
Expand All @@ -30,6 +35,7 @@ import com.woowacourse.staccato.data.StaccatoClient.momentApiService
import com.woowacourse.staccato.data.moment.MomentDefaultRepository
import com.woowacourse.staccato.data.moment.MomentRemoteDataSource
import com.woowacourse.staccato.domain.model.MomentLocation
import com.woowacourse.staccato.presentation.main.SharedViewModel
import com.woowacourse.staccato.presentation.maps.model.MarkerUiModel
import com.woowacourse.staccato.presentation.moment.MomentFragment.Companion.MOMENT_ID_KEY

Expand All @@ -41,12 +47,13 @@ class MapsFragment : Fragment() {
),
)
}
private val sharedViewModel: SharedViewModel by activityViewModels<SharedViewModel>()
private lateinit var fusedLocationProviderClient: FusedLocationProviderClient

private val mapReadyCallback =
OnMapReadyCallback { googleMap ->
checkLocationPermissions(googleMap)
observeMomentLocations(googleMap)
moveCamera(googleMap)
onMarkerClicked(googleMap)
}

Expand All @@ -63,6 +70,7 @@ class MapsFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?,
): View? {
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(requireActivity())
return inflater.inflate(R.layout.fragment_maps, container, false)
}

Expand All @@ -74,6 +82,12 @@ class MapsFragment : Fragment() {
val mapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
mapFragment?.getMapAsync(mapReadyCallback)
observeStaccatoId()
observeDeletedStaccato()
}

override fun onResume() {
super.onResume()
viewModel.loadStaccatos()
}

private fun checkLocationPermissions(googleMap: GoogleMap) {
Expand All @@ -91,6 +105,14 @@ class MapsFragment : Fragment() {

if (isAccessFineLocationGranted || isAccessCoarseLocationGranted) {
googleMap.isMyLocationEnabled = true
val currentLocation =
fusedLocationProviderClient.getCurrentLocation(
LocationRequest.PRIORITY_HIGH_ACCURACY,
null,
)
currentLocation.addOnSuccessListener { location ->
moveCamera(googleMap, location)
}
return
} else {
requestPermission.launch(locationPermissions)
Expand Down Expand Up @@ -126,6 +148,7 @@ class MapsFragment : Fragment() {

private fun observeMomentLocations(googleMap: GoogleMap) {
viewModel.momentLocations.observe(viewLifecycleOwner) { momentLocations ->
googleMap.clear()
addMarkers(momentLocations, googleMap)
}
}
Expand All @@ -145,9 +168,12 @@ class MapsFragment : Fragment() {
viewModel.setMarkers(markers)
}

private fun moveCamera(googleMap: GoogleMap) {
val woowacourse = LatLng(37.5057434, 127.0506698)
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(woowacourse, 15f))
private fun moveCamera(
googleMap: GoogleMap,
location: Location,
) {
val currentLocation = LatLng(location.latitude, location.longitude)
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLocation, 15f))
}

private fun onMarkerClicked(googleMap: GoogleMap) {
Expand Down Expand Up @@ -177,6 +203,14 @@ class MapsFragment : Fragment() {
findNavController().navigate(R.id.momentFragment, bundle, navOptions)
}

private fun observeDeletedStaccato() {
sharedViewModel.isStaccatosUpdated.observe(viewLifecycleOwner) { isDeleted ->
if (isDeleted) {
viewModel.loadStaccatos()
}
}
}

companion object {
const val PACKAGE_SCHEME = "package"
const val BOTTOM_SHEET_STATE_REQUEST_KEY = "requestKey"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import com.woowacourse.staccato.presentation.common.MutableSingleLiveData
import com.woowacourse.staccato.presentation.common.SingleLiveData
import com.woowacourse.staccato.presentation.maps.model.MarkerUiModel
import kotlinx.coroutines.launch
import java.lang.IllegalArgumentException

class MapsViewModel(
private val momentRepository: MomentRepository,
Expand All @@ -31,10 +30,6 @@ class MapsViewModel(
private val _staccatoId = MutableLiveData<Long>()
val staccatoId: LiveData<Long> get() = _staccatoId

init {
loadMoments()
}

fun setMarkers(markers: List<MarkerUiModel>) {
_markers.value = markers
}
Expand All @@ -44,7 +39,7 @@ class MapsViewModel(
_staccatoId.value = markers.first { it.markerId == markerId }.staccatoId
}

private fun loadMoments() {
fun loadStaccatos() {
viewModelScope.launch {
val result = momentRepository.getMoments()
result.onSuccess(::setMomentLocations)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ class MemoryFragment :
val bundle =
bundleOf(
MOMENT_ID_KEY to visitId,
// MEMORY_ID_KEY to memoryId,
// MEMORY_TITLE_KEY to it.title,
)
findNavController().navigate(R.id.action_memoryFragment_to_momentFragment, bundle)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.woowacourse.staccato.presentation.moment
import android.os.Bundle
import android.view.View
import androidx.core.os.bundleOf
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.google.android.material.tabs.TabLayoutMediator
Expand All @@ -11,6 +12,7 @@ import com.woowacourse.staccato.databinding.FragmentMomentBinding
import com.woowacourse.staccato.presentation.base.BindingFragment
import com.woowacourse.staccato.presentation.common.DeleteDialogFragment
import com.woowacourse.staccato.presentation.main.MainActivity
import com.woowacourse.staccato.presentation.main.SharedViewModel
import com.woowacourse.staccato.presentation.moment.comments.MomentCommentsFragment
import com.woowacourse.staccato.presentation.moment.detail.ViewpagePhotoAdapter
import com.woowacourse.staccato.presentation.moment.feeling.MomentFeelingSelectionFragment
Expand All @@ -25,6 +27,7 @@ class MomentFragment :
private val momentViewModel: MomentViewModel by viewModels { MomentViewModelFactory() }
private var momentId by Delegates.notNull<Long>()
private lateinit var pagePhotoAdapter: ViewpagePhotoAdapter
private val sharedViewModel: SharedViewModel by activityViewModels<SharedViewModel>()
private val deleteDialog =
DeleteDialogFragment {
momentViewModel.deleteMoment(momentId)
Expand Down Expand Up @@ -94,7 +97,10 @@ class MomentFragment :
}
}
momentViewModel.isDeleted.observe(viewLifecycleOwner) { isDeleted ->
if (isDeleted) findNavController().popBackStack()
if (isDeleted) {
sharedViewModel.setStaccatosHasUpdated()
findNavController().popBackStack()
}
}
}

Expand Down

0 comments on commit a1a00f7

Please sign in to comment.