Add drawer favorites item to display starred/favorites items
This commit is contained in:
parent
04e2f3eadb
commit
9406416ce7
@ -41,6 +41,7 @@ public class DrawerManager {
|
||||
|
||||
public static final int ARTICLES_ITEM_ID = -5;
|
||||
public static final int READ_LATER_ID = -6;
|
||||
public static final int STARS_ID = -10;
|
||||
public static final int ADD_ACCOUNT_ID = -4;
|
||||
public static final int ABOUT_ID = -7;
|
||||
public static final int SETTINGS_ID = -8;
|
||||
@ -208,6 +209,12 @@ public class DrawerManager {
|
||||
.withSelectable(true)
|
||||
.withIdentifier(READ_LATER_ID);
|
||||
|
||||
PrimaryDrawerItem favorites = new PrimaryDrawerItem()
|
||||
.withName(R.string.favorites)
|
||||
.withIcon(R.drawable.ic_star)
|
||||
.withSelectable(true)
|
||||
.withIdentifier(STARS_ID);
|
||||
|
||||
PrimaryDrawerItem aboutItem = new PrimaryDrawerItem()
|
||||
.withName(R.string.about)
|
||||
.withIcon(R.drawable.ic_about_grey)
|
||||
@ -224,6 +231,7 @@ public class DrawerManager {
|
||||
drawer.addStickyFooterItem(aboutItem);
|
||||
|
||||
drawer.addItem(articles);
|
||||
drawer.addItem(favorites);
|
||||
drawer.addItem(toReadLater);
|
||||
drawer.addItem(new DividerDrawerItem());
|
||||
}
|
||||
|
@ -120,9 +120,6 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
|
||||
|
||||
viewModel = ViewModelCompat.getViewModel(this, MainViewModel.class);
|
||||
|
||||
viewModel.setShowReadItems(SharedPreferencesManager.readBoolean(
|
||||
SharedPreferencesManager.SharedPrefKey.SHOW_READ_ARTICLES));
|
||||
|
||||
viewModel.getItemsWithFeed().observe(this, itemWithFeeds -> {
|
||||
allItems = itemWithFeeds;
|
||||
|
||||
@ -256,6 +253,10 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
|
||||
viewModel.setFilterType(FilterType.READ_IT_LATER_FILTER);
|
||||
viewModel.invalidate();
|
||||
break;
|
||||
case DrawerManager.STARS_ID:
|
||||
viewModel.setFilterType(FilterType.STARS_FILTER);
|
||||
viewModel.invalidate();
|
||||
break;
|
||||
case DrawerManager.ABOUT_ID:
|
||||
startAboutActivity();
|
||||
break;
|
||||
|
@ -8,8 +8,10 @@ import androidx.paging.LivePagedListBuilder;
|
||||
import androidx.paging.PagedList;
|
||||
|
||||
import com.readrops.app.repositories.ARepository;
|
||||
import com.readrops.app.utils.SharedPreferencesManager;
|
||||
import com.readrops.db.Database;
|
||||
import com.readrops.db.ItemsListQueryBuilder;
|
||||
import com.readrops.db.ItemsQueryBuilder;
|
||||
import com.readrops.db.QueryFilters;
|
||||
import com.readrops.db.RoomFactoryWrapper;
|
||||
import com.readrops.db.entities.Feed;
|
||||
import com.readrops.db.entities.Folder;
|
||||
@ -39,19 +41,18 @@ public class MainViewModel extends ViewModel {
|
||||
private ARepository repository;
|
||||
private final Database database;
|
||||
|
||||
private final ItemsListQueryBuilder queryBuilder;
|
||||
private final QueryFilters queryFilters;
|
||||
|
||||
private Account currentAccount;
|
||||
private List<Account> accounts;
|
||||
|
||||
public MainViewModel(@NonNull Database database) {
|
||||
queryBuilder = new ItemsListQueryBuilder();
|
||||
|
||||
queryBuilder.setFilterType(FilterType.NO_FILTER);
|
||||
queryBuilder.setSortType(ListSortType.NEWEST_TO_OLDEST);
|
||||
|
||||
this.database = database;
|
||||
itemsWithFeed = new MediatorLiveData<>();
|
||||
|
||||
queryFilters = new QueryFilters();
|
||||
queryFilters.setShowReadItems(SharedPreferencesManager.readBoolean(
|
||||
SharedPreferencesManager.SharedPrefKey.SHOW_READ_ARTICLES));
|
||||
}
|
||||
|
||||
//region main query
|
||||
@ -62,10 +63,12 @@ public class MainViewModel extends ViewModel {
|
||||
}
|
||||
|
||||
private void buildPagedList() {
|
||||
if (lastFetch != null)
|
||||
if (lastFetch != null) {
|
||||
itemsWithFeed.removeSource(lastFetch);
|
||||
}
|
||||
|
||||
lastFetch = new LivePagedListBuilder<>(new RoomFactoryWrapper<>(database.itemDao().selectAll(queryBuilder.getQuery())),
|
||||
lastFetch = new LivePagedListBuilder<>(new RoomFactoryWrapper<>(database.itemDao()
|
||||
.selectAll(ItemsQueryBuilder.buildQuery(queryFilters))),
|
||||
new PagedList.Config.Builder()
|
||||
.setPageSize(100)
|
||||
.setPrefetchDistance(150)
|
||||
@ -81,31 +84,31 @@ public class MainViewModel extends ViewModel {
|
||||
}
|
||||
|
||||
public void setShowReadItems(boolean showReadItems) {
|
||||
queryBuilder.setShowReadItems(showReadItems);
|
||||
queryFilters.setShowReadItems(showReadItems);
|
||||
}
|
||||
|
||||
public boolean showReadItems() {
|
||||
return queryBuilder.showReadItems();
|
||||
return queryFilters.getShowReadItems();
|
||||
}
|
||||
|
||||
public void setFilterType(FilterType filterType) {
|
||||
queryBuilder.setFilterType(filterType);
|
||||
queryFilters.setFilterType(filterType);
|
||||
}
|
||||
|
||||
public FilterType getFilterType() {
|
||||
return queryBuilder.getFilterType();
|
||||
return queryFilters.getFilterType();
|
||||
}
|
||||
|
||||
public void setSortType(ListSortType sortType) {
|
||||
queryBuilder.setSortType(sortType);
|
||||
queryFilters.setSortType(sortType);
|
||||
}
|
||||
|
||||
public ListSortType getSortType() {
|
||||
return queryBuilder.getSortType();
|
||||
return queryFilters.getSortType();
|
||||
}
|
||||
|
||||
public void setFilterFeedId(int filterFeedId) {
|
||||
queryBuilder.setFilterFeedId(filterFeedId);
|
||||
queryFilters.setFilterFeedId(filterFeedId);
|
||||
}
|
||||
|
||||
public MediatorLiveData<PagedList<ItemWithFeed>> getItemsWithFeed() {
|
||||
@ -160,7 +163,7 @@ public class MainViewModel extends ViewModel {
|
||||
public void setCurrentAccount(Account currentAccount) {
|
||||
this.currentAccount = currentAccount;
|
||||
setRepository();
|
||||
queryBuilder.setAccountId(currentAccount.getId());
|
||||
queryFilters.setAccountId(currentAccount.getId());
|
||||
buildPagedList();
|
||||
|
||||
// set the new account as the current one
|
||||
@ -191,7 +194,7 @@ public class MainViewModel extends ViewModel {
|
||||
currentAccountExists = true;
|
||||
|
||||
setRepository();
|
||||
queryBuilder.setAccountId(currentAccount.getId());
|
||||
queryFilters.setAccountId(currentAccount.getId());
|
||||
buildPagedList();
|
||||
break;
|
||||
}
|
||||
@ -230,8 +233,8 @@ public class MainViewModel extends ViewModel {
|
||||
}
|
||||
|
||||
public Completable setAllItemsReadState(boolean read) {
|
||||
if (queryBuilder.getFilterType() == FilterType.FEED_FILTER)
|
||||
return repository.setAllFeedItemsReadState(queryBuilder.getFilterFeedId(), read);
|
||||
if (queryFilters.getFilterType() == FilterType.FEED_FILTER)
|
||||
return repository.setAllFeedItemsReadState(queryFilters.getFilterFeedId(), read);
|
||||
else
|
||||
return repository.setAllItemsReadState(read);
|
||||
}
|
||||
|
5
app/src/main/res/drawable/ic_star.xml
Normal file
5
app/src/main/res/drawable/ic_star.xml
Normal file
@ -0,0 +1,5 @@
|
||||
<vector android:height="24dp" android:tint="#727272"
|
||||
android:viewportHeight="24" android:viewportWidth="24"
|
||||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<path android:fillColor="@android:color/white" android:pathData="M12,17.27L18.18,21l-1.64,-7.03L22,9.24l-7.19,-0.61L12,2 9.19,8.63 2,9.24l5.46,4.73L5.82,21z"/>
|
||||
</vector>
|
@ -133,5 +133,6 @@
|
||||
<string name="password_helper">Votre mot de passe d\'API (Configuration > Profil)</string>
|
||||
<string name="synchronize">Synchroniser</string>
|
||||
<string name="navigator_view">Vue navigateur</string>
|
||||
<string name="favorites">Favoris</string>
|
||||
|
||||
</resources>
|
@ -139,4 +139,5 @@
|
||||
<string name="show_caption">Show caption</string>
|
||||
<string name="synchronize">Synchronize</string>
|
||||
<string name="navigator_view">Navigator view</string>
|
||||
<string name="favorites">Favorites</string>
|
||||
</resources>
|
||||
|
@ -1,114 +0,0 @@
|
||||
package com.readrops.db;
|
||||
|
||||
import androidx.sqlite.db.SupportSQLiteQuery;
|
||||
import androidx.sqlite.db.SupportSQLiteQueryBuilder;
|
||||
|
||||
import com.readrops.db.filters.FilterType;
|
||||
import com.readrops.db.filters.ListSortType;
|
||||
|
||||
public class ItemsListQueryBuilder {
|
||||
|
||||
private String[] columns = {"Item.id", "title", "clean_description", "image_link", "pub_date", "read",
|
||||
"read_changed", "read_it_later", "Feed.name", "text_color", "background_color", "icon_url", "read_time", "Item.remoteId",
|
||||
"Feed.id as feedId", "Feed.account_id", "Folder.id as folder_id", "Folder.name as folder_name"};
|
||||
|
||||
private String SELECT_ALL_JOIN = "Item INNER JOIN Feed on Item.feed_id = Feed.id " +
|
||||
"LEFT JOIN Folder on Feed.folder_id = Folder.id";
|
||||
|
||||
private String ORDER_BY_ASC = "Item.id DESC";
|
||||
|
||||
private String ORDER_BY_DESC = "pub_date ASC";
|
||||
|
||||
private SupportSQLiteQueryBuilder queryBuilder;
|
||||
|
||||
private boolean showReadItems;
|
||||
private int filterFeedId;
|
||||
private int accountId;
|
||||
|
||||
private FilterType filterType;
|
||||
private ListSortType sortType;
|
||||
|
||||
public ItemsListQueryBuilder() {
|
||||
queryBuilder = SupportSQLiteQueryBuilder.builder(SELECT_ALL_JOIN);
|
||||
}
|
||||
|
||||
private String buildWhereClause() {
|
||||
StringBuilder stringBuilder = new StringBuilder(80);
|
||||
|
||||
stringBuilder.append("Feed.account_id = ").append(accountId).append(" And ");
|
||||
|
||||
if (!showReadItems)
|
||||
stringBuilder.append("read = 0 And ");
|
||||
|
||||
switch (filterType) {
|
||||
case FEED_FILTER:
|
||||
stringBuilder.append("feed_id = ").append(filterFeedId).append(" And read_it_later = 0");
|
||||
break;
|
||||
case READ_IT_LATER_FILTER:
|
||||
stringBuilder.append("read_it_later = 1");
|
||||
break;
|
||||
case NO_FILTER:
|
||||
stringBuilder.append("read_it_later = 0");
|
||||
break;
|
||||
default:
|
||||
stringBuilder.append("read_it_later = 0");
|
||||
break;
|
||||
}
|
||||
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
|
||||
|
||||
public SupportSQLiteQuery getQuery() {
|
||||
queryBuilder.columns(columns);
|
||||
|
||||
queryBuilder.selection(buildWhereClause(), new String[0]);
|
||||
|
||||
if (sortType == ListSortType.NEWEST_TO_OLDEST)
|
||||
queryBuilder.orderBy(ORDER_BY_ASC);
|
||||
else
|
||||
queryBuilder.orderBy(ORDER_BY_DESC);
|
||||
|
||||
return queryBuilder.create();
|
||||
}
|
||||
|
||||
public boolean showReadItems() {
|
||||
return showReadItems;
|
||||
}
|
||||
|
||||
public void setShowReadItems(boolean showReadItems) {
|
||||
this.showReadItems = showReadItems;
|
||||
}
|
||||
|
||||
public int getFilterFeedId() {
|
||||
return filterFeedId;
|
||||
}
|
||||
|
||||
public void setFilterFeedId(int filterFeedId) {
|
||||
this.filterFeedId = filterFeedId;
|
||||
}
|
||||
|
||||
public FilterType getFilterType() {
|
||||
return filterType;
|
||||
}
|
||||
|
||||
public void setFilterType(FilterType filterType) {
|
||||
this.filterType = filterType;
|
||||
}
|
||||
|
||||
public ListSortType getSortType() {
|
||||
return sortType;
|
||||
}
|
||||
|
||||
public void setSortType(ListSortType sortType) {
|
||||
this.sortType = sortType;
|
||||
}
|
||||
|
||||
public int getAccountId() {
|
||||
return accountId;
|
||||
}
|
||||
|
||||
public void setAccountId(int accountId) {
|
||||
this.accountId = accountId;
|
||||
}
|
||||
}
|
62
db/src/main/java/com/readrops/db/ItemsQueryBuilder.kt
Normal file
62
db/src/main/java/com/readrops/db/ItemsQueryBuilder.kt
Normal file
@ -0,0 +1,62 @@
|
||||
package com.readrops.db
|
||||
|
||||
import androidx.sqlite.db.SupportSQLiteQuery
|
||||
import androidx.sqlite.db.SupportSQLiteQueryBuilder
|
||||
import com.readrops.db.filters.FilterType
|
||||
import com.readrops.db.filters.ListSortType
|
||||
|
||||
object ItemsQueryBuilder {
|
||||
|
||||
private val COLUMNS = arrayOf("Item.id", "title", "clean_description", "image_link", "pub_date", "read",
|
||||
"read_changed", "read_it_later", "Feed.name", "text_color", "background_color", "icon_url", "read_time", "Item.remoteId",
|
||||
"Feed.id as feedId", "Feed.account_id", "Folder.id as folder_id", "Folder.name as folder_name")
|
||||
|
||||
private const val SELECT_ALL_JOIN = "Item INNER JOIN Feed on Item.feed_id = Feed.id " +
|
||||
"LEFT JOIN Folder on Feed.folder_id = Folder.id"
|
||||
|
||||
private const val ORDER_BY_ASC = "Item.id DESC"
|
||||
|
||||
private const val ORDER_BY_DESC = "pub_date ASC"
|
||||
|
||||
|
||||
@JvmStatic
|
||||
fun buildQuery(queryFilters: QueryFilters): SupportSQLiteQuery {
|
||||
if (queryFilters.accountId == 0)
|
||||
throw IllegalArgumentException("AccountId must be greater than 0")
|
||||
|
||||
if (queryFilters.filterType == FilterType.FEED_FILTER && queryFilters.filterFeedId == 0)
|
||||
throw IllegalArgumentException("FeedId must be greater than 0 if current filter is FEED_FILTER")
|
||||
|
||||
return SupportSQLiteQueryBuilder.builder(SELECT_ALL_JOIN).run {
|
||||
columns(COLUMNS)
|
||||
selection(buildWhereClause(queryFilters), null)
|
||||
orderBy(if (queryFilters.sortType == ListSortType.NEWEST_TO_OLDEST) ORDER_BY_ASC else ORDER_BY_DESC)
|
||||
|
||||
create()
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildWhereClause(queryFilters: QueryFilters): String? {
|
||||
return StringBuilder(500).run {
|
||||
append("Feed.account_id = ${queryFilters.accountId} And ")
|
||||
|
||||
if (!queryFilters.showReadItems) append("read = 0 And ")
|
||||
|
||||
when (queryFilters.filterType) {
|
||||
FilterType.FEED_FILTER -> append("feed_id = ${queryFilters.filterFeedId} And read_it_later = 0")
|
||||
FilterType.READ_IT_LATER_FILTER -> append("read_it_later = 1")
|
||||
FilterType.STARS_FILTER -> append("starred = 1 And read_it_later = 0")
|
||||
else -> append("read_it_later = 0")
|
||||
}
|
||||
|
||||
toString()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class QueryFilters(var showReadItems: Boolean = true,
|
||||
var filterFeedId: Int = 0,
|
||||
var accountId: Int = 0,
|
||||
var filterType: FilterType = FilterType.NO_FILTER,
|
||||
var sortType: ListSortType = ListSortType.NEWEST_TO_OLDEST)
|
@ -3,5 +3,6 @@ package com.readrops.db.filters;
|
||||
public enum FilterType {
|
||||
FEED_FILTER,
|
||||
READ_IT_LATER_FILTER,
|
||||
STARS_FILTER,
|
||||
NO_FILTER
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user