Download service now has a listener for the badge and fab icon
This commit is contained in:
parent
e23973cc9c
commit
733dc3765e
@ -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<Boolean> = BehaviorRelay.create(false)
|
||||
|
||||
private val listeners = mutableSetOf<DownloadServiceListener>()
|
||||
|
||||
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)
|
||||
}
|
||||
|
@ -91,7 +91,7 @@ class Downloader(
|
||||
launchNow {
|
||||
val chapters = async { store.restore() }
|
||||
queue.addAll(chapters.await())
|
||||
MainActivity.setDownloadBadge(queue.isNotEmpty())
|
||||
DownloadService.callListeners()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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()
|
||||
|
@ -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<DownloadManager>().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<DownloadManager>()
|
||||
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<DownloadManager>()
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -182,7 +182,6 @@ class SearchActivity: MainActivity() {
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
bottomNav = false
|
||||
bottomNavView = null
|
||||
}
|
||||
|
||||
override fun handleIntentAction(intent: Intent): Boolean {
|
||||
|
@ -28,14 +28,10 @@
|
||||
|
||||
<com.google.android.material.floatingactionbutton.FloatingActionButton
|
||||
android:id="@+id/fab"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
app:layout_anchor="@id/bottom_sheet"
|
||||
style="@style/Theme.Widget.FABFixed"
|
||||
app:layout_anchorGravity="end|top"
|
||||
android:backgroundTint="?attr/colorAccent"
|
||||
android:tint="@color/md_white_1000"
|
||||
app:tint="@color/md_white_1000"
|
||||
android:layout_margin="24dp"
|
||||
android:layout_gravity="center"
|
||||
app:srcCompat="@drawable/ic_file_download_white_24dp"/>
|
||||
|
||||
<View
|
||||
|
Loading…
x
Reference in New Issue
Block a user