Closed jonsmirl closed 1 year ago
The way code is written it will reinitialize the viewmodel on each compose (rotate the phone).
LaunchedEffect(Unit) { viewModel.initialize(taskId) }
Instead the ViewModel should have code like this
class EditTaskViewModel @Inject constructor( savedStateHandle: SavedStateHandle, logService: LogService, private val storageService: StorageService, ) : MakeItSoViewModel(logService) { val task = mutableStateOf(Task()) init { val taskId = savedStateHandle.get<String>(TASK_ID) launchCatching { if (taskId != TASK_DEFAULT_ID) { task.value = storageService.getTask(taskId.idFromParameter()) ?: Task() } } }
Then remove the taskId parameter on the Screen
fun EditTaskScreen( popUpScreen: () -> Unit, taskId: String, ----> remove this modifier: Modifier = Modifier, viewModel: EditTaskViewModel = hiltViewModel() ) {
Hi @jonsmirl, thanks for catching this issue and for suggesting the changes. Would you like to contribute with the code and submit a pull request with this solution?
Fixed in #38
Thank you @jonsmirl
The way code is written it will reinitialize the viewmodel on each compose (rotate the phone).
Instead the ViewModel should have code like this
Then remove the taskId parameter on the Screen