From 7c503648ffffb4a7060fd7c9129e6331af79c384 Mon Sep 17 00:00:00 2001 From: len Date: Wed, 6 Apr 2016 16:45:13 +0200 Subject: [PATCH] Minor changes. Also fix #240 --- app/src/main/java/eu/kanade/tachiyomi/App.kt | 1 - .../eu/kanade/tachiyomi/EventBusIndex.java | 75 ------------------- ...aryMangasEvent.kt => LibraryMangaEvent.kt} | 2 +- .../ui/base/activity/BaseActivity.kt | 9 --- .../ui/base/fragment/BaseFragment.kt | 9 --- .../ui/base/presenter/BasePresenter.kt | 9 --- .../ui/library/LibraryCategoryFragment.kt | 23 +++--- .../tachiyomi/ui/library/LibraryFragment.kt | 5 +- .../tachiyomi/ui/library/LibraryPresenter.kt | 18 ++--- .../ui/manga/info/MangaInfoPresenter.kt | 4 +- 10 files changed, 28 insertions(+), 127 deletions(-) delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/EventBusIndex.java rename app/src/main/java/eu/kanade/tachiyomi/event/{LibraryMangasEvent.kt => LibraryMangaEvent.kt} (80%) diff --git a/app/src/main/java/eu/kanade/tachiyomi/App.kt b/app/src/main/java/eu/kanade/tachiyomi/App.kt index e615ab9928..54f81002a5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/App.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/App.kt @@ -53,7 +53,6 @@ open class App : Application() { protected open fun setupEventBus() { EventBus.builder() - .addIndex(EventBusIndex()) .logNoSubscriberMessages(false) .installDefaultEventBus() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/EventBusIndex.java b/app/src/main/java/eu/kanade/tachiyomi/EventBusIndex.java deleted file mode 100644 index 2e3a61d000..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/EventBusIndex.java +++ /dev/null @@ -1,75 +0,0 @@ -package eu.kanade.tachiyomi; - -import org.greenrobot.eventbus.meta.SimpleSubscriberInfo; -import org.greenrobot.eventbus.meta.SubscriberMethodInfo; -import org.greenrobot.eventbus.meta.SubscriberInfo; -import org.greenrobot.eventbus.meta.SubscriberInfoIndex; - -import org.greenrobot.eventbus.ThreadMode; - -import java.util.HashMap; -import java.util.Map; - -/** This class is generated by EventBus, do not edit. */ -public class EventBusIndex implements SubscriberInfoIndex { - private static final Map, SubscriberInfo> SUBSCRIBER_INDEX; - - static { - SUBSCRIBER_INDEX = new HashMap, SubscriberInfo>(); - - putIndex(new SimpleSubscriberInfo(eu.kanade.tachiyomi.ui.manga.MangaPresenter.class, true, - new SubscriberMethodInfo[] { - new SubscriberMethodInfo("onEvent", eu.kanade.tachiyomi.data.database.models.Manga.class, ThreadMode.MAIN, 0, - true), - })); - - putIndex(new SimpleSubscriberInfo(eu.kanade.tachiyomi.ui.manga.myanimelist.MyAnimeListPresenter.class, true, - new SubscriberMethodInfo[] { - new SubscriberMethodInfo("onEvent", eu.kanade.tachiyomi.event.MangaEvent.class, ThreadMode.MAIN, 0, true), - })); - - putIndex(new SimpleSubscriberInfo(eu.kanade.tachiyomi.ui.reader.ReaderPresenter.class, true, - new SubscriberMethodInfo[] { - new SubscriberMethodInfo("onEvent", eu.kanade.tachiyomi.event.ReaderEvent.class, ThreadMode.MAIN, 0, true), - })); - - putIndex(new SimpleSubscriberInfo(eu.kanade.tachiyomi.ui.library.LibraryCategoryFragment.class, true, - new SubscriberMethodInfo[] { - new SubscriberMethodInfo("onEvent", eu.kanade.tachiyomi.event.LibraryMangasEvent.class, ThreadMode.MAIN, 0, - true), - })); - - putIndex(new SimpleSubscriberInfo(eu.kanade.tachiyomi.data.download.DownloadService.class, true, - new SubscriberMethodInfo[] { - new SubscriberMethodInfo("onEvent", eu.kanade.tachiyomi.event.DownloadChaptersEvent.class, ThreadMode.MAIN, - 0, true), - })); - - putIndex(new SimpleSubscriberInfo(eu.kanade.tachiyomi.ui.manga.chapter.ChaptersPresenter.class, true, - new SubscriberMethodInfo[] { - new SubscriberMethodInfo("onEvent", eu.kanade.tachiyomi.event.MangaEvent.class, ThreadMode.MAIN, 0, true), - })); - - putIndex(new SimpleSubscriberInfo(eu.kanade.tachiyomi.ui.manga.info.MangaInfoPresenter.class, true, - new SubscriberMethodInfo[] { - new SubscriberMethodInfo("onEvent", eu.kanade.tachiyomi.event.MangaEvent.class, ThreadMode.MAIN, 0, true), - new SubscriberMethodInfo("onEvent", eu.kanade.tachiyomi.event.ChapterCountEvent.class, ThreadMode.MAIN, 0, - true), - })); - - } - - private static void putIndex(SubscriberInfo info) { - SUBSCRIBER_INDEX.put(info.getSubscriberClass(), info); - } - - @Override - public SubscriberInfo getSubscriberInfo(Class subscriberClass) { - SubscriberInfo info = SUBSCRIBER_INDEX.get(subscriberClass); - if (info != null) { - return info; - } else { - return null; - } - } -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/event/LibraryMangasEvent.kt b/app/src/main/java/eu/kanade/tachiyomi/event/LibraryMangaEvent.kt similarity index 80% rename from app/src/main/java/eu/kanade/tachiyomi/event/LibraryMangasEvent.kt rename to app/src/main/java/eu/kanade/tachiyomi/event/LibraryMangaEvent.kt index 8dfd585f92..8c6c55cde3 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/event/LibraryMangasEvent.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/event/LibraryMangaEvent.kt @@ -3,7 +3,7 @@ package eu.kanade.tachiyomi.event import eu.kanade.tachiyomi.data.database.models.Category import eu.kanade.tachiyomi.data.database.models.Manga -class LibraryMangasEvent(val mangas: Map>) { +class LibraryMangaEvent(val mangas: Map>) { fun getMangasForCategory(category: Category): List? { return mangas[category.id] diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt index a92a216cdd..ed41051a13 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt @@ -9,7 +9,6 @@ import android.view.View import android.widget.TextView import eu.kanade.tachiyomi.App import eu.kanade.tachiyomi.R -import org.greenrobot.eventbus.EventBus open class BaseActivity : AppCompatActivity() { @@ -51,14 +50,6 @@ open class BaseActivity : AppCompatActivity() { return super.onOptionsItemSelected(item) } - fun registerForEvents() { - EventBus.getDefault().register(this) - } - - fun unregisterForEvents() { - EventBus.getDefault().unregister(this) - } - fun snack(text: String?, duration: Int = Snackbar.LENGTH_LONG) { val snack = Snackbar.make(findViewById(android.R.id.content)!!, text ?: getString(R.string.unknown_error), duration) val textView = snack.view.findViewById(android.support.design.R.id.snackbar_text) as TextView diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/fragment/BaseFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/fragment/BaseFragment.kt index 260869d9d3..888d4b1a2e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/fragment/BaseFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/fragment/BaseFragment.kt @@ -2,7 +2,6 @@ package eu.kanade.tachiyomi.ui.base.fragment import android.support.v4.app.Fragment import eu.kanade.tachiyomi.ui.base.activity.BaseActivity -import org.greenrobot.eventbus.EventBus open class BaseFragment : Fragment() { @@ -17,12 +16,4 @@ open class BaseFragment : Fragment() { val baseActivity: BaseActivity get() = activity as BaseActivity - fun registerForEvents() { - EventBus.getDefault().register(this) - } - - fun unregisterForEvents() { - EventBus.getDefault().unregister(this) - } - } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/presenter/BasePresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/presenter/BasePresenter.kt index bd205f72b7..50f9eda785 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/presenter/BasePresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/presenter/BasePresenter.kt @@ -2,21 +2,12 @@ package eu.kanade.tachiyomi.ui.base.presenter import android.content.Context import nucleus.view.ViewWithPresenter -import org.greenrobot.eventbus.EventBus import rx.Observable open class BasePresenter> : RxPresenter() { lateinit var context: Context - fun registerForEvents() { - EventBus.getDefault().register(this) - } - - fun unregisterForEvents() { - EventBus.getDefault().unregister(this) - } - fun Observable.subscribeFirst(onNext: (V, T) -> Unit, onError: ((V, Throwable) -> Unit)? = null) = compose(deliverFirst()).subscribe(split(onNext, onError)) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryFragment.kt index b0925ecae5..6a2a1faa73 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryFragment.kt @@ -9,13 +9,11 @@ import com.f2prateek.rx.preferences.Preference import eu.davidea.flexibleadapter.FlexibleAdapter import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.models.Manga -import eu.kanade.tachiyomi.event.LibraryMangasEvent +import eu.kanade.tachiyomi.event.LibraryMangaEvent import eu.kanade.tachiyomi.ui.base.adapter.FlexibleViewHolder import eu.kanade.tachiyomi.ui.base.fragment.BaseFragment import eu.kanade.tachiyomi.ui.manga.MangaActivity import kotlinx.android.synthetic.main.fragment_library_category.* -import org.greenrobot.eventbus.Subscribe -import org.greenrobot.eventbus.ThreadMode import rx.Subscription import java.util.* @@ -44,6 +42,11 @@ class LibraryCategoryFragment : BaseFragment(), FlexibleViewHolder.OnListItemCli field = value ?: ArrayList() } + /** + * Subscription for the library manga. + */ + private var libraryMangaSubscription: Subscription? = null + /** * Subscription of the number of manga per row. */ @@ -115,11 +118,12 @@ class LibraryCategoryFragment : BaseFragment(), FlexibleViewHolder.OnListItemCli override fun onResume() { super.onResume() - registerForEvents() + libraryMangaSubscription = libraryPresenter.libraryMangaSubject + .subscribe({ onNextLibraryManga(it) }) } override fun onPause() { - unregisterForEvents() + libraryMangaSubscription?.unsubscribe() super.onPause() } @@ -130,13 +134,12 @@ class LibraryCategoryFragment : BaseFragment(), FlexibleViewHolder.OnListItemCli } /** - * Subscribe to [LibraryMangasEvent]. When an event is received, it updates [mangas] if needed + * Subscribe to [LibraryMangaEvent]. When an event is received, it updates [mangas] if needed * and refresh the content of the adapter. * * @param event the event received. */ - @Subscribe(sticky = true, threadMode = ThreadMode.MAIN) - fun onEvent(event: LibraryMangasEvent) { + fun onNextLibraryManga(event: LibraryMangaEvent?) { // Get the categories from the parent fragment. val categories = libraryFragment.adapter.categories ?: return @@ -144,13 +147,13 @@ class LibraryCategoryFragment : BaseFragment(), FlexibleViewHolder.OnListItemCli if (position >= categories.size) return // Get the manga list for this category - val mangaForCategory = event.getMangasForCategory(categories[position]) + val mangaForCategory = event?.getMangasForCategory(categories[position]) // Update the list only if the reference to the list is different, avoiding reseting the // adapter after every onResume. if (mangas !== mangaForCategory) { mangas = mangaForCategory - mangas?.let { adapter.setItems(it) } + adapter.setItems(mangas ?: emptyList()) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.kt index a1ea3c26ee..041f6baa37 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.kt @@ -14,7 +14,7 @@ import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.models.Category import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.library.LibraryUpdateService -import eu.kanade.tachiyomi.event.LibraryMangasEvent +import eu.kanade.tachiyomi.event.LibraryMangaEvent import eu.kanade.tachiyomi.ui.base.fragment.BaseRxFragment import eu.kanade.tachiyomi.ui.category.CategoryActivity import eu.kanade.tachiyomi.ui.main.MainActivity @@ -23,7 +23,6 @@ import eu.kanade.tachiyomi.util.toast import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.fragment_library.* import nucleus.factory.RequiresPresenter -import org.greenrobot.eventbus.EventBus import java.io.IOException /** @@ -273,7 +272,7 @@ class LibraryFragment : BaseRxFragment(), ActionMode.Callback } // Send the manga map to child fragments after the adapter is updated. - EventBus.getDefault().postSticky(LibraryMangasEvent(mangaMap)) + presenter.libraryMangaSubject.onNext(LibraryMangaEvent(mangaMap)) } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt index e3bb2254b0..322666a953 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt @@ -11,9 +11,8 @@ import eu.kanade.tachiyomi.data.download.DownloadManager import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.data.source.SourceManager -import eu.kanade.tachiyomi.event.LibraryMangasEvent +import eu.kanade.tachiyomi.event.LibraryMangaEvent import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter -import org.greenrobot.eventbus.EventBus import rx.Observable import rx.android.schedulers.AndroidSchedulers import rx.subjects.BehaviorSubject @@ -35,12 +34,17 @@ class LibraryPresenter : BasePresenter() { /** * Currently selected manga. */ - lateinit var selectedMangas: MutableList + var selectedMangas = mutableListOf() /** * Search query of the library. */ - lateinit var searchSubject: BehaviorSubject + val searchSubject = BehaviorSubject.create() + + /** + * Subject to notify the library's viewpager for updates. + */ + val libraryMangaSubject = BehaviorSubject.create() /** * Database. @@ -77,10 +81,6 @@ class LibraryPresenter : BasePresenter() { override fun onCreate(savedState: Bundle?) { super.onCreate(savedState) - selectedMangas = ArrayList() - - searchSubject = BehaviorSubject.create() - restartableLatestCache(GET_LIBRARY, { getLibraryObservable() }, { view, pair -> view.onNextLibraryUpdate(pair.first, pair.second) }) @@ -92,7 +92,7 @@ class LibraryPresenter : BasePresenter() { } override fun onDropView() { - EventBus.getDefault().removeStickyEvent(LibraryMangasEvent::class.java) + libraryMangaSubject.onNext(null) super.onDropView() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoPresenter.kt index e2136cc249..efc0719cae 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoPresenter.kt @@ -79,7 +79,9 @@ class MangaInfoPresenter : BasePresenter() { // Update chapter count SharedData.get(ChapterCountEvent::class.java)?.let { - add(it.observable.subscribeLatestCache({ view, count -> view.setChapterCount(count) })) + add(it.observable + .observeOn(AndroidSchedulers.mainThread()) + .subscribeLatestCache({ view, count -> view.setChapterCount(count) })) } }