Bug 1269001 - Show number of items for reading list smartfolder r?mcomella
MozReview-Commit-ID: EEtOqum7ejn
--- 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);
}
}