-
Notifications
You must be signed in to change notification settings - Fork 0
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
#308 [feat] 대표 룰 수정 api 및 presentation 로직 #310
Merged
murjune
merged 10 commits into
develop
from
feature/#308-represent-rule-api-presentation
Sep 9, 2023
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
af42cc3
#308 [feat] update Represent Rules api, usecase
murjune 6de1ab8
#308 [add] 대표 룰 string 추가
murjune bfa7b45
#308 [feat] 대표 룰 Reducer, viewModel 구현
murjune 68b72c0
#308 [feat] RuleScreen SideEffect, Effect 처리
murjune 190a1b6
#308 [fix] 대표룰 수정 @Body 어노테이션 추가
murjune 88980e5
#308 [fix] 저장 가능, 대표 룰 수정 로직 변경
murjune 407b8d0
#308 [fix] HousDot
murjune db58146
#308 [ui] MainRuleList Preview 수정
murjune 57e468e
#308 [ui] PhotoGrid X 버튼 위치 조정
murjune c11af11
#308 [chore] ktlint
murjune File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
app/src/main/java/hous/release/android/presentation/our_rules/event/RepresentRulesReducer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package hous.release.android.presentation.our_rules.event | ||
|
||
import hous.release.android.presentation.our_rules.model.RepresentRuleUiModel | ||
import hous.release.android.presentation.our_rules.viewmodel.RepresentRulesEvent | ||
import hous.release.android.presentation.our_rules.viewmodel.RepresentRulesState | ||
import hous.release.android.util.event.Reducer | ||
import javax.inject.Inject | ||
|
||
class RepresentRulesReducer @Inject constructor() : | ||
Reducer<RepresentRulesState, RepresentRulesEvent> { | ||
override fun dispatch( | ||
state: RepresentRulesState, | ||
event: RepresentRulesEvent | ||
): RepresentRulesState { | ||
return when (event) { | ||
is RepresentRulesEvent.FetchRules -> { | ||
state.copy( | ||
originRules = event.rules.map { RepresentRuleUiModel.from(it) }, | ||
rules = event.rules.map { RepresentRuleUiModel.from(it) } | ||
) | ||
} | ||
|
||
is RepresentRulesEvent.UpdateRule -> { | ||
state.copy( | ||
rules = state.rules.map { rule -> | ||
if (rule.id == event.id) rule.copy(isRepresent = rule.isRepresent.not()) | ||
else rule | ||
} | ||
) | ||
} | ||
} | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
...main/java/hous/release/android/presentation/our_rules/viewmodel/RepresentRuleViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package hous.release.android.presentation.our_rules.viewmodel | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import hous.release.android.di.RepresentRules | ||
import hous.release.android.presentation.our_rules.model.RepresentRuleUiModel | ||
import hous.release.android.util.event.Reducer | ||
import hous.release.domain.entity.rule.Rule | ||
import hous.release.domain.usecase.rule.GetRulesUseCase | ||
import hous.release.domain.usecase.rule.UpdateRepresentRulesUseCase | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.flow.SharingStarted | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.receiveAsFlow | ||
import kotlinx.coroutines.flow.runningFold | ||
import kotlinx.coroutines.flow.stateIn | ||
import kotlinx.coroutines.launch | ||
import retrofit2.HttpException | ||
import timber.log.Timber | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class RepresentRuleViewModel @Inject constructor( | ||
private val updateRepresentRulesUseCase: UpdateRepresentRulesUseCase, | ||
private val getRulesUseCase: GetRulesUseCase, | ||
@RepresentRules private val reducer: Reducer<RepresentRulesState, RepresentRulesEvent> | ||
) : ViewModel() { | ||
|
||
private val uiEvents = Channel<RepresentRulesEvent>() | ||
val uiState: StateFlow<RepresentRulesState> = uiEvents | ||
.receiveAsFlow() | ||
.runningFold(RepresentRulesState(), reducer::dispatch) | ||
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), RepresentRulesState()) | ||
|
||
private val _sideEffect: Channel<RepresentRulesSideEffect> = Channel() | ||
val sideEffect = _sideEffect.receiveAsFlow() | ||
|
||
init { | ||
viewModelScope.launch { | ||
val rules = getRulesUseCase() | ||
uiEvents.send(RepresentRulesEvent.FetchRules(rules)) | ||
} | ||
} | ||
|
||
val isSavable get() = uiState.value.rules == uiState.value.originRules | ||
|
||
fun updateRuleBy(id: Int) { | ||
viewModelScope.launch { | ||
if (canAddRepresentRule()) { | ||
uiEvents.send(RepresentRulesEvent.UpdateRule(id)) | ||
return@launch | ||
} | ||
_sideEffect.send(RepresentRulesSideEffect.ShowLimitRulesToast) | ||
} | ||
} | ||
|
||
fun saveRules() { | ||
viewModelScope.launch { | ||
_sideEffect.send(RepresentRulesSideEffect.LoadingBar(true)) | ||
runCatching { | ||
updateRepresentRulesUseCase( | ||
uiState.value.rules.filter { it.isRepresent } | ||
.map { it.id } | ||
) | ||
}.onSuccess { | ||
_sideEffect.send(RepresentRulesSideEffect.LoadingBar(false)) | ||
_sideEffect.send(RepresentRulesSideEffect.PopBackStack) | ||
}.onFailure { e -> | ||
Timber.e("updateRule() - onFailure() - e: ${e.stackTraceToString()}") | ||
if (e is HttpException) { | ||
when (e.code()) { | ||
LIMIT_RULES_CODE -> _sideEffect.send(RepresentRulesSideEffect.ShowLimitRulesToast) | ||
} | ||
} | ||
_sideEffect.send(RepresentRulesSideEffect.LoadingBar(false)) | ||
} | ||
} | ||
} | ||
|
||
private fun canAddRepresentRule(): Boolean { | ||
return uiState.value.rules.filter { it.isRepresent }.size < 3 | ||
} | ||
|
||
private companion object { | ||
const val LIMIT_RULES_CODE = 403 | ||
} | ||
} | ||
|
||
sealed class RepresentRulesSideEffect { | ||
object IDLE : RepresentRulesSideEffect() | ||
data class LoadingBar(val isLoading: Boolean) : RepresentRulesSideEffect() | ||
object ShowLimitRulesToast : RepresentRulesSideEffect() | ||
object PopBackStack : RepresentRulesSideEffect() | ||
} | ||
|
||
data class RepresentRulesState( | ||
val originRules: List<RepresentRuleUiModel> = emptyList(), | ||
val rules: List<RepresentRuleUiModel> = emptyList() | ||
) | ||
|
||
sealed class RepresentRulesEvent { | ||
data class FetchRules(val rules: List<Rule>) : RepresentRulesEvent() | ||
data class UpdateRule(val id: Int) : RepresentRulesEvent() | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오.. 항상 combine 사용했었는데 이런 식으로 짧게도 가능하네요... 👍