From 859e9ca653b5f65bfbaae30196b9e1072e9981e0 Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 29 Mar 2020 16:43:00 -0400 Subject: [PATCH] Add update library menu item --- .../data/library/LibraryUpdateService.kt | 7 ++++- .../ui/library/LibraryCategoryView.kt | 4 +-- .../tachiyomi/ui/library/LibraryController.kt | 6 +++- .../ui/recent/updates/UpdatesController.kt | 31 ++++++++++++++++--- app/src/main/res/menu/updates.xml | 13 ++++++++ app/src/main/res/values/strings.xml | 3 ++ 6 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 app/src/main/res/menu/updates.xml diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt index 07d066f062..8fb260d994 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt @@ -146,8 +146,9 @@ class LibraryUpdateService( * @param context the application context. * @param category a specific category to update, or null for global update. * @param target defines what should be updated. + * @return true if service newly started, false otherwise */ - fun start(context: Context, category: Category? = null, target: Target = Target.CHAPTERS) { + fun start(context: Context, category: Category? = null, target: Target = Target.CHAPTERS): Boolean { if (!isRunning(context)) { val intent = Intent(context, LibraryUpdateService::class.java).apply { putExtra(KEY_TARGET, target) @@ -158,7 +159,11 @@ class LibraryUpdateService( } else { context.startForegroundService(intent) } + + return true } + + return false } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt index b2159bc0d7..c258f43a8c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt @@ -96,10 +96,10 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att // Double the distance required to trigger sync swipe_refresh.setDistanceToTriggerSync((2 * 64 * resources.displayMetrics.density).toInt()) swipe_refresh.setOnRefreshListener { - if (!LibraryUpdateService.isRunning(context)) { - LibraryUpdateService.start(context, category) + if (LibraryUpdateService.start(context, category)) { context.toast(R.string.updating_category) } + // It can be a very long operation, so we disable swipe refresh and show a toast. swipe_refresh.isRefreshing = false } 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 d383c82896..806178c876 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 @@ -362,7 +362,11 @@ class LibraryController( R.id.action_search -> expandActionViewFromInteraction = true R.id.action_filter -> showSettingsSheet() R.id.action_update_library -> { - activity?.let { LibraryUpdateService.start(it) } + activity?.let { + if (LibraryUpdateService.start(it)) { + it.toast(R.string.updating_library) + } + } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesController.kt index 324822db3d..ae277acdcf 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesController.kt @@ -2,6 +2,7 @@ package eu.kanade.tachiyomi.ui.recent.updates import android.view.LayoutInflater import android.view.Menu +import android.view.MenuInflater import android.view.MenuItem import android.view.View import android.view.ViewGroup @@ -59,6 +60,10 @@ class UpdatesController : NucleusController(), var adapter: UpdatesAdapter? = null private set + init { + setHasOptionsMenu(true) + } + override fun getTitle(): String? { return resources?.getString(R.string.label_recent_updates) } @@ -94,10 +99,8 @@ class UpdatesController : NucleusController(), swipe_refresh.setDistanceToTriggerSync((2 * 64 * view.resources.displayMetrics.density).toInt()) swipe_refresh.refreshes().subscribeUntilDestroy { - if (!LibraryUpdateService.isRunning(view.context)) { - LibraryUpdateService.start(view.context) - view.context.toast(R.string.action_update_library) - } + updateLibrary() + // It can be a very long operation, so we disable swipe refresh and show a toast. swipe_refresh.isRefreshing = false } @@ -110,6 +113,26 @@ class UpdatesController : NucleusController(), super.onDestroyView(view) } + override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { + inflater.inflate(R.menu.updates, menu) + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.action_update_library -> updateLibrary() + } + + return super.onOptionsItemSelected(item) + } + + private fun updateLibrary() { + activity?.let { + if (LibraryUpdateService.start(it)) { + it.toast(R.string.updating_library) + } + } + } + /** * Returns selected chapters * @return list of selected chapters diff --git a/app/src/main/res/menu/updates.xml b/app/src/main/res/menu/updates.xml new file mode 100644 index 0000000000..38a131c391 --- /dev/null +++ b/app/src/main/res/menu/updates.xml @@ -0,0 +1,13 @@ + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index cafdc1adf9..6ce98cba99 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -504,6 +504,9 @@ Loading pages… Failed to load pages: %1$s + + Updating library + %1$s - Ch.%2$s