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

[FIX/#154] 홈 뷰 1차 QA #157

Merged
merged 7 commits into from
Jul 19, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fun ChangeFilterRoute(
when (sideEffect) {
is HomeSideEffect.ShowToast -> context.toast(sideEffect.message)
is HomeSideEffect.NavigateToChangeFilter -> navController.navigateChangeFilter()
is HomeSideEffect.NavigateToHome -> navController.navigateHome()
is HomeSideEffect.NavigateToHome -> navController.navigateUp()
}
}
}
Expand Down
32 changes: 23 additions & 9 deletions feature/src/main/java/com/terning/feature/home/home/HomeRoute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ import com.terning.feature.home.home.component.HomeRecommendInternDialog
import com.terning.feature.home.home.component.HomeTodayEmptyWithImg
import com.terning.feature.home.home.component.HomeTodayIntern
import com.terning.feature.home.home.model.HomeDialogState
import com.terning.feature.home.home.navigation.navigateHome
import com.terning.feature.intern.navigation.navigateIntern

const val NAME_START_LENGTH = 7
Expand Down Expand Up @@ -93,13 +92,17 @@ fun HomeRoute(
val homeUserState by viewModel.homeUserState.collectAsStateWithLifecycle()
val homeDialogState by viewModel.homeDialogState.collectAsStateWithLifecycle()

val homeTodayInternList: MutableState<List<HomeTodayInternModel>> = remember {
mutableStateOf(emptyList())
}

LaunchedEffect(viewModel.homeSideEffect, lifecycleOwner) {
viewModel.homeSideEffect.flowWithLifecycle(lifecycle = lifecycleOwner.lifecycle)
.collect { sideEffect ->
when (sideEffect) {
is HomeSideEffect.ShowToast -> context.toast(sideEffect.message)
is HomeSideEffect.NavigateToChangeFilter -> navController.navigateChangeFilter()
is HomeSideEffect.NavigateToHome -> navController.navigateHome()
is HomeSideEffect.NavigateToHome -> navController.navigateUp()
}
}
}
Expand All @@ -119,12 +122,20 @@ fun HomeRoute(
}
}

val homeTodayInternList = when (homeTodayState) {
LaunchedEffect(homeFilteringState) {
viewModel.getHomeTodayInternList()
}

when (homeTodayState) {
is UiState.Success -> {
(homeTodayState as UiState.Success<List<HomeTodayInternModel>>).data
homeTodayInternList.value =
(homeTodayState as UiState.Success<List<HomeTodayInternModel>>).data
}

else -> emptyList()
is UiState.Loading -> {}
else -> {
homeTodayInternList.value = emptyList()
}
}

val homeRecommendInternList = when (homeRecommendInternState) {
Expand All @@ -149,7 +160,7 @@ fun HomeRoute(
currentSortBy,
homeUserName = homeUserName,
homeFilteringInfo = homeFilteringInfo,
homeTodayInternList = homeTodayInternList,
homeTodayInternList = homeTodayInternList.value,
recommendInternList = homeRecommendInternList,
homeDialogState = homeDialogState,
onChangeFilterClick = { navController.navigateChangeFilter() },
Expand Down Expand Up @@ -276,7 +287,10 @@ fun HomeScreen(

if (homeDialogState.isScrapDialogVisible && !homeDialogState.isToday) {
TerningBasicDialog(
onDismissRequest = { viewModel.updateScrapDialogVisible(false) },
onDismissRequest = {
viewModel.updateScrapDialogVisible(false)
viewModel.updatePaletteOpen(false)
},
content = {
if (recommendInternList[scrapId].scrapId != null) {
ScrapCancelDialogContent(
Expand All @@ -285,7 +299,7 @@ fun HomeScreen(
viewModel.deleteScrap(
recommendInternList[scrapId].scrapId ?: -1,
)
if(homeDialogState.isScrappedState) {
if (homeDialogState.isScrappedState) {
viewModel.getRecommendInternsData(
currentSortBy.value,
homeFilteringInfo.startYear ?: viewModel.currentYear,
Expand All @@ -305,7 +319,7 @@ fun HomeScreen(
),
clickAction = {
viewModel.updateScrapDialogVisible(false)
if(homeDialogState.isScrappedState) {
if (homeDialogState.isScrappedState) {
viewModel.getRecommendInternsData(
currentSortBy.value,
homeFilteringInfo.startYear ?: viewModel.currentYear,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ class HomeViewModel @Inject constructor(
init {
getProfile()
getFilteringInfo()
getHomeTodayInternList()
}

fun getRecommendInternsData(sortBy: Int, startYear: Int, startMonth: Int) {
Expand Down Expand Up @@ -157,6 +156,26 @@ class HomeViewModel @Inject constructor(
}
}

fun patchScrap(
scrapId: Long,
colorIndex: Int,
) {
viewModelScope.launch {
scrapRepository.patchScrap(
ScrapRequestModel(
id = scrapId,
color = colorIndex,
)
).onSuccess {
updateScrapDialogVisible(visible = false)
updateScrapped(scrapped = true)
getHomeTodayInternList()
}.onFailure {
_homeSideEffect.emit(HomeSideEffect.ShowToast(R.string.server_failure))
}
}
}

fun updateSelectColor(newColor: Color) {
_homeDialogState.update {
it.copy(selectedColor = newColor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ fun HomeRecommendInternDialog(
if (state.isColorChange) {
viewModel.updateColorChange(false)
}
viewModel.updatePaletteOpen(false)
viewModel.updateScrapDialogVisible(false)
}
viewModel.postScrap(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ fun HomeTodayIntern(
.noRippleClickable {
homeViewModel.updateScrapDialogVisible(true)
homeViewModel.updateIsToday(true)
homeViewModel.updateSelectColor(
Color(
android.graphics.Color.parseColor(
internList[index].color
)
)
)
selectedIndex = index
}
)
Expand All @@ -55,6 +62,7 @@ fun HomeTodayIntern(
TerningBasicDialog(
onDismissRequest = {
homeViewModel.updateScrapDialogVisible(false)
homeViewModel.updatePaletteOpen(false)
},
content = {
with(internList[selectedIndex]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,19 +232,26 @@ fun HomeTodayInternDialog(
style = TerningTheme.typography.button3,
paddingVertical = 12.dp,
cornerRadius = 8.dp,
text = R.string.dialog_scrap_move_to_intern,
text = if (state.isPaletteOpen) R.string.dialog_content_calendar_color_change else R.string.dialog_scrap_move_to_intern,
onButtonClick = {
if (state.isPaletteOpen) {
viewModel.updatePaletteOpen(false)
viewModel.updateColorChange(false)
viewModel.updateScrapDialogVisible(false)
viewModel.patchScrap(
scrapId = homeTodayInternModel.scrapId,
colorIndex = selectedColorIndex,
Comment on lines 237 to +242
Copy link
Member

Choose a reason for hiding this comment

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

이 코드를 보니까 생각난건데 LaunchedEffect(state.isPaletteOpen)과 같은 형식으로 통신을 관리하는 방법도 좋을 것 같네요! 물론 저도 그렇게 관리하진 않았어요ㅎ

Copy link
Member Author

Choose a reason for hiding this comment

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

오 좋은 생각이네요!!

)
} else {
if (state.isColorChange) {
viewModel.updateColorChange(false)
viewModel.patchScrap(
scrapId = homeTodayInternModel.scrapId,
colorIndex = selectedColorIndex,
)
}
viewModel.updateScrapDialogVisible(false)
navigateTo()
}
navigateTo()
},
modifier = Modifier.padding(bottom = 8.dp)
)
Expand Down
Loading