Skip to content

Commit

Permalink
#86 fix : 음식점 마커 오류 수정
Browse files Browse the repository at this point in the history
Co-authored-by: plashdof <[email protected]>
Co-authored-by: yy0ung <[email protected]>
  • Loading branch information
3 people committed Dec 11, 2023
1 parent bdfa7c8 commit b4cec83
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import com.naver.maps.map.overlay.OverlayImage
import com.naver.maps.map.util.FusedLocationSource
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.async
import kotlinx.coroutines.channels.Channel

@AndroidEntryPoint
class HomeFragment : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home), OnMapReadyCallback {
Expand All @@ -45,6 +46,7 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home),
override val parentViewModel: MainViewModel by activityViewModels()
private val args: HomeFragmentArgs by navArgs()
private val restaurantId by lazy { args.addRestaurantId }
private val initCompletedNaverMap = Channel<Unit>()

companion object {
const val LOCATION_PERMISSION_REQUEST_CODE = 1000
Expand All @@ -68,7 +70,11 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home),
}

override fun initNetworkView() {
viewModel.getFilterList()
repeatOnStarted {
viewModel.getFilterList()
initCompletedNaverMap.receive()
viewModel.getMarkerList()
}
}

private fun initStateObserver() {
Expand Down Expand Up @@ -137,8 +143,7 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home),
naverMap.moveCamera(cameraUpdate)

viewModel.uiState.value.markerList.forEach { data ->
val isNear = viewModel.uiState.value.curFilter == NEAR_RESTAURANT
setMarker(data, isNear)
setMarker(data)
}
}

Expand All @@ -152,10 +157,8 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home),
locationSource = FusedLocationSource(this, LOCATION_PERMISSION_REQUEST_CODE)
}

// NaverMap 관련 Setting
override fun onMapReady(nM: NaverMap) {
this.naverMap = nM
// 맵 ui Settings
with(naverMap.uiSettings) {
isCompassEnabled = false
isZoomControlEnabled = false
Expand All @@ -164,7 +167,9 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home),
naverMap.locationSource = locationSource
setMapListener()
initStateObserver()
viewModel.getMarkerList()
repeatOnStarted {
initCompletedNaverMap.send(Unit)
}
}

private fun setMapListener() {
Expand Down Expand Up @@ -209,12 +214,14 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home),
else viewModel.trackingOff()
}

private fun setMarker(data: UiRestaurantData, isNear: Boolean) {
private fun setMarker(data: UiRestaurantData) {
val marker = Marker()

marker.position = LatLng(data.latitude, data.longitude)

marker.icon = if (isNear)
// Log.d("marker test--", "${viewModel.uiState.value.curFilter}..${NEAR_RESTAURANT}.. ")

marker.icon = if (viewModel.uiState.value.curFilter == NEAR_RESTAURANT)
OverlayImage.fromResource(R.drawable.ic_marker_near)
else
OverlayImage.fromResource(R.drawable.ic_marker)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.naver.maps.geometry.LatLng
import com.naver.maps.map.overlay.Marker
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.async
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
Expand All @@ -43,9 +44,9 @@ data class HomeUiState(
val markerList: List<UiRestaurantData> = emptyList(),
val recommendList: List<UiRecommendRestaurantData> = emptyList(),
val curFilter: String = MY_LIST,
val cameraLatitude: Double = 0.0,
val cameraLongitude: Double = 0.0,
val cameraZoom: Double = 0.0,
val cameraLatitude: Double = 37.553836,
val cameraLongitude: Double = 126.969652,
val cameraZoom: Double = 12.0,
val cameraRadius: Double = 0.0,
val curLatitude: Double = 0.0,
val curLongitude: Double = 0.0,
Expand Down Expand Up @@ -87,17 +88,13 @@ class HomeViewModel @Inject constructor(
private val _uiState = MutableStateFlow(HomeUiState())
val uiState: StateFlow<HomeUiState> = _uiState.asStateFlow()

private val _events = MutableSharedFlow<HomeEvents>()
private val _events = MutableSharedFlow<HomeEvents>(
replay = 0,
extraBufferCapacity = 1,
onBufferOverflow = BufferOverflow.DROP_OLDEST
)
val events: SharedFlow<HomeEvents> = _events.asSharedFlow()

init {
_uiState.update { state ->
state.copy(
filterList = listOf(UiFilterData(MY_LIST, true, ::onFilterItemClicked))
)
}
}

fun updateLocation(latitude: Double, longitude: Double) {
_uiState.update { state ->
state.copy(
Expand Down Expand Up @@ -173,13 +170,13 @@ class HomeViewModel @Inject constructor(
fun getFilterList() {
followRepository.getMyFollowing().onEach { it ->
val initialFilterList = listOf(
UiFilterData(MY_LIST, true, ::onFilterItemClicked),
UiFilterData(NEAR_RESTAURANT, false, ::onFilterItemClicked)
UiFilterData(MY_LIST, isChecked(MY_LIST), ::onFilterItemClicked),
UiFilterData(NEAR_RESTAURANT, isChecked(NEAR_RESTAURANT), ::onFilterItemClicked)
)
when (it) {
is BaseState.Success -> {
val filterList = initialFilterList + it.data.map {
UiFilterData(it.nickName, false, ::onFilterItemClicked)
UiFilterData(it.nickName, isChecked(it.nickName), ::onFilterItemClicked)
}
_uiState.update { state ->
state.copy(
Expand All @@ -202,42 +199,19 @@ class HomeViewModel @Inject constructor(
}.launchIn(viewModelScope)
}

fun updateNearRestaurant() {
val nearRestaurantFilter = uiState.value.filterList.find { it.name == NEAR_RESTAURANT }
if (uiState.value.curFilter == NEAR_RESTAURANT && nearRestaurantFilter?.isSelected == true) {
nearRestaurantList()
} else {
onFilterItemClicked(NEAR_RESTAURANT)
}
private fun isChecked(filterName: String): Boolean {
return filterName == uiState.value.curFilter
}

private fun resetMarkerList() {
viewModelScope.launch {
_uiState.update { state ->
state.copy(markerList = emptyList())
}
_events.emit(HomeEvents.SetNewMarkers)
}
fun updateNearRestaurant() {
onFilterItemClicked(NEAR_RESTAURANT)
}

fun getMarkerList() {
if (uiState.value.filterList.all { !it.isSelected }) {
resetMarkerList()
return
}

when (_uiState.value.curFilter) {
NEAR_RESTAURANT -> {
nearRestaurantList()
}

MY_LIST -> {
myRestaurantList()
}

else -> {
userRestaurantList()
}
when (uiState.value.curFilter) {
NEAR_RESTAURANT -> nearRestaurantList()
MY_LIST -> myRestaurantList()
else -> userRestaurantList()
}
}

Expand All @@ -247,10 +221,12 @@ class HomeViewModel @Inject constructor(
longitude = uiState.value.cameraLongitude.toString(),
latitude = uiState.value.cameraLatitude.toString()
).onEach {
_events.emit(HomeEvents.RemoveMarkers)
when (it) {
is BaseState.Success -> {
_uiState.update { state ->
state.copy(
curFilter = NEAR_RESTAURANT,
markerList = it.data.map { restaurants ->
restaurants.toUiRestaurantData()
}
Expand Down Expand Up @@ -279,6 +255,7 @@ class HomeViewModel @Inject constructor(
}
_uiState.update { state ->
state.copy(
curFilter = MY_LIST,
markerList = restaurantsList
)
}
Expand Down

0 comments on commit b4cec83

Please sign in to comment.