Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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 @@ -47,7 +47,6 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavBackStackEntry
import com.konkuk.medicarecall.R
import com.konkuk.medicarecall.data.dto.response.HomeResponseDto
import com.konkuk.medicarecall.ui.common.component.NameBar
Expand Down Expand Up @@ -79,7 +78,6 @@ fun HomeScreen(
navigateToStateHealthDetailScreen: (Int) -> Unit,
navigateToStateMentalDetailScreen: (Int) -> Unit,
navigateToGlucoseDetailScreen: (Int) -> Unit,
mainBackStackEntry: NavBackStackEntry,
) {
val homeUiState by homeViewModel.homeUiState.collectAsStateWithLifecycle()
val elderInfoList by homeViewModel.elderInfoList.collectAsStateWithLifecycle()
Expand All @@ -92,14 +90,13 @@ fun HomeScreen(
val snackbarHostState = remember { SnackbarHostState() }
val scope = rememberCoroutineScope()

val updatedName by mainBackStackEntry.savedStateHandle
.getStateFlow<String?>("ELDER_NAME_UPDATED", null)
.collectAsStateWithLifecycle()
// 네비게이션 결과
val updatedName by homeViewModel.updatedName.collectAsStateWithLifecycle()

LaunchedEffect(updatedName) {
updatedName?.let {
homeViewModel.overrideName(it)
mainBackStackEntry.savedStateHandle.remove<String>("ELDER_NAME_UPDATED") // 원샷 처리
homeViewModel.clearUpdatedName()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ class HomeViewModel(
private val savedStateHandle: SavedStateHandle,
private val eldersHealthInfoRepository: EldersHealthInfoRepository,
) : ViewModel() {

// 이름 업데이트 수신
private val _updatedName: StateFlow<String?> =
savedStateHandle.getStateFlow("ELDER_NAME_UPDATED", null)
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
Collaborator Author

Choose a reason for hiding this comment

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

ElderDetailScreen에서 넘겨주고 있습니다!

Copy link
Contributor

Choose a reason for hiding this comment

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

만약 Home 에서 초기 데이터 로딩을 init { } 이 아닌, LaunchedEffect 혹은 onStart { } 사용해서 하면, 다시 화면에 접근했을 때 데이터를 자동으로 로드하는 API를 실행시켜서 인자 전달 없이도 가능할 것 같습니다!

기존처럼 해도 괜찮을 것 같은데, ELDER_NAME 뿐만 아니라 다른 값들이 변경되었을 때도 savedStateHandle로 데이터를 주고받는다면 코드가 길어질 것 같습니다.

image

참고
https://github.com/Project-Unifest/unifest-android/blob/develop/feature/booth/src/main/kotlin/com/unifest/android/feature/booth/viewmodel/BoothViewModel.kt
https://onlyfor-me-blog.tistory.com/1095


val updatedName: StateFlow<String?> = _updatedName

fun clearUpdatedName() {
savedStateHandle.remove<String>("ELDER_NAME_UPDATED")
}


fun overrideName(newName: String) {
val id = selectedElderId.value ?: return

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import com.konkuk.medicarecall.ui.feature.statistics.weeklycard.WeeklyMentalCard
import com.konkuk.medicarecall.ui.feature.statistics.weeklycard.WeeklySleepCard
import com.konkuk.medicarecall.ui.feature.statistics.weeklycard.WeeklySummaryCard
import com.konkuk.medicarecall.ui.theme.MediCareCallTheme
import kotlinx.coroutines.flow.MutableStateFlow
import org.koin.androidx.compose.koinViewModel
import java.time.LocalDate

Expand Down Expand Up @@ -105,18 +104,22 @@ fun StatisticsScreen(
LaunchedEffect(selectedElderId) {
selectedElderId?.let { statisticsViewModel.setSelectedElderId(it) }
}
val savedStateHandle = navController.currentBackStackEntry?.savedStateHandle
val medsChanged by (savedStateHandle?.getStateFlow("medsChanged", false) ?: MutableStateFlow(
false,
))
.collectAsStateWithLifecycle()

LaunchedEffect(medsChanged) {
if (medsChanged) {
statisticsViewModel.refresh()
savedStateHandle?.set("medsChanged", false)
}
// 복약 변경 이벤트 수신
LaunchedEffect(Unit) {
navController.currentBackStackEntry
?.savedStateHandle
?.getStateFlow("medsChanged", false)
?.collect { changed ->
if (changed) {
statisticsViewModel.onMedsChanged()
navController.currentBackStackEntry
?.savedStateHandle
?.set("medsChanged", false)
}
}
}

StatisticsScreenLayout(
modifier = modifier,
uiState = uiState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ class StatisticsViewModel(
private val repository: StatisticsRepository,
private val eldersHealthInfoRepository: EldersHealthInfoRepository,
) : ViewModel() {

// 네비게이션 결과(복약 변경 등)를 ViewModel 책임으로 처리
fun onMedsChanged() {
refresh()
}
private val _uiState = MutableStateFlow(StatisticsUiState())
val uiState: StateFlow<StatisticsUiState> = _uiState.asStateFlow()

Expand Down
Loading