From 0be4f56538fd240df010d3f72886dc5912cff28b Mon Sep 17 00:00:00 2001 From: Koitharu Date: Wed, 8 Apr 2020 20:30:44 +0300 Subject: [PATCH] Refactor shortcut helper --- .../ui/details/MangaDetailsActivity.kt | 4 +- .../main/list/history/HistoryListPresenter.kt | 6 +-- .../ui/main/list/local/LocalListPresenter.kt | 4 +- .../kotatsu/ui/reader/ReaderActivity.kt | 4 +- .../{ShortcutUtils.kt => MangaShortcut.kt} | 50 +++++++++---------- 5 files changed, 33 insertions(+), 35 deletions(-) rename app/src/main/java/org/koitharu/kotatsu/utils/{ShortcutUtils.kt => MangaShortcut.kt} (79%) diff --git a/app/src/main/java/org/koitharu/kotatsu/ui/details/MangaDetailsActivity.kt b/app/src/main/java/org/koitharu/kotatsu/ui/details/MangaDetailsActivity.kt index 1bba6e99e..1e91e3da3 100644 --- a/app/src/main/java/org/koitharu/kotatsu/ui/details/MangaDetailsActivity.kt +++ b/app/src/main/java/org/koitharu/kotatsu/ui/details/MangaDetailsActivity.kt @@ -25,8 +25,8 @@ import org.koitharu.kotatsu.core.model.MangaSource import org.koitharu.kotatsu.ui.browser.BrowserActivity import org.koitharu.kotatsu.ui.common.BaseActivity import org.koitharu.kotatsu.ui.download.DownloadService +import org.koitharu.kotatsu.utils.MangaShortcut import org.koitharu.kotatsu.utils.ShareHelper -import org.koitharu.kotatsu.utils.ShortcutUtils import org.koitharu.kotatsu.utils.ext.getDisplayMessage class MangaDetailsActivity : BaseActivity(), MangaDetailsView { @@ -155,7 +155,7 @@ class MangaDetailsActivity : BaseActivity(), MangaDetailsView { R.id.action_shortcut -> { manga?.let { lifecycleScope.launch { - if (!ShortcutUtils.requestPinShortcut(this@MangaDetailsActivity, manga)) { + if (!MangaShortcut(it).requestPinShortcut(this@MangaDetailsActivity)) { Snackbar.make( pager, R.string.operation_not_supported, diff --git a/app/src/main/java/org/koitharu/kotatsu/ui/main/list/history/HistoryListPresenter.kt b/app/src/main/java/org/koitharu/kotatsu/ui/main/list/history/HistoryListPresenter.kt index 37ad9c40b..535d5a576 100644 --- a/app/src/main/java/org/koitharu/kotatsu/ui/main/list/history/HistoryListPresenter.kt +++ b/app/src/main/java/org/koitharu/kotatsu/ui/main/list/history/HistoryListPresenter.kt @@ -14,7 +14,7 @@ import org.koitharu.kotatsu.core.model.MangaHistory import org.koitharu.kotatsu.domain.history.HistoryRepository import org.koitharu.kotatsu.ui.common.BasePresenter import org.koitharu.kotatsu.ui.main.list.MangaListView -import org.koitharu.kotatsu.utils.ShortcutUtils +import org.koitharu.kotatsu.utils.MangaShortcut @InjectViewState class HistoryListPresenter : BasePresenter>() { @@ -62,7 +62,7 @@ class HistoryListPresenter : BasePresenter>() { repository.clear() } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { - ShortcutUtils.clearAppShortcuts(get()) + MangaShortcut.clearAppShortcuts(get()) } viewState.onListChanged(emptyList()) } catch (_: CancellationException) { @@ -84,7 +84,7 @@ class HistoryListPresenter : BasePresenter>() { repository.delete(manga) } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { - ShortcutUtils.removeAppShortcut(get(), manga) + MangaShortcut(manga).removeAppShortcut(get()) } viewState.onItemRemoved(manga) } catch (_: CancellationException) { diff --git a/app/src/main/java/org/koitharu/kotatsu/ui/main/list/local/LocalListPresenter.kt b/app/src/main/java/org/koitharu/kotatsu/ui/main/list/local/LocalListPresenter.kt index c0485fe62..9e4edc8f9 100644 --- a/app/src/main/java/org/koitharu/kotatsu/ui/main/list/local/LocalListPresenter.kt +++ b/app/src/main/java/org/koitharu/kotatsu/ui/main/list/local/LocalListPresenter.kt @@ -19,8 +19,8 @@ import org.koitharu.kotatsu.domain.MangaProviderFactory import org.koitharu.kotatsu.domain.history.HistoryRepository import org.koitharu.kotatsu.ui.common.BasePresenter import org.koitharu.kotatsu.ui.main.list.MangaListView +import org.koitharu.kotatsu.utils.MangaShortcut import org.koitharu.kotatsu.utils.MediaStoreCompat -import org.koitharu.kotatsu.utils.ShortcutUtils import org.koitharu.kotatsu.utils.ext.safe import org.koitharu.kotatsu.utils.ext.sub import java.io.File @@ -98,7 +98,7 @@ class LocalListPresenter : BasePresenter>() { } } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { - ShortcutUtils.removeAppShortcut(get(), manga) + MangaShortcut(manga).removeAppShortcut(get()) } viewState.onItemRemoved(manga) } catch (e: CancellationException) { diff --git a/app/src/main/java/org/koitharu/kotatsu/ui/reader/ReaderActivity.kt b/app/src/main/java/org/koitharu/kotatsu/ui/reader/ReaderActivity.kt index 6012a77e5..f842297a6 100644 --- a/app/src/main/java/org/koitharu/kotatsu/ui/reader/ReaderActivity.kt +++ b/app/src/main/java/org/koitharu/kotatsu/ui/reader/ReaderActivity.kt @@ -35,8 +35,8 @@ import org.koitharu.kotatsu.ui.reader.thumbnails.OnPageSelectListener import org.koitharu.kotatsu.ui.reader.thumbnails.PagesThumbnailsSheet import org.koitharu.kotatsu.ui.reader.wetoon.WebtoonReaderFragment import org.koitharu.kotatsu.utils.GridTouchHelper +import org.koitharu.kotatsu.utils.MangaShortcut import org.koitharu.kotatsu.utils.ShareHelper -import org.koitharu.kotatsu.utils.ShortcutUtils import org.koitharu.kotatsu.utils.anim.Motion import org.koitharu.kotatsu.utils.ext.* @@ -92,7 +92,7 @@ class ReaderActivity : BaseFullscreenActivity(), ReaderView, ChaptersDialog.OnCh if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { GlobalScope.launch { safe { - ShortcutUtils.addAppShortcut(applicationContext, state.manga) + MangaShortcut(state.manga).addAppShortcut(applicationContext) } } } diff --git a/app/src/main/java/org/koitharu/kotatsu/utils/ShortcutUtils.kt b/app/src/main/java/org/koitharu/kotatsu/utils/MangaShortcut.kt similarity index 79% rename from app/src/main/java/org/koitharu/kotatsu/utils/ShortcutUtils.kt rename to app/src/main/java/org/koitharu/kotatsu/utils/MangaShortcut.kt index f89da5400..46d71bc47 100644 --- a/app/src/main/java/org/koitharu/kotatsu/utils/ShortcutUtils.kt +++ b/app/src/main/java/org/koitharu/kotatsu/utils/MangaShortcut.kt @@ -22,25 +22,18 @@ import org.koitharu.kotatsu.domain.MangaDataRepository import org.koitharu.kotatsu.ui.details.MangaDetailsActivity import org.koitharu.kotatsu.utils.ext.safe -object ShortcutUtils { +class MangaShortcut(private val manga: Manga) { - suspend fun requestPinShortcut(context: Context, manga: Manga?): Boolean { - return manga != null && ShortcutManagerCompat.requestPinShortcut( - context, - buildShortcutInfo(context, manga).build(), - null - ) - } + private val shortcutId = manga.id.toString() @RequiresApi(Build.VERSION_CODES.N_MR1) - suspend fun addAppShortcut(context: Context, manga: Manga) { - val id = manga.id.toString() - val builder = buildShortcutInfo(context, manga) + suspend fun addAppShortcut(context: Context) { val manager = context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager val limit = manager.maxShortcutCountPerActivity + val builder = buildShortcutInfo(context, manga) val shortcuts = manager.dynamicShortcuts for (shortcut in shortcuts) { - if (shortcut.id == id) { + if (shortcut.id == shortcutId) { builder.setRank(shortcut.rank + 1) manager.updateShortcuts(listOf(builder.build().toShortcutInfo())) return @@ -53,22 +46,23 @@ object ShortcutUtils { manager.addDynamicShortcuts(listOf(builder.build().toShortcutInfo())) } - @RequiresApi(Build.VERSION_CODES.N_MR1) - fun removeAppShortcut(context: Context, manga: Manga) { - val id = manga.id.toString() - val manager = context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager - manager.removeDynamicShortcuts(listOf(id)) + suspend fun requestPinShortcut(context: Context): Boolean { + return ShortcutManagerCompat.requestPinShortcut( + context, + buildShortcutInfo(context, manga).build(), + null + ) } @RequiresApi(Build.VERSION_CODES.N_MR1) - fun clearAppShortcuts(context: Context) { + fun removeAppShortcut(context: Context) { val manager = context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager - manager.removeAllDynamicShortcuts() + manager.removeDynamicShortcuts(listOf(shortcutId)) } private suspend fun buildShortcutInfo( context: Context, - manga: Manga + manga: Manga, ): ShortcutInfoCompat.Builder { val icon = safe { val size = getIconSize(context) @@ -77,12 +71,7 @@ object ShortcutUtils { size(size) scale(Scale.FILL) }.toBitmap() - ThumbnailUtils.extractThumbnail( - bmp, - size.width, - size.height, - 0 - ) + ThumbnailUtils.extractThumbnail(bmp, size.width, size.height, 0) } } MangaDataRepository().storeManga(manga) @@ -109,4 +98,13 @@ object ShortcutUtils { } } } + + companion object { + + @RequiresApi(Build.VERSION_CODES.N_MR1) + fun clearAppShortcuts(context: Context) { + val manager = context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager + manager.removeAllDynamicShortcuts() + } + } } \ No newline at end of file