diff --git a/app/src/main/java/eu/kanade/mangafeed/data/DataModule.java b/app/src/main/java/eu/kanade/mangafeed/data/DataModule.java index 713ba7d0c..1f1ab4897 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/DataModule.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/DataModule.java @@ -6,8 +6,8 @@ import javax.inject.Singleton; import dagger.Module; import dagger.Provides; -import eu.kanade.mangafeed.data.helpers.PreferencesHelper; import eu.kanade.mangafeed.data.helpers.DatabaseHelper; +import eu.kanade.mangafeed.data.helpers.PreferencesHelper; import rx.Scheduler; import rx.schedulers.Schedulers; diff --git a/app/src/main/java/eu/kanade/mangafeed/data/helpers/DatabaseHelper.java b/app/src/main/java/eu/kanade/mangafeed/data/helpers/DatabaseHelper.java index afa07905e..7dc05bbbe 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/helpers/DatabaseHelper.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/helpers/DatabaseHelper.java @@ -7,15 +7,15 @@ import com.pushtorefresh.storio.sqlite.StorIOSQLite; import com.pushtorefresh.storio.sqlite.impl.DefaultStorIOSQLite; import eu.kanade.mangafeed.data.managers.ChapterManager; +import eu.kanade.mangafeed.data.managers.MangaManager; import eu.kanade.mangafeed.data.models.Chapter; import eu.kanade.mangafeed.data.models.ChapterStorIOSQLiteDeleteResolver; import eu.kanade.mangafeed.data.models.ChapterStorIOSQLiteGetResolver; import eu.kanade.mangafeed.data.models.ChapterStorIOSQLitePutResolver; import eu.kanade.mangafeed.data.models.Manga; import eu.kanade.mangafeed.data.models.MangaStorIOSQLiteDeleteResolver; -import eu.kanade.mangafeed.data.models.MangaStorIOSQLiteGetResolver; import eu.kanade.mangafeed.data.models.MangaStorIOSQLitePutResolver; -import eu.kanade.mangafeed.data.managers.MangaManager; +import eu.kanade.mangafeed.data.resolvers.MangaWithUnreadGetResolver; public class DatabaseHelper { @@ -24,11 +24,12 @@ public class DatabaseHelper { public ChapterManager chapter; public DatabaseHelper(Context context) { + db = DefaultStorIOSQLite.builder() .sqliteOpenHelper(new DbOpenHelper(context)) .addTypeMapping(Manga.class, SQLiteTypeMapping.builder() .putResolver(new MangaStorIOSQLitePutResolver()) - .getResolver(new MangaStorIOSQLiteGetResolver()) + .getResolver(new MangaWithUnreadGetResolver()) .deleteResolver(new MangaStorIOSQLiteDeleteResolver()) .build()) .addTypeMapping(Chapter.class, SQLiteTypeMapping.builder() diff --git a/app/src/main/java/eu/kanade/mangafeed/data/managers/MangaManager.java b/app/src/main/java/eu/kanade/mangafeed/data/managers/MangaManager.java index 5a1b49e8a..022cb5bba 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/managers/MangaManager.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/managers/MangaManager.java @@ -3,10 +3,12 @@ package eu.kanade.mangafeed.data.managers; import com.pushtorefresh.storio.sqlite.StorIOSQLite; import com.pushtorefresh.storio.sqlite.operations.put.PutResult; import com.pushtorefresh.storio.sqlite.queries.Query; +import com.pushtorefresh.storio.sqlite.queries.RawQuery; import java.util.List; import eu.kanade.mangafeed.data.models.Manga; +import eu.kanade.mangafeed.data.tables.ChaptersTable; import eu.kanade.mangafeed.data.tables.MangasTable; import rx.Observable; @@ -16,6 +18,18 @@ public class MangaManager extends BaseManager { super(db); } + private final String mangasWithUnreadQuery = String.format( + "SELECT %1$s.*, COUNT(C.%4$s) AS %5$s FROM %1$s LEFT JOIN " + + "(SELECT %4$s FROM %2$s WHERE %6$s = 0) AS C ON %3$s = C.%4$s " + + "GROUP BY %3$s", + MangasTable.TABLE, + ChaptersTable.TABLE, + MangasTable.TABLE + "." + MangasTable.COLUMN_ID, + ChaptersTable.COLUMN_MANGA_ID, + MangasTable.COLUMN_UNREAD, + ChaptersTable.COLUMN_READ + ); + public Observable> get() { return db.get() .listOfObjects(Manga.class) @@ -26,6 +40,17 @@ public class MangaManager extends BaseManager { .createObservable(); } + public Observable> getWithUnread() { + return db.get() + .listOfObjects(Manga.class) + .withQuery(RawQuery.builder() + .query(mangasWithUnreadQuery) + .observesTables(MangasTable.TABLE, ChaptersTable.TABLE) + .build()) + .prepare() + .createObservable(); + } + public Observable insert(Manga manga) { return db.put() .object(manga) @@ -34,7 +59,15 @@ public class MangaManager extends BaseManager { } public void createDummyManga() { + insert(createDummyManga("One Piece")).subscribe(); + insert(createDummyManga("Übel Blatt")).subscribe(); + insert(createDummyManga("Berserk")).subscribe(); + insert(createDummyManga("Horimiya")).subscribe(); + } + + private Manga createDummyManga(String title) { Manga m = new Manga(); + m.title = title; m.url="http://example.com"; m.artist="Eiichiro Oda"; m.author="Eiichiro Oda"; @@ -42,8 +75,7 @@ public class MangaManager extends BaseManager { m.genre="Action, Drama"; m.status="Ongoing"; m.thumbnail_url="http://example.com/pic.png"; - m.title="One Piece"; - insert(m).subscribe(); + return m; } } diff --git a/app/src/main/java/eu/kanade/mangafeed/data/models/Manga.java b/app/src/main/java/eu/kanade/mangafeed/data/models/Manga.java index 4fb5483ad..d2a643016 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/models/Manga.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/models/Manga.java @@ -70,12 +70,36 @@ public class Manga { @StorIOSQLiteColumn(name = MangasTable.COLUMN_CHAPTER_ORDER) public int chapter_order; + @NonNull + public int unread = 0; + public Manga() {} public Manga(String title) { this.title = title; } + public Manga(long id, String title, String author, String artist, String url, + String description, String genre, String status, int rank, + String thumbnail_url) { + this.id = id; + this.title = title; + this.author = author; + this.artist = artist; + this.url = url; + this.description = description; + this.genre = genre; + this.status = status; + this.rank = rank; + this.thumbnail_url = thumbnail_url; + } + + public static Manga newManga(long id, String title, String author, String artist, String url, + String description, String genre, String status, int rank, + String thumbnail_url) { + return new Manga(id, title, author, artist, url, description, genre, status, rank, thumbnail_url); + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/app/src/main/java/eu/kanade/mangafeed/data/resolvers/MangaWithUnreadGetResolver.java b/app/src/main/java/eu/kanade/mangafeed/data/resolvers/MangaWithUnreadGetResolver.java new file mode 100644 index 000000000..f5e5eb888 --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/data/resolvers/MangaWithUnreadGetResolver.java @@ -0,0 +1,22 @@ +package eu.kanade.mangafeed.data.resolvers; + +import android.database.Cursor; +import android.support.annotation.NonNull; + +import eu.kanade.mangafeed.data.models.Manga; +import eu.kanade.mangafeed.data.models.MangaStorIOSQLiteGetResolver; +import eu.kanade.mangafeed.data.tables.MangasTable; + + +public class MangaWithUnreadGetResolver extends MangaStorIOSQLiteGetResolver { + + @Override + public Manga mapFromCursor(@NonNull Cursor cursor) { + Manga manga = super.mapFromCursor(cursor); + int unreadColumn = cursor.getColumnIndex(MangasTable.COLUMN_UNREAD); + if (unreadColumn != -1) + manga.unread = cursor.getInt(unreadColumn); + return manga; + } + +} diff --git a/app/src/main/java/eu/kanade/mangafeed/data/tables/MangasTable.java b/app/src/main/java/eu/kanade/mangafeed/data/tables/MangasTable.java index e166cbcba..48e852e92 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/tables/MangasTable.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/tables/MangasTable.java @@ -55,6 +55,9 @@ public class MangasTable { @NonNull public static final String COLUMN_CHAPTER_ORDER = "chapter_order"; + @NonNull + public static final String COLUMN_UNREAD = "unread"; + // This is just class with Meta Data, we don't need instances private MangasTable() { throw new IllegalStateException("No instances please"); diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java index ff699ccb3..3d305d325 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java @@ -45,8 +45,7 @@ public class LibraryPresenter extends BasePresenter { } public void initializeMangas() { - - db.manga.get() + db.manga.getWithUnread() .observeOn(mainThread()) .subscribe(mangas -> { adapter = new LibraryAdapter<>(view.getActivity(), mangas);