Add folder db model

This commit is contained in:
Shinokuni 2019-02-18 19:05:50 +00:00
parent 2211017472
commit d76d2e356f
7 changed files with 104 additions and 10 deletions

View File

@ -8,12 +8,14 @@ import android.content.Context;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import com.readrops.app.database.dao.FeedDao; import com.readrops.app.database.dao.FeedDao;
import com.readrops.app.database.dao.FolderDao;
import com.readrops.app.database.dao.ItemDao; import com.readrops.app.database.dao.ItemDao;
import com.readrops.app.database.entities.Feed; import com.readrops.app.database.entities.Feed;
import com.readrops.app.database.entities.Folder;
import com.readrops.app.database.entities.Item; import com.readrops.app.database.entities.Item;
@android.arch.persistence.room.Database(entities = {Feed.class, Item.class}, version = 1, exportSchema = false) @android.arch.persistence.room.Database(entities = {Feed.class, Item.class, Folder.class}, version = 1, exportSchema = false)
@TypeConverters({Converters.class}) @TypeConverters({Converters.class})
public abstract class Database extends RoomDatabase { public abstract class Database extends RoomDatabase {
@ -21,6 +23,8 @@ public abstract class Database extends RoomDatabase {
public abstract ItemDao itemDao(); public abstract ItemDao itemDao();
public abstract FolderDao folderDao();
private static Database database; private static Database database;
public static Database getInstance(Context context) { public static Database getInstance(Context context) {
@ -35,9 +39,8 @@ public abstract class Database extends RoomDatabase {
public void onCreate(@NonNull SupportSQLiteDatabase db) { public void onCreate(@NonNull SupportSQLiteDatabase db) {
super.onCreate(db); super.onCreate(db);
/*Feed feed1 = new Feed("Le Media", "this is a description", "https://lemediapresse.fr/feed/"); Folder folder = new Folder("reserved");
new Thread(() -> database.folderDao().insert(folder)).start();
new Thread(() -> database.feedDao().insert(feed1)).start();*/
} }
@Override @Override

View File

@ -4,6 +4,7 @@ import android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Embedded; import android.arch.persistence.room.Embedded;
import android.support.annotation.ColorInt; import android.support.annotation.ColorInt;
import com.readrops.app.database.entities.Folder;
import com.readrops.app.database.entities.Item; import com.readrops.app.database.entities.Item;
public class ItemWithFeed { public class ItemWithFeed {
@ -26,6 +27,9 @@ public class ItemWithFeed {
@ColumnInfo(name = "siteUrl") @ColumnInfo(name = "siteUrl")
private String websiteUrl; private String websiteUrl;
@Embedded(prefix = "folder_")
private Folder folder;
public Item getItem() { public Item getItem() {
return item; return item;
} }
@ -66,6 +70,14 @@ public class ItemWithFeed {
this.bgColor = bgColor; this.bgColor = bgColor;
} }
public Folder getFolder() {
return folder;
}
public void setFolder(Folder folder) {
this.folder = folder;
}
public String getWebsiteUrl() { public String getWebsiteUrl() {
return websiteUrl; return websiteUrl;
} }

View File

@ -25,10 +25,10 @@ public interface FeedDao {
@Query("Select * from Feed Where url = :feedUrl") @Query("Select * from Feed Where url = :feedUrl")
Feed getFeedByUrl(String feedUrl); Feed getFeedByUrl(String feedUrl);
@Query("Select id from Feed Where url = :feedUrl")
int getFeedIdByUrl(String feedUrl);
@Query("Update Feed set etag = :etag, last_modified = :lastModified Where id = :feedId") @Query("Update Feed set etag = :etag, last_modified = :lastModified Where id = :feedId")
void updateHeaders(String etag, String lastModified, int feedId); void updateHeaders(String etag, String lastModified, int feedId);
@Query("Update Feed set folder_id = :folderId Where id = :feedId")
void updateFeedFolder(int feedId, int folderId);
} }

View File

@ -0,0 +1,20 @@
package com.readrops.app.database.dao;
import android.arch.lifecycle.LiveData;
import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Insert;
import android.arch.persistence.room.Query;
import com.readrops.app.database.entities.Folder;
import java.util.List;
@Dao
public interface FolderDao {
@Query("Select * from Folder")
List<Folder> getAllFolders();
@Insert
long insert(Folder folder);
}

View File

@ -20,7 +20,7 @@ public interface ItemDao {
@Query("Select * from Item Order By pub_date DESC") @Query("Select * from Item Order By pub_date DESC")
LiveData<List<Item>> getAll(); LiveData<List<Item>> getAll();
@Query("Select Item.id, title, clean_description, image_link, pub_date, name, text_color, background_color, icon_url, read_time from Item Inner Join Feed on Item.feed_id = Feed.id Order By Item.id DESC") @Query("Select Item.id, title, clean_description, image_link, pub_date, Feed.name, text_color, background_color, icon_url, read_time, 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(); LiveData<List<ItemWithFeed>> getAllItemWithFeeds();
@Query("Select case When :guid In (Select guid from Item) Then 'true' else 'false' end") @Query("Select case When :guid In (Select guid from Item) Then 'true' else 'false' end")
@ -32,6 +32,6 @@ public interface ItemDao {
@Insert @Insert
void insertAll(List<Item> items); void insertAll(List<Item> items);
@Query("Select title, Item.description, content, link, pub_date, image_link, author, text_color, background_color, read_time, name, siteUrl from Item Inner Join Feed on Item.feed_id = Feed.id And Item.id = :id") @Query("Select title, Item.description, content, link, pub_date, image_link, author, text_color, background_color, read_time, Feed.name, 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); LiveData<ItemWithFeed> getItemById(int id);
} }

View File

@ -3,13 +3,14 @@ package com.readrops.app.database.entities;
import android.arch.persistence.room.*; import android.arch.persistence.room.*;
import android.support.annotation.ColorInt; import android.support.annotation.ColorInt;
import android.support.annotation.Nullable;
import com.readrops.readropslibrary.localfeed.atom.ATOMFeed; import com.readrops.readropslibrary.localfeed.atom.ATOMFeed;
import com.readrops.readropslibrary.localfeed.json.JSONFeed; import com.readrops.readropslibrary.localfeed.json.JSONFeed;
import com.readrops.readropslibrary.localfeed.rss.RSSChannel; import com.readrops.readropslibrary.localfeed.rss.RSSChannel;
import com.readrops.readropslibrary.localfeed.rss.RSSFeed; import com.readrops.readropslibrary.localfeed.rss.RSSFeed;
@Entity @Entity(foreignKeys = @ForeignKey(entity = Folder.class, parentColumns = "id", childColumns = "folder_id"))
public class Feed { public class Feed {
@PrimaryKey(autoGenerate = true) @PrimaryKey(autoGenerate = true)
@ -39,6 +40,9 @@ public class Feed {
@ColumnInfo(name = "last_modified") @ColumnInfo(name = "last_modified")
private String lastModified; private String lastModified;
@ColumnInfo(name = "folder_id", index = true)
private int folderId;
public Feed() { public Feed() {
} }
@ -138,6 +142,14 @@ public class Feed {
this.lastModified = lastModified; this.lastModified = lastModified;
} }
public int getFolderId() {
return folderId;
}
public void setFolderId(int folderId) {
this.folderId = folderId;
}
public static Feed feedFromRSS(RSSFeed rssFeed) { public static Feed feedFromRSS(RSSFeed rssFeed) {
Feed feed = new Feed(); Feed feed = new Feed();
RSSChannel channel = rssFeed.getChannel(); RSSChannel channel = rssFeed.getChannel();
@ -151,6 +163,10 @@ public class Feed {
feed.setEtag(rssFeed.getEtag()); feed.setEtag(rssFeed.getEtag());
feed.setLastModified(rssFeed.getLastModified()); feed.setLastModified(rssFeed.getLastModified());
// as sqlite doesn't support null foreign keys, a default folder is linked to the feed
// This default folder was inserted at room db creation (see Database.java)
feed.setFolderId(1);
return feed; return feed;
} }
@ -167,6 +183,8 @@ public class Feed {
feed.setEtag(atomFeed.getEtag()); feed.setEtag(atomFeed.getEtag());
feed.setLastModified(atomFeed.getLastModified()); feed.setLastModified(atomFeed.getLastModified());
feed.setFolderId(1);
return feed; return feed;
} }
@ -181,6 +199,8 @@ public class Feed {
feed.setEtag(jsonFeed.getEtag()); feed.setEtag(jsonFeed.getEtag());
feed.setLastModified(jsonFeed.getLastModified()); feed.setLastModified(jsonFeed.getLastModified());
feed.setFolderId(1);
return feed; return feed;
} }
} }

View File

@ -0,0 +1,39 @@
package com.readrops.app.database.entities;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.Ignore;
import android.arch.persistence.room.PrimaryKey;
@Entity
public class Folder {
@PrimaryKey(autoGenerate = true)
private int id;
private String name;
public Folder() {
}
@Ignore
public Folder(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}