From 2aaaad7a24d04d4252190f60581e35b33f1f78cf Mon Sep 17 00:00:00 2001 From: inorichi Date: Mon, 12 Oct 2015 01:01:20 +0200 Subject: [PATCH] Show mangas from catalogue --- .../data/helpers/DatabaseHelper.java | 10 +++++ .../mangafeed/data/managers/MangaManager.java | 4 ++ .../data/managers/MangaManagerImpl.java | 17 +++++++- .../mangafeed/data/tables/MangasTable.java | 16 ++++---- .../presenter/CatalogueListPresenter.java | 40 +++++++++++++++++-- .../mangafeed/presenter/LibraryPresenter.java | 3 -- .../ui/activity/CatalogueListActivity.java | 9 +++++ .../ui/adapter/CatalogueListHolder.java | 27 +++++++++++++ .../mangafeed/view/CatalogueListView.java | 2 + .../res/layout/activity_catalogue_list.xml | 11 ++++- app/src/main/res/layout/item_catalogue.xml | 14 +++++++ 11 files changed, 137 insertions(+), 16 deletions(-) create mode 100644 app/src/main/java/eu/kanade/mangafeed/ui/adapter/CatalogueListHolder.java create mode 100644 app/src/main/res/layout/item_catalogue.xml 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 93ecfb428a..3a8a754137 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 @@ -92,6 +92,16 @@ public class DatabaseHelper implements MangaManager, ChapterManager { return mMangaManager.getMangasWithUnread(); } + @Override + public Observable> getManga(String url) { + return mMangaManager.getManga(url); + } + + @Override + public Observable> getManga(int id) { + return mMangaManager.getManga(id); + } + @Override public Observable insertManga(Manga manga) { return mMangaManager.insertManga(manga); 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 dbbf6081a8..b655859054 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 @@ -16,6 +16,10 @@ public interface MangaManager { Observable> getMangasWithUnread(); + Observable> getManga(String url); + + Observable> getManga(int id); + Observable insertManga(Manga manga); Observable> insertMangas(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 index e41d548a4b..79046fb11f 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/managers/MangaManagerImpl.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/managers/MangaManagerImpl.java @@ -8,7 +8,6 @@ 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; @@ -55,6 +54,22 @@ public class MangaManagerImpl extends BaseManager implements MangaManager { .createObservable(); } + public Observable> getManga(String url) { + return db.get() + .listOfObjects(Manga.class) + .withQuery(Query.builder() + .table(MangasTable.TABLE) + .where(MangasTable.COLUMN_URL + "=?") + .whereArgs(url) + .build()) + .prepare() + .createObservable(); + } + + public Observable> getManga(int id) { + return null; + } + public Observable insertManga(Manga manga) { return db.put() .object(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 48e852e924..f5ceeb6855 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 @@ -71,15 +71,15 @@ public class MangasTable { + COLUMN_ID + " INTEGER NOT NULL PRIMARY KEY, " + COLUMN_SOURCE + " INTEGER NOT NULL, " + COLUMN_URL + " TEXT NOT NULL, " - + COLUMN_ARTIST + " TEXT NOT NULL, " - + COLUMN_AUTHOR + " TEXT NOT NULL, " - + COLUMN_DESCRIPTION + " TEXT NOT NULL, " - + COLUMN_GENRE + " TEXT NOT NULL, " + + COLUMN_ARTIST + " TEXT, " + + COLUMN_AUTHOR + " TEXT, " + + COLUMN_DESCRIPTION + " TEXT, " + + COLUMN_GENRE + " TEXT, " + COLUMN_TITLE + " TEXT NOT NULL, " - + COLUMN_STATUS + " TEXT NOT NULL, " - + COLUMN_THUMBNAIL_URL + " TEXT NOT NULL, " - + COLUMN_RANK + " INTEGER NOT NULL, " - + COLUMN_LAST_UPDATE + " LONG NOT NULL, " + + COLUMN_STATUS + " TEXT, " + + COLUMN_THUMBNAIL_URL + " TEXT, " + + COLUMN_RANK + " INTEGER, " + + COLUMN_LAST_UPDATE + " LONG, " + COLUMN_INITIALIZED + " BOOLEAN NOT NULL, " + COLUMN_VIEWER + " INTEGER NOT NULL, " + COLUMN_CHAPTER_ORDER + " INTEGER NOT NULL" diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/CatalogueListPresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/CatalogueListPresenter.java index 564b68e8b3..48f6ba33c0 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/CatalogueListPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/CatalogueListPresenter.java @@ -5,14 +5,26 @@ import android.content.Intent; import javax.inject.Inject; import eu.kanade.mangafeed.App; +import eu.kanade.mangafeed.data.helpers.DatabaseHelper; import eu.kanade.mangafeed.data.helpers.SourceManager; +import eu.kanade.mangafeed.data.models.Manga; +import eu.kanade.mangafeed.sources.Source; +import eu.kanade.mangafeed.ui.adapter.CatalogueListHolder; import eu.kanade.mangafeed.view.CatalogueListView; +import rx.Observable; +import rx.android.schedulers.AndroidSchedulers; +import rx.schedulers.Schedulers; +import uk.co.ribot.easyadapter.EasyAdapter; public class CatalogueListPresenter { CatalogueListView view; + EasyAdapter adapter; + Source selectedSource; @Inject SourceManager sourceManager; + @Inject DatabaseHelper db; + public CatalogueListPresenter(CatalogueListView view) { this.view = view; @@ -20,8 +32,30 @@ public class CatalogueListPresenter { } public void initializeSource() { - Intent intent = view.getIntent(); - int sourceId = intent.getIntExtra(Intent.EXTRA_UID, -1); - view.setSource(sourceManager.get(sourceId)); + int sourceId = view.getIntent().getIntExtra(Intent.EXTRA_UID, -1); + selectedSource = sourceManager.get(sourceId); + view.setSource(selectedSource); + + adapter = new EasyAdapter<>(view.getActivity(), CatalogueListHolder.class); + view.setAdapter(adapter); + + getMangasFromSource(); } + + private void getMangasFromSource() { + selectedSource.pullPopularMangasFromNetwork(1) + .subscribeOn(Schedulers.io()) + .flatMap(Observable::from) + .flatMap(networkManga -> db.getManga(networkManga.url) + .flatMap(result -> { + if (result.size() == 0) { + return db.insertManga(networkManga) + .flatMap(i -> Observable.just(networkManga)); + } + return Observable.just(networkManga); + })) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(adapter::addItem); + } + } 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 039760e1af..aa8343926a 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java @@ -16,8 +16,6 @@ import eu.kanade.mangafeed.view.LibraryView; import rx.Observable; import rx.schedulers.Schedulers; -import static rx.android.schedulers.AndroidSchedulers.mainThread; - public class LibraryPresenter extends BasePresenter { private LibraryView view; @@ -54,7 +52,6 @@ public class LibraryPresenter extends BasePresenter { view.setMangaClickListener(); subscriptions.add(db.getMangasWithUnread() - .observeOn(mainThread()) .subscribe(adapter::setNewItems) ); diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/activity/CatalogueListActivity.java b/app/src/main/java/eu/kanade/mangafeed/ui/activity/CatalogueListActivity.java index aaf321b6a5..7c681ec72f 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/activity/CatalogueListActivity.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/activity/CatalogueListActivity.java @@ -2,6 +2,7 @@ package eu.kanade.mangafeed.ui.activity; import android.os.Bundle; import android.support.v7.widget.Toolbar; +import android.widget.ListView; import butterknife.Bind; import butterknife.ButterKnife; @@ -9,12 +10,16 @@ import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.presenter.CatalogueListPresenter; import eu.kanade.mangafeed.sources.Source; import eu.kanade.mangafeed.view.CatalogueListView; +import uk.co.ribot.easyadapter.EasyAdapter; public class CatalogueListActivity extends BaseActivity implements CatalogueListView { @Bind(R.id.toolbar) Toolbar toolbar; + @Bind(R.id.catalogue_manga_list) + ListView manga_list; + private CatalogueListPresenter presenter; private Source source; @@ -35,4 +40,8 @@ public class CatalogueListActivity extends BaseActivity implements CatalogueList setToolbarTitle(source.getName()); } + public void setAdapter(EasyAdapter adapter) { + manga_list.setAdapter(adapter); + } + } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/adapter/CatalogueListHolder.java b/app/src/main/java/eu/kanade/mangafeed/ui/adapter/CatalogueListHolder.java new file mode 100644 index 0000000000..f5432a69dc --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/ui/adapter/CatalogueListHolder.java @@ -0,0 +1,27 @@ +package eu.kanade.mangafeed.ui.adapter; + +import android.view.View; +import android.widget.TextView; + +import eu.kanade.mangafeed.R; +import eu.kanade.mangafeed.data.models.Manga; +import uk.co.ribot.easyadapter.ItemViewHolder; +import uk.co.ribot.easyadapter.PositionInfo; +import uk.co.ribot.easyadapter.annotations.LayoutId; +import uk.co.ribot.easyadapter.annotations.ViewId; + +@LayoutId(R.layout.item_catalogue) +public class CatalogueListHolder extends ItemViewHolder { + + @ViewId(R.id.catalogue_title) + TextView title; + + public CatalogueListHolder(View view) { + super(view); + } + + @Override + public void onSetValues(Manga manga, PositionInfo positionInfo) { + title.setText(manga.title); + } +} diff --git a/app/src/main/java/eu/kanade/mangafeed/view/CatalogueListView.java b/app/src/main/java/eu/kanade/mangafeed/view/CatalogueListView.java index dc9b8c1933..8cf2f73556 100644 --- a/app/src/main/java/eu/kanade/mangafeed/view/CatalogueListView.java +++ b/app/src/main/java/eu/kanade/mangafeed/view/CatalogueListView.java @@ -3,8 +3,10 @@ package eu.kanade.mangafeed.view; import android.content.Intent; import eu.kanade.mangafeed.sources.Source; +import uk.co.ribot.easyadapter.EasyAdapter; public interface CatalogueListView extends BaseView { Intent getIntent(); void setSource(Source source); + void setAdapter(EasyAdapter adapter); } diff --git a/app/src/main/res/layout/activity_catalogue_list.xml b/app/src/main/res/layout/activity_catalogue_list.xml index b74ed09a66..238cefee61 100644 --- a/app/src/main/res/layout/activity_catalogue_list.xml +++ b/app/src/main/res/layout/activity_catalogue_list.xml @@ -1,7 +1,9 @@ @@ -9,4 +11,11 @@ android:id="@+id/toolbar" layout="@layout/toolbar"/> + + diff --git a/app/src/main/res/layout/item_catalogue.xml b/app/src/main/res/layout/item_catalogue.xml new file mode 100644 index 0000000000..125ecebffc --- /dev/null +++ b/app/src/main/res/layout/item_catalogue.xml @@ -0,0 +1,14 @@ + + + + + \ No newline at end of file