From 7040129ddcd8e52eeeba6c51e0aa344d1b7422ee Mon Sep 17 00:00:00 2001
From: Dan Roundhill <dan@automattic.com>
Date: Mon, 9 Dec 2013 15:30:29 -0800
Subject: [PATCH] =?UTF-8?q?Better=20solution=20for=20#411=20based=20on=20f?=
 =?UTF-8?q?eedback=20from=20@nbradbury.=20Limit=20the=20number=20of=20?=
 =?UTF-8?q?=E2=80=98list=E2=80=99=20type=20stats=20fragments=20to=20the=20?=
 =?UTF-8?q?STATS=5FGROUP=5FMAX=5FITEMS=20constant=20value=20at=20the=20sql?=
 =?UTF-8?q?=20level.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../android/datasets/StatsClickGroupsTable.java          | 5 +++--
 .../wordpress/android/datasets/StatsGeoviewsTable.java   | 3 ++-
 .../android/datasets/StatsReferrerGroupsTable.java       | 9 +++++----
 .../android/datasets/StatsSearchEngineTermsTable.java    | 3 ++-
 .../android/datasets/StatsTopPostsAndPagesTable.java     | 3 ++-
 src/org/wordpress/android/ui/stats/StatsActivity.java    | 3 +++
 .../wordpress/android/ui/stats/StatsCursorFragment.java  | 5 +----
 .../android/ui/stats/StatsCursorTreeFragment.java        | 8 +++-----
 8 files changed, 21 insertions(+), 18 deletions(-)

diff --git a/src/org/wordpress/android/datasets/StatsClickGroupsTable.java b/src/org/wordpress/android/datasets/StatsClickGroupsTable.java
index f07f354c7678..d4bd2d69c2dc 100644
--- a/src/org/wordpress/android/datasets/StatsClickGroupsTable.java
+++ b/src/org/wordpress/android/datasets/StatsClickGroupsTable.java
@@ -9,6 +9,7 @@
 import android.net.Uri;
 
 import org.wordpress.android.models.StatsClickGroup;
+import org.wordpress.android.ui.stats.StatsActivity;
 import org.wordpress.android.ui.stats.StatsTimeframe;
 
 /**
@@ -87,12 +88,12 @@ public static ContentValues getContentValues(StatsClickGroup item) {
     @Override
     public Cursor query(SQLiteDatabase database, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
         
-        String sort = NAME + "." + Columns.TOTAL + " DESC, " + NAME + "." + Columns.NAME + " ASC";
+        String sort = NAME + "." + Columns.TOTAL + " DESC, " + NAME + "." + Columns.NAME + " ASC LIMIT " + StatsActivity.STATS_GROUP_MAX_ITEMS;
         
         String timeframe = uri.getQueryParameter("timeframe");
         if (timeframe == null)
             return super.query(database, uri, projection, selection, selectionArgs, sort);
-        
+
         // get the latest for "Today", and the next latest for "Yesterday"
         if (timeframe.equals(StatsTimeframe.TODAY.name())) {
             return database.rawQuery("SELECT * FROM " + NAME +", " +
diff --git a/src/org/wordpress/android/datasets/StatsGeoviewsTable.java b/src/org/wordpress/android/datasets/StatsGeoviewsTable.java
index d87abac3c252..d4f55ba05d2d 100644
--- a/src/org/wordpress/android/datasets/StatsGeoviewsTable.java
+++ b/src/org/wordpress/android/datasets/StatsGeoviewsTable.java
@@ -9,6 +9,7 @@
 import android.net.Uri;
 
 import org.wordpress.android.models.StatsGeoview;
+import org.wordpress.android.ui.stats.StatsActivity;
 import org.wordpress.android.ui.stats.StatsTimeframe;
 
 /**
@@ -77,7 +78,7 @@ public static ContentValues getContentValues(StatsGeoview item) {
     @Override
     public Cursor query(SQLiteDatabase database, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
         
-        String sort = NAME + "." + Columns.VIEWS + " DESC, " + NAME + "." + Columns.COUNTRY + " ASC";
+        String sort = NAME + "." + Columns.VIEWS + " DESC, " + NAME + "." + Columns.COUNTRY + " ASC LIMIT " + StatsActivity.STATS_GROUP_MAX_ITEMS;
         
         String timeframe = uri.getQueryParameter("timeframe");
         if (timeframe == null)
diff --git a/src/org/wordpress/android/datasets/StatsReferrerGroupsTable.java b/src/org/wordpress/android/datasets/StatsReferrerGroupsTable.java
index ad7a87e21e6c..62ecfcd74651 100644
--- a/src/org/wordpress/android/datasets/StatsReferrerGroupsTable.java
+++ b/src/org/wordpress/android/datasets/StatsReferrerGroupsTable.java
@@ -1,16 +1,17 @@
 package org.wordpress.android.datasets;
 
-import java.util.LinkedHashMap;
-import java.util.Map;
-
 import android.content.ContentValues;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
 import android.net.Uri;
 
 import org.wordpress.android.models.StatsReferrerGroup;
+import org.wordpress.android.ui.stats.StatsActivity;
 import org.wordpress.android.ui.stats.StatsTimeframe;
 
+import java.util.LinkedHashMap;
+import java.util.Map;
+
 /**
  * A database table to represent groups in the stats for referrers.
  * A group may or may not have children. 
@@ -88,7 +89,7 @@ public static ContentValues getContentValues(StatsReferrerGroup item) {
     @Override
     public Cursor query(SQLiteDatabase database, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
         
-        String sort = NAME + "." + Columns.TOTAL + " DESC, " + NAME + "." + Columns.NAME + " ASC";
+        String sort = NAME + "." + Columns.TOTAL + " DESC, " + NAME + "." + Columns.NAME + " ASC LIMIT " + StatsActivity.STATS_GROUP_MAX_ITEMS;
         
         String timeframe = uri.getQueryParameter("timeframe");
         if (timeframe == null)
diff --git a/src/org/wordpress/android/datasets/StatsSearchEngineTermsTable.java b/src/org/wordpress/android/datasets/StatsSearchEngineTermsTable.java
index 9f48c7c69c77..1c48d0b15c82 100644
--- a/src/org/wordpress/android/datasets/StatsSearchEngineTermsTable.java
+++ b/src/org/wordpress/android/datasets/StatsSearchEngineTermsTable.java
@@ -9,6 +9,7 @@
 import android.net.Uri;
 
 import org.wordpress.android.models.StatsSearchEngineTerm;
+import org.wordpress.android.ui.stats.StatsActivity;
 import org.wordpress.android.ui.stats.StatsTimeframe;
 
 /**
@@ -73,7 +74,7 @@ public static ContentValues getContentValues(StatsSearchEngineTerm item) {
 
     @Override
     public Cursor query(SQLiteDatabase database, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
-        String sort = NAME + "." + Columns.VIEWS + " DESC, " + NAME + "." + Columns.SEARCH + " ASC";
+        String sort = NAME + "." + Columns.VIEWS + " DESC, " + NAME + "." + Columns.SEARCH + " ASC LIMIT " + StatsActivity.STATS_GROUP_MAX_ITEMS;
         
         String timeframe = uri.getQueryParameter("timeframe");
         if (timeframe == null)
diff --git a/src/org/wordpress/android/datasets/StatsTopPostsAndPagesTable.java b/src/org/wordpress/android/datasets/StatsTopPostsAndPagesTable.java
index 2f626b44fa5b..31b39b79bf4e 100644
--- a/src/org/wordpress/android/datasets/StatsTopPostsAndPagesTable.java
+++ b/src/org/wordpress/android/datasets/StatsTopPostsAndPagesTable.java
@@ -9,6 +9,7 @@
 import android.net.Uri;
 
 import org.wordpress.android.models.StatsTopPostsAndPages;
+import org.wordpress.android.ui.stats.StatsActivity;
 import org.wordpress.android.ui.stats.StatsTimeframe;
 
 /**
@@ -79,7 +80,7 @@ public static ContentValues getContentValues(StatsTopPostsAndPages item) {
 
     @Override
     public Cursor query(SQLiteDatabase database, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
-        String sort = NAME + "." + Columns.VIEWS + " DESC, " + NAME + "." + Columns.TITLE + " ASC";
+        String sort = NAME + "." + Columns.VIEWS + " DESC, " + NAME + "." + Columns.TITLE + " ASC LIMIT " + StatsActivity.STATS_GROUP_MAX_ITEMS;
         
         String timeframe = uri.getQueryParameter("timeframe");
         if (timeframe == null)
diff --git a/src/org/wordpress/android/ui/stats/StatsActivity.java b/src/org/wordpress/android/ui/stats/StatsActivity.java
index a8a564178120..0939aa6128a5 100644
--- a/src/org/wordpress/android/ui/stats/StatsActivity.java
+++ b/src/org/wordpress/android/ui/stats/StatsActivity.java
@@ -54,6 +54,9 @@
  */
 public class StatsActivity extends WPActionBarActivity {
 
+    // Max number of rows to show in a stats fragment
+    public static final int STATS_GROUP_MAX_ITEMS = 10;
+
     private static final String SAVED_NAV_POSITION = "SAVED_NAV_POSITION";
     private static final String SAVED_WP_LOGIN_STATE = "SAVED_WP_LOGIN_STATE";
     private static final int REQUEST_JETPACK = 7000;
diff --git a/src/org/wordpress/android/ui/stats/StatsCursorFragment.java b/src/org/wordpress/android/ui/stats/StatsCursorFragment.java
index 264ccc684915..78813dee693e 100644
--- a/src/org/wordpress/android/ui/stats/StatsCursorFragment.java
+++ b/src/org/wordpress/android/ui/stats/StatsCursorFragment.java
@@ -17,14 +17,12 @@
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.LinearLayout;
-import android.widget.ListView;
 import android.widget.TextView;
 
 import com.actionbarsherlock.app.SherlockFragment;
 
 import org.wordpress.android.R;
 import org.wordpress.android.WordPress;
-import org.wordpress.android.util.Utils;
 
 /**
  * A fragment that appears as a 'page' in the {@link StatsAbsPagedViewFragment}. 
@@ -41,7 +39,6 @@
  */
 public class StatsCursorFragment extends SherlockFragment implements LoaderManager.LoaderCallbacks<Cursor> {
 
-    private static final int MAX_ITEMS_ON_TABLET = 10;
     private static final String ARGS_URI = "ARGS_URI";
     private static final String ARGS_ENTRY_LABEL = "ARGS_ENTRY_LABEL";
     private static final String ARGS_TOTALS_LABEL = "ARGS_TOTALS_LABEL";
@@ -164,7 +161,7 @@ private void reloadLinearLayout() {
         mLinearLayout.removeAllViews();
         
         // limit number of items to show otherwise it would cause performance issues on the linearlayout
-        int count = Math.min(mAdapter.getCount(), MAX_ITEMS_ON_TABLET);
+        int count = Math.min(mAdapter.getCount(), StatsActivity.STATS_GROUP_MAX_ITEMS);
         for (int i = 0; i < count; i++) {
             View view = mAdapter.getView(i, null, mLinearLayout);
             if (i % 2 == 1)
diff --git a/src/org/wordpress/android/ui/stats/StatsCursorTreeFragment.java b/src/org/wordpress/android/ui/stats/StatsCursorTreeFragment.java
index 81d9aa942e64..01e73e4715bb 100644
--- a/src/org/wordpress/android/ui/stats/StatsCursorTreeFragment.java
+++ b/src/org/wordpress/android/ui/stats/StatsCursorTreeFragment.java
@@ -46,9 +46,7 @@
  */
 public class StatsCursorTreeFragment extends SherlockFragment implements LoaderManager.LoaderCallbacks<Cursor>, StatsCursorLoaderCallback {
 
-    private static final int MAX_ITEMS = 10;
     private static final int LOADER_URI_GROUP_INDEX = -1;
-    
     private static final String ARGS_GROUP_URI = "ARGS_GROUP_URI";
     private static final String ARGS_CHILDREN_URI = "ARGS_CHILDREN_URI";
     private static final String ARGS_ENTRY_LABEL = "ARGS_ENTRY_LABEL";
@@ -175,8 +173,8 @@ public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
         if (loader.getId() == LOADER_URI_GROUP_INDEX) {
 
             // start loaders on children
-            for (int i = 0; i < Math.min(data.getCount(), MAX_ITEMS); i++) {
-                data.moveToPosition(i);
+            while (data.moveToNext()) {
+                //data.moveToPosition(i);
                 String groupId = data.getString(data.getColumnIndex("groupId"));
                 long date = data.getLong(data.getColumnIndex("date"));
 
@@ -234,7 +232,7 @@ private void reloadLinearLayout() {
         mLinearLayout.removeAllViews();
         
         // limit number of items to show otherwise it would cause performance issues on the linearlayout
-        int groupCount = Math.min(mAdapter.getGroupCount(), MAX_ITEMS);
+        int groupCount = Math.min(mAdapter.getGroupCount(), StatsActivity.STATS_GROUP_MAX_ITEMS);
         for (int i = 0; i < groupCount; i++) {
             
             boolean isExpanded = mGroupIdToExpandedMap.get(i);