Refactor shortcut helper

This commit is contained in:
Koitharu
2020-04-08 20:30:44 +03:00
parent 679c06557e
commit 0be4f56538
5 changed files with 33 additions and 35 deletions

View File

@@ -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,

View File

@@ -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<MangaListView<MangaHistory>>() {
@@ -62,7 +62,7 @@ class HistoryListPresenter : BasePresenter<MangaListView<MangaHistory>>() {
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<MangaListView<MangaHistory>>() {
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) {

View File

@@ -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<MangaListView<File>>() {
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
ShortcutUtils.removeAppShortcut(get(), manga)
MangaShortcut(manga).removeAppShortcut(get())
}
viewState.onItemRemoved(manga)
} catch (e: CancellationException) {

View File

@@ -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)
}
}
}

View File

@@ -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()
}
}
}