Bug 1269001 - Show number of items for reading list smartfolder r?mcomella draft
authorAndrzej Hunt <andrzej@ahunt.org>
Tue, 10 May 2016 13:38:36 -0700
changeset 365466 1078d73440aa427b5a15eec4715ab0a961271faf
parent 365465 e0dbe5cef22eb1cbb1702c0d8aba0981985f2f87
child 365467 cfa07f86dfc8d96f0fbcb5022919c877352d7a51
push id17748
push userbmo:ahunt@mozilla.com
push dateTue, 10 May 2016 20:42:10 +0000
reviewersmcomella
bugs1269001
milestone49.0a1
Bug 1269001 - Show number of items for reading list smartfolder r?mcomella MozReview-Commit-ID: EEtOqum7ejn
mobile/android/base/java/org/mozilla/gecko/home/BookmarkFolderView.java
--- a/mobile/android/base/java/org/mozilla/gecko/home/BookmarkFolderView.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/BookmarkFolderView.java
@@ -1,27 +1,46 @@
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.home;
 
+import org.mozilla.gecko.GeckoProfile;
 import org.mozilla.gecko.R;
+import org.mozilla.gecko.db.BrowserContract;
+import org.mozilla.gecko.db.BrowserDB;
+import org.mozilla.gecko.util.ThreadUtils;
+import org.mozilla.gecko.util.UIAsyncTask;
 
 import android.content.Context;
 import android.support.annotation.NonNull;
 import android.util.AttributeSet;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
 import android.widget.TextView;
 
+import java.lang.ref.WeakReference;
+import java.util.Collections;
+import java.util.Set;
+import java.util.TreeSet;
+
 public class BookmarkFolderView extends LinearLayout {
+    private static final Set<Integer> FOLDERS_WITH_COUNT;
+
+    static {
+        final Set<Integer> folders = new TreeSet<>();
+        folders.add(BrowserContract.Bookmarks.FAKE_READINGLIST_SMARTFOLDER_ID);
+
+        FOLDERS_WITH_COUNT = Collections.unmodifiableSet(folders);
+    }
+
     public enum FolderState {
         /**
          * A standard folder, i.e. a folder in a list of bookmarks and folders.
          */
         FOLDER(R.drawable.folder_closed),
 
         /**
          * The parent folder: this indicates that you are able to return to the previous
@@ -56,23 +75,66 @@ public class BookmarkFolderView extends 
 
         mTitle = (TextView) findViewById(R.id.title);
         mSubtitle = (TextView) findViewById(R.id.subtitle);
         mIcon =  (ImageView) findViewById(R.id.icon);
     }
 
     public void update(String title, int folderID) {
         setTitle(title);
+        setID(folderID);
     }
 
     private void setTitle(String title) {
         mTitle.setText(title);
     }
 
-        }
+    private static void updateItemCount(final WeakReference<TextView> textViewReference,
+                                        final int folderID) {
+        (new UIAsyncTask.WithoutParams<Integer>(ThreadUtils.getBackgroundHandler()) {
+
+            @Override
+            protected Integer doInBackground() {
+                final TextView textView = textViewReference.get();
+
+                if (textView == null) {
+                    return null;
+                }
+
+                final BrowserDB db = GeckoProfile.get(textView.getContext()).getDB();
+                return db.getBookmarkCountForFolder(textView.getContext().getContentResolver(), folderID);
+            }
+
+            @Override
+            protected void onPostExecute(Integer count) {
+                final TextView textView = textViewReference.get();
 
-        mSubtitle.setVisibility(View.GONE);
+                if (textView == null) {
+                    return;
+                }
+
+                final String text;
+                if (count == 1) {
+                    text = textView.getContext().getResources().getString(R.string.bookmark_folder_one_item);
+                } else {
+                    text = textView.getContext().getResources().getString(R.string.bookmark_folder_items, count);
+                }
+
+                textView.setText(text);
+                textView.setVisibility(View.VISIBLE);
+            }
+        }).execute();
+    }
+
+    private void setID(final int folderID) {
+        if (FOLDERS_WITH_COUNT.contains(folderID)) {
+            final WeakReference<TextView> subTitleReference = new WeakReference<TextView>(mSubtitle);
+
+            updateItemCount(subTitleReference, folderID);
+        } else {
+            mSubtitle.setVisibility(View.GONE);
+        }
     }
 
     public void setState(@NonNull FolderState state) {
         mIcon.setImageResource(state.image);
     }
 }