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 02fa24c0..1bdb54a8 100644 --- a/app/src/main/java/com/readrops/app/activities/MainActivity.java +++ b/app/src/main/java/com/readrops/app/activities/MainActivity.java @@ -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 allItems; + private List 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 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 + } } diff --git a/app/src/main/java/com/readrops/app/utils/SharedPreferencesManager.java b/app/src/main/java/com/readrops/app/utils/SharedPreferencesManager.java index 044fe5c8..81988a8c 100644 --- a/app/src/main/java/com/readrops/app/utils/SharedPreferencesManager.java +++ b/app/src/main/java/com/readrops/app/utils/SharedPreferencesManager.java @@ -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); diff --git a/app/src/main/res/drawable/ic_filter.xml b/app/src/main/res/drawable/ic_filter.xml new file mode 100644 index 00000000..5d4ec18e --- /dev/null +++ b/app/src/main/res/drawable/ic_filter.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/menu/item_list_menu.xml b/app/src/main/res/menu/item_list_menu.xml index 4013fcb7..30586852 100644 --- a/app/src/main/res/menu/item_list_menu.xml +++ b/app/src/main/res/menu/item_list_menu.xml @@ -1,9 +1,17 @@ - + + + \ 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 9f0d9619..ca5d76ac 100644 --- a/app/src/main/res/values-fr-rFR/strings.xml +++ b/app/src/main/res/values-fr-rFR/strings.xml @@ -41,5 +41,11 @@ Articles A lire plus tard Afficher les articles lus + Filtrer + + + Du plus récent au plus ancien + Du plus ancien au plus récent + \ 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 3ced31e3..478a19f5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -43,4 +43,10 @@ Articles To read later Show read articles + Filter + + + Newest to oldest + Oldest to newsest +