The following is the first few sections of a chapter from Exploring Android, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Tracking Our Load Status

There are three logical states that our RosterListFragment and its RecyclerView can be in:

That third state is not being handled by the app. Instead, we treat “do not know” as being the same as “we do not have to-do items” — we show the “empty” view if our RosterListAdapter is empty, no matter why it is empty. Plus, it would be nice to show some sort of “loading” indicator while the data load is in progress… such as a ProgressBar.

So, in this tutorial, we will fix this. For most Android devices, and for shorter to-do lists, the difference will not be visible, as the data will load very rapidly. However, on slower devices, or with large to-do lists, the difference may be noticeable.

This is a continuation of the work we did in the previous tutorial. The book’s GitHub repository contains the results of the previous tutorial as well as the results of completing the work in this tutorial](https://github.com/commonsguy/cw-andexplore/tree/master/T31-Load/ToDo).

Step #1: Add an isLoaded() Property

First, we need to track whether or not we have loaded our data. Since we need to render this information in the UI, part of the “state” tracked by the ViewState should be a flag indicating whether we have loaded our data yet or not. Initially, that flag would indicate that the data is not yet loaded; we can then flip that flag when the data is loaded.

First, add a new property (to be managed by AutoValue) to ViewState:

  public abstract boolean isLoaded();

Then, add the corresponding setter method to the ViewState.Builder class:

    abstract Builder isLoaded(boolean isLoaded);

Next, modify toBuilder() to copy the isLoaded value from the current instance to the Builder, the way that we are copying the other properties:

  Builder toBuilder() {
    return builder().items(items()).current(current()).isLoaded(isLoaded());
  }

Finally, to initially set the value, modify builder() to set isLoaded to false:

  static Builder builder() {
    return new AutoValue_ViewState.Builder().isLoaded(false);
  }

Pre-populating a Builder is a common way with AutoValue to provide initial default values. In practice, probably the underlying boolean would be initialized to false, but this way, we are making that assignment explicit.

Step #2: Updating the Loaded Status

The preview of this section was traded for a bag of magic beans.

Step #3: Adjusting Our Layout

The preview of this section may contain nuts.

Step #4: Reacting to the Loaded Status

The preview of this section was lost due to a rupture in the space-time continuum.

What We Changed

The preview of this section is sleeping in.