From 18dd20505183ba43d5ad2ea390a6680e0a14078f Mon Sep 17 00:00:00 2001 From: Koitharu Date: Sat, 11 May 2024 17:53:45 +0300 Subject: [PATCH] Hide widgets content when app protected --- .../koitharu/kotatsu/core/util/ext/Coil.kt | 9 -------- .../kotatsu/reader/ui/ReaderActivity.kt | 2 +- .../koitharu/kotatsu/reader/ui/ReaderState.kt | 6 ++--- .../kotatsu/reader/ui/ReaderViewModel.kt | 3 ++- .../widget/recent/RecentListFactory.kt | 13 ++++++++--- .../widget/recent/RecentWidgetProvider.kt | 1 - .../widget/recent/RecentWidgetService.kt | 9 ++++++-- .../kotatsu/widget/shelf/ShelfListFactory.kt | 23 ++++++++++++------- .../widget/shelf/ShelfWidgetProvider.kt | 1 - .../widget/shelf/ShelfWidgetService.kt | 11 ++++++--- 10 files changed, 46 insertions(+), 32 deletions(-) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Coil.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Coil.kt index 845f0ea50..1ddba8d32 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Coil.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Coil.kt @@ -47,15 +47,6 @@ fun ImageResult.getDrawableOrThrow() = when (this) { is ErrorResult -> throw throwable } -@Deprecated( - "", - ReplaceWith( - "getDrawableOrThrow().toBitmap()", - "androidx.core.graphics.drawable.toBitmap", - ), -) -fun ImageResult.requireBitmap() = getDrawableOrThrow().toBitmap() - fun ImageResult.toBitmapOrNull() = when (this) { is SuccessResult -> try { drawable.toBitmap() diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ReaderActivity.kt b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ReaderActivity.kt index 92b7b98bb..728f422da 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ReaderActivity.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ReaderActivity.kt @@ -130,7 +130,7 @@ class ReaderActivity : if (isResolved) { viewModel.reload() } else if (viewModel.content.value.pages.isEmpty()) { - finishAfterTransition() + dispatchNavigateUp() } }, ), diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ReaderState.kt b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ReaderState.kt index 2eb7cc960..8ba525228 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ReaderState.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ReaderState.kt @@ -19,10 +19,10 @@ data class ReaderState( ) constructor(manga: Manga, branch: String?) : this( - chapterId = manga.chapters?.firstOrNull { - it.branch == branch + chapterId = manga.chapters?.let { + it.firstOrNull { x -> x.branch == branch } ?: it.firstOrNull() }?.id ?: error("Cannot find first chapter"), page = 0, scroll = 0, ) -} \ No newline at end of file +} diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ReaderViewModel.kt b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ReaderViewModel.kt index b4dd6d818..a608f1132 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ReaderViewModel.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ReaderViewModel.kt @@ -32,6 +32,7 @@ import kotlinx.coroutines.plus import org.koitharu.kotatsu.R import org.koitharu.kotatsu.bookmarks.domain.Bookmark import org.koitharu.kotatsu.bookmarks.domain.BookmarksRepository +import org.koitharu.kotatsu.core.model.getPreferredBranch import org.koitharu.kotatsu.core.os.AppShortcutManager import org.koitharu.kotatsu.core.parser.MangaDataRepository import org.koitharu.kotatsu.core.parser.MangaIntent @@ -386,7 +387,7 @@ class ReaderViewModel @Inject constructor( if (currentState.value == null) { currentState.value = historyRepository.getOne(manga)?.let { ReaderState(it) - } ?: ReaderState(manga, preselectedBranch) + } ?: ReaderState(manga, preselectedBranch ?: manga.getPreferredBranch(null)) } val mode = detectReaderModeUseCase.invoke(manga, currentState.value) val branch = chaptersLoader.peekChapter(currentState.value?.chapterId ?: 0L)?.branch diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/widget/recent/RecentListFactory.kt b/app/src/main/kotlin/org/koitharu/kotatsu/widget/recent/RecentListFactory.kt index 9dd997814..30d28b251 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/widget/recent/RecentListFactory.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/widget/recent/RecentListFactory.kt @@ -10,9 +10,11 @@ import coil.executeBlocking import coil.request.ImageRequest import coil.size.Size import coil.transform.RoundedCornersTransformation +import dagger.Lazy import kotlinx.coroutines.runBlocking import org.koitharu.kotatsu.R import org.koitharu.kotatsu.core.parser.MangaIntent +import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.util.ext.getDrawableOrThrow import org.koitharu.kotatsu.history.data.HistoryRepository import org.koitharu.kotatsu.parsers.model.Manga @@ -21,7 +23,8 @@ import org.koitharu.kotatsu.parsers.util.replaceWith class RecentListFactory( private val context: Context, private val historyRepository: HistoryRepository, - private val coil: ImageLoader, + private val coilLazy: Lazy, + private val settings: AppSettings, ) : RemoteViewsService.RemoteViewsFactory { private val dataSet = ArrayList() @@ -40,7 +43,11 @@ class RecentListFactory( override fun getItemId(position: Int) = dataSet.getOrNull(position)?.id ?: 0L override fun onDataSetChanged() { - val data = runBlocking { historyRepository.getList(0, 10) } + val data = if (settings.appPassword.isNullOrEmpty()) { + runBlocking { historyRepository.getList(0, 10) } + } else { + emptyList() + } dataSet.replaceWith(data) } @@ -50,7 +57,7 @@ class RecentListFactory( val views = RemoteViews(context.packageName, R.layout.item_recent) val item = dataSet.getOrNull(position) ?: return views runCatching { - coil.executeBlocking( + coilLazy.get().executeBlocking( ImageRequest.Builder(context) .data(item.coverUrl) .size(coverSize) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/widget/recent/RecentWidgetProvider.kt b/app/src/main/kotlin/org/koitharu/kotatsu/widget/recent/RecentWidgetProvider.kt index 706b17f9a..c94e449b9 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/widget/recent/RecentWidgetProvider.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/widget/recent/RecentWidgetProvider.kt @@ -31,7 +31,6 @@ class RecentWidgetProvider : BaseAppWidgetProvider() { } else { views.setInt(R.id.widget_root, "setBackgroundResource", R.drawable.bg_appwidget_root) } - // TODO security val adapter = Intent(context, RecentWidgetService::class.java) adapter.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, config.widgetId) adapter.data = Uri.parse(adapter.toUri(Intent.URI_INTENT_SCHEME)) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/widget/recent/RecentWidgetService.kt b/app/src/main/kotlin/org/koitharu/kotatsu/widget/recent/RecentWidgetService.kt index a5052477a..3009db546 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/widget/recent/RecentWidgetService.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/widget/recent/RecentWidgetService.kt @@ -3,7 +3,9 @@ package org.koitharu.kotatsu.widget.recent import android.content.Intent import android.widget.RemoteViewsService import coil.ImageLoader +import dagger.Lazy import dagger.hilt.android.AndroidEntryPoint +import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.history.data.HistoryRepository import javax.inject.Inject @@ -14,9 +16,12 @@ class RecentWidgetService : RemoteViewsService() { lateinit var historyRepository: HistoryRepository @Inject - lateinit var coil: ImageLoader + lateinit var settings: AppSettings + + @Inject + lateinit var coilLazy: Lazy override fun onGetViewFactory(intent: Intent): RemoteViewsFactory { - return RecentListFactory(applicationContext, historyRepository, coil) + return RecentListFactory(applicationContext, historyRepository, coilLazy, settings) } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/widget/shelf/ShelfListFactory.kt b/app/src/main/kotlin/org/koitharu/kotatsu/widget/shelf/ShelfListFactory.kt index 4f66f82b1..3c74a4428 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/widget/shelf/ShelfListFactory.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/widget/shelf/ShelfListFactory.kt @@ -10,9 +10,11 @@ import coil.executeBlocking import coil.request.ImageRequest import coil.size.Size import coil.transform.RoundedCornersTransformation +import dagger.Lazy import kotlinx.coroutines.runBlocking import org.koitharu.kotatsu.R import org.koitharu.kotatsu.core.parser.MangaIntent +import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.prefs.AppWidgetConfig import org.koitharu.kotatsu.core.ui.image.TrimTransformation import org.koitharu.kotatsu.core.util.ext.getDrawableOrThrow @@ -23,7 +25,8 @@ import org.koitharu.kotatsu.parsers.util.replaceWith class ShelfListFactory( private val context: Context, private val favouritesRepository: FavouritesRepository, - private val coil: ImageLoader, + private val coilLazy: Lazy, + private val settings: AppSettings, widgetId: Int, ) : RemoteViewsService.RemoteViewsFactory { @@ -44,13 +47,17 @@ class ShelfListFactory( override fun getItemId(position: Int) = dataSet.getOrNull(position)?.id ?: 0L override fun onDataSetChanged() { - val data = runBlocking { - val category = config.categoryId - if (category == 0L) { - favouritesRepository.getAllManga() - } else { - favouritesRepository.getManga(category) + val data = if (settings.appPassword.isNullOrEmpty()) { + runBlocking { + val category = config.categoryId + if (category == 0L) { + favouritesRepository.getAllManga() + } else { + favouritesRepository.getManga(category) + } } + } else { + emptyList() } dataSet.replaceWith(data) } @@ -62,7 +69,7 @@ class ShelfListFactory( val item = dataSet.getOrNull(position) ?: return views views.setTextViewText(R.id.textView_title, item.title) runCatching { - coil.executeBlocking( + coilLazy.get().executeBlocking( ImageRequest.Builder(context) .data(item.coverUrl) .size(coverSize) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/widget/shelf/ShelfWidgetProvider.kt b/app/src/main/kotlin/org/koitharu/kotatsu/widget/shelf/ShelfWidgetProvider.kt index 70d309018..5246ecd3f 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/widget/shelf/ShelfWidgetProvider.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/widget/shelf/ShelfWidgetProvider.kt @@ -31,7 +31,6 @@ class ShelfWidgetProvider : BaseAppWidgetProvider() { } else { views.setInt(R.id.widget_root, "setBackgroundResource", R.drawable.bg_appwidget_root) } - // TODO security val adapter = Intent(context, ShelfWidgetService::class.java) adapter.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, config.widgetId) adapter.data = Uri.parse(adapter.toUri(Intent.URI_INTENT_SCHEME)) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/widget/shelf/ShelfWidgetService.kt b/app/src/main/kotlin/org/koitharu/kotatsu/widget/shelf/ShelfWidgetService.kt index 40c0d86bb..b8fdffea3 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/widget/shelf/ShelfWidgetService.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/widget/shelf/ShelfWidgetService.kt @@ -4,9 +4,11 @@ import android.appwidget.AppWidgetManager import android.content.Intent import android.widget.RemoteViewsService import coil.ImageLoader +import dagger.Lazy import dagger.hilt.android.AndroidEntryPoint -import javax.inject.Inject +import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.favourites.domain.FavouritesRepository +import javax.inject.Inject @AndroidEntryPoint class ShelfWidgetService : RemoteViewsService() { @@ -15,13 +17,16 @@ class ShelfWidgetService : RemoteViewsService() { lateinit var favouritesRepository: FavouritesRepository @Inject - lateinit var coil: ImageLoader + lateinit var settings: AppSettings + + @Inject + lateinit var coilLazy: Lazy override fun onGetViewFactory(intent: Intent): RemoteViewsFactory { val widgetId = intent.getIntExtra( AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID, ) - return ShelfListFactory(applicationContext, favouritesRepository, coil, widgetId) + return ShelfListFactory(applicationContext, favouritesRepository, coilLazy, settings, widgetId) } }