From 67a2e99cc113f7f13a71366b30a6b8e76b9c571a Mon Sep 17 00:00:00 2001 From: inorichi Date: Thu, 8 Oct 2015 13:15:29 +0200 Subject: [PATCH] Use interfaces for database managers, create a source manager --- .../eu/kanade/mangafeed/data/DataModule.java | 6 ++ .../data/helpers/DatabaseHelper.java | 82 ++++++++++++-- .../mangafeed/data/helpers/SourceManager.java | 38 +++++++ .../data/managers/ChapterManager.java | 93 ++-------------- .../data/managers/ChapterManagerImpl.java | 101 ++++++++++++++++++ .../mangafeed/data/managers/MangaManager.java | 86 ++------------- .../data/managers/MangaManagerImpl.java | 85 +++++++++++++++ .../mangafeed/data/tables/ChaptersTable.java | 3 +- .../mangafeed/presenter/LibraryPresenter.java | 10 +- .../presenter/MangaDetailPresenter.java | 2 +- .../eu/kanade/mangafeed/sources/Batoto.java | 66 +++++------- .../eu/kanade/mangafeed/sources/Source.java | 5 + .../kanade/mangafeed/util/DummyDataUtil.java | 52 +++++++++ 13 files changed, 415 insertions(+), 214 deletions(-) create mode 100644 app/src/main/java/eu/kanade/mangafeed/data/helpers/SourceManager.java create mode 100644 app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManagerImpl.java create mode 100644 app/src/main/java/eu/kanade/mangafeed/data/managers/MangaManagerImpl.java create mode 100644 app/src/main/java/eu/kanade/mangafeed/sources/Source.java create mode 100644 app/src/main/java/eu/kanade/mangafeed/util/DummyDataUtil.java 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 acd5bb8e2c..b18a3dc933 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/DataModule.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/DataModule.java @@ -10,6 +10,7 @@ import eu.kanade.mangafeed.data.caches.CacheManager; import eu.kanade.mangafeed.data.helpers.DatabaseHelper; import eu.kanade.mangafeed.data.helpers.NetworkHelper; import eu.kanade.mangafeed.data.helpers.PreferencesHelper; +import eu.kanade.mangafeed.data.helpers.SourceManager; import rx.Scheduler; import rx.schedulers.Schedulers; @@ -49,4 +50,9 @@ public class DataModule { return new NetworkHelper(); } + @Provides + @Singleton + SourceManager provideSourceManager(NetworkHelper networkHelper, CacheManager cacheManager) { + return new SourceManager(networkHelper, cacheManager); + } } \ No newline at end of file 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 7dc05bbbe5..93ecfb428a 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 @@ -5,9 +5,17 @@ import android.content.Context; import com.pushtorefresh.storio.sqlite.SQLiteTypeMapping; import com.pushtorefresh.storio.sqlite.StorIOSQLite; import com.pushtorefresh.storio.sqlite.impl.DefaultStorIOSQLite; +import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResult; +import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResults; +import com.pushtorefresh.storio.sqlite.operations.put.PutResult; +import com.pushtorefresh.storio.sqlite.operations.put.PutResults; + +import java.util.List; import eu.kanade.mangafeed.data.managers.ChapterManager; +import eu.kanade.mangafeed.data.managers.ChapterManagerImpl; import eu.kanade.mangafeed.data.managers.MangaManager; +import eu.kanade.mangafeed.data.managers.MangaManagerImpl; import eu.kanade.mangafeed.data.models.Chapter; import eu.kanade.mangafeed.data.models.ChapterStorIOSQLiteDeleteResolver; import eu.kanade.mangafeed.data.models.ChapterStorIOSQLiteGetResolver; @@ -16,16 +24,17 @@ import eu.kanade.mangafeed.data.models.Manga; import eu.kanade.mangafeed.data.models.MangaStorIOSQLiteDeleteResolver; import eu.kanade.mangafeed.data.models.MangaStorIOSQLitePutResolver; import eu.kanade.mangafeed.data.resolvers.MangaWithUnreadGetResolver; +import rx.Observable; -public class DatabaseHelper { +public class DatabaseHelper implements MangaManager, ChapterManager { - private StorIOSQLite db; - public MangaManager manga; - public ChapterManager chapter; + private StorIOSQLite mDb; + private MangaManagerImpl mMangaManager; + private ChapterManagerImpl mChapterManager; public DatabaseHelper(Context context) { - db = DefaultStorIOSQLite.builder() + mDb = DefaultStorIOSQLite.builder() .sqliteOpenHelper(new DbOpenHelper(context)) .addTypeMapping(Manga.class, SQLiteTypeMapping.builder() .putResolver(new MangaStorIOSQLitePutResolver()) @@ -39,8 +48,67 @@ public class DatabaseHelper { .build()) .build(); - manga = new MangaManager(db); - chapter = new ChapterManager(db); + mMangaManager = new MangaManagerImpl(mDb); + mChapterManager = new ChapterManagerImpl(mDb); } + @Override + public Observable> getChapters(Manga manga) { + return mChapterManager.getChapters(manga); + } + + @Override + public Observable insertChapter(Chapter chapter) { + return mChapterManager.insertChapter(chapter); + } + + @Override + public Observable> insertChapters(List chapters) { + return mChapterManager.insertChapters(chapters); + } + + @Override + public Observable insertOrRemoveChapters(Manga manga, List chapters) { + return mChapterManager.insertOrRemoveChapters(manga, chapters); + } + + @Override + public Observable deleteChapter(Chapter chapter) { + return mChapterManager.deleteChapter(chapter); + } + + @Override + public Observable> deleteChapters(List chapters) { + return mChapterManager.deleteChapters(chapters); + } + + @Override + public Observable> getMangas() { + return mMangaManager.getMangas(); + } + + @Override + public Observable> getMangasWithUnread() { + return mMangaManager.getMangasWithUnread(); + } + + @Override + public Observable insertManga(Manga manga) { + return mMangaManager.insertManga(manga); + } + + @Override + public Observable> insertMangas(List mangas) { + return mMangaManager.insertMangas(mangas); + } + + @Override + public Observable deleteManga(Manga manga) { + return mMangaManager.deleteManga(manga); + } + + @Override + public Observable> deleteMangas(List mangas) { + return mMangaManager.deleteMangas(mangas); + } } diff --git a/app/src/main/java/eu/kanade/mangafeed/data/helpers/SourceManager.java b/app/src/main/java/eu/kanade/mangafeed/data/helpers/SourceManager.java new file mode 100644 index 0000000000..8466cdbe2e --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/data/helpers/SourceManager.java @@ -0,0 +1,38 @@ +package eu.kanade.mangafeed.data.helpers; + +import java.util.HashMap; + +import eu.kanade.mangafeed.data.caches.CacheManager; +import eu.kanade.mangafeed.sources.Batoto; +import eu.kanade.mangafeed.sources.Source; + +public class SourceManager { + + public static final int BATOTO = 1; + + private HashMap mSourcesMap; + private NetworkHelper mNetworkHelper; + private CacheManager mCacheManager; + + public SourceManager(NetworkHelper networkHelper, CacheManager cacheManager) { + mSourcesMap = new HashMap<>(); + mNetworkHelper = networkHelper; + mCacheManager = cacheManager; + } + + public Source get(int sourceKey) { + if (!mSourcesMap.containsKey(sourceKey)) { + mSourcesMap.put(sourceKey, createSource(sourceKey)); + } + return mSourcesMap.get(sourceKey); + } + + private Source createSource(int sourceKey) { + switch (sourceKey) { + case BATOTO: + return new Batoto(mNetworkHelper, mCacheManager); + } + + return null; + } +} diff --git a/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManager.java b/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManager.java index 00f5ca1de1..cbc1bf718b 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManager.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManager.java @@ -1,107 +1,28 @@ package eu.kanade.mangafeed.data.managers; -import com.pushtorefresh.storio.sqlite.StorIOSQLite; import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResult; import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResults; -import com.pushtorefresh.storio.sqlite.operations.get.PreparedGetListOfObjects; import com.pushtorefresh.storio.sqlite.operations.put.PutResult; import com.pushtorefresh.storio.sqlite.operations.put.PutResults; -import com.pushtorefresh.storio.sqlite.queries.Query; import java.util.List; import eu.kanade.mangafeed.data.models.Chapter; import eu.kanade.mangafeed.data.models.Manga; -import eu.kanade.mangafeed.data.tables.ChaptersTable; import rx.Observable; -public class ChapterManager extends BaseManager { +public interface ChapterManager { - public ChapterManager(StorIOSQLite db) { - super(db); - } + Observable> getChapters(Manga manga); - private PreparedGetListOfObjects prepareGet(Manga manga) { - return db.get() - .listOfObjects(Chapter.class) - .withQuery(Query.builder() - .table(ChaptersTable.TABLE) - .where(ChaptersTable.COLUMN_MANGA_ID + "=?") - .whereArgs(manga.id) - .build()) - .prepare(); - } + Observable insertChapter(Chapter chapter); - public Observable> get(Manga manga) { - return prepareGet(manga).createObservable(); - } + Observable> insertChapters(List chapters); - public Observable insert(Chapter chapter) { - return db.put() - .object(chapter) - .prepare() - .createObservable(); - } + Observable insertOrRemoveChapters(Manga manga, List chapters); - public Observable> insert(List chapters) { - return db.put() - .objects(chapters) - .prepare() - .createObservable(); - } + Observable deleteChapter(Chapter chapter); - // Add new chapters or delete if the source deletes them - public Observable insertOrRemove(Manga manga, List chapters) { - // I don't know a better approach - // TODO Fix this method - return Observable.create(subscriber -> { - List dbChapters = prepareGet(manga).executeAsBlocking(); + Observable> deleteChapters(List chapters); - Observable> newChaptersObs = - Observable.from(chapters) - .filter(c -> !dbChapters.contains(c)) - .toList(); - - Observable> deletedChaptersObs = - Observable.from(dbChapters) - .filter(c -> !chapters.contains(c)) - .toList(); - - Observable.zip(newChaptersObs, deletedChaptersObs, - (newChapters, deletedChapters) -> { - insert(newChapters).subscribe(); - delete(deletedChapters).subscribe(); - subscriber.onCompleted(); - return null; - }).subscribe(); - }); - - } - - public void createDummyChapters() { - Chapter c; - - for (int i = 1; i < 100; i++) { - c = new Chapter(); - c.manga_id = 1L; - c.name = "Chapter " + i; - c.url = "http://example.com/1"; - insert(c).subscribe(); - } - - } - - public Observable> delete(List chapters) { - return db.delete() - .objects(chapters) - .prepare() - .createObservable(); - } - - public Observable delete(Chapter chapter) { - return db.delete() - .object(chapter) - .prepare() - .createObservable(); - } } diff --git a/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManagerImpl.java b/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManagerImpl.java new file mode 100644 index 0000000000..c595a5c560 --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManagerImpl.java @@ -0,0 +1,101 @@ +package eu.kanade.mangafeed.data.managers; + +import com.pushtorefresh.storio.sqlite.StorIOSQLite; +import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResult; +import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResults; +import com.pushtorefresh.storio.sqlite.operations.get.PreparedGetListOfObjects; +import com.pushtorefresh.storio.sqlite.operations.put.PutResult; +import com.pushtorefresh.storio.sqlite.operations.put.PutResults; +import com.pushtorefresh.storio.sqlite.queries.Query; + +import java.util.ArrayList; +import java.util.List; + +import eu.kanade.mangafeed.data.models.Chapter; +import eu.kanade.mangafeed.data.models.Manga; +import eu.kanade.mangafeed.data.tables.ChaptersTable; +import rx.Observable; + +public class ChapterManagerImpl extends BaseManager implements ChapterManager { + + public ChapterManagerImpl(StorIOSQLite db) { + super(db); + } + + private PreparedGetListOfObjects prepareGet(Manga manga) { + return db.get() + .listOfObjects(Chapter.class) + .withQuery(Query.builder() + .table(ChaptersTable.TABLE) + .where(ChaptersTable.COLUMN_MANGA_ID + "=?") + .whereArgs(manga.id) + .build()) + .prepare(); + } + + @Override + public Observable> getChapters(Manga manga) { + return prepareGet(manga).createObservable(); + } + + @Override + public Observable insertChapter(Chapter chapter) { + return db.put() + .object(chapter) + .prepare() + .createObservable(); + } + + @Override + public Observable> insertChapters(List chapters) { + return db.put() + .objects(chapters) + .prepare() + .createObservable(); + } + + // Add new chapters or delete if the source deletes them + @Override + public Observable insertOrRemoveChapters(Manga manga, List chapters) { + // I don't know a better approach + // TODO Fix this method + return Observable.create(subscriber -> { + List dbChapters = prepareGet(manga).executeAsBlocking(); + + Observable> newChaptersObs = + Observable.from(chapters) + .filter(c -> !dbChapters.contains(c)) + .toList(); + + Observable> deletedChaptersObs = + Observable.from(dbChapters) + .filter(c -> !chapters.contains(c)) + .toList(); + + Observable.zip(newChaptersObs, deletedChaptersObs, + (newChapters, deletedChapters) -> { + insertChapters(newChapters).subscribe(); + deleteChapters(deletedChapters).subscribe(); + subscriber.onCompleted(); + return null; + }).subscribe(); + }); + } + + @Override + public Observable deleteChapter(Chapter chapter) { + return db.delete() + .object(chapter) + .prepare() + .createObservable(); + } + + @Override + public Observable> deleteChapters(List chapters) { + return db.delete() + .objects(chapters) + .prepare() + .createObservable(); + } + +} 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 1177a0c7d3..dbbf6081a8 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 @@ -1,97 +1,27 @@ package eu.kanade.mangafeed.data.managers; -import com.pushtorefresh.storio.sqlite.StorIOSQLite; import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResult; import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResults; import com.pushtorefresh.storio.sqlite.operations.put.PutResult; -import com.pushtorefresh.storio.sqlite.queries.Query; -import com.pushtorefresh.storio.sqlite.queries.RawQuery; +import com.pushtorefresh.storio.sqlite.operations.put.PutResults; 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; -public class MangaManager extends BaseManager { +public interface MangaManager { - public MangaManager(StorIOSQLite db) { - super(db); - } + Observable> getMangas(); - 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 - ); + Observable> getMangasWithUnread(); - public Observable> get() { - return db.get() - .listOfObjects(Manga.class) - .withQuery(Query.builder() - .table(MangasTable.TABLE) - .build()) - .prepare() - .createObservable(); - } + Observable insertManga(Manga manga); - public Observable> getWithUnread() { - return db.get() - .listOfObjects(Manga.class) - .withQuery(RawQuery.builder() - .query(mangasWithUnreadQuery) - .observesTables(MangasTable.TABLE, ChaptersTable.TABLE) - .build()) - .prepare() - .createObservable(); - } + Observable> insertMangas(List mangas); - public Observable insert(Manga manga) { - return db.put() - .object(manga) - .prepare() - .createObservable(); - } + Observable deleteManga(Manga manga); - 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"; - m.description="..."; - m.genre="Action, Drama"; - m.status="Ongoing"; - m.thumbnail_url="http://example.com/pic.png"; - return m; - } - - public Observable delete(Manga manga) { - return db.delete() - .object(manga) - .prepare() - .createObservable(); - } - - public Observable> delete(List mangas) { - return db.delete() - .objects(mangas) - .prepare() - .createObservable(); - } + Observable> deleteMangas(List mangas); } diff --git a/app/src/main/java/eu/kanade/mangafeed/data/managers/MangaManagerImpl.java b/app/src/main/java/eu/kanade/mangafeed/data/managers/MangaManagerImpl.java new file mode 100644 index 0000000000..e41d548a4b --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/data/managers/MangaManagerImpl.java @@ -0,0 +1,85 @@ +package eu.kanade.mangafeed.data.managers; + +import com.pushtorefresh.storio.sqlite.StorIOSQLite; +import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResult; +import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResults; +import com.pushtorefresh.storio.sqlite.operations.put.PutResult; +import com.pushtorefresh.storio.sqlite.operations.put.PutResults; +import com.pushtorefresh.storio.sqlite.queries.Query; +import com.pushtorefresh.storio.sqlite.queries.RawQuery; + +import java.util.ArrayList; +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; + +public class MangaManagerImpl extends BaseManager implements MangaManager { + + public MangaManagerImpl(StorIOSQLite db) { + 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> getMangas() { + return db.get() + .listOfObjects(Manga.class) + .withQuery(Query.builder() + .table(MangasTable.TABLE) + .build()) + .prepare() + .createObservable(); + } + + public Observable> getMangasWithUnread() { + return db.get() + .listOfObjects(Manga.class) + .withQuery(RawQuery.builder() + .query(mangasWithUnreadQuery) + .observesTables(MangasTable.TABLE, ChaptersTable.TABLE) + .build()) + .prepare() + .createObservable(); + } + + public Observable insertManga(Manga manga) { + return db.put() + .object(manga) + .prepare() + .createObservable(); + } + + public Observable> insertMangas(List mangas) { + return db.put() + .objects(mangas) + .prepare() + .createObservable(); + } + + public Observable deleteManga(Manga manga) { + return db.delete() + .object(manga) + .prepare() + .createObservable(); + } + + public Observable> deleteMangas(List mangas) { + return db.delete() + .objects(mangas) + .prepare() + .createObservable(); + } +} diff --git a/app/src/main/java/eu/kanade/mangafeed/data/tables/ChaptersTable.java b/app/src/main/java/eu/kanade/mangafeed/data/tables/ChaptersTable.java index 2145ac72e6..f8fe05993e 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/tables/ChaptersTable.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/tables/ChaptersTable.java @@ -40,7 +40,8 @@ public class ChaptersTable { + COLUMN_DATE_UPLOAD + " LONG NOT NULL, " + "FOREIGN KEY(" + COLUMN_MANGA_ID + ") REFERENCES " + MangasTable.TABLE + "(" + MangasTable.COLUMN_ID + ") " + "ON DELETE CASCADE" - + ");"; + + ");" + + "CREATE INDEX " + TABLE + "_" + COLUMN_MANGA_ID + "_index ON " + TABLE + "(" + COLUMN_MANGA_ID + ");"; } } 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 585f867ea0..039760e1af 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java @@ -11,6 +11,7 @@ import eu.kanade.mangafeed.data.helpers.PreferencesHelper; import eu.kanade.mangafeed.data.models.Manga; import eu.kanade.mangafeed.ui.activity.MangaDetailActivity; import eu.kanade.mangafeed.ui.adapter.LibraryAdapter; +import eu.kanade.mangafeed.util.DummyDataUtil; import eu.kanade.mangafeed.view.LibraryView; import rx.Observable; import rx.schedulers.Schedulers; @@ -32,8 +33,8 @@ public class LibraryPresenter extends BasePresenter { //TODO remove, only for testing if (prefs.isFirstRun()) { - db.manga.createDummyManga(); - db.chapter.createDummyChapters(); + db.insertMangas(DummyDataUtil.createDummyManga()).toBlocking().single(); + db.insertChapters(DummyDataUtil.createDummyChapters()).subscribe(); prefs.setNotFirstRun(); } @@ -52,10 +53,11 @@ public class LibraryPresenter extends BasePresenter { view.setAdapter(adapter); view.setMangaClickListener(); - subscriptions.add(db.manga.getWithUnread() + subscriptions.add(db.getMangasWithUnread() .observeOn(mainThread()) .subscribe(adapter::setNewItems) ); + } public void onQueryTextChange(String query) { @@ -68,7 +70,7 @@ public class LibraryPresenter extends BasePresenter { .map(checkedItems::keyAt) .map(adapter::getItem) .toList() - .flatMap(db.manga::delete) + .flatMap(db::deleteMangas) .subscribe(); } diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/MangaDetailPresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/MangaDetailPresenter.java index 77cff579fa..8f08945897 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/MangaDetailPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/MangaDetailPresenter.java @@ -30,7 +30,7 @@ public class MangaDetailPresenter extends BasePresenter { } public void initializeChapters(Manga manga) { - db.chapter.get(manga) + db.getChapters(manga) .subscribe(view::setChapters); } } diff --git a/app/src/main/java/eu/kanade/mangafeed/sources/Batoto.java b/app/src/main/java/eu/kanade/mangafeed/sources/Batoto.java index 4916f0cf4e..8a3929e0e3 100644 --- a/app/src/main/java/eu/kanade/mangafeed/sources/Batoto.java +++ b/app/src/main/java/eu/kanade/mangafeed/sources/Batoto.java @@ -1,7 +1,6 @@ package eu.kanade.mangafeed.sources; import com.squareup.okhttp.Headers; -import com.squareup.okhttp.Response; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; @@ -18,18 +17,17 @@ import java.util.concurrent.atomic.AtomicInteger; import eu.kanade.mangafeed.data.caches.CacheManager; import eu.kanade.mangafeed.data.helpers.NetworkHelper; +import eu.kanade.mangafeed.data.helpers.SourceManager; import eu.kanade.mangafeed.data.models.Chapter; import eu.kanade.mangafeed.data.models.Manga; import rx.Observable; -import rx.functions.Func1; import rx.schedulers.Schedulers; -import timber.log.Timber; -public class Batoto { +public class Batoto extends Source { public static final String NAME = "Batoto (EN)"; public static final String BASE_URL = "www.bato.to"; - public static final String INITIAL_UPDATE_URL = "http://bato.to/search_ajax?order_cond=update&order=desc&p=1"; + public static final String INITIAL_UPDATE_URL = "http://bato.to/search_ajax?order_cond=views&order=desc&p=1"; private static final Headers REQUEST_HEADERS = constructRequestHeaders(); private static Headers constructRequestHeaders() { @@ -105,38 +103,32 @@ public class Batoto { return Observable.just(genres); } - /* - public Observable pullLatestUpdatesFromNetwork(final UpdatePageMarker newUpdate) { - return mNetworkService - .getResponse(newUpdate.getNextPageUrl(), NetworkModule.NULL_CACHE_CONTROL, REQUEST_HEADERS) - .flatMap(new Func1>() { - @Override - public Observable call(Response response) { - return mNetworkService.mapResponseToString(response); - } - }) - .flatMap(new Func1>() { - @Override - public Observable call(String unparsedHtml) { - return Observable.just(parseHtmlToLatestUpdates(newUpdate.getNextPageUrl(), unparsedHtml)); - } - }); + public String getUrlFromPageNumber(int page) { + if (page == 1) + return INITIAL_UPDATE_URL; + + return INITIAL_UPDATE_URL.substring(0, INITIAL_UPDATE_URL.length() - 1) + page; } - private UpdatePageMarker parseHtmlToLatestUpdates(String requestUrl, String unparsedHtml) { + public Observable> pullPopularMangasFromNetwork(int page) { + String url = getUrlFromPageNumber(page); + return mNetworkService + .getStringResponse(url, mNetworkService.NULL_CACHE_CONTROL, REQUEST_HEADERS) + .flatMap(response -> Observable.just(parseHtmlToLatestUpdates(response))); + } + + private List parseHtmlToLatestUpdates(String unparsedHtml) { Document parsedDocument = Jsoup.parse(unparsedHtml); List updatedMangaList = scrapeUpdateMangasFromParsedDocument(parsedDocument); - updateLibraryInDatabase(updatedMangaList); + //updateLibraryInDatabase(updatedMangaList); - String nextPageUrl = findNextUrlFromParsedDocument(requestUrl, unparsedHtml); - int lastMangaPostion = updatedMangaList.size(); - - return new UpdatePageMarker(nextPageUrl, lastMangaPostion); + return updatedMangaList; } + private List scrapeUpdateMangasFromParsedDocument(Document parsedDocument) { - List updatedMangaList = new ArrayList(); + List updatedMangaList = new ArrayList<>(); Elements updatedHtmlBlocks = parsedDocument.select("tr:not([id]):not([class])"); for (Element currentHtmlBlock : updatedHtmlBlocks) { @@ -149,29 +141,27 @@ public class Batoto { } private Manga constructMangaFromHtmlBlock(Element htmlBlock) { - Manga mangaFromHtmlBlock = DefaultFactory.Manga.constructDefault(); - mangaFromHtmlBlock.setSource(NAME); + Manga mangaFromHtmlBlock = new Manga(); Element urlElement = htmlBlock.select("a[href^=http://bato.to]").first(); Element nameElement = urlElement; Element updateElement = htmlBlock.select("td").get(5); + mangaFromHtmlBlock.source = SourceManager.BATOTO; + if (urlElement != null) { String fieldUrl = urlElement.attr("href"); - mangaFromHtmlBlock.setUrl(fieldUrl); + mangaFromHtmlBlock.url = fieldUrl; } if (nameElement != null) { String fieldName = nameElement.text().trim(); - mangaFromHtmlBlock.setName(fieldName); + mangaFromHtmlBlock.title = fieldName; } if (updateElement != null) { long fieldUpdate = parseUpdateFromElement(updateElement); - mangaFromHtmlBlock.setUpdated(fieldUpdate); + mangaFromHtmlBlock.last_update = fieldUpdate; } - int updateCount = 1; - mangaFromHtmlBlock.setUpdateCount(updateCount); - return mangaFromHtmlBlock; } @@ -186,9 +176,11 @@ public class Batoto { // Do Nothing. } - return DefaultFactory.Manga.DEFAULT_UPDATED; + return 0; } + /* + private void updateLibraryInDatabase(List mangaList) { mQueryManager.beginLibraryTransaction(); try { diff --git a/app/src/main/java/eu/kanade/mangafeed/sources/Source.java b/app/src/main/java/eu/kanade/mangafeed/sources/Source.java new file mode 100644 index 0000000000..4d0d231aac --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/sources/Source.java @@ -0,0 +1,5 @@ +package eu.kanade.mangafeed.sources; + + +public class Source { +} diff --git a/app/src/main/java/eu/kanade/mangafeed/util/DummyDataUtil.java b/app/src/main/java/eu/kanade/mangafeed/util/DummyDataUtil.java new file mode 100644 index 0000000000..eb9f77d224 --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/util/DummyDataUtil.java @@ -0,0 +1,52 @@ +package eu.kanade.mangafeed.util; + +import java.util.ArrayList; +import java.util.List; + +import eu.kanade.mangafeed.data.models.Chapter; +import eu.kanade.mangafeed.data.models.Manga; + +/** + * Created by len on 8/10/15. + */ +public class DummyDataUtil { + + public static List createDummyManga() { + ArrayList mangas = new ArrayList<>(); + mangas.add(createDummyManga("One Piece")); + mangas.add(createDummyManga("Berserk")); + mangas.add(createDummyManga("Horimiya")); + mangas.add(createDummyManga("Übel Blatt")); + + return mangas; + } + + private static Manga createDummyManga(String title) { + Manga m = new Manga(); + m.title = title; + m.url="http://example.com"; + m.artist="Eiichiro Oda"; + m.author="Eiichiro Oda"; + m.description="..."; + m.genre="Action, Drama"; + m.status="Ongoing"; + m.thumbnail_url="http://example.com/pic.png"; + return m; + } + + public static List createDummyChapters() { + List chapters = new ArrayList<>(); + Chapter c; + + for (int i = 1; i < 50; i++) { + c = new Chapter(); + c.manga_id = 1L; + c.name = "Chapter " + i; + c.url = "http://example.com/1"; + chapters.add(c); + } + + return chapters; + } + +}