From 3104232e5d8c355aa090e13cc9ff871beb200b4e Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Sat, 2 Jan 2021 20:17:17 +0100 Subject: [PATCH] Calculate layer in makeFlatDrawerData --- .../antennapod/adapter/NavListAdapter.java | 2 +- .../fragment/NavDrawerFragment.java | 27 ++++++++++++++----- .../antennapod/core/storage/DBReader.java | 10 +++---- .../core/storage/NavDrawerData.java | 21 ++++++++++----- 4 files changed, 40 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/de/danoeh/antennapod/adapter/NavListAdapter.java b/app/src/main/java/de/danoeh/antennapod/adapter/NavListAdapter.java index 83abd4091..5533197b9 100644 --- a/app/src/main/java/de/danoeh/antennapod/adapter/NavListAdapter.java +++ b/app/src/main/java/de/danoeh/antennapod/adapter/NavListAdapter.java @@ -303,7 +303,7 @@ public class NavListAdapter extends RecyclerView.Adapter holder.count.setVisibility(View.GONE); } holder.title.setText(item.getTitle()); - holder.itemView.setPadding(item.layer * 50, 0, 0, 0); // TODO + holder.itemView.setPadding(item.getLayer() * 50, 0, 0, 0); // TODO } private void bindFeedView(NavDrawerData.FeedDrawerItem drawerItem, FeedHolder holder) { diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/NavDrawerFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/NavDrawerFragment.java index b7d968828..3d82bf7a1 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/NavDrawerFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/NavDrawerFragment.java @@ -19,6 +19,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.VisibleForTesting; import androidx.appcompat.app.AlertDialog; +import androidx.core.util.Pair; import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -379,8 +380,15 @@ public class NavDrawerFragment extends Fragment implements SharedPreferences.OnS } else { openFolders.add(folder.name); } - flatItemList = makeFlatDrawerData(navDrawerData.items); - navAdapter.notifyDataSetChanged(); + + disposable = Observable.fromCallable(() -> makeFlatDrawerData(navDrawerData.items, 0)) + .subscribeOn(Schedulers.computation()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe( + result -> { + flatItemList = result; + navAdapter.notifyDataSetChanged(); + }, error -> Log.e(TAG, Log.getStackTraceString(error))); } } } else if (UserPreferences.getSubscriptionsFilter().isEnabled() @@ -403,13 +411,17 @@ public class NavDrawerFragment extends Fragment implements SharedPreferences.OnS private void loadData() { progressBar.setVisibility(View.VISIBLE); - disposable = Observable.fromCallable(DBReader::getNavDrawerData) + disposable = Observable.fromCallable( + () -> { + NavDrawerData data = DBReader.getNavDrawerData(); + return new Pair<>(data, makeFlatDrawerData(data.items, 0)); + }) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( result -> { - navDrawerData = result; - flatItemList = makeFlatDrawerData(navDrawerData.items); // TODO: This is the main thread! + navDrawerData = result.first; + flatItemList = result.second; updateSelection(); // Selected item might be a feed navAdapter.notifyDataSetChanged(); progressBar.setVisibility(View.GONE); @@ -419,15 +431,16 @@ public class NavDrawerFragment extends Fragment implements SharedPreferences.OnS }); } - private List makeFlatDrawerData(List items) { + private List makeFlatDrawerData(List items, int layer) { List flatItems = new ArrayList<>(); for (NavDrawerData.DrawerItem item : items) { + item.setLayer(layer); flatItems.add(item); if (item.type == NavDrawerData.DrawerItem.Type.FOLDER) { NavDrawerData.FolderDrawerItem folder = ((NavDrawerData.FolderDrawerItem) item); folder.isOpen = openFolders.contains(folder.name); if (folder.isOpen) { - flatItems.addAll(makeFlatDrawerData(((NavDrawerData.FolderDrawerItem) item).children)); + flatItems.addAll(makeFlatDrawerData(((NavDrawerData.FolderDrawerItem) item).children, layer + 1)); } } } diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java index e1bb93db3..e285d82ff 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java @@ -868,22 +868,22 @@ public final class DBReader { List items = new ArrayList<>(); for (int i = 0, feedsSize = feeds.size(); i < feedsSize; i++) { - items.add(new NavDrawerData.FeedDrawerItem(feeds.get(i), 0, i, feedCounters.get(feeds.get(i).getId()))); + items.add(new NavDrawerData.FeedDrawerItem(feeds.get(i), i, feedCounters.get(feeds.get(i).getId()))); } List folderItems = new ArrayList<>(); for (int i = 0, feedsSize = feeds.size(); i < feedsSize; i++) { - folderItems.add(new NavDrawerData.FeedDrawerItem(feeds.get(i), 1, 10001 + i, + folderItems.add(new NavDrawerData.FeedDrawerItem(feeds.get(i), 10001 + i, feedCounters.get(feeds.get(i).getId()))); } - items.add(new NavDrawerData.FolderDrawerItem("Folder 1", folderItems, 0, 10000)); + items.add(new NavDrawerData.FolderDrawerItem("Folder 1", folderItems, 10000)); List folderItems2 = new ArrayList<>(); for (int i = 0, feedsSize = feeds.size(); i < feedsSize; i++) { - folderItems2.add(new NavDrawerData.FeedDrawerItem(feeds.get(i), 1, 20001 + i, + folderItems2.add(new NavDrawerData.FeedDrawerItem(feeds.get(i), 20001 + i, feedCounters.get(feeds.get(i).getId()))); } - items.add(new NavDrawerData.FolderDrawerItem("Folder 2", folderItems2, 0, 20000)); + items.add(new NavDrawerData.FolderDrawerItem("Folder 2", folderItems2, 20000)); NavDrawerData result = new NavDrawerData(items, queueSize, numNewItems, numDownloadedItems, feedCounters, UserPreferences.getEpisodeCleanupAlgorithm().getReclaimableItems()); diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/NavDrawerData.java b/core/src/main/java/de/danoeh/antennapod/core/storage/NavDrawerData.java index 3af9ef9d3..66d3f7940 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/NavDrawerData.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/NavDrawerData.java @@ -33,15 +33,22 @@ public class NavDrawerData { } public final Type type; - public final int layer; + private int layer; public final long id; - public DrawerItem(Type type, int layer, long id) { + public DrawerItem(Type type, long id) { this.type = type; - this.layer = layer; this.id = id; } + public int getLayer() { + return layer; + } + + public void setLayer(int layer) { + this.layer = layer; + } + public abstract String getTitle(); public abstract int getCounter(); @@ -52,8 +59,8 @@ public class NavDrawerData { public final String name; public boolean isOpen; - public FolderDrawerItem(String name, List children, int layer, long id) { - super(DrawerItem.Type.FOLDER, layer, id); + public FolderDrawerItem(String name, List children, long id) { + super(DrawerItem.Type.FOLDER, id); this.children = children; this.name = name; } @@ -75,8 +82,8 @@ public class NavDrawerData { public final Feed feed; public final int counter; - public FeedDrawerItem(Feed feed, int layer, long id, int counter) { - super(DrawerItem.Type.FEED, layer, id); + public FeedDrawerItem(Feed feed, long id, int counter) { + super(DrawerItem.Type.FEED, id); this.feed = feed; this.counter = counter; }