diff --git a/app/src/main/java/eu/kanade/tachiyomi/event/MangaEvent.java b/app/src/main/java/eu/kanade/tachiyomi/event/MangaEvent.java new file mode 100644 index 000000000..9cfa8d173 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/event/MangaEvent.java @@ -0,0 +1,12 @@ +package eu.kanade.tachiyomi.event; + +import eu.kanade.tachiyomi.data.database.models.Manga; + +public class MangaEvent { + + public final Manga manga; + + public MangaEvent(Manga manga) { + this.manga = manga; + } +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/presenter/RxPresenter.java b/app/src/main/java/eu/kanade/tachiyomi/ui/base/presenter/RxPresenter.java index 57ba302de..914750a19 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/presenter/RxPresenter.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/presenter/RxPresenter.java @@ -107,14 +107,14 @@ public class RxPresenter extends Presenter { } /** - * Checks if a restartable is subscribed. + * Checks if a restartable is unsubscribed. * - * @param restartableId id of a restartable. - * @return True if the restartable is subscribed, false otherwise. + * @param restartableId id of the restartable. + * @return true if the subscription is null or unsubscribed, false otherwise. */ - public boolean isSubscribed(int restartableId) { - Subscription s = restartableSubscriptions.get(restartableId); - return s != null && !s.isUnsubscribed(); + public boolean isUnsubscribed(int restartableId) { + Subscription subscription = restartableSubscriptions.get(restartableId); + return subscription == null || subscription.isUnsubscribed(); } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueFragment.java b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueFragment.java index f9d43a420..5dfcd50bb 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueFragment.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueFragment.java @@ -243,7 +243,8 @@ public class CatalogueFragment extends BaseRxFragment private void restartRequest(String newQuery) { // If text didn't change, do nothing - if (query.equals(newQuery)) return; + if (query.equals(newQuery) || getPresenter().getSource() == null) + return; query = newQuery; showProgressBar(); @@ -325,9 +326,8 @@ public class CatalogueFragment extends BaseRxFragment @Override public void onListItemLongClick(int position) { final Manga selectedManga = adapter.getItem(position); - final Manga dbManga = getPresenter().getDbManga(selectedManga.id); - int textRes = dbManga.favorite ? R.string.remove_from_library : R.string.add_to_library; + int textRes = selectedManga.favorite ? R.string.remove_from_library : R.string.add_to_library; new MaterialDialog.Builder(getActivity()) .items(getString(textRes)) @@ -335,6 +335,7 @@ public class CatalogueFragment extends BaseRxFragment switch (which) { case 0: getPresenter().changeMangaFavorite(selectedManga); + adapter.notifyItemChanged(position); break; } }) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueListHolder.java b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueListHolder.java index a4fd788a4..94042c449 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueListHolder.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueListHolder.java @@ -1,5 +1,6 @@ package eu.kanade.tachiyomi.ui.catalogue; +import android.support.v4.content.ContextCompat; import android.view.View; import android.widget.TextView; @@ -12,13 +13,20 @@ public class CatalogueListHolder extends CatalogueHolder { @Bind(R.id.title) TextView title; + private final int favoriteColor; + private final int unfavoriteColor; + public CatalogueListHolder(View view, CatalogueAdapter adapter, OnListItemClickListener listener) { super(view, adapter, listener); ButterKnife.bind(this, view); + + favoriteColor = ContextCompat.getColor(view.getContext(), R.color.hint_text); + unfavoriteColor = ContextCompat.getColor(view.getContext(), R.color.primary_text); } @Override public void onSetValues(Manga manga, CataloguePresenter presenter) { title.setText(manga.title); + title.setTextColor(manga.favorite ? favoriteColor : unfavoriteColor); } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CataloguePresenter.java b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CataloguePresenter.java index 97381fde9..97e835c20 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CataloguePresenter.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CataloguePresenter.java @@ -167,10 +167,6 @@ public class CataloguePresenter extends BasePresenter { .onErrorResumeNext(error -> Observable.just(manga)); } - public Manga getDbManga(long id) { - return db.getManga(id).executeAsBlocking(); - } - public Source getSource() { return source; } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.java b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.java index a70ce73c1..b566794fb 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.java @@ -58,7 +58,7 @@ public class LibraryPresenter extends BasePresenter { @Override protected void onTakeView(LibraryFragment libraryFragment) { super.onTakeView(libraryFragment); - if (!isSubscribed(GET_LIBRARY)) { + if (isUnsubscribed(GET_LIBRARY)) { start(GET_LIBRARY); } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaActivity.java b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaActivity.java index 8899cf04f..7a550b536 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaActivity.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaActivity.java @@ -14,6 +14,7 @@ import javax.inject.Inject; import butterknife.Bind; import butterknife.ButterKnife; +import de.greenrobot.event.EventBus; import eu.kanade.tachiyomi.App; import eu.kanade.tachiyomi.R; import eu.kanade.tachiyomi.data.database.models.Manga; @@ -30,21 +31,21 @@ public class MangaActivity extends BaseRxActivity { @Bind(R.id.toolbar) Toolbar toolbar; @Bind(R.id.tabs) TabLayout tabs; - @Bind(R.id.view_pager) ViewPager view_pager; + @Bind(R.id.view_pager) ViewPager viewPager; @Inject PreferencesHelper preferences; @Inject MangaSyncManager mangaSyncManager; private MangaDetailAdapter adapter; - private long manga_id; - private boolean is_online; + private boolean isOnline; - public final static String MANGA_ID = "manga_id"; public final static String MANGA_ONLINE = "manga_online"; public static Intent newIntent(Context context, Manga manga) { Intent intent = new Intent(context, MangaActivity.class); - intent.putExtra(MANGA_ID, manga.id); + if (manga != null) { + EventBus.getDefault().postSticky(manga); + } return intent; } @@ -59,23 +60,19 @@ public class MangaActivity extends BaseRxActivity { Intent intent = getIntent(); - manga_id = intent.getLongExtra(MANGA_ID, -1); - is_online = intent.getBooleanExtra(MANGA_ONLINE, false); + isOnline = intent.getBooleanExtra(MANGA_ONLINE, false); setupViewPager(); - - if (savedState == null) - getPresenter().queryManga(manga_id); } private void setupViewPager() { adapter = new MangaDetailAdapter(getSupportFragmentManager(), this); - view_pager.setAdapter(adapter); - tabs.setupWithViewPager(view_pager); + viewPager.setAdapter(adapter); + tabs.setupWithViewPager(viewPager); - if (!is_online) - view_pager.setCurrentItem(MangaDetailAdapter.CHAPTERS_FRAGMENT); + if (!isOnline) + viewPager.setCurrentItem(MangaDetailAdapter.CHAPTERS_FRAGMENT); } public void setManga(Manga manga) { @@ -83,7 +80,7 @@ public class MangaActivity extends BaseRxActivity { } public boolean isCatalogueManga() { - return is_online; + return isOnline; } class MangaDetailAdapter extends FragmentPagerAdapter { @@ -104,7 +101,7 @@ public class MangaActivity extends BaseRxActivity { }; pageCount = 2; - if (!is_online && mangaSyncManager.getMyAnimeList().isLogged()) + if (!isOnline && mangaSyncManager.getMyAnimeList().isLogged()) pageCount++; } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.java b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.java index 15faffb3e..797ea674c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.java @@ -7,17 +7,17 @@ import javax.inject.Inject; import de.greenrobot.event.EventBus; import eu.kanade.tachiyomi.data.database.DatabaseHelper; import eu.kanade.tachiyomi.data.database.models.Manga; +import eu.kanade.tachiyomi.event.MangaEvent; import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter; +import eu.kanade.tachiyomi.util.EventBusHook; import icepick.State; import rx.Observable; -import rx.android.schedulers.AndroidSchedulers; -import rx.schedulers.Schedulers; public class MangaPresenter extends BasePresenter { @Inject DatabaseHelper db; - @State long mangaId; + @State Manga manga; private static final int DB_MANGA = 1; @@ -26,24 +26,28 @@ public class MangaPresenter extends BasePresenter { super.onCreate(savedState); restartableLatestCache(DB_MANGA, this::getDbMangaObservable, MangaActivity::setManga); + + if (savedState == null) + registerForStickyEvents(); } @Override protected void onDestroy() { super.onDestroy(); // Avoid new instances receiving wrong manga - EventBus.getDefault().removeStickyEvent(Manga.class); + EventBus.getDefault().removeStickyEvent(MangaEvent.class); } private Observable getDbMangaObservable() { - return db.getManga(mangaId).asRxObservable() - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .doOnNext(manga -> EventBus.getDefault().postSticky(manga)); + return Observable.just(manga) + .doOnNext(manga -> EventBus.getDefault().postSticky(new MangaEvent(manga))); } - public void queryManga(long mangaId) { - this.mangaId = mangaId; + @EventBusHook + public void onEventMainThread(Manga manga) { + EventBus.getDefault().removeStickyEvent(manga); + unregisterForEvents(); + this.manga = manga; start(DB_MANGA); } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersHolder.java b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersHolder.java index b57813e82..373ace8de 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersHolder.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersHolder.java @@ -31,23 +31,24 @@ public class ChaptersHolder extends FlexibleViewHolder { SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); + private final int readColor; + private final int unreadColor; + public ChaptersHolder(View view, ChaptersAdapter adapter, OnListItemClickListener listener) { super(view, adapter, listener); this.adapter = adapter; ButterKnife.bind(this, view); + readColor = ContextCompat.getColor(view.getContext(), R.color.hint_text); + unreadColor = ContextCompat.getColor(view.getContext(), R.color.primary_text); + chapterMenu.setOnClickListener(v -> v.post(() -> showPopupMenu(v))); } public void onSetValues(Context context, Chapter chapter) { this.item = chapter; title.setText(chapter.name); - - if (chapter.read) { - title.setTextColor(ContextCompat.getColor(context, R.color.hint_text)); - } else { - title.setTextColor(ContextCompat.getColor(context, R.color.primary_text)); - } + title.setTextColor(chapter.read ? readColor : unreadColor); if (!chapter.read && chapter.last_page_read > 0) { pages.setText(context.getString(R.string.chapter_progress, chapter.last_page_read + 1)); diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersPresenter.java b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersPresenter.java index de89c1c79..034543b59 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersPresenter.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersPresenter.java @@ -18,6 +18,7 @@ import eu.kanade.tachiyomi.data.source.SourceManager; import eu.kanade.tachiyomi.data.source.base.Source; import eu.kanade.tachiyomi.event.ChapterCountEvent; import eu.kanade.tachiyomi.event.DownloadChaptersEvent; +import eu.kanade.tachiyomi.event.MangaEvent; import eu.kanade.tachiyomi.event.ReaderEvent; import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter; import eu.kanade.tachiyomi.util.EventBusHook; @@ -90,10 +91,10 @@ public class ChaptersPresenter extends BasePresenter { } @EventBusHook - public void onEventMainThread(Manga manga) { - this.manga = manga; + public void onEventMainThread(MangaEvent event) { + this.manga = event.manga; - if (!isSubscribed(DB_CHAPTERS)) { + if (isUnsubscribed(DB_CHAPTERS)) { source = sourceManager.get(manga.source); start(DB_CHAPTERS); diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoPresenter.java b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoPresenter.java index 896541905..a955b7849 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoPresenter.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoPresenter.java @@ -10,6 +10,7 @@ import eu.kanade.tachiyomi.data.database.models.Manga; import eu.kanade.tachiyomi.data.source.SourceManager; import eu.kanade.tachiyomi.data.source.base.Source; import eu.kanade.tachiyomi.event.ChapterCountEvent; +import eu.kanade.tachiyomi.event.MangaEvent; import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter; import eu.kanade.tachiyomi.util.EventBusHook; import rx.Observable; @@ -26,8 +27,6 @@ public class MangaInfoPresenter extends BasePresenter { protected Source source; private int count = -1; - private boolean isFetching; - private static final int GET_MANGA = 1; private static final int GET_CHAPTER_COUNT = 2; private static final int FETCH_MANGA_INFO = 3; @@ -69,10 +68,10 @@ public class MangaInfoPresenter extends BasePresenter { } @EventBusHook - public void onEventMainThread(Manga manga) { - this.manga = manga; + public void onEventMainThread(MangaEvent event) { + this.manga = event.manga; source = sourceManager.get(manga.source); - start(GET_MANGA); + refreshManga(); } @EventBusHook @@ -84,8 +83,7 @@ public class MangaInfoPresenter extends BasePresenter { } public void fetchMangaFromSource() { - if (!isFetching) { - isFetching = true; + if (isUnsubscribed(FETCH_MANGA_INFO)) { start(FETCH_MANGA_INFO); } } @@ -97,15 +95,16 @@ public class MangaInfoPresenter extends BasePresenter { db.insertManga(manga).executeAsBlocking(); return Observable.just(manga); }) - .finallyDo(() -> isFetching = false) .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()); + .observeOn(AndroidSchedulers.mainThread()) + .doOnCompleted(this::refreshManga); } public void toggleFavorite() { manga.favorite = !manga.favorite; onMangaFavoriteChange(manga.favorite); db.insertManga(manga).executeAsBlocking(); + refreshManga(); } private void onMangaFavoriteChange(boolean isFavorite) { @@ -116,4 +115,9 @@ public class MangaInfoPresenter extends BasePresenter { } } + // Used to refresh the view + private void refreshManga() { + start(GET_MANGA); + } + } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/myanimelist/MyAnimeListPresenter.java b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/myanimelist/MyAnimeListPresenter.java index a49b169cd..db23a4e65 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/myanimelist/MyAnimeListPresenter.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/myanimelist/MyAnimeListPresenter.java @@ -12,6 +12,7 @@ import eu.kanade.tachiyomi.data.database.models.Manga; import eu.kanade.tachiyomi.data.database.models.MangaSync; import eu.kanade.tachiyomi.data.mangasync.MangaSyncManager; import eu.kanade.tachiyomi.data.mangasync.services.MyAnimeList; +import eu.kanade.tachiyomi.event.MangaEvent; import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter; import eu.kanade.tachiyomi.util.EventBusHook; import eu.kanade.tachiyomi.util.ToastUtil; @@ -102,8 +103,8 @@ public class MyAnimeListPresenter extends BasePresenter { } @EventBusHook - public void onEventMainThread(Manga manga) { - this.manga = manga; + public void onEventMainThread(MangaEvent event) { + this.manga = event.manga; start(GET_MANGA_SYNC); } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.java b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.java index 7022d1291..e4321a82d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.java @@ -342,7 +342,7 @@ public class ReaderPresenter extends BasePresenter { } private void stopPreloadingNextChapter() { - if (isSubscribed(PRELOAD_NEXT_CHAPTER)) { + if (!isUnsubscribed(PRELOAD_NEXT_CHAPTER)) { stop(PRELOAD_NEXT_CHAPTER); if (nextChapterPageList != null) source.savePageList(nextChapter.url, nextChapterPageList); diff --git a/app/src/main/res/layout/activity_edit_categories.xml b/app/src/main/res/layout/activity_edit_categories.xml index 564865341..c709d0773 100644 --- a/app/src/main/res/layout/activity_edit_categories.xml +++ b/app/src/main/res/layout/activity_edit_categories.xml @@ -23,6 +23,7 @@ android:layout_gravity="bottom|right" android:layout_margin="@dimen/fab_margin" android:src="@drawable/ic_action_add_18dp" + app:backgroundTint="@color/colorPrimary" app:layout_anchor="@id/categories_list" app:layout_anchorGravity="bottom|right|end" app:layout_behavior="eu.kanade.tachiyomi.ui.base.fab.ScrollAwareFABBehavior"/>