From 7494e7daf4b318056fa7edc328a856936e53dd2e Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Mon, 8 Apr 2019 11:25:43 +0200 Subject: [PATCH] Add item checkbox in main activity menu for showing read articles (choice is saved in SharedPreferences) --- .../readrops/app/activities/MainActivity.java | 82 ++++++++++++++----- .../app/utils/SharedPreferencesManager.java | 44 ++++++++++ app/src/main/res/menu/item_list_menu.xml | 9 ++ app/src/main/res/values-fr-rFR/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 5 files changed, 116 insertions(+), 21 deletions(-) create mode 100644 app/src/main/java/com/readrops/app/utils/SharedPreferencesManager.java create mode 100644 app/src/main/res/menu/item_list_menu.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 b30a7d1f..02fa24c0 100644 --- a/app/src/main/java/com/readrops/app/activities/MainActivity.java +++ b/app/src/main/java/com/readrops/app/activities/MainActivity.java @@ -14,48 +14,40 @@ import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.support.v7.widget.helper.ItemTouchHelper; import android.util.Log; +import android.view.Menu; +import android.view.MenuItem; import android.view.View; import android.widget.ProgressBar; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; - import com.bumptech.glide.Glide; import com.bumptech.glide.integration.recyclerview.RecyclerViewPreloader; import com.bumptech.glide.util.ViewPreloadSizeProvider; import com.github.clans.fab.FloatingActionMenu; -import com.mikepenz.materialdrawer.AccountHeader; -import com.mikepenz.materialdrawer.AccountHeaderBuilder; import com.mikepenz.materialdrawer.Drawer; import com.mikepenz.materialdrawer.DrawerBuilder; import com.mikepenz.materialdrawer.model.PrimaryDrawerItem; -import com.mikepenz.materialdrawer.model.ProfileDrawerItem; 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 com.readrops.app.utils.DrawerManager; -import com.readrops.app.views.MainItemListAdapter; -import com.readrops.app.viewmodels.MainViewModel; -import com.readrops.app.R; import com.readrops.app.database.pojo.ItemWithFeed; -import com.readrops.app.database.entities.Item; +import com.readrops.app.utils.DrawerManager; import com.readrops.app.utils.GlideApp; +import com.readrops.app.utils.SharedPreferencesManager; +import com.readrops.app.viewmodels.MainViewModel; +import com.readrops.app.views.MainItemListAdapter; - -import org.apache.commons.collections4.Predicate; -import org.joda.time.LocalDateTime; import org.apache.commons.collections4.CollectionUtils; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.TreeMap; import io.reactivex.Observer; -import io.reactivex.SingleObserver; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.Disposable; import io.reactivex.observers.DisposableCompletableObserver; @@ -77,7 +69,6 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou private FloatingActionMenu actionMenu; private List allItems; - private TreeMap itemsMap; private MainViewModel viewModel; private DrawerManager drawerManager; @@ -88,7 +79,8 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou private int feedCount; private int feedNb; - private int itemToUpdatePos; + + private boolean showReadItems; @Override protected void onCreate(Bundle savedInstanceState) { @@ -101,14 +93,17 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou actionMenu = findViewById(R.id.fab_menu); viewModel = ViewModelProviders.of(this).get(MainViewModel.class); - itemsMap = new TreeMap<>(LocalDateTime::compareTo); allItems = new ArrayList<>(); + showReadItems = SharedPreferencesManager.readBoolean(this, + SharedPreferencesManager.SharedPrefKey.SHOW_READ_ARTICLES); + viewModel.getItemsWithFeed().observe(this, (itemWithFeeds -> { allItems = itemWithFeeds; if (!refreshLayout.isRefreshing()) - adapter.submitList(allItems); + filterItems(0); + })); refreshLayout = findViewById(R.id.swipe_refresh_layout); @@ -132,7 +127,6 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou .build(); drawerManager = new DrawerManager(drawer); - updateDrawerFeeds(); } @@ -157,7 +151,18 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou private void filterItems(int id) { List filteredItems = new ArrayList<>(allItems); - CollectionUtils.filter(filteredItems, object -> object.getFeedId() == id); + CollectionUtils.filter(filteredItems, object -> { + boolean showRead; + if (object.getItem().isRead()) + showRead = (object.getItem().isRead() == showReadItems); + else + showRead = true; // item unread + + if (id == 0) + return showRead; + else + return object.getItem().getId() == id && showRead; + }); adapter.submitList(filteredItems); } @@ -362,8 +367,43 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou refreshLayout.setRefreshing(false); adapter.submitList(allItems); + filterItems(0); updateDrawerFeeds(); // update drawer after syncing feeds } }); } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.item_list_menu, menu); + + MenuItem articlesItem = menu.findItem(R.id.item_filter_read_items); + articlesItem.setChecked(showReadItems); + + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case R.id.item_filter_read_items: + if (item.isChecked()) { + item.setChecked(false); + showReadItems = false; + SharedPreferencesManager.writeValue(this, + SharedPreferencesManager.SharedPrefKey.SHOW_READ_ARTICLES, false); + } else { + item.setChecked(true); + showReadItems = true; + SharedPreferencesManager.writeValue(this, + SharedPreferencesManager.SharedPrefKey.SHOW_READ_ARTICLES, true); + + } + + filterItems(0); + return true; + } + + return super.onOptionsItemSelected(item); + } } diff --git a/app/src/main/java/com/readrops/app/utils/SharedPreferencesManager.java b/app/src/main/java/com/readrops/app/utils/SharedPreferencesManager.java new file mode 100644 index 00000000..044fe5c8 --- /dev/null +++ b/app/src/main/java/com/readrops/app/utils/SharedPreferencesManager.java @@ -0,0 +1,44 @@ +package com.readrops.app.utils; + +import android.content.Context; +import android.content.SharedPreferences; + +public final class SharedPreferencesManager { + + private static final String PREFS = "readrops.uniquepreferences"; + + private static SharedPreferences getSharedPreferences(Context context) { + return context.getSharedPreferences(PREFS, Context.MODE_PRIVATE); + } + + public static void writeValue(Context context, SharedPrefKey key, Object value) { + SharedPreferences sharedPref = getSharedPreferences(context); + SharedPreferences.Editor editor = sharedPref.edit(); + + if (value instanceof Boolean) + editor.putBoolean(key.toString(), (Boolean) value); + else if (value instanceof String) + editor.putString(key.toString(), (String) value); + + editor.apply(); + } + + public static int readInt(Context context, SharedPrefKey key) { + SharedPreferences sharedPreferences = getSharedPreferences(context); + return sharedPreferences.getInt(key.toString(), 0); + } + + public static boolean readBoolean(Context context, SharedPrefKey key) { + SharedPreferences sharedPreferences = getSharedPreferences(context); + return sharedPreferences.getBoolean(key.toString(), false); + } + + public static String readString(Context context, SharedPrefKey key) { + SharedPreferences sharedPreferences = getSharedPreferences(context); + return sharedPreferences.getString(key.toString(), null); + } + + public enum SharedPrefKey { + SHOW_READ_ARTICLES + } +} diff --git a/app/src/main/res/menu/item_list_menu.xml b/app/src/main/res/menu/item_list_menu.xml new file mode 100644 index 00000000..4013fcb7 --- /dev/null +++ b/app/src/main/res/menu/item_list_menu.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values-fr-rFR/strings.xml b/app/src/main/res/values-fr-rFR/strings.xml index 3bbf861d..9f0d9619 100644 --- a/app/src/main/res/values-fr-rFR/strings.xml +++ b/app/src/main/res/values-fr-rFR/strings.xml @@ -40,5 +40,6 @@ Erreur inconnue pour le flux %1$s Articles A lire plus tard + Afficher les articles lus \ 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 485c5db1..3ced31e3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -42,4 +42,5 @@ Unknown error for feed %1$s Articles To read later + Show read articles