Refactor shortcut helper

remotes/weblate/feature/pagecrop
Koitharu 6 years ago
parent 679c06557e
commit 0be4f56538

@ -25,8 +25,8 @@ import org.koitharu.kotatsu.core.model.MangaSource
import org.koitharu.kotatsu.ui.browser.BrowserActivity import org.koitharu.kotatsu.ui.browser.BrowserActivity
import org.koitharu.kotatsu.ui.common.BaseActivity import org.koitharu.kotatsu.ui.common.BaseActivity
import org.koitharu.kotatsu.ui.download.DownloadService import org.koitharu.kotatsu.ui.download.DownloadService
import org.koitharu.kotatsu.utils.MangaShortcut
import org.koitharu.kotatsu.utils.ShareHelper import org.koitharu.kotatsu.utils.ShareHelper
import org.koitharu.kotatsu.utils.ShortcutUtils
import org.koitharu.kotatsu.utils.ext.getDisplayMessage import org.koitharu.kotatsu.utils.ext.getDisplayMessage
class MangaDetailsActivity : BaseActivity(), MangaDetailsView { class MangaDetailsActivity : BaseActivity(), MangaDetailsView {
@ -155,7 +155,7 @@ class MangaDetailsActivity : BaseActivity(), MangaDetailsView {
R.id.action_shortcut -> { R.id.action_shortcut -> {
manga?.let { manga?.let {
lifecycleScope.launch { lifecycleScope.launch {
if (!ShortcutUtils.requestPinShortcut(this@MangaDetailsActivity, manga)) { if (!MangaShortcut(it).requestPinShortcut(this@MangaDetailsActivity)) {
Snackbar.make( Snackbar.make(
pager, pager,
R.string.operation_not_supported, R.string.operation_not_supported,

@ -14,7 +14,7 @@ import org.koitharu.kotatsu.core.model.MangaHistory
import org.koitharu.kotatsu.domain.history.HistoryRepository import org.koitharu.kotatsu.domain.history.HistoryRepository
import org.koitharu.kotatsu.ui.common.BasePresenter import org.koitharu.kotatsu.ui.common.BasePresenter
import org.koitharu.kotatsu.ui.main.list.MangaListView import org.koitharu.kotatsu.ui.main.list.MangaListView
import org.koitharu.kotatsu.utils.ShortcutUtils import org.koitharu.kotatsu.utils.MangaShortcut
@InjectViewState @InjectViewState
class HistoryListPresenter : BasePresenter<MangaListView<MangaHistory>>() { class HistoryListPresenter : BasePresenter<MangaListView<MangaHistory>>() {
@ -62,7 +62,7 @@ class HistoryListPresenter : BasePresenter<MangaListView<MangaHistory>>() {
repository.clear() repository.clear()
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
ShortcutUtils.clearAppShortcuts(get()) MangaShortcut.clearAppShortcuts(get())
} }
viewState.onListChanged(emptyList()) viewState.onListChanged(emptyList())
} catch (_: CancellationException) { } catch (_: CancellationException) {
@ -84,7 +84,7 @@ class HistoryListPresenter : BasePresenter<MangaListView<MangaHistory>>() {
repository.delete(manga) repository.delete(manga)
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
ShortcutUtils.removeAppShortcut(get(), manga) MangaShortcut(manga).removeAppShortcut(get())
} }
viewState.onItemRemoved(manga) viewState.onItemRemoved(manga)
} catch (_: CancellationException) { } catch (_: CancellationException) {

@ -19,8 +19,8 @@ import org.koitharu.kotatsu.domain.MangaProviderFactory
import org.koitharu.kotatsu.domain.history.HistoryRepository import org.koitharu.kotatsu.domain.history.HistoryRepository
import org.koitharu.kotatsu.ui.common.BasePresenter import org.koitharu.kotatsu.ui.common.BasePresenter
import org.koitharu.kotatsu.ui.main.list.MangaListView 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.MediaStoreCompat
import org.koitharu.kotatsu.utils.ShortcutUtils
import org.koitharu.kotatsu.utils.ext.safe import org.koitharu.kotatsu.utils.ext.safe
import org.koitharu.kotatsu.utils.ext.sub import org.koitharu.kotatsu.utils.ext.sub
import java.io.File import java.io.File
@ -98,7 +98,7 @@ class LocalListPresenter : BasePresenter<MangaListView<File>>() {
} }
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
ShortcutUtils.removeAppShortcut(get(), manga) MangaShortcut(manga).removeAppShortcut(get())
} }
viewState.onItemRemoved(manga) viewState.onItemRemoved(manga)
} catch (e: CancellationException) { } catch (e: CancellationException) {

@ -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.thumbnails.PagesThumbnailsSheet
import org.koitharu.kotatsu.ui.reader.wetoon.WebtoonReaderFragment import org.koitharu.kotatsu.ui.reader.wetoon.WebtoonReaderFragment
import org.koitharu.kotatsu.utils.GridTouchHelper import org.koitharu.kotatsu.utils.GridTouchHelper
import org.koitharu.kotatsu.utils.MangaShortcut
import org.koitharu.kotatsu.utils.ShareHelper import org.koitharu.kotatsu.utils.ShareHelper
import org.koitharu.kotatsu.utils.ShortcutUtils
import org.koitharu.kotatsu.utils.anim.Motion import org.koitharu.kotatsu.utils.anim.Motion
import org.koitharu.kotatsu.utils.ext.* 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) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
GlobalScope.launch { GlobalScope.launch {
safe { safe {
ShortcutUtils.addAppShortcut(applicationContext, state.manga) MangaShortcut(state.manga).addAppShortcut(applicationContext)
} }
} }
} }

@ -22,25 +22,18 @@ import org.koitharu.kotatsu.domain.MangaDataRepository
import org.koitharu.kotatsu.ui.details.MangaDetailsActivity import org.koitharu.kotatsu.ui.details.MangaDetailsActivity
import org.koitharu.kotatsu.utils.ext.safe import org.koitharu.kotatsu.utils.ext.safe
object ShortcutUtils { class MangaShortcut(private val manga: Manga) {
suspend fun requestPinShortcut(context: Context, manga: Manga?): Boolean { private val shortcutId = manga.id.toString()
return manga != null && ShortcutManagerCompat.requestPinShortcut(
context,
buildShortcutInfo(context, manga).build(),
null
)
}
@RequiresApi(Build.VERSION_CODES.N_MR1) @RequiresApi(Build.VERSION_CODES.N_MR1)
suspend fun addAppShortcut(context: Context, manga: Manga) { suspend fun addAppShortcut(context: Context) {
val id = manga.id.toString()
val builder = buildShortcutInfo(context, manga)
val manager = context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager val manager = context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager
val limit = manager.maxShortcutCountPerActivity val limit = manager.maxShortcutCountPerActivity
val builder = buildShortcutInfo(context, manga)
val shortcuts = manager.dynamicShortcuts val shortcuts = manager.dynamicShortcuts
for (shortcut in shortcuts) { for (shortcut in shortcuts) {
if (shortcut.id == id) { if (shortcut.id == shortcutId) {
builder.setRank(shortcut.rank + 1) builder.setRank(shortcut.rank + 1)
manager.updateShortcuts(listOf(builder.build().toShortcutInfo())) manager.updateShortcuts(listOf(builder.build().toShortcutInfo()))
return return
@ -53,22 +46,23 @@ object ShortcutUtils {
manager.addDynamicShortcuts(listOf(builder.build().toShortcutInfo())) manager.addDynamicShortcuts(listOf(builder.build().toShortcutInfo()))
} }
@RequiresApi(Build.VERSION_CODES.N_MR1) suspend fun requestPinShortcut(context: Context): Boolean {
fun removeAppShortcut(context: Context, manga: Manga) { return ShortcutManagerCompat.requestPinShortcut(
val id = manga.id.toString() context,
val manager = context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager buildShortcutInfo(context, manga).build(),
manager.removeDynamicShortcuts(listOf(id)) null
)
} }
@RequiresApi(Build.VERSION_CODES.N_MR1) @RequiresApi(Build.VERSION_CODES.N_MR1)
fun clearAppShortcuts(context: Context) { fun removeAppShortcut(context: Context) {
val manager = context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager val manager = context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager
manager.removeAllDynamicShortcuts() manager.removeDynamicShortcuts(listOf(shortcutId))
} }
private suspend fun buildShortcutInfo( private suspend fun buildShortcutInfo(
context: Context, context: Context,
manga: Manga manga: Manga,
): ShortcutInfoCompat.Builder { ): ShortcutInfoCompat.Builder {
val icon = safe { val icon = safe {
val size = getIconSize(context) val size = getIconSize(context)
@ -77,12 +71,7 @@ object ShortcutUtils {
size(size) size(size)
scale(Scale.FILL) scale(Scale.FILL)
}.toBitmap() }.toBitmap()
ThumbnailUtils.extractThumbnail( ThumbnailUtils.extractThumbnail(bmp, size.width, size.height, 0)
bmp,
size.width,
size.height,
0
)
} }
} }
MangaDataRepository().storeManga(manga) 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()
}
}
} }
Loading…
Cancel
Save