diff --git a/app/src/main/java/com/readrops/app/database/Database.java b/app/src/main/java/com/readrops/app/database/Database.java index 87fd66c3..35116818 100644 --- a/app/src/main/java/com/readrops/app/database/Database.java +++ b/app/src/main/java/com/readrops/app/database/Database.java @@ -8,12 +8,14 @@ import android.content.Context; import android.support.annotation.NonNull; 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.entities.Feed; +import com.readrops.app.database.entities.Folder; 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}) public abstract class Database extends RoomDatabase { @@ -21,6 +23,8 @@ public abstract class Database extends RoomDatabase { public abstract ItemDao itemDao(); + public abstract FolderDao folderDao(); + private static Database database; public static Database getInstance(Context context) { @@ -35,9 +39,8 @@ public abstract class Database extends RoomDatabase { public void onCreate(@NonNull SupportSQLiteDatabase db) { super.onCreate(db); - /*Feed feed1 = new Feed("Le Media", "this is a description", "https://lemediapresse.fr/feed/"); - - new Thread(() -> database.feedDao().insert(feed1)).start();*/ + Folder folder = new Folder("reserved"); + new Thread(() -> database.folderDao().insert(folder)).start(); } @Override diff --git a/app/src/main/java/com/readrops/app/database/ItemWithFeed.java b/app/src/main/java/com/readrops/app/database/ItemWithFeed.java index c8dd9ab0..3fcf358d 100644 --- a/app/src/main/java/com/readrops/app/database/ItemWithFeed.java +++ b/app/src/main/java/com/readrops/app/database/ItemWithFeed.java @@ -4,6 +4,7 @@ import android.arch.persistence.room.ColumnInfo; import android.arch.persistence.room.Embedded; import android.support.annotation.ColorInt; +import com.readrops.app.database.entities.Folder; import com.readrops.app.database.entities.Item; public class ItemWithFeed { @@ -26,6 +27,9 @@ public class ItemWithFeed { @ColumnInfo(name = "siteUrl") private String websiteUrl; + @Embedded(prefix = "folder_") + private Folder folder; + public Item getItem() { return item; } @@ -66,6 +70,14 @@ public class ItemWithFeed { this.bgColor = bgColor; } + public Folder getFolder() { + return folder; + } + + public void setFolder(Folder folder) { + this.folder = folder; + } + public String getWebsiteUrl() { return websiteUrl; } diff --git a/app/src/main/java/com/readrops/app/database/dao/FeedDao.java b/app/src/main/java/com/readrops/app/database/dao/FeedDao.java index ad8fff93..e34046a3 100644 --- a/app/src/main/java/com/readrops/app/database/dao/FeedDao.java +++ b/app/src/main/java/com/readrops/app/database/dao/FeedDao.java @@ -25,10 +25,10 @@ public interface FeedDao { @Query("Select * from Feed Where url = :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") void updateHeaders(String etag, String lastModified, int feedId); + @Query("Update Feed set folder_id = :folderId Where id = :feedId") + void updateFeedFolder(int feedId, int folderId); + } diff --git a/app/src/main/java/com/readrops/app/database/dao/FolderDao.java b/app/src/main/java/com/readrops/app/database/dao/FolderDao.java new file mode 100644 index 00000000..6205de17 --- /dev/null +++ b/app/src/main/java/com/readrops/app/database/dao/FolderDao.java @@ -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 getAllFolders(); + + @Insert + long insert(Folder folder); +} diff --git a/app/src/main/java/com/readrops/app/database/dao/ItemDao.java b/app/src/main/java/com/readrops/app/database/dao/ItemDao.java index 97673ce7..66cd792e 100644 --- a/app/src/main/java/com/readrops/app/database/dao/ItemDao.java +++ b/app/src/main/java/com/readrops/app/database/dao/ItemDao.java @@ -20,7 +20,7 @@ public interface ItemDao { @Query("Select * from Item Order By pub_date DESC") LiveData> 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> getAllItemWithFeeds(); @Query("Select case When :guid In (Select guid from Item) Then 'true' else 'false' end") @@ -32,6 +32,6 @@ public interface ItemDao { @Insert void insertAll(List 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 getItemById(int id); } diff --git a/app/src/main/java/com/readrops/app/database/entities/Feed.java b/app/src/main/java/com/readrops/app/database/entities/Feed.java index 0cea4039..0d0a6316 100644 --- a/app/src/main/java/com/readrops/app/database/entities/Feed.java +++ b/app/src/main/java/com/readrops/app/database/entities/Feed.java @@ -3,13 +3,14 @@ package com.readrops.app.database.entities; import android.arch.persistence.room.*; import android.support.annotation.ColorInt; +import android.support.annotation.Nullable; import com.readrops.readropslibrary.localfeed.atom.ATOMFeed; import com.readrops.readropslibrary.localfeed.json.JSONFeed; import com.readrops.readropslibrary.localfeed.rss.RSSChannel; import com.readrops.readropslibrary.localfeed.rss.RSSFeed; -@Entity +@Entity(foreignKeys = @ForeignKey(entity = Folder.class, parentColumns = "id", childColumns = "folder_id")) public class Feed { @PrimaryKey(autoGenerate = true) @@ -39,6 +40,9 @@ public class Feed { @ColumnInfo(name = "last_modified") private String lastModified; + @ColumnInfo(name = "folder_id", index = true) + private int folderId; + public Feed() { } @@ -138,6 +142,14 @@ public class Feed { this.lastModified = lastModified; } + public int getFolderId() { + return folderId; + } + + public void setFolderId(int folderId) { + this.folderId = folderId; + } + public static Feed feedFromRSS(RSSFeed rssFeed) { Feed feed = new Feed(); RSSChannel channel = rssFeed.getChannel(); @@ -151,6 +163,10 @@ public class Feed { feed.setEtag(rssFeed.getEtag()); 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; } @@ -167,6 +183,8 @@ public class Feed { feed.setEtag(atomFeed.getEtag()); feed.setLastModified(atomFeed.getLastModified()); + feed.setFolderId(1); + return feed; } @@ -181,6 +199,8 @@ public class Feed { feed.setEtag(jsonFeed.getEtag()); feed.setLastModified(jsonFeed.getLastModified()); + feed.setFolderId(1); + return feed; } } diff --git a/app/src/main/java/com/readrops/app/database/entities/Folder.java b/app/src/main/java/com/readrops/app/database/entities/Folder.java new file mode 100644 index 00000000..3ef44212 --- /dev/null +++ b/app/src/main/java/com/readrops/app/database/entities/Folder.java @@ -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; + } +}