add support for read it later items

This commit is contained in:
Shinokuni 2019-04-17 21:57:14 +02:00
parent 8c57d977fb
commit f07d998214
5 changed files with 49 additions and 16 deletions

View File

@ -94,6 +94,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
private int feedCount;
private int feedNb;
private int filterFeedId;
private boolean readItLater;
private boolean showReadItems;
private ListSortType sortType;
@ -121,7 +122,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
allItems = itemWithFeeds;
if (!refreshLayout.isRefreshing())
filterItems(0);
filterItems(filterFeedId);
}));
refreshLayout = findViewById(R.id.swipe_refresh_layout);
@ -157,13 +158,17 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
switch (id) {
case DrawerManager.ARTICLES_ITEM_ID:
readItLater = false;
filterItems(0);
break;
case DrawerManager.READ_LATER_ID:
readItLater = true;
filterItems(0);
break;
}
} else if (drawerItem instanceof SecondaryDrawerItem) {
readItLater = false;
drawer.closeDrawer();
filterItems((int)drawerItem.getIdentifier());
}
@ -180,14 +185,20 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
else
showRead = true; // item unread
if (id == 0)
return showRead;
else
return object.getFeedId() == id && showRead;
if (id != 0) {
if (readItLater)
return object.getItem().isReadItLater() && object.getFeedId() == id && showRead;
else
return !object.getItem().isReadItLater() && object.getFeedId() == id && showRead;
} else {
if (readItLater)
return object.getItem().isReadItLater() && showRead;
else
return !object.getItem().isReadItLater() && showRead;
}
});
sortItems();
adapter.submitList(filteredItems);
}
@ -374,14 +385,15 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
itemWithFeed.getItem().setRead(!itemWithFeed.getItem().isRead());
// work-around to undo swipe as only notifyItemChanged(position) can do it,
// but we need first to update the viewHolder.
adapter.notifyItemChanged(viewHolder.getAdapterPosition(), itemWithFeed);
adapter.notifyItemChanged(viewHolder.getAdapterPosition());
} else { // add item to read it later section
adapter.notifyItemRemoved(viewHolder.getAdapterPosition());
viewModel.setItemReadItLater((int) adapter.getItemId(viewHolder.getAdapterPosition()))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe();
if (readItLater)
adapter.notifyItemChanged(viewHolder.getAdapterPosition());
}
}
@ -569,6 +581,4 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
NEWEST_TO_OLDEST,
OLDEST_TO_NEWEST
}
}

View File

@ -20,7 +20,7 @@ public interface ItemDao {
@Query("Select * from Item Order By pub_date DESC")
LiveData<List<Item>> getAll();
@Query("Select Item.id, title, clean_description, image_link, pub_date, read, Feed.name, text_color, background_color, icon_url, read_time, Feed.id as feedId, Folder.id as folder_id, Folder.name as folder_name from Item Inner Join Feed, Folder on Item.feed_id = Feed.id And Folder.id = Feed.folder_id Order By Item.id DESC")
@Query("Select Item.id, title, clean_description, image_link, pub_date, read, read_it_later, Feed.name, text_color, background_color, icon_url, read_time, Feed.id as feedId, Folder.id as folder_id, Folder.name as folder_name from Item Inner Join Feed, Folder on Item.feed_id = Feed.id And Folder.id = Feed.folder_id Order By Item.id DESC")
LiveData<List<ItemWithFeed>> getAllItemWithFeeds();
@Query("Select case When :guid In (Select guid from Item) Then 'true' else 'false' end")
@ -40,6 +40,9 @@ public interface ItemDao {
@Query("Update Item set read = :readState Where id = :itemId")
void setReadState(int itemId, int readState);
@Query("Update Item set read_it_later = 1 Where id = :itemId")
void setReadItLater(int itemId);
@Query("Select count(*) From Item Where feed_id = :feedId And read = 0")
int getUnreadCount(int feedId);

View File

@ -60,6 +60,9 @@ public class Item implements Comparable<Item> {
private boolean read;
@ColumnInfo(name = "read_it_later")
private boolean readItLater;
public int getId() {
return id;
}
@ -175,6 +178,14 @@ public class Item implements Comparable<Item> {
this.read = read;
}
public boolean isReadItLater() {
return readItLater;
}
public void setReadItLater(boolean readItLater) {
this.readItLater = readItLater;
}
public static List<Item> itemsFromRSS(List<RSSItem> items, Feed feed) throws ParseException {
List<Item> dbItems = new ArrayList<>();

View File

@ -82,4 +82,11 @@ public class MainViewModel extends AndroidViewModel {
return Completable.concat(completableList);
}
public Completable setItemReadItLater(int itemId) {
return Completable.create(emitter -> {
db.itemDao().setReadItLater(itemId);
emitter.onComplete();
});
}
}

View File

@ -72,7 +72,9 @@ public class MainItemListAdapter extends ListAdapter<ItemWithFeed, MainItemListA
return item.getTitle().equals(item1.getTitle()) &&
itemWithFeed.getFeedName().equals(t1.getFeedName()) &&
itemWithFeed.getFolder().getName().equals(t1.getFolder().getName());
itemWithFeed.getFolder().getName().equals(t1.getFolder().getName()) &&
item.isRead() == item1.isRead() &&
item.isReadItLater() == item1.isReadItLater();
}
};