Step #6: Adapting DisplayFragment

Similarly, we need to update DisplayFragment and EditFragment to handle the changes that we made to SingleModelMotor. First, let’s fix DisplayFragment, as it is simpler: change onViewCreated() to be:

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    viewLifecycleOwner.lifecycleScope.launchWhenStarted {
      motor.states.collect { state ->
        state.item?.let {
          binding?.apply {
            completed.visibility =
              if (it.isCompleted) View.VISIBLE else View.GONE
            desc.text = it.description
            createdOn.text = DateUtils.getRelativeDateTimeString(
              requireContext(),
              it.createdOn.toEpochMilli(),
              DateUtils.MINUTE_IN_MILLIS,
              DateUtils.WEEK_IN_MILLIS,
              0
            )
            notes.text = it.notes
          }
        }
      }
    }
  }

Here, we observe the view-states from our SingleModelMotor. When we get one, we do the same work as before: if the model is not null, we populate the widgets based on that model.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.