Skip to content

Commit 4118a0e

Browse files
committed
code optimization.
1 parent bb1a808 commit 4118a0e

3 files changed

Lines changed: 13 additions & 15 deletions

File tree

app/src/main/java/com/commandiron/vacationchecklist/presentation/checklist/ChecklistScreen.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@ fun ChecklistScreen(
3131
}
3232
}
3333
val state = viewModel.state
34-
val checkItems = viewModel.checkItems
3534
val spacing = LocalSpacing.current
3635
state.activeVacation?.let { vacation ->
37-
checkItems.value?.let { checkItems ->
36+
state.checkItems?.let { checkItems ->
3837
Column(
3938
modifier = Modifier
4039
.fillMaxSize()

app/src/main/java/com/commandiron/vacationchecklist/presentation/checklist/ChecklistState.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.commandiron.vacationchecklist.domain.model.Vacation
55

66
data class ChecklistState(
77
val activeVacation: Vacation? = null,
8+
val checkItems: List<CheckItem>? = null,
89
val checkedItem: CheckItem? = null,
910
val doubleCheckEnabled: Boolean = false,
1011
val isLoading: Boolean = false,

app/src/main/java/com/commandiron/vacationchecklist/presentation/checklist/ChecklistViewModel.kt

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package com.commandiron.vacationchecklist.presentation.checklist
22

3-
import androidx.compose.runtime.State
43
import androidx.compose.runtime.getValue
54
import androidx.compose.runtime.mutableStateOf
65
import androidx.compose.runtime.setValue
76
import androidx.lifecycle.ViewModel
87
import androidx.lifecycle.viewModelScope
9-
import com.commandiron.vacationchecklist.domain.model.CheckItem
108
import com.commandiron.vacationchecklist.domain.model.vacations
119
import com.commandiron.vacationchecklist.domain.preferences.Preferences
1210
import com.commandiron.vacationchecklist.domain.use_cases.UseCases
@@ -26,9 +24,6 @@ class ChecklistViewModel @Inject constructor(
2624
var state by mutableStateOf(ChecklistState())
2725
private set
2826

29-
private val _checkItems = mutableStateOf<List<CheckItem>?>(null)
30-
val checkItems: State<List<CheckItem>?> = _checkItems
31-
3227
private val _uiEvent = Channel<UiEvent>()
3328
val uiEvent = _uiEvent.receiveAsFlow()
3429

@@ -122,7 +117,9 @@ class ChecklistViewModel @Inject constructor(
122117

123118
private fun getChecklistItems() {
124119
viewModelScope.launch {
125-
_checkItems.value = useCases.getAllCheckItems()
120+
state = state.copy(
121+
checkItems = useCases.getAllCheckItems()
122+
)
126123
calculateCheckCount()
127124
}
128125
}
@@ -155,11 +152,13 @@ class ChecklistViewModel @Inject constructor(
155152

156153
private fun check(){
157154
state.checkedItem?.let { checkedItem ->
158-
_checkItems.value = _checkItems.value?.toMutableList()?.also {
159-
it[it.indexOf(checkedItem)] = it[it.indexOf(checkedItem)].copy(
160-
isChecked = !it[it.indexOf(checkedItem)].isChecked
161-
)
162-
}
155+
state = state.copy(
156+
checkItems = state.checkItems?.toMutableList()?.also {
157+
it[it.indexOf(checkedItem)] = it[it.indexOf(checkedItem)].copy(
158+
isChecked = !it[it.indexOf(checkedItem)].isChecked
159+
)
160+
}
161+
)
163162
viewModelScope.launch {
164163
useCases.insertCheckItem(checkedItem.copy(isChecked = !checkedItem.isChecked))
165164
}
@@ -169,13 +168,12 @@ class ChecklistViewModel @Inject constructor(
169168

170169

171170
private fun calculateCheckCount(){
172-
_checkItems.value?.let { checkItems ->
171+
state.checkItems?.let { checkItems ->
173172
state = state.copy(
174173
totalCheckCount = checkItems.size,
175174
checkCount = checkItems.filter { it.isChecked }.size
176175
)
177176
}
178-
179177
if(state.checkCount == state.totalCheckCount){
180178
state = state.copy(
181179
isChecklistCompeted = true

0 commit comments

Comments
 (0)