Add filter item in main activity menu

This commit is contained in:
Shinokuni 2019-04-09 11:53:26 +02:00
parent 7494e7daf4
commit da13d487e4
6 changed files with 87 additions and 7 deletions

View File

@ -22,6 +22,7 @@ import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.afollestad.materialdialogs.MaterialDialog;
import com.bumptech.glide.Glide;
import com.bumptech.glide.integration.recyclerview.RecyclerViewPreloader;
import com.bumptech.glide.util.ViewPreloadSizeProvider;
@ -44,6 +45,7 @@ import com.readrops.app.views.MainItemListAdapter;
import org.apache.commons.collections4.CollectionUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@ -69,6 +71,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
private FloatingActionMenu actionMenu;
private List<ItemWithFeed> allItems;
private List<ItemWithFeed> filteredItems;
private MainViewModel viewModel;
private DrawerManager drawerManager;
@ -79,8 +82,10 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
private int feedCount;
private int feedNb;
private int filterFeedId;
private boolean showReadItems;
private ListSortType sortType;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -103,7 +108,6 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
if (!refreshLayout.isRefreshing())
filterItems(0);
}));
refreshLayout = findViewById(R.id.swipe_refresh_layout);
@ -115,6 +119,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
feedCount = 0;
initRecyclerView();
sortType = ListSortType.NEWEST_TO_OLDEST;
drawer = new DrawerBuilder()
.withActivity(this)
@ -134,10 +139,11 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
if (drawerItem instanceof PrimaryDrawerItem) {
drawer.closeDrawer();
int id = (int)drawerItem.getIdentifier();
filterFeedId = 0;
switch (id) {
case DrawerManager.ARTICLES_ITEM_ID:
adapter.submitList(allItems);
filterItems(0);
break;
case DrawerManager.READ_LATER_ID:
break;
@ -150,7 +156,9 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
}
private void filterItems(int id) {
List<ItemWithFeed> filteredItems = new ArrayList<>(allItems);
filterFeedId = id;
filteredItems = new ArrayList<>(allItems);
CollectionUtils.filter(filteredItems, object -> {
boolean showRead;
if (object.getItem().isRead())
@ -161,12 +169,28 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
if (id == 0)
return showRead;
else
return object.getItem().getId() == id && showRead;
return object.getFeedId() == id && showRead;
});
sortItems();
adapter.submitList(filteredItems);
}
private void sortItems() {
switch (sortType) {
case OLDEST_TO_NEWEST:
Collections.sort(filteredItems, ((o1, o2) -> o1.getItem().getPubDate().compareTo(o2.getItem().getPubDate())));
break;
case NEWEST_TO_OLDEST:
Collections.sort(filteredItems, ((o1, o2) -> -1 * o1.getItem().getPubDate().compareTo(o2.getItem().getPubDate())));
break;
default:
Collections.sort(filteredItems, ((o1, o2) -> -1 * o1.getItem().getPubDate().compareTo(o2.getItem().getPubDate())));
break;
}
}
private void updateDrawerFeeds() {
viewModel.getFoldersWithFeeds()
.subscribeOn(Schedulers.io())
@ -400,10 +424,41 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
}
filterItems(0);
filterItems(filterFeedId);
return true;
case R.id.item_sort:
displayFilterDialog();
return true;
}
return super.onOptionsItemSelected(item);
}
private void displayFilterDialog() {
int index = sortType == ListSortType.OLDEST_TO_NEWEST ? 1 : 0;
new MaterialDialog.Builder(this)
.title(getString(R.string.filter))
.items(R.array.filter_items)
.itemsCallbackSingleChoice(index, (dialog, itemView, which, text) -> {
String[] items = getResources().getStringArray(R.array.filter_items);
if (text.toString().equals(items[0]))
sortType = ListSortType.NEWEST_TO_OLDEST;
else
sortType = ListSortType.OLDEST_TO_NEWEST;
sortItems();
adapter.submitList(filteredItems);
adapter.notifyDataSetChanged();
return true;
})
.show();
}
public enum ListSortType {
NEWEST_TO_OLDEST,
OLDEST_TO_NEWEST
}
}

View File

@ -5,7 +5,7 @@ import android.content.SharedPreferences;
public final class SharedPreferencesManager {
private static final String PREFS = "readrops.uniquepreferences";
private static final String PREFS = "com.readrops.app.uniquepreferences";
private static SharedPreferences getSharedPreferences(Context context) {
return context.getSharedPreferences(PREFS, Context.MODE_PRIVATE);

View File

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M10,18h4v-2h-4v2zM3,6v2h18L21,6L3,6zM6,13h12v-2L6,11v2z"/>
</vector>

View File

@ -1,9 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/item_filter_read_items"
android:title="@string/show_read_articles"
app:showAsAction="never"
android:checkable="true"/>
<item
android:id="@+id/item_sort"
android:title="@string/filter"
android:icon="@drawable/ic_filter"
app:showAsAction="ifRoom" />
</menu>

View File

@ -41,5 +41,11 @@
<string name="articles">Articles</string>
<string name="read_later">A lire plus tard</string>
<string name="show_read_articles">Afficher les articles lus</string>
<string name="filter">Filtrer</string>
<string-array name="filter_items">
<item>Du plus récent au plus ancien</item>
<item>Du plus ancien au plus récent</item>
</string-array>
</resources>

View File

@ -43,4 +43,10 @@
<string name="articles">Articles</string>
<string name="read_later">To read later</string>
<string name="show_read_articles">Show read articles</string>
<string name="filter">Filter</string>
<string-array name="filter_items">
<item>Newest to oldest</item>
<item>Oldest to newsest</item>
</string-array>
</resources>