-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
196 additions
and
13 deletions.
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
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
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
29 changes: 29 additions & 0 deletions
29
feature/community/src/main/java/com/susu/feature/community/voteadd/VoteAddContract.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,29 @@ | ||
package com.susu.feature.community.voteadd | ||
|
||
import com.susu.core.model.Category | ||
import com.susu.core.ui.base.SideEffect | ||
import com.susu.core.ui.base.UiState | ||
import kotlinx.collections.immutable.PersistentList | ||
import kotlinx.collections.immutable.persistentListOf | ||
import java.util.UUID | ||
|
||
data class VoteAddState( | ||
val categoryConfigList: PersistentList<Category> = persistentListOf(), | ||
val selectedCategory: Category = Category(), | ||
val voteOptionStateList: PersistentList<VoteOptionState> = persistentListOf(VoteOptionState(), VoteOptionState()), | ||
val content: String = "", | ||
val isLoading: Boolean = false, | ||
) : UiState { | ||
val buttonEnabled = content.length in 1 .. 50 && | ||
voteOptionStateList.all { it.content.length in 1 ..10 } | ||
} | ||
|
||
data class VoteOptionState( | ||
val content: String = "", | ||
val isSaved: Boolean = false, | ||
) | ||
|
||
sealed interface VoteAddSideEffect : SideEffect { | ||
data object PopBackStack : VoteAddSideEffect | ||
data class HandleException(val throwable: Throwable, val retry: () -> Unit) : VoteAddSideEffect | ||
} |
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
77 changes: 77 additions & 0 deletions
77
feature/community/src/main/java/com/susu/feature/community/voteadd/VoteAddViewModel.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,77 @@ | ||
package com.susu.feature.community.voteadd | ||
|
||
import androidx.lifecycle.viewModelScope | ||
import com.susu.core.model.Category | ||
import com.susu.core.ui.base.BaseViewModel | ||
import com.susu.domain.usecase.categoryconfig.GetCategoryConfigUseCase | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.collections.immutable.toPersistentList | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class VoteAddViewModel @Inject constructor( | ||
private val getCategoryConfigUseCase: GetCategoryConfigUseCase, | ||
) : BaseViewModel<VoteAddState, VoteAddSideEffect>( | ||
VoteAddState(), | ||
) { | ||
companion object { | ||
private const val MIN_OPTION_COUNT = 2 | ||
private const val MAX_OPTION_COUNT = 5 | ||
} | ||
|
||
fun getCategoryConfig() = viewModelScope.launch { | ||
if (currentState.categoryConfigList.isNotEmpty()) return@launch | ||
|
||
getCategoryConfigUseCase() | ||
.onSuccess { categoryConfig -> | ||
intent { | ||
copy( | ||
categoryConfigList = categoryConfig.toPersistentList(), | ||
selectedCategory = categoryConfig.first(), | ||
) | ||
} | ||
} | ||
} | ||
|
||
fun popBackStack() = postSideEffect(VoteAddSideEffect.PopBackStack) | ||
fun selectCategory(category: Category) = intent { | ||
copy(selectedCategory = category) | ||
} | ||
|
||
fun updateContent(content: String) = intent { | ||
copy(content = content) | ||
} | ||
|
||
fun updateOptionContent(index: Int, content: String) = intent { | ||
copy( | ||
voteOptionStateList = voteOptionStateList.mapIndexed { voteIndex, voteOptionState -> | ||
if (index == voteIndex) voteOptionState.copy(content = content) | ||
else voteOptionState | ||
}.toPersistentList() | ||
) | ||
} | ||
|
||
fun toggleOptionSavedState(index: Int) = intent { | ||
copy( | ||
voteOptionStateList = voteOptionStateList.mapIndexed { voteIndex, voteOptionState -> | ||
if (index == voteIndex) voteOptionState.copy(isSaved = voteOptionState.isSaved.not()) | ||
else voteOptionState | ||
}.toPersistentList() | ||
) | ||
} | ||
|
||
fun removeOptionState(index: Int) = intent { | ||
if (voteOptionStateList.size <= MIN_OPTION_COUNT) return@intent this | ||
copy( | ||
voteOptionStateList = voteOptionStateList.removeAt(index) | ||
) | ||
} | ||
|
||
fun addOptionState() = intent { | ||
if (voteOptionStateList.size >= MAX_OPTION_COUNT) return@intent this | ||
copy( | ||
voteOptionStateList = voteOptionStateList.add(VoteOptionState()) | ||
) | ||
} | ||
} |