From 733dc3765e8cecfb193bc19b350ff1a0990ea796 Mon Sep 17 00:00:00 2001 From: Jay Date: Thu, 6 Feb 2020 14:01:36 -0800 Subject: [PATCH] Download service now has a listener for the badge and fab icon --- .../data/download/DownloadService.kt | 26 ++++++++-- .../tachiyomi/data/download/Downloader.kt | 2 +- .../tachiyomi/ui/library/LibraryController.kt | 15 +++++- .../kanade/tachiyomi/ui/main/MainActivity.kt | 49 ++++++++----------- .../tachiyomi/ui/main/SearchActivity.kt | 1 - .../main/res/layout/library_controller.xml | 8 +-- 6 files changed, 60 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadService.kt index e725d10502..9d90657937 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadService.kt @@ -1,6 +1,5 @@ package eu.kanade.tachiyomi.data.download -import android.app.Activity import android.app.Notification import android.app.Service import android.content.Context @@ -17,7 +16,6 @@ import com.jakewharton.rxrelay.BehaviorRelay import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.data.preference.PreferencesHelper -import eu.kanade.tachiyomi.ui.main.MainActivity import eu.kanade.tachiyomi.util.lang.plusAssign import eu.kanade.tachiyomi.util.system.connectivityManager import eu.kanade.tachiyomi.util.system.isServiceRunning @@ -42,13 +40,29 @@ class DownloadService : Service() { */ val runningRelay: BehaviorRelay = BehaviorRelay.create(false) + private val listeners = mutableSetOf() + + fun addListener(listener: DownloadServiceListener) { + listeners.add(listener) + } + + fun removeListener(listener: DownloadServiceListener) { + listeners.remove(listener) + } + + fun callListeners() { + val downloadManager: DownloadManager by injectLazy() + listeners.forEach { + it.downloadStatusChanged(downloadManager.hasQueue()) + } + } /** * Starts this service. * * @param context the application context. */ fun start(context: Context) { - MainActivity.setDownloadBadge(true) + callListeners() val intent = Intent(context, DownloadService::class.java) if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { context.startService(intent) @@ -63,7 +77,7 @@ class DownloadService : Service() { * @param context the application context. */ fun stop(context: Context) { - MainActivity.setDownloadBadge(false) + callListeners() context.stopService(Intent(context, DownloadService::class.java)) } @@ -209,3 +223,7 @@ class DownloadService : Service() { } } + +interface DownloadServiceListener { + fun downloadStatusChanged(downloading: Boolean) +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt index c8dd261f37..069a8b78b0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt @@ -91,7 +91,7 @@ class Downloader( launchNow { val chapters = async { store.restore() } queue.addAll(chapters.await()) - MainActivity.setDownloadBadge(queue.isNotEmpty()) + DownloadService.callListeners() } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt index b4e03ab38f..91c15a0a26 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt @@ -36,6 +36,8 @@ import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.models.Category import eu.kanade.tachiyomi.data.database.models.LibraryManga import eu.kanade.tachiyomi.data.database.models.Manga +import eu.kanade.tachiyomi.data.download.DownloadService +import eu.kanade.tachiyomi.data.download.DownloadServiceListener import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.ui.base.controller.NucleusController @@ -74,7 +76,8 @@ class LibraryController( SecondaryDrawerController, ActionMode.Callback, ChangeMangaCategoriesDialog.Listener, - MigrationInterface { + MigrationInterface, + DownloadServiceListener { /** * Position of the active category. @@ -226,6 +229,8 @@ class LibraryController( fab.setOnClickListener { router.pushController(DownloadController().withFadeTransaction()) } + fab.scaleX = 0f + fab.scaleY = 0f } fun enableReorderItems(category: Category) { @@ -259,11 +264,13 @@ class LibraryController( if (type.isEnter) { activity?.tabs?.setupWithViewPager(library_pager) presenter.subscribeLibrary() + DownloadService.addListener(this) } } override fun onDestroyView(view: View) { adapter?.onDestroy() + DownloadService.removeListener(this) adapter = null actionMode = null tabsVisibilitySubscription?.unsubscribe() @@ -271,6 +278,12 @@ class LibraryController( super.onDestroyView(view) } + override fun downloadStatusChanged(downloading: Boolean) { + val scale = if (downloading) 1f else 0f + fab.animate().scaleX(scale).scaleY(scale).setDuration(200).start() + fab.isClickable = downloading + fab.isFocusable = downloading + } override fun onDetach(view: View) { destroyActionModeIfNeeded() snack?.dismiss() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt index a2345212be..2d393de4cd 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt @@ -33,6 +33,8 @@ import com.google.android.material.snackbar.Snackbar import eu.kanade.tachiyomi.Migrations import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.download.DownloadManager +import eu.kanade.tachiyomi.data.download.DownloadService +import eu.kanade.tachiyomi.data.download.DownloadServiceListener import eu.kanade.tachiyomi.data.notification.NotificationReceiver import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.getOrDefault @@ -75,7 +77,7 @@ import uy.kohesive.injekt.injectLazy import java.util.Date import java.util.concurrent.TimeUnit -open class MainActivity : BaseActivity() { +open class MainActivity : BaseActivity(), DownloadServiceListener { protected lateinit var router: Router @@ -198,7 +200,7 @@ open class MainActivity : BaseActivity() { val content: ViewGroup = findViewById(R.id.main_content) bottomNav = preferences.useBottonNav().getOrDefault() - bottomNavView = navigationView + DownloadService.addListener(this) content.fitsSystemWindows = !bottomNav if (!bottomNav) { container.systemUiVisibility = @@ -378,8 +380,7 @@ open class MainActivity : BaseActivity() { super.onResume() bottomNav = preferences.useBottonNav().getOrDefault() getExtensionUpdates() - bottomNavView = navigationView - setDownloadBadge(Injekt.get().hasQueue()) + DownloadService.callListeners() val useBiometrics = preferences.useBiometrics().getOrDefault() if (useBiometrics && BiometricManager.from(this) .canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS) { @@ -394,11 +395,6 @@ open class MainActivity : BaseActivity() { preferences.useBiometrics().set(false) } - override fun onPause() { - super.onPause() - bottomNavView = null - } - private fun getExtensionUpdates() { if (Date().time >= preferences.lastExtCheck().getOrDefault() + TimeUnit.HOURS.toMillis(1)) { @@ -469,7 +465,7 @@ open class MainActivity : BaseActivity() { override fun onDestroy() { super.onDestroy() - bottomNavView = null + DownloadService.removeListener(this) nav_view?.setNavigationItemSelectedListener(null) toolbar?.setNavigationOnClickListener(null) } @@ -642,6 +638,21 @@ open class MainActivity : BaseActivity() { } } + override fun downloadStatusChanged(downloading: Boolean) { + if (!bottomNav) return + val downloadManager = Injekt.get() + val hasQueue = downloading || downloadManager.hasQueue() + if (hasQueue) { + val badge = navigationView?.getOrCreateBadge(R.id.nav_drawer_library) + ?: return + badge.clearNumber() + badge.backgroundColor = getResourceColor(R.attr.badgeColor) + } + else { + navigationView?.removeBadge(R.id.nav_drawer_library) + } + } + companion object { // Shortcut actions const val SHORTCUT_LIBRARY = "eu.kanade.tachiyomi.SHOW_LIBRARY" @@ -662,24 +673,6 @@ open class MainActivity : BaseActivity() { var bottomNav = false internal set - - internal var bottomNavView:BottomNavigationView? = null - - fun setDownloadBadge(downloading: Boolean) { - if (!bottomNav) return - val downloadManager = Injekt.get() - val hasQueue = downloading || downloadManager.hasQueue() - if (hasQueue) { - val badge = bottomNavView?.getOrCreateBadge(R.id.nav_drawer_library) - ?: return - badge.clearNumber() - badge.backgroundColor = bottomNavView?.context?.getResourceColor(R.attr - .badgeColor) ?: Color.BLACK - } - else { - bottomNavView?.removeBadge(R.id.nav_drawer_library) - } - } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/main/SearchActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/main/SearchActivity.kt index 5c0dcb4076..969220aaf7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/main/SearchActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/main/SearchActivity.kt @@ -182,7 +182,6 @@ class SearchActivity: MainActivity() { override fun onResume() { super.onResume() bottomNav = false - bottomNavView = null } override fun handleIntentAction(intent: Intent): Boolean { diff --git a/app/src/main/res/layout/library_controller.xml b/app/src/main/res/layout/library_controller.xml index d273e627d5..aeb1bab571 100644 --- a/app/src/main/res/layout/library_controller.xml +++ b/app/src/main/res/layout/library_controller.xml @@ -28,14 +28,10 @@