diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt index 1ce8a688c8..122d289839 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt @@ -356,6 +356,10 @@ class MangaController : activity?.toast(error.message) } + fun onTrackingCount(trackCount: Int) { + mangaInfoAdapter?.setTrackingCount(trackCount) + } + fun openMangaInWebView() { val source = presenter.source as? HttpSource ?: return diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt index a0adc55d45..196f26efa5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt @@ -13,6 +13,7 @@ import eu.kanade.tachiyomi.data.database.models.MangaCategory import eu.kanade.tachiyomi.data.download.DownloadManager import eu.kanade.tachiyomi.data.download.model.Download import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.source.LocalSource import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter @@ -39,6 +40,7 @@ class MangaPresenter( val source: Source, val preferences: PreferencesHelper = Injekt.get(), private val db: DatabaseHelper = Injekt.get(), + private val trackManager: TrackManager = Injekt.get(), private val downloadManager: DownloadManager = Injekt.get(), private val coverCache: CoverCache = Injekt.get() ) : BasePresenter() { @@ -83,8 +85,13 @@ class MangaPresenter( // Manga info - start getMangaObservable() + .observeOn(AndroidSchedulers.mainThread()) .subscribeLatestCache({ view, manga -> view.onNextMangaInfo(manga, source) }) + getTrackingObservable() + .observeOn(AndroidSchedulers.mainThread()) + .subscribeLatestCache(MangaController::onTrackingCount) { _, error -> Timber.e(error) } + // Prepare the relay. chaptersRelay.flatMap { applyChapterFilters(it) } .observeOn(AndroidSchedulers.mainThread()) @@ -122,7 +129,19 @@ class MangaPresenter( private fun getMangaObservable(): Observable { return db.getManga(manga.url, manga.source).asRxObservable() - .observeOn(AndroidSchedulers.mainThread()) + } + + private fun getTrackingObservable(): Observable { + if (!trackManager.hasLoggedServices()) { + return Observable.just(0) + } + + return db.getTracks(manga).asRxObservable() + .map { tracks -> + val loggedServices = trackManager.services.filter { it.isLogged }.map { it.id } + tracks.filter { it.sync_id in loggedServices } + } + .map { it.size } } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoHeaderAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoHeaderAdapter.kt index ec707b40b1..cd140d298a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoHeaderAdapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoHeaderAdapter.kt @@ -14,7 +14,6 @@ import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.glide.GlideApp import eu.kanade.tachiyomi.data.glide.MangaThumbnail import eu.kanade.tachiyomi.data.glide.toMangaThumbnail -import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.databinding.MangaInfoHeaderBinding import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.SourceManager @@ -47,6 +46,7 @@ class MangaInfoHeaderAdapter( private var manga: Manga = controller.presenter.manga private var source: Source = controller.presenter.source + private var trackCount: Int = 0 private val scope = CoroutineScope(Job() + Dispatchers.Main) private lateinit var binding: MangaInfoHeaderBinding @@ -78,6 +78,12 @@ class MangaInfoHeaderAdapter( notifyDataSetChanged() } + fun setTrackingCount(trackCount: Int) { + this.trackCount = trackCount + + notifyDataSetChanged() + } + inner class HeaderViewHolder(private val view: View) : RecyclerView.ViewHolder(view) { fun bind() { // For rounded corners @@ -93,13 +99,26 @@ class MangaInfoHeaderAdapter( .launchIn(scope) } - if (controller.presenter.manga.favorite && Injekt.get().hasLoggedServices()) { - binding.btnTracking.visible() - binding.btnTracking.clicks() - .onEach { controller.onTrackingClick() } - .launchIn(scope) - } else { - binding.btnTracking.gone() + with(binding.btnTracking) { + if (controller.presenter.manga.favorite) { + visible() + + if (trackCount > 0) { + setIconResource(R.drawable.ic_done_24dp) + text = view.context.resources.getQuantityString(R.plurals.num_trackers, trackCount, trackCount) + isChecked = true + } else { + setIconResource(R.drawable.ic_sync_24dp) + text = view.context.getString(R.string.manga_tracking_tab) + isChecked = false + } + + clicks() + .onEach { controller.onTrackingClick() } + .launchIn(scope) + } else { + gone() + } } if (controller.presenter.source is HttpSource) { diff --git a/app/src/main/res/drawable/ic_done_white_18dp.xml b/app/src/main/res/drawable/ic_done_white_18dp.xml deleted file mode 100644 index 3e9103eb03..0000000000 --- a/app/src/main/res/drawable/ic_done_white_18dp.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5704b2d3a0..cca960ba6f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -514,6 +514,10 @@ Tracking + + 1 tracker + %d trackers + Add tracking Reading Currently reading