Skip to content

Commit

Permalink
[REFACTOR] Improve Navigation Structure
Browse files Browse the repository at this point in the history
  - Allow MoreCategorizedNotificationViewModel access requested category using savedStateHandle instance provided by ViewModel.
  - Category would be determined based on the destination where MoreCategorizedNotification Composable is being called.
  • Loading branch information
doyoonkim3312 committed Nov 11, 2024
1 parent c56dd73 commit 85ce874
Showing 1 changed file with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.doyoonkim.knutice.viewModel

import android.util.Log
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import com.doyoonkim.knutice.domain.CrawlFullContentImpl
import androidx.navigation.toRoute
import com.doyoonkim.knutice.domain.FetchNoticesPerPageInCategory
import com.doyoonkim.knutice.model.Destination
import com.doyoonkim.knutice.model.NavDestination
import com.doyoonkim.knutice.model.Notice
import com.doyoonkim.knutice.model.NoticeCategory
import dagger.hilt.android.lifecycle.HiltViewModel
Expand All @@ -20,17 +23,23 @@ import javax.inject.Inject

@HiltViewModel
class MoreCategorizedNotificationViewModel @Inject constructor(
private val fetchListOfNoticesUseCase: FetchNoticesPerPageInCategory
private val fetchListOfNoticesUseCase: FetchNoticesPerPageInCategory,
private val savedStateHandle: SavedStateHandle
): ViewModel() {
private val filename = "MoreCategorizedNotificationViewModel"

// UI State
private var _uiState = MutableStateFlow<MoreNotificationListState>(MoreNotificationListState())
val uiState = _uiState.asStateFlow()

fun setNotificationCategory(category: NoticeCategory) {
_uiState.update {
it.copy(notificationCategory = category)
// Category of Requested Notice List
private val category = savedStateHandle.toRoute<NavDestination>().run {
when (this.arrived) {
Destination.MORE_GENERAL -> NoticeCategory.GENERAL_NEWS
Destination.MORE_ACADEMIC -> NoticeCategory.ACADEMIC_NEWS
Destination.MORE_SCHOLARSHIP -> NoticeCategory.SCHOLARSHIP_NEWS
Destination.MORE_EVENT -> NoticeCategory.EVENT_NEWS
else -> NoticeCategory.Unspecified
}
}

Expand All @@ -54,7 +63,7 @@ class MoreCategorizedNotificationViewModel @Inject constructor(
fun fetchNotificationPerPage() {
CoroutineScope(Dispatchers.IO).launch {
fetchListOfNoticesUseCase.getNoticesPerPage(
_uiState.value.notificationCategory, _uiState.value.currentLastNttId
category, _uiState.value.currentLastNttId
)
.map { Result.success(it) }
.catch { emit(Result.failure(it)) }
Expand Down Expand Up @@ -96,7 +105,6 @@ class MoreCategorizedNotificationViewModel @Inject constructor(

data class MoreNotificationListState(
val currentLastNttId: Int = 0,
val notificationCategory: NoticeCategory = NoticeCategory.Unspecified,
val notices: List<Notice> = List<Notice>(20) { Notice() },
val isLoading: Boolean = false,
val isRefreshRequested: Boolean = false
Expand Down

0 comments on commit 85ce874

Please sign in to comment.