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

현위치에서 키워드로 재검색, 검색 시 필터 초기화 #89

Merged
merged 10 commits into from
Feb 21, 2024
Merged
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.example.presentation.ui

import android.app.Activity
import android.util.Log
import android.widget.Toast
import androidx.activity.compose.BackHandler
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -72,10 +71,6 @@ fun MainScreen(
mutableStateOf(false)
}

val (isKindFilterClicked, onKindFilterChanged) = remember { mutableStateOf(false) }
val (isGreatFilterClicked, onGreatFilterChanged) = remember { mutableStateOf(false) }
val (isSafeFilterClicked, onSafeFilterChanged) = remember { mutableStateOf(false) }

val (screenCoordinate, onScreenChanged) = remember {
mutableStateOf(
ScreenCoordinate(
Expand Down Expand Up @@ -112,8 +107,6 @@ fun MainScreen(

val (isLoading, onLoadingChanged) = remember { mutableStateOf(false) }

val (isFilteredMarker, onFilteredMarkerChanged) = remember { mutableStateOf(false) }

val (errorToastMsg, onErrorToastChanged) = remember { mutableStateOf("") }

val (isListItemClicked, onListItemChanged) = remember { mutableStateOf(false) }
Expand Down Expand Up @@ -177,8 +170,6 @@ fun MainScreen(
onBackPressedChanged,
mapViewModel,
navController,
isFilteredMarker,
onFilteredMarkerChanged,
isReSearchButtonClicked
)

Expand Down Expand Up @@ -216,12 +207,6 @@ fun MainScreen(
)

FilterComponent(
isKindFilterClicked,
onKindFilterChanged,
isGreatFilterClicked,
onGreatFilterChanged,
isSafeFilterClicked,
onSafeFilterChanged,
mapViewModel,
onFilterStateChanged
)
Expand Down Expand Up @@ -270,7 +255,6 @@ fun MainScreen(

val mapCenterCoordinate by mapViewModel.mapCenterCoordinate.collectAsStateWithLifecycle()
if (isReSearchButtonClicked && isScreenCoordinateChanged) {
Log.d("테스트", "키워드 재검색 눌리고있음?")
mapViewModel.updateIsFilteredMarker(false)
mapViewModel.searchStore(
mapCenterCoordinate.longitude,
Expand All @@ -284,7 +268,6 @@ fun MainScreen(
if ((isReloadButtonClicked && isScreenCoordinateChanged)) {
mapViewModel.updateIsFilteredMarker(false)
onErrorToastChanged("")
onFilteredMarkerChanged(false)
mapViewModel.getStoreDetail(
nwLong = screenCoordinate.northWest.longitude,
nwLat = screenCoordinate.northWest.latitude,
Expand Down Expand Up @@ -321,14 +304,15 @@ fun MainScreen(
@Composable
fun PressBack(
mapViewModel: MapViewModel,
onBackPressedChanged: (Boolean) -> Unit
onBackPressedChanged: (Boolean) -> Unit,
) {
val mapScreenType by mapViewModel.mapScreenType.collectAsStateWithLifecycle()
val context = LocalContext.current
var backPressedTime = 0L

BackHandler {
if (mapScreenType == MapScreenType.SEARCH) {
mapViewModel.updateIsSearchTerminated(true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이걸 여기에도 추가한 이유가 있나요??

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MapScreenType.SEARCH에서 BackHandler를 통해 SearchScreen으로 갔다가 다른 키워드를 검색해 MapScreenType.SEARCH으로 넘어가는 경우, 필터 초기화를 해주기 위함입니다! 만약 이 때 필터 초기화가 안 된다면 새로운 키워드에 대한 결과 마커가 필터로 모두 가려졌을 때 사용자에게 혼란을 줄 수 있다고 생각했습니다.

onBackPressedChanged(true)
mapViewModel.updateMapScreenType(MapScreenType.MAIN)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ class MapViewModel @Inject constructor(
}
}

fun initializeFilterSet() {
filterSet.clear()
}

fun setLocationTrackingMode(): LocationTrackingButton {
return if (isLocationPermissionGranted.value) {
LocationTrackingButton.FOLLOW
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavHostController
import com.example.domain.model.map.ShowMoreCount
Expand All @@ -28,6 +29,7 @@ import com.example.presentation.model.LocationTrackingButton
import com.example.presentation.model.ScreenCoordinate
import com.example.presentation.model.StoreDetail
import com.example.presentation.model.StoreType
import com.example.presentation.ui.map.filter.FilterViewModel
import com.example.presentation.ui.map.location.CurrentLocationComponent
import com.example.presentation.ui.map.marker.StoreMarker
import com.example.presentation.ui.map.reload.setReloadButtonBottomPadding
Expand Down Expand Up @@ -92,9 +94,8 @@ fun NaverMapScreen(
onBackPressedChanged: (Boolean) -> Unit,
mapViewModel: MapViewModel,
navController: NavHostController,
isFilteredMarker: Boolean,
onFilteredMarkerChanged: (Boolean) -> Unit,
isReSearchButtonClicked: Boolean
isReSearchButtonClicked: Boolean,
filterViewModel:FilterViewModel = hiltViewModel()
) {
val cameraPositionState = rememberCameraPositionState {}

Expand Down Expand Up @@ -225,6 +226,7 @@ fun NaverMapScreen(
}

val isFilteredMarker by mapViewModel.isFilteredMarker.collectAsStateWithLifecycle()

if (isFilteredMarker) {
FilteredMarkers(
mapViewModel,
Expand Down Expand Up @@ -273,6 +275,8 @@ fun NaverMapScreen(
)
mapViewModel.updateMapZoomLevel(cameraPositionState.position.zoom)
navController.navigate(Screen.Search.route)
filterViewModel.updateAllFilterUnClicked()
mapViewModel.initializeFilterSet()
onSearchComponentChanged(false)
}
if (isBackPressed) {
Expand Down Expand Up @@ -508,7 +512,8 @@ private fun CheckSearchTerminationButtonClicked(
onSearchTerminationButtonChanged: (Boolean) -> Unit,
onReloadButtonChanged: (Boolean) -> Unit,
mapCenterCoordinate: Coordinate,
mapZoomLevel: Double
mapZoomLevel: Double,
filterViewModel: FilterViewModel = hiltViewModel()
) {
if (isSearchTerminationButtonClicked) {
mapViewModel.updateMapCenterCoordinate(
Expand All @@ -532,6 +537,8 @@ private fun CheckSearchTerminationButtonClicked(

LaunchedEffect(key1 = isSearchTerminated) {
if (isSearchTerminated) {
filterViewModel.updateAllFilterUnClicked()
mapViewModel.initializeFilterSet()
movePrevCamera(cameraPositionState, mapCenterCoordinate, mapZoomLevel)
onReloadButtonChanged(true)
mapViewModel.updateIsSearchTerminated(false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.presentation.ui.map.filter

import android.annotation.SuppressLint
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
Expand Down Expand Up @@ -27,6 +28,8 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.example.presentation.model.StoreType
import com.example.presentation.ui.map.MapViewModel
import com.example.presentation.ui.theme.Black
Expand All @@ -36,43 +39,42 @@ import com.example.presentation.util.MainConstants.DEFAULT_MARGIN
import com.example.presentation.util.MainConstants.SEARCH_TEXT_FIELD_HEIGHT
import com.example.presentation.util.MainConstants.SEARCH_TEXT_FIELD_TOP_PADDING

@SuppressLint("StateFlowValueCalledInComposition")
@Composable
fun FilterComponent(
isKindFilterClicked: Boolean,
onKindFilterChanged: (Boolean) -> Unit,
isGreatFilterClicked: Boolean,
onGreatFilterChanged: (Boolean) -> Unit,
isSafeFilterClicked: Boolean,
onSafeFilterChanged: (Boolean) -> Unit,
mapViewModel: MapViewModel,
onFilterStateChanged: (Boolean) -> Unit,
filterViewModel: FilterViewModel = hiltViewModel()
) {

Row(
modifier = Modifier
.fillMaxHeight()
.fillMaxWidth()
.padding(top = (SEARCH_TEXT_FIELD_HEIGHT + SEARCH_TEXT_FIELD_TOP_PADDING + 8).dp, start = DEFAULT_MARGIN.dp),
.padding(
top = (SEARCH_TEXT_FIELD_HEIGHT + SEARCH_TEXT_FIELD_TOP_PADDING + 8).dp,
start = DEFAULT_MARGIN.dp
),
verticalAlignment = Alignment.Top
) {
val isKindClicked = filterViewModel.isKindFilterClicked.collectAsStateWithLifecycle()
val isGreatClicked = filterViewModel.isGreatFilterClicked.collectAsStateWithLifecycle()
val isSafeClicked = filterViewModel.isSafeFilterClicked.collectAsStateWithLifecycle()
FilterButton(
storeType = StoreType.KIND,
isKindFilterClicked,
onKindFilterChanged,
isKindClicked.value,
mapViewModel,
onFilterStateChanged
)
FilterButton(
storeType = StoreType.GREAT,
isGreatFilterClicked,
onGreatFilterChanged,
isGreatClicked.value,
mapViewModel,
onFilterStateChanged
)
FilterButton(
storeType = StoreType.SAFE,
isSafeFilterClicked,
onSafeFilterChanged,
isSafeClicked.value,
mapViewModel,
onFilterStateChanged
)
Expand All @@ -84,16 +86,20 @@ fun FilterComponent(
fun FilterButton(
storeType: StoreType,
isFilterClicked: Boolean,
onFilterChanged: (Boolean) -> Unit,
mapViewModel: MapViewModel,
onFilterStateChanged: (Boolean) -> Unit
onFilterStateChanged: (Boolean) -> Unit,
filterViewModel: FilterViewModel = hiltViewModel(),
) {
val certificationName = stringResource(id = storeType.storeTypeName).replace(" ", "")
CompositionLocalProvider(LocalMinimumInteractiveComponentEnforcement provides false) {
Button(
onClick = {
mapViewModel.updateFilterSet(certificationName, isFilterClicked.not())
onFilterChanged(isFilterClicked.not())
when (storeType) {
StoreType.KIND -> filterViewModel.updateKindFilterClicked()
StoreType.SAFE -> filterViewModel.updateSafeFilterClicked()
StoreType.GREAT -> filterViewModel.updateGreatFilterClicked()
}
onFilterStateChanged(true)
},
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.example.presentation.ui.map.filter

import android.util.Log
import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import javax.inject.Inject


@HiltViewModel
class FilterViewModel @Inject constructor() : ViewModel() {

private val _isKindFilterClicked = MutableStateFlow(false)
val isKindFilterClicked: StateFlow<Boolean> get() = _isKindFilterClicked

private val _isGreatFilterClicked = MutableStateFlow(false)
val isGreatFilterClicked: StateFlow<Boolean> get() = _isGreatFilterClicked

private val _isSafeFilterClicked = MutableStateFlow(false)
val isSafeFilterClicked: StateFlow<Boolean> get() = _isSafeFilterClicked

fun updateKindFilterClicked() {
_isKindFilterClicked.value = _isKindFilterClicked.value.not()
}

fun updateGreatFilterClicked() {
_isGreatFilterClicked.value = _isGreatFilterClicked.value.not()
}

fun updateSafeFilterClicked() {
_isSafeFilterClicked.value = _isSafeFilterClicked.value.not()
}

fun updateAllFilterUnClicked() {
_isKindFilterClicked.value = false
_isGreatFilterClicked.value = false
_isSafeFilterClicked.value = false
Log.d("테스트","${_isKindFilterClicked.value}")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import com.example.domain.model.search.SearchWord
import com.example.presentation.R
import com.example.presentation.ui.component.EmptyScreen
import com.example.presentation.ui.map.MapViewModel
import com.example.presentation.ui.map.filter.FilterViewModel
import com.example.presentation.ui.navigation.Screen
import com.example.presentation.ui.theme.Black
import com.example.presentation.ui.theme.DarkGray
Expand Down Expand Up @@ -132,6 +133,7 @@ fun SearchTextField(
navController: NavHostController,
mapViewModel: MapViewModel,
searchViewModel: SearchViewModel = hiltViewModel(),
filterViewModel: FilterViewModel = hiltViewModel()
) {
var searchText by remember { mutableStateOf("") }

Expand Down Expand Up @@ -195,6 +197,9 @@ fun SearchTextField(
textStyle = TextStyle(color = Black, fontSize = 14.sp, fontWeight = Medium),
modifier = Modifier.focusRequester(focusRequester),
keyboardActions = KeyboardActions(onDone = {
filterViewModel.updateAllFilterUnClicked()
mapViewModel.initializeFilterSet()

if (searchText.isNotBlank()) {
insertSearchWord(searchText, searchViewModel)

Expand Down