Calculate layer in makeFlatDrawerData

This commit is contained in:
ByteHamster 2021-01-02 20:17:17 +01:00
parent 052884e081
commit 3104232e5d
4 changed files with 40 additions and 20 deletions

View File

@ -303,7 +303,7 @@ public class NavListAdapter extends RecyclerView.Adapter<NavListAdapter.Holder>
holder.count.setVisibility(View.GONE); holder.count.setVisibility(View.GONE);
} }
holder.title.setText(item.getTitle()); 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) { private void bindFeedView(NavDrawerData.FeedDrawerItem drawerItem, FeedHolder holder) {

View File

@ -19,6 +19,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.core.util.Pair;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
@ -379,8 +380,15 @@ public class NavDrawerFragment extends Fragment implements SharedPreferences.OnS
} else { } else {
openFolders.add(folder.name); openFolders.add(folder.name);
} }
flatItemList = makeFlatDrawerData(navDrawerData.items);
disposable = Observable.fromCallable(() -> makeFlatDrawerData(navDrawerData.items, 0))
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
result -> {
flatItemList = result;
navAdapter.notifyDataSetChanged(); navAdapter.notifyDataSetChanged();
}, error -> Log.e(TAG, Log.getStackTraceString(error)));
} }
} }
} else if (UserPreferences.getSubscriptionsFilter().isEnabled() } else if (UserPreferences.getSubscriptionsFilter().isEnabled()
@ -403,13 +411,17 @@ public class NavDrawerFragment extends Fragment implements SharedPreferences.OnS
private void loadData() { private void loadData() {
progressBar.setVisibility(View.VISIBLE); 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()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe( .subscribe(
result -> { result -> {
navDrawerData = result; navDrawerData = result.first;
flatItemList = makeFlatDrawerData(navDrawerData.items); // TODO: This is the main thread! flatItemList = result.second;
updateSelection(); // Selected item might be a feed updateSelection(); // Selected item might be a feed
navAdapter.notifyDataSetChanged(); navAdapter.notifyDataSetChanged();
progressBar.setVisibility(View.GONE); progressBar.setVisibility(View.GONE);
@ -419,15 +431,16 @@ public class NavDrawerFragment extends Fragment implements SharedPreferences.OnS
}); });
} }
private List<NavDrawerData.DrawerItem> makeFlatDrawerData(List<NavDrawerData.DrawerItem> items) { private List<NavDrawerData.DrawerItem> makeFlatDrawerData(List<NavDrawerData.DrawerItem> items, int layer) {
List<NavDrawerData.DrawerItem> flatItems = new ArrayList<>(); List<NavDrawerData.DrawerItem> flatItems = new ArrayList<>();
for (NavDrawerData.DrawerItem item : items) { for (NavDrawerData.DrawerItem item : items) {
item.setLayer(layer);
flatItems.add(item); flatItems.add(item);
if (item.type == NavDrawerData.DrawerItem.Type.FOLDER) { if (item.type == NavDrawerData.DrawerItem.Type.FOLDER) {
NavDrawerData.FolderDrawerItem folder = ((NavDrawerData.FolderDrawerItem) item); NavDrawerData.FolderDrawerItem folder = ((NavDrawerData.FolderDrawerItem) item);
folder.isOpen = openFolders.contains(folder.name); folder.isOpen = openFolders.contains(folder.name);
if (folder.isOpen) { if (folder.isOpen) {
flatItems.addAll(makeFlatDrawerData(((NavDrawerData.FolderDrawerItem) item).children)); flatItems.addAll(makeFlatDrawerData(((NavDrawerData.FolderDrawerItem) item).children, layer + 1));
} }
} }
} }

View File

@ -868,22 +868,22 @@ public final class DBReader {
List<NavDrawerData.DrawerItem> items = new ArrayList<>(); List<NavDrawerData.DrawerItem> items = new ArrayList<>();
for (int i = 0, feedsSize = feeds.size(); i < feedsSize; i++) { 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<NavDrawerData.DrawerItem> folderItems = new ArrayList<>(); List<NavDrawerData.DrawerItem> folderItems = new ArrayList<>();
for (int i = 0, feedsSize = feeds.size(); i < feedsSize; i++) { 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()))); 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<NavDrawerData.DrawerItem> folderItems2 = new ArrayList<>(); List<NavDrawerData.DrawerItem> folderItems2 = new ArrayList<>();
for (int i = 0, feedsSize = feeds.size(); i < feedsSize; i++) { 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()))); 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, NavDrawerData result = new NavDrawerData(items, queueSize, numNewItems, numDownloadedItems,
feedCounters, UserPreferences.getEpisodeCleanupAlgorithm().getReclaimableItems()); feedCounters, UserPreferences.getEpisodeCleanupAlgorithm().getReclaimableItems());

View File

@ -33,15 +33,22 @@ public class NavDrawerData {
} }
public final Type type; public final Type type;
public final int layer; private int layer;
public final long id; public final long id;
public DrawerItem(Type type, int layer, long id) { public DrawerItem(Type type, long id) {
this.type = type; this.type = type;
this.layer = layer;
this.id = id; this.id = id;
} }
public int getLayer() {
return layer;
}
public void setLayer(int layer) {
this.layer = layer;
}
public abstract String getTitle(); public abstract String getTitle();
public abstract int getCounter(); public abstract int getCounter();
@ -52,8 +59,8 @@ public class NavDrawerData {
public final String name; public final String name;
public boolean isOpen; public boolean isOpen;
public FolderDrawerItem(String name, List<DrawerItem> children, int layer, long id) { public FolderDrawerItem(String name, List<DrawerItem> children, long id) {
super(DrawerItem.Type.FOLDER, layer, id); super(DrawerItem.Type.FOLDER, id);
this.children = children; this.children = children;
this.name = name; this.name = name;
} }
@ -75,8 +82,8 @@ public class NavDrawerData {
public final Feed feed; public final Feed feed;
public final int counter; public final int counter;
public FeedDrawerItem(Feed feed, int layer, long id, int counter) { public FeedDrawerItem(Feed feed, long id, int counter) {
super(DrawerItem.Type.FEED, layer, id); super(DrawerItem.Type.FEED, id);
this.feed = feed; this.feed = feed;
this.counter = counter; this.counter = counter;
} }