Hide widgets content when app protected

This commit is contained in:
Koitharu
2024-05-11 17:53:45 +03:00
parent 0e10fdaf36
commit 18dd205051
10 changed files with 46 additions and 32 deletions

View File

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

View File

@@ -130,7 +130,7 @@ class ReaderActivity :
if (isResolved) {
viewModel.reload()
} else if (viewModel.content.value.pages.isEmpty()) {
finishAfterTransition()
dispatchNavigateUp()
}
},
),

View File

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

View File

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

View File

@@ -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<ImageLoader>,
private val settings: AppSettings,
) : RemoteViewsService.RemoteViewsFactory {
private val dataSet = ArrayList<Manga>()
@@ -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)

View File

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

View File

@@ -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<ImageLoader>
override fun onGetViewFactory(intent: Intent): RemoteViewsFactory {
return RecentListFactory(applicationContext, historyRepository, coil)
return RecentListFactory(applicationContext, historyRepository, coilLazy, settings)
}
}

View File

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

View File

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

View File

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