Add item checkbox in main activity menu for showing read articles (choice is saved in SharedPreferences)

This commit is contained in:
Shinokuni 2019-04-08 11:25:43 +02:00
parent 04d4c8dd7c
commit 7494e7daf4
5 changed files with 116 additions and 21 deletions

View File

@ -14,48 +14,40 @@ import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.support.v7.widget.helper.ItemTouchHelper; import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log; import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.bumptech.glide.integration.recyclerview.RecyclerViewPreloader; import com.bumptech.glide.integration.recyclerview.RecyclerViewPreloader;
import com.bumptech.glide.util.ViewPreloadSizeProvider; import com.bumptech.glide.util.ViewPreloadSizeProvider;
import com.github.clans.fab.FloatingActionMenu; 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.Drawer;
import com.mikepenz.materialdrawer.DrawerBuilder; import com.mikepenz.materialdrawer.DrawerBuilder;
import com.mikepenz.materialdrawer.model.PrimaryDrawerItem; import com.mikepenz.materialdrawer.model.PrimaryDrawerItem;
import com.mikepenz.materialdrawer.model.ProfileDrawerItem;
import com.mikepenz.materialdrawer.model.SecondaryDrawerItem; import com.mikepenz.materialdrawer.model.SecondaryDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem; 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.Feed;
import com.readrops.app.database.entities.Folder; 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.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.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 org.apache.commons.collections4.CollectionUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.TreeMap;
import io.reactivex.Observer; import io.reactivex.Observer;
import io.reactivex.SingleObserver;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable; import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableCompletableObserver; import io.reactivex.observers.DisposableCompletableObserver;
@ -77,7 +69,6 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
private FloatingActionMenu actionMenu; private FloatingActionMenu actionMenu;
private List<ItemWithFeed> allItems; private List<ItemWithFeed> allItems;
private TreeMap<LocalDateTime, Item> itemsMap;
private MainViewModel viewModel; private MainViewModel viewModel;
private DrawerManager drawerManager; private DrawerManager drawerManager;
@ -88,7 +79,8 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
private int feedCount; private int feedCount;
private int feedNb; private int feedNb;
private int itemToUpdatePos;
private boolean showReadItems;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -101,14 +93,17 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
actionMenu = findViewById(R.id.fab_menu); actionMenu = findViewById(R.id.fab_menu);
viewModel = ViewModelProviders.of(this).get(MainViewModel.class); viewModel = ViewModelProviders.of(this).get(MainViewModel.class);
itemsMap = new TreeMap<>(LocalDateTime::compareTo);
allItems = new ArrayList<>(); allItems = new ArrayList<>();
showReadItems = SharedPreferencesManager.readBoolean(this,
SharedPreferencesManager.SharedPrefKey.SHOW_READ_ARTICLES);
viewModel.getItemsWithFeed().observe(this, (itemWithFeeds -> { viewModel.getItemsWithFeed().observe(this, (itemWithFeeds -> {
allItems = itemWithFeeds; allItems = itemWithFeeds;
if (!refreshLayout.isRefreshing()) if (!refreshLayout.isRefreshing())
adapter.submitList(allItems); filterItems(0);
})); }));
refreshLayout = findViewById(R.id.swipe_refresh_layout); refreshLayout = findViewById(R.id.swipe_refresh_layout);
@ -132,7 +127,6 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
.build(); .build();
drawerManager = new DrawerManager(drawer); drawerManager = new DrawerManager(drawer);
updateDrawerFeeds(); updateDrawerFeeds();
} }
@ -157,7 +151,18 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
private void filterItems(int id) { private void filterItems(int id) {
List<ItemWithFeed> filteredItems = new ArrayList<>(allItems); List<ItemWithFeed> 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); adapter.submitList(filteredItems);
} }
@ -362,8 +367,43 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
refreshLayout.setRefreshing(false); refreshLayout.setRefreshing(false);
adapter.submitList(allItems); adapter.submitList(allItems);
filterItems(0);
updateDrawerFeeds(); // update drawer after syncing feeds 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);
}
} }

View File

@ -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
}
}

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/item_filter_read_items"
android:title="@string/show_read_articles"
android:checkable="true"/>
</menu>

View File

@ -40,5 +40,6 @@
<string name="feed_insertion_unknown_error">Erreur inconnue pour le flux %1$s</string> <string name="feed_insertion_unknown_error">Erreur inconnue pour le flux %1$s</string>
<string name="articles">Articles</string> <string name="articles">Articles</string>
<string name="read_later">A lire plus tard</string> <string name="read_later">A lire plus tard</string>
<string name="show_read_articles">Afficher les articles lus</string>
</resources> </resources>

View File

@ -42,4 +42,5 @@
<string name="feed_insertion_unknown_error">Unknown error for feed %1$s</string> <string name="feed_insertion_unknown_error">Unknown error for feed %1$s</string>
<string name="articles">Articles</string> <string name="articles">Articles</string>
<string name="read_later">To read later</string> <string name="read_later">To read later</string>
<string name="show_read_articles">Show read articles</string>
</resources> </resources>