Change List: Do not load another page each time the list is shown

Load the first page when the change list is shown for the first time,
else just show the changes which have already been loaded.

Change-Id: I45dae639bd2b93e25d152a708c4414258d7fd3d6
Signed-off-by: Edwin Kempin <ekempin@google.com>
diff --git a/app/src/main/java/com/google/reviewit/ReviewChangesFragment.java b/app/src/main/java/com/google/reviewit/ReviewChangesFragment.java
index 6e92452..c6fd272 100644
--- a/app/src/main/java/com/google/reviewit/ReviewChangesFragment.java
+++ b/app/src/main/java/com/google/reviewit/ReviewChangesFragment.java
@@ -61,7 +61,13 @@
 
     TaskObserver.enableProgressBar(getWindow());
     init();
-    display();
+
+    List<Change> loadedChanges = getApp().getQueryHandler().getLoadedChanges();
+    if (loadedChanges.isEmpty()) {
+      loadAndDisplay();
+    } else {
+      display(loadedChanges);
+    }
   }
 
   private void init() {
@@ -74,7 +80,7 @@
           @Override
           public void onRefresh() {
             getApp().getQueryHandler().reset();
-            display(true);
+            loadAndDisplay(true);
           }
         });
 
@@ -97,18 +103,18 @@
               if ((lastChild.getBottom()
                   - (scrollView.getHeight() + scrollView.getScrollY())) == 0) {
                 setVisible(nextPageProgress);
-                display();
+                loadAndDisplay();
               }
             }
           }
         });
   }
 
-  private void display() {
-    display(false);
+  private void loadAndDisplay() {
+    loadAndDisplay(false);
   }
 
-  private void display(final boolean clear) {
+  private void loadAndDisplay(final boolean clear) {
     if (!isOnline()) {
       setInvisible(v(R.id.progress));
       setGone(v(R.id.initialProgress));
@@ -198,6 +204,20 @@
     }.execute();
   }
 
+  private void display(List<Change> changes) {
+    setInvisible(v(R.id.progress));
+    setGone(v(R.id.initialProgress, R.id.reloadButton, R.id.nextPageProgress,
+        R.id.statusText));
+    ((SwipeRefreshLayout) v(R.id.swipeRefreshLayout)).setRefreshing(false);
+    ViewGroup changeList = vg(R.id.changeList);
+    for (Change change : changes) {
+      ChangeEntry changeEntry = new ChangeEntry(getContext());
+      changeEntry.init(ReviewChangesFragment.this, getApp(), change);
+      changeList.addView(changeEntry);
+      addSeparator(changeList);
+    }
+  }
+
   private void addSeparator(ViewGroup viewGroup) {
     View separator = new View(getContext());
     separator.setLayoutParams(
diff --git a/app/src/main/java/com/google/reviewit/app/QueryHandler.java b/app/src/main/java/com/google/reviewit/app/QueryHandler.java
index bb796e6..03f60ca 100644
--- a/app/src/main/java/com/google/reviewit/app/QueryHandler.java
+++ b/app/src/main/java/com/google/reviewit/app/QueryHandler.java
@@ -17,6 +17,7 @@
 import com.google.common.base.Function;
 import com.google.common.base.Predicate;
 import com.google.common.collect.Collections2;
+import com.google.common.collect.ImmutableList;
 import com.google.gerrit.extensions.client.ListChangesOption;
 import com.google.gerrit.extensions.common.ChangeInfo;
 import com.google.gerrit.extensions.restapi.RestApiException;
@@ -75,6 +76,10 @@
     return more;
   }
 
+  public List<Change> getLoadedChanges() {
+    return ImmutableList.copyOf(result);
+  }
+
   private void query() throws RestApiException {
     result.addAll(Collections2.transform(Collections2.filter(
         gerrit.api()