From aa268868944831ec0cc40bc249f41121a4028eb1 Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Mon, 30 Dec 2019 13:52:24 +0100 Subject: [PATCH] Add a progress bar in the folder layout to represent the number of feeds by folder out of the max number of feeds --- .../readrops/app/adapters/FoldersAdapter.java | 37 ++++++++++--------- .../app/fragments/FoldersFragment.java | 21 ++++++++++- .../ManageFeedsFoldersViewModel.java | 4 ++ app/src/main/res/layout/folder_layout.xml | 16 +++++++- 4 files changed, 59 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/readrops/app/adapters/FoldersAdapter.java b/app/src/main/java/com/readrops/app/adapters/FoldersAdapter.java index bd77bed9..9614c26a 100644 --- a/app/src/main/java/com/readrops/app/adapters/FoldersAdapter.java +++ b/app/src/main/java/com/readrops/app/adapters/FoldersAdapter.java @@ -21,6 +21,7 @@ import java.util.List; public class FoldersAdapter extends ListAdapter { private ManageFoldersListener listener; + private int totalFeedCount; public FoldersAdapter(ManageFoldersListener listener) { super(DIFF_CALLBACK); @@ -28,6 +29,9 @@ public class FoldersAdapter extends ListAdapter DIFF_CALLBACK = new DiffUtil.ItemCallback() { @Override @@ -59,13 +63,10 @@ public class FoldersAdapter extends ListAdapter payloads) { - if (payloads.size() > 0) { - FolderWithFeedCount folder = (FolderWithFeedCount) payloads.get(0); + if (!payloads.isEmpty()) { + FolderWithFeedCount folderWithFeedCount = (FolderWithFeedCount) payloads.get(0); - holder.binding.folderName.setText(folder.getFolder().getName()); - - int stringRes = folder.getFeedCount() > 1 ? R.string.feeds_number : R.string.feed_number; - holder.binding.folderFeedsCount.setText(holder.itemView.getContext().getString(stringRes, String.valueOf(folder.getFeedCount()))); + holder.bind(folderWithFeedCount); } else onBindViewHolder(holder, position); @@ -73,18 +74,10 @@ public class FoldersAdapter extends ListAdapter 1 ? R.string.feeds_number : R.string.feed_number; - holder.binding.folderFeedsCount.setText(holder.itemView.getContext().getString(stringRes, String.valueOf(folder.getFeedCount()))); - - holder.itemView.setOnClickListener(v -> listener.onClick(folder.getFolder())); - } - - public Folder getFolder(int position) { - return getItem(position).getFolder(); + holder.bind(folderWithFeedCount); + holder.itemView.setOnClickListener(v -> listener.onClick(folderWithFeedCount.getFolder())); } public interface ManageFoldersListener { @@ -100,5 +93,15 @@ public class FoldersAdapter extends ListAdapter 1 ? R.string.feeds_number : R.string.feed_number; + binding.folderFeedsCount.setText(itemView.getContext().getString(stringRes, String.valueOf(folderWithFeedCount.getFeedCount()))); + + binding.folderProgressBar.setMax(totalFeedCount); + binding.folderProgressBar.setProgress(folderWithFeedCount.getFeedCount()); + } } } diff --git a/app/src/main/java/com/readrops/app/fragments/FoldersFragment.java b/app/src/main/java/com/readrops/app/fragments/FoldersFragment.java index 8efde396..47839447 100644 --- a/app/src/main/java/com/readrops/app/fragments/FoldersFragment.java +++ b/app/src/main/java/com/readrops/app/fragments/FoldersFragment.java @@ -27,6 +27,7 @@ import com.readrops.readropslibrary.utils.ConflictException; import com.readrops.readropslibrary.utils.UnknownFormatException; import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.observers.DisposableSingleObserver; import io.reactivex.schedulers.Schedulers; import static com.readrops.app.utils.ReadropsKeys.ACCOUNT; @@ -68,10 +69,28 @@ public class FoldersFragment extends Fragment { viewModel = ViewModelProviders.of(this).get(ManageFeedsFoldersViewModel.class); viewModel.setAccount(account); + viewModel.getFeedCountByAccount() + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(new DisposableSingleObserver() { + @Override + public void onSuccess(Integer feedCount) { + adapter.setTotalFeedCount(feedCount); + getFoldersWithFeedCount(); + } + + @Override + public void onError(Throwable e) { + Utils.showSnackbar(binding.foldersRoot, e.getMessage()); + } + }); + } + + private void getFoldersWithFeedCount() { viewModel.getFoldersWithFeedCount().observe(this, folders -> { adapter.submitList(folders); - if (folders.size() > 0) { + if (!folders.isEmpty()) { binding.foldersEmptyList.setVisibility(View.GONE); } else { binding.foldersEmptyList.setVisibility(View.VISIBLE); diff --git a/app/src/main/java/com/readrops/app/viewmodels/ManageFeedsFoldersViewModel.java b/app/src/main/java/com/readrops/app/viewmodels/ManageFeedsFoldersViewModel.java index 4fe9f790..8ad3f76d 100644 --- a/app/src/main/java/com/readrops/app/viewmodels/ManageFeedsFoldersViewModel.java +++ b/app/src/main/java/com/readrops/app/viewmodels/ManageFeedsFoldersViewModel.java @@ -85,4 +85,8 @@ public class ManageFeedsFoldersViewModel extends AndroidViewModel { public Completable deleteFeed(Feed feed) { return repository.deleteFeed(feed); } + + public Single getFeedCountByAccount() { + return db.feedDao().getFeedCount(account.getId()); + } } diff --git a/app/src/main/res/layout/folder_layout.xml b/app/src/main/res/layout/folder_layout.xml index ab1fd741..45e0cb2f 100644 --- a/app/src/main/res/layout/folder_layout.xml +++ b/app/src/main/res/layout/folder_layout.xml @@ -15,7 +15,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:background="?attr/selectableItemBackground" - android:padding="8dp"> + android:paddingTop="8dp"> @@ -32,8 +33,21 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" + android:layout_marginEnd="8dp" tools:text="15 feeds" /> + +