Add an option to get unread chapters of mangas

This commit is contained in:
inorichi 2015-10-01 14:30:08 +02:00
parent db49c3ee88
commit 4e795b69e5
7 changed files with 89 additions and 8 deletions

View file

@ -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;

View file

@ -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.<Manga>builder()
.putResolver(new MangaStorIOSQLitePutResolver())
.getResolver(new MangaStorIOSQLiteGetResolver())
.getResolver(new MangaWithUnreadGetResolver())
.deleteResolver(new MangaStorIOSQLiteDeleteResolver())
.build())
.addTypeMapping(Chapter.class, SQLiteTypeMapping.<Chapter>builder()

View file

@ -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<List<Manga>> get() {
return db.get()
.listOfObjects(Manga.class)
@ -26,6 +40,17 @@ public class MangaManager extends BaseManager {
.createObservable();
}
public Observable<List<Manga>> getWithUnread() {
return db.get()
.listOfObjects(Manga.class)
.withQuery(RawQuery.builder()
.query(mangasWithUnreadQuery)
.observesTables(MangasTable.TABLE, ChaptersTable.TABLE)
.build())
.prepare()
.createObservable();
}
public Observable<PutResult> 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;
}
}

View file

@ -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;

View file

@ -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;
}
}

View file

@ -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");

View file

@ -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);