Add unread items count badges to drawers items

This commit is contained in:
Shinokuni 2019-04-04 22:11:28 +02:00
parent ed224cced3
commit 51764a189e
5 changed files with 27 additions and 1 deletions

View File

@ -136,9 +136,9 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
}
private void handleDrawerClick(IDrawerItem drawerItem) {
drawer.closeDrawer();
if (drawerItem instanceof PrimaryDrawerItem) {
drawer.closeDrawer();
int id = (int)drawerItem.getIdentifier();
switch (id) {
@ -150,6 +150,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
}
} else if (drawerItem instanceof SecondaryDrawerItem) {
drawer.closeDrawer();
filterItems((int)drawerItem.getIdentifier());
}
}

View File

@ -35,6 +35,9 @@ public interface ItemDao {
@Query("Update Item set read = 1 Where id = :itemId")
void setRead(int itemId);
@Query("Select count(*) From Item Where feed_id = :feedId And read = 0")
int getUnreadCount(int feedId);
@Query("Select title, Item.description, content, link, pub_date, image_link, author, read, text_color, background_color, read_time, Feed.name, Feed.id as feedId, siteUrl, Folder.id as folder_id, Folder.name as folder_name from Item Inner Join Feed, Folder on Item.feed_id = Feed.id And Item.id = :id And Folder.id = Feed.folder_id")
LiveData<ItemWithFeed> getItemById(int id);
}

View File

@ -49,6 +49,9 @@ public class Feed implements Parcelable {
@ColumnInfo(name = "folder_id")
private int folderId;
@Ignore
private int unreadCount;
public Feed() {
}
@ -183,6 +186,14 @@ public class Feed implements Parcelable {
this.folderId = folderId;
}
public int getUnreadCount() {
return unreadCount;
}
public void setUnreadCount(int unreadCount) {
this.unreadCount = unreadCount;
}
public static Feed feedFromRSS(RSSFeed rssFeed) {
Feed feed = new Feed();
RSSChannel channel = rssFeed.getChannel();

View File

@ -51,12 +51,15 @@ public class DrawerManager {
.withIcon(context.getDrawable(R.drawable.ic_folder_grey));
List<IDrawerItem> secondaryDrawerItems = new ArrayList<>();
int expandableUnreadCount = 0;
for (Feed feed : folderListMap.get(folder)) {
int color = feed.getTextColor();
expandableUnreadCount += feed.getUnreadCount();
SecondaryDrawerItem secondaryDrawerItem = new SecondaryDrawerItem()
.withName(feed.getName())
.withBadge(String.valueOf(feed.getUnreadCount()))
.withIcon(color != 0 ? drawableWithColor(color) : drawableWithColor(context.getResources().getColor(R.color.colorPrimary)))
.withIdentifier(feed.getId());
@ -65,6 +68,7 @@ public class DrawerManager {
if (secondaryDrawerItems.size() > 0) {
badgeDrawerItem.withSubItems(secondaryDrawerItems);
badgeDrawerItem.withBadge(String.valueOf(expandableUnreadCount));
drawer.addItem(badgeDrawerItem);
}
} else { // no folder case, items to add after the folders
@ -73,6 +77,7 @@ public class DrawerManager {
SecondaryDrawerItem primaryDrawerItem = new SecondaryDrawerItem()
.withName(feed.getName())
.withBadge(String.valueOf(feed.getUnreadCount()))
.withIcon(color != 0 ? drawableWithColor(color) : drawableWithColor(context.getResources().getColor(R.color.colorPrimary)))
.withIdentifier(feed.getId());

View File

@ -52,6 +52,12 @@ public class MainViewModel extends AndroidViewModel {
for (Folder folder : folders) {
List<Feed> feeds = db.feedDao().getFeedsByFolder(folder.getId());
for (Feed feed : feeds) {
int unreadCount = db.itemDao().getUnreadCount(feed.getId());
feed.setUnreadCount(unreadCount);
}
foldersWithFeeds.put(folder, feeds);
}