Skip to content

Commit

Permalink
[Feat]: 로직 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
jinuemong committed Feb 8, 2024
1 parent dc6857c commit bc7b443
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
package ac.dnd.bookkeeping.android.presentation.ui.main.home.history.detail

sealed interface HistoryDetailIntent
import ac.dnd.bookkeeping.android.presentation.model.history.HistoryViewType

sealed interface HistoryDetailIntent {
data class ClickTab(val type: HistoryViewType) : HistoryDetailIntent
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package ac.dnd.bookkeeping.android.presentation.ui.main.home.history.detail

import ac.dnd.bookkeeping.android.domain.model.feature.relation.RelationDetailWithUserInfo

data class HistoryDetailModel(
val state: HistoryDetailState,
val relationDetail: RelationDetailWithUserInfo
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ package ac.dnd.bookkeeping.android.presentation.ui.main.home.history.detail

sealed interface HistoryDetailState {
data object Init : HistoryDetailState
data object Loading : HistoryDetailState
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package ac.dnd.bookkeeping.android.presentation.ui.main.home.history.detail

import ac.dnd.bookkeeping.android.domain.model.error.ServerException
import ac.dnd.bookkeeping.android.domain.model.feature.relation.RelationDetailGroup
import ac.dnd.bookkeeping.android.domain.model.feature.relation.RelationDetailWithUserInfo
import ac.dnd.bookkeeping.android.domain.usecase.feature.relation.GetRelationUseCase
import ac.dnd.bookkeeping.android.presentation.common.base.BaseViewModel
import ac.dnd.bookkeeping.android.presentation.common.base.ErrorEvent
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.EventFlow
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.MutableEventFlow
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.asEventFlow
import ac.dnd.bookkeeping.android.presentation.model.history.HistoryViewType
import androidx.lifecycle.SavedStateHandle
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -14,6 +20,7 @@ import javax.inject.Inject
@HiltViewModel
class HistoryDetailViewModel @Inject constructor(
private val savedStateHandle: SavedStateHandle,
private val getRelationUseCase: GetRelationUseCase
) : BaseViewModel() {

private val _state: MutableStateFlow<HistoryDetailState> =
Expand All @@ -23,7 +30,54 @@ class HistoryDetailViewModel @Inject constructor(
private val _event: MutableEventFlow<HistoryDetailEvent> = MutableEventFlow()
val event: EventFlow<HistoryDetailEvent> = _event.asEventFlow()

fun onIntent(intent: HistoryDetailIntent) {
private val _historyType: MutableStateFlow<HistoryViewType> =
MutableStateFlow(HistoryViewType.TOTAL)
val historyType: StateFlow<HistoryViewType> = _historyType.asStateFlow()

private val _relationDetail: MutableStateFlow<RelationDetailWithUserInfo> =
MutableStateFlow(
RelationDetailWithUserInfo(
id = -1,
name = "",
imageUrl = "",
memo = "",
group = RelationDetailGroup(
id = -1,
name = ""
),
giveMoney = 0L,
takeMoney = 0L
)
)
val relationDetail: StateFlow<RelationDetailWithUserInfo> = _relationDetail.asStateFlow()

fun loadRelationDetail(id: Long) {
launch {
_state.value = HistoryDetailState.Loading
getRelationUseCase(id)
.onSuccess {
_state.value = HistoryDetailState.Init
_relationDetail.value = it
}.onFailure { exception ->
_state.value = HistoryDetailState.Loading
when (exception) {
is ServerException -> {
_errorEvent.emit(ErrorEvent.InvalidRequest(exception))
}

else -> {
_errorEvent.emit(ErrorEvent.UnavailableServer(exception))
}
}
}
}
}

fun onIntent(intent: HistoryDetailIntent) {
when (intent) {
is HistoryDetailIntent.ClickTab -> {
_historyType.value = intent.type
}
}
}
}

0 comments on commit bc7b443

Please sign in to comment.