G'day everyone. The app's now saving timing data, but it's not very user friendly.
We need to update the display, when the user starts timing a task.
The Timing data is being saved in our TaskTimerViewModel, and we want to display
some information in MainActivity. That sounds like the job of another LiveData
object, that MainActivity can observe. I'll create the LiveData objects in
TaskTimerViewModel, after the database cursor, LiveData.
We expose a LiveData object called Timing, and update it via the mutable LiveData object called taskTiming.
Refer back to the videos in section 11, if you're not sure how this works.
The code to update timing goes at the end of our timeTask function.
We have to check that something is being timed, because this function gets called
to stop timing, as well as to start it. Okay, MainActivity has to observe this
LiveData object, and update the display when it changes. We start by declaring
the view model,
then we can observe it to update the display. I'll set that up in onCreate.
You want android x dot lifecycle dot observer, when prompted for the input.
Run the app, and the task being timed is now shown at the top of the display.
When I stop typing a task, the message disappears. We can do better
by displaying a message, instead of just a task name.
Split the function here, and add the following code.
I haven't added the task name to the string, Currently timing, because it's
easier to extract the string resources without it. Remember, the option doesn't
appear if you include the variable. I'll call the string resource timing underscore message.
We then need to add a value to the string. We can add that in
this dialog, so that the text becomes Currently timing space % $1 s. We haven't
done that before, but it can be easier than going into the strings dot XML file.
I'll open that anyway, just so that we can check.
While I'm here, the message No tasks selected may be better as No task being timed.
I'll change that on line 9.
Now we can provide the task name in MainActivity.
Alright, let's see what that looks like.
When I long-tap a task, we get an informative message. As I long-tap
different tasks, the message changes. When I long-tap the same task again, we have
the No task being timed message. That wasn't a lot of extra work, and it makes
the app much more user friendly. In the next video, we'll look at what happens if
the app is closed down, while we're timing a task. See you in the next one.