FirebaseExtended / make-it-so-android

Apache License 2.0
214 stars 58 forks source link

The EditTask ViewModel is not being initialized correctly. #37

Closed jonsmirl closed 1 year ago

jonsmirl commented 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()
) {
marinacoelho commented 1 year ago

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?

thatfiredev commented 1 year ago

Fixed in #38

Thank you @jonsmirl