From 0734eaa5dab868bb7b854baa1f95f8a4efeada57 Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Tue, 2 Apr 2019 10:33:46 +0200 Subject: [PATCH] Add a drawer manager --- .../readrops/app/activities/MainActivity.java | 50 ++--------- .../com/readrops/app/utils/DrawerManager.java | 89 +++++++++++++++++++ .../main/res/drawable/ic_read_later_grey.xml | 5 ++ .../main/res/drawable/ic_rss_feed_grey.xml | 6 ++ app/src/main/res/values-fr-rFR/strings.xml | 2 + app/src/main/res/values/strings.xml | 2 + 6 files changed, 110 insertions(+), 44 deletions(-) create mode 100644 app/src/main/java/com/readrops/app/utils/DrawerManager.java create mode 100644 app/src/main/res/drawable/ic_read_later_grey.xml create mode 100644 app/src/main/res/drawable/ic_rss_feed_grey.xml diff --git a/app/src/main/java/com/readrops/app/activities/MainActivity.java b/app/src/main/java/com/readrops/app/activities/MainActivity.java index b7fc7489..b18ee78c 100644 --- a/app/src/main/java/com/readrops/app/activities/MainActivity.java +++ b/app/src/main/java/com/readrops/app/activities/MainActivity.java @@ -38,6 +38,7 @@ import com.mikepenz.materialdrawer.model.SecondaryDrawerItem; import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem; import com.readrops.app.database.entities.Feed; import com.readrops.app.database.entities.Folder; +import com.readrops.app.utils.DrawerManager; import com.readrops.app.views.MainItemListAdapter; import com.readrops.app.viewmodels.MainViewModel; import com.readrops.app.R; @@ -78,6 +79,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou private TreeMap itemsMap; private MainViewModel viewModel; + private DrawerManager drawerManager; private RelativeLayout syncProgressLayout; private TextView syncProgress; @@ -123,7 +125,9 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou .withShowDrawerOnFirstLaunch(true) .build(); - updateDrawerFeeds(); + drawerManager = new DrawerManager(drawer); + + updateDrawerFeeds(); } private void updateDrawerFeeds() { @@ -133,7 +137,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou .subscribe(new DisposableSingleObserver>>() { @Override public void onSuccess(HashMap> folderListHashMap) { - populateDrawer(folderListHashMap); + drawerManager.updateDrawer(getApplicationContext(), folderListHashMap); } @Override @@ -143,48 +147,6 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou }); } - private void populateDrawer(HashMap> folderListHashMap) { - drawer.removeAllItems(); - List feedsWithoutFolder = new ArrayList<>(); - - for (Folder folder : folderListHashMap.keySet()) { - if (folder.getId() != 1) { - ExpandableBadgeDrawerItem badgeDrawerItem = new ExpandableBadgeDrawerItem() - .withName(folder.getName()) - .withIdentifier(folder.getId()) - .withIcon(getDrawable(R.drawable.ic_folder_grey)); - - List secondaryDrawerItems = new ArrayList<>(); - - for (Feed feed : folderListHashMap.get(folder)) { - SecondaryDrawerItem secondaryDrawerItem = new SecondaryDrawerItem() - .withName(feed.getName()) - .withIdentifier(feed.getId()); - - secondaryDrawerItems.add(secondaryDrawerItem); - } - - if (secondaryDrawerItems.size() > 0) { - badgeDrawerItem.withSubItems(secondaryDrawerItems); - drawer.addItem(badgeDrawerItem); - } - } else { // no folder case - for (Feed feed : folderListHashMap.get(folder)) { - SecondaryDrawerItem primaryDrawerItem = new SecondaryDrawerItem() - .withName(feed.getName()) - .withIdentifier(feed.getId()); - - feedsWithoutFolder.add(primaryDrawerItem); - } - } - } - - // work-around as MaterialDrawer doesn't accept an item list - for (SecondaryDrawerItem primaryDrawerItem : feedsWithoutFolder) { - drawer.addItem(primaryDrawerItem); - } - } - @Override public void onBackPressed() { if (drawer.isDrawerOpen()) diff --git a/app/src/main/java/com/readrops/app/utils/DrawerManager.java b/app/src/main/java/com/readrops/app/utils/DrawerManager.java new file mode 100644 index 00000000..f2e8010e --- /dev/null +++ b/app/src/main/java/com/readrops/app/utils/DrawerManager.java @@ -0,0 +1,89 @@ +package com.readrops.app.utils; + +import android.content.Context; + +import com.mikepenz.materialdrawer.Drawer; +import com.mikepenz.materialdrawer.model.DividerDrawerItem; +import com.mikepenz.materialdrawer.model.ExpandableBadgeDrawerItem; +import com.mikepenz.materialdrawer.model.PrimaryDrawerItem; +import com.mikepenz.materialdrawer.model.SecondaryDrawerItem; +import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem; +import com.readrops.app.R; +import com.readrops.app.database.entities.Feed; +import com.readrops.app.database.entities.Folder; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class DrawerManager { + + public static final int ARTICLES_ITEM_ID = 1; + public static final int READ_LATER_ID = 2; + + private Drawer drawer; + + public DrawerManager(Drawer drawer) { + this.drawer = drawer; + } + + public void updateDrawer(Context context, Map> folderListMap) { + drawer.removeAllItems(); + addDefaultPlaces(context); + + List feedsWithoutFolder = new ArrayList<>(); + + for (Folder folder : folderListMap.keySet()) { + if (folder.getId() != 1) { + ExpandableBadgeDrawerItem badgeDrawerItem = new ExpandableBadgeDrawerItem() + .withName(folder.getName()) + .withIdentifier(folder.getId()) + .withIcon(context.getDrawable(R.drawable.ic_folder_grey)); + + List secondaryDrawerItems = new ArrayList<>(); + + for (Feed feed : folderListMap.get(folder)) { + SecondaryDrawerItem secondaryDrawerItem = new SecondaryDrawerItem() + .withName(feed.getName()) + .withIdentifier(feed.getId()); + + secondaryDrawerItems.add(secondaryDrawerItem); + } + + if (secondaryDrawerItems.size() > 0) { + badgeDrawerItem.withSubItems(secondaryDrawerItems); + drawer.addItem(badgeDrawerItem); + } + } else { // no folder case, items to add after the folders + for (Feed feed : folderListMap.get(folder)) { + SecondaryDrawerItem primaryDrawerItem = new SecondaryDrawerItem() + .withName(feed.getName()) + .withIdentifier(feed.getId()); + + feedsWithoutFolder.add(primaryDrawerItem); + } + } + } + + // work-around as MaterialDrawer doesn't accept an item list + for (SecondaryDrawerItem primaryDrawerItem : feedsWithoutFolder) { + drawer.addItem(primaryDrawerItem); + } + } + + private void addDefaultPlaces(Context context) { + PrimaryDrawerItem articles = new PrimaryDrawerItem() + .withName(context.getString(R.string.articles)) + .withIcon(context.getDrawable(R.drawable.ic_rss_feed_grey)) + .withIdentifier(ARTICLES_ITEM_ID); + + PrimaryDrawerItem toReadLater = new PrimaryDrawerItem() + .withName(context.getString(R.string.read_later)) + .withIcon(context.getDrawable(R.drawable.ic_read_later_grey)) + .withIdentifier(READ_LATER_ID); + + drawer.addItem(articles); + drawer.addItem(toReadLater); + drawer.addItem(new DividerDrawerItem()); + } +} diff --git a/app/src/main/res/drawable/ic_read_later_grey.xml b/app/src/main/res/drawable/ic_read_later_grey.xml new file mode 100644 index 00000000..d7583971 --- /dev/null +++ b/app/src/main/res/drawable/ic_read_later_grey.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/drawable/ic_rss_feed_grey.xml b/app/src/main/res/drawable/ic_rss_feed_grey.xml new file mode 100644 index 00000000..fedffd69 --- /dev/null +++ b/app/src/main/res/drawable/ic_rss_feed_grey.xml @@ -0,0 +1,6 @@ + + + + diff --git a/app/src/main/res/values-fr-rFR/strings.xml b/app/src/main/res/values-fr-rFR/strings.xml index 0625d589..3bbf861d 100644 --- a/app/src/main/res/values-fr-rFR/strings.xml +++ b/app/src/main/res/values-fr-rFR/strings.xml @@ -38,5 +38,7 @@ Erreur de traitement pour le flux %1$s Mauvais format pour le flux %1$s Erreur inconnue pour le flux %1$s + Articles + A lire plus tard \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 270bf20b..485c5db1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -40,4 +40,6 @@ Failure when parsing feed %1$s Wrong format for feed %1$s Unknown error for feed %1$s + Articles + To read later