Skip to content

Commit d2237b5

Browse files
committed
Resetting SortingViewModel values on dispose
1 parent 06b2d86 commit d2237b5

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

app/src/main/java/me/brisson/algorithm_visualizer/ui/screens/sorting/SortingScreen.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import androidx.compose.material3.IconButton
1616
import androidx.compose.material3.MaterialTheme
1717
import androidx.compose.material3.Text
1818
import androidx.compose.runtime.Composable
19+
import androidx.compose.runtime.DisposableEffect
1920
import androidx.compose.runtime.LaunchedEffect
2021
import androidx.compose.runtime.getValue
2122
import androidx.compose.ui.Alignment
@@ -46,6 +47,10 @@ fun SortingRoute(
4647
viewModel.updateInfoButtonAvailability()
4748
}
4849

50+
DisposableEffect(Unit) {
51+
onDispose { viewModel.resetAllValues() }
52+
}
53+
4954
SortingScreen(
5055
modifier = modifier,
5156
uiState = uiState,

app/src/main/java/me/brisson/algorithm_visualizer/ui/screens/sorting/SortingViewModel.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class SortingViewModel(savedStateHandle: SavedStateHandle) : ViewModel() {
2626
private val _sortingClassName: String =
2727
checkNotNull(savedStateHandle[AppNavigationArgs.SORT_ALGORITHM_CLASS_NAME])
2828

29-
private val sortClass: Sort? = Sort.instantiateClass(_sortingClassName)
29+
private var sortClass: Sort? = Sort.instantiateClass(_sortingClassName)
3030

3131
private val _uiState = MutableStateFlow(SortingUIState())
3232
val uiState: StateFlow<SortingUIState> = _uiState.asStateFlow()
@@ -69,12 +69,13 @@ class SortingViewModel(savedStateHandle: SavedStateHandle) : ViewModel() {
6969
_uiState.update { it.copy(infoMdResId = enumAlgorithm?.mdResId) }
7070
}
7171

72-
// fun getAlgorithmMdResId(): Int {
73-
// val enumAlgorithm = enumValues<SortingAlgorithms>().find {
74-
// it.algorithmClass.algorithmName == sortClass?.algorithmName
75-
// }
76-
// return enumAlgorithm?.mdResId ?: -1
77-
// }
72+
fun resetAllValues() {
73+
sortClass = Sort.instantiateClass(_sortingClassName)
74+
chartStateList = emptyList()
75+
messageLogList = emptyList()
76+
77+
_uiState.value = SortingUIState()
78+
}
7879

7980
fun onEvent(event: SortingEvents) {
8081
when (event) {

0 commit comments

Comments
 (0)