Hide widgets content when app protected

master
Koitharu 2 years ago
parent 0e10fdaf36
commit 18dd205051
Signed by: Koitharu
GPG Key ID: 676DEE768C17A9D7

@ -47,15 +47,6 @@ fun ImageResult.getDrawableOrThrow() = when (this) {
is ErrorResult -> throw throwable is ErrorResult -> throw throwable
} }
@Deprecated(
"",
ReplaceWith(
"getDrawableOrThrow().toBitmap()",
"androidx.core.graphics.drawable.toBitmap",
),
)
fun ImageResult.requireBitmap() = getDrawableOrThrow().toBitmap()
fun ImageResult.toBitmapOrNull() = when (this) { fun ImageResult.toBitmapOrNull() = when (this) {
is SuccessResult -> try { is SuccessResult -> try {
drawable.toBitmap() drawable.toBitmap()

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

@ -19,10 +19,10 @@ data class ReaderState(
) )
constructor(manga: Manga, branch: String?) : this( constructor(manga: Manga, branch: String?) : this(
chapterId = manga.chapters?.firstOrNull { chapterId = manga.chapters?.let {
it.branch == branch it.firstOrNull { x -> x.branch == branch } ?: it.firstOrNull()
}?.id ?: error("Cannot find first chapter"), }?.id ?: error("Cannot find first chapter"),
page = 0, page = 0,
scroll = 0, scroll = 0,
) )
} }

@ -32,6 +32,7 @@ import kotlinx.coroutines.plus
import org.koitharu.kotatsu.R import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.bookmarks.domain.Bookmark import org.koitharu.kotatsu.bookmarks.domain.Bookmark
import org.koitharu.kotatsu.bookmarks.domain.BookmarksRepository 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.os.AppShortcutManager
import org.koitharu.kotatsu.core.parser.MangaDataRepository import org.koitharu.kotatsu.core.parser.MangaDataRepository
import org.koitharu.kotatsu.core.parser.MangaIntent import org.koitharu.kotatsu.core.parser.MangaIntent
@ -386,7 +387,7 @@ class ReaderViewModel @Inject constructor(
if (currentState.value == null) { if (currentState.value == null) {
currentState.value = historyRepository.getOne(manga)?.let { currentState.value = historyRepository.getOne(manga)?.let {
ReaderState(it) ReaderState(it)
} ?: ReaderState(manga, preselectedBranch) } ?: ReaderState(manga, preselectedBranch ?: manga.getPreferredBranch(null))
} }
val mode = detectReaderModeUseCase.invoke(manga, currentState.value) val mode = detectReaderModeUseCase.invoke(manga, currentState.value)
val branch = chaptersLoader.peekChapter(currentState.value?.chapterId ?: 0L)?.branch val branch = chaptersLoader.peekChapter(currentState.value?.chapterId ?: 0L)?.branch

@ -10,9 +10,11 @@ import coil.executeBlocking
import coil.request.ImageRequest import coil.request.ImageRequest
import coil.size.Size import coil.size.Size
import coil.transform.RoundedCornersTransformation import coil.transform.RoundedCornersTransformation
import dagger.Lazy
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import org.koitharu.kotatsu.R import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.parser.MangaIntent 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.core.util.ext.getDrawableOrThrow
import org.koitharu.kotatsu.history.data.HistoryRepository import org.koitharu.kotatsu.history.data.HistoryRepository
import org.koitharu.kotatsu.parsers.model.Manga import org.koitharu.kotatsu.parsers.model.Manga
@ -21,7 +23,8 @@ import org.koitharu.kotatsu.parsers.util.replaceWith
class RecentListFactory( class RecentListFactory(
private val context: Context, private val context: Context,
private val historyRepository: HistoryRepository, private val historyRepository: HistoryRepository,
private val coil: ImageLoader, private val coilLazy: Lazy<ImageLoader>,
private val settings: AppSettings,
) : RemoteViewsService.RemoteViewsFactory { ) : RemoteViewsService.RemoteViewsFactory {
private val dataSet = ArrayList<Manga>() private val dataSet = ArrayList<Manga>()
@ -40,7 +43,11 @@ class RecentListFactory(
override fun getItemId(position: Int) = dataSet.getOrNull(position)?.id ?: 0L override fun getItemId(position: Int) = dataSet.getOrNull(position)?.id ?: 0L
override fun onDataSetChanged() { 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) dataSet.replaceWith(data)
} }
@ -50,7 +57,7 @@ class RecentListFactory(
val views = RemoteViews(context.packageName, R.layout.item_recent) val views = RemoteViews(context.packageName, R.layout.item_recent)
val item = dataSet.getOrNull(position) ?: return views val item = dataSet.getOrNull(position) ?: return views
runCatching { runCatching {
coil.executeBlocking( coilLazy.get().executeBlocking(
ImageRequest.Builder(context) ImageRequest.Builder(context)
.data(item.coverUrl) .data(item.coverUrl)
.size(coverSize) .size(coverSize)

@ -31,7 +31,6 @@ class RecentWidgetProvider : BaseAppWidgetProvider() {
} else { } else {
views.setInt(R.id.widget_root, "setBackgroundResource", R.drawable.bg_appwidget_root) views.setInt(R.id.widget_root, "setBackgroundResource", R.drawable.bg_appwidget_root)
} }
// TODO security
val adapter = Intent(context, RecentWidgetService::class.java) val adapter = Intent(context, RecentWidgetService::class.java)
adapter.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, config.widgetId) adapter.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, config.widgetId)
adapter.data = Uri.parse(adapter.toUri(Intent.URI_INTENT_SCHEME)) adapter.data = Uri.parse(adapter.toUri(Intent.URI_INTENT_SCHEME))

@ -3,7 +3,9 @@ package org.koitharu.kotatsu.widget.recent
import android.content.Intent import android.content.Intent
import android.widget.RemoteViewsService import android.widget.RemoteViewsService
import coil.ImageLoader import coil.ImageLoader
import dagger.Lazy
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import org.koitharu.kotatsu.core.prefs.AppSettings
import org.koitharu.kotatsu.history.data.HistoryRepository import org.koitharu.kotatsu.history.data.HistoryRepository
import javax.inject.Inject import javax.inject.Inject
@ -14,9 +16,12 @@ class RecentWidgetService : RemoteViewsService() {
lateinit var historyRepository: HistoryRepository lateinit var historyRepository: HistoryRepository
@Inject @Inject
lateinit var coil: ImageLoader lateinit var settings: AppSettings
@Inject
lateinit var coilLazy: Lazy<ImageLoader>
override fun onGetViewFactory(intent: Intent): RemoteViewsFactory { override fun onGetViewFactory(intent: Intent): RemoteViewsFactory {
return RecentListFactory(applicationContext, historyRepository, coil) return RecentListFactory(applicationContext, historyRepository, coilLazy, settings)
} }
} }

@ -10,9 +10,11 @@ import coil.executeBlocking
import coil.request.ImageRequest import coil.request.ImageRequest
import coil.size.Size import coil.size.Size
import coil.transform.RoundedCornersTransformation import coil.transform.RoundedCornersTransformation
import dagger.Lazy
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import org.koitharu.kotatsu.R import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.parser.MangaIntent 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.prefs.AppWidgetConfig
import org.koitharu.kotatsu.core.ui.image.TrimTransformation import org.koitharu.kotatsu.core.ui.image.TrimTransformation
import org.koitharu.kotatsu.core.util.ext.getDrawableOrThrow import org.koitharu.kotatsu.core.util.ext.getDrawableOrThrow
@ -23,7 +25,8 @@ import org.koitharu.kotatsu.parsers.util.replaceWith
class ShelfListFactory( class ShelfListFactory(
private val context: Context, private val context: Context,
private val favouritesRepository: FavouritesRepository, private val favouritesRepository: FavouritesRepository,
private val coil: ImageLoader, private val coilLazy: Lazy<ImageLoader>,
private val settings: AppSettings,
widgetId: Int, widgetId: Int,
) : RemoteViewsService.RemoteViewsFactory { ) : RemoteViewsService.RemoteViewsFactory {
@ -44,13 +47,17 @@ class ShelfListFactory(
override fun getItemId(position: Int) = dataSet.getOrNull(position)?.id ?: 0L override fun getItemId(position: Int) = dataSet.getOrNull(position)?.id ?: 0L
override fun onDataSetChanged() { override fun onDataSetChanged() {
val data = runBlocking { val data = if (settings.appPassword.isNullOrEmpty()) {
val category = config.categoryId runBlocking {
if (category == 0L) { val category = config.categoryId
favouritesRepository.getAllManga() if (category == 0L) {
} else { favouritesRepository.getAllManga()
favouritesRepository.getManga(category) } else {
favouritesRepository.getManga(category)
}
} }
} else {
emptyList()
} }
dataSet.replaceWith(data) dataSet.replaceWith(data)
} }
@ -62,7 +69,7 @@ class ShelfListFactory(
val item = dataSet.getOrNull(position) ?: return views val item = dataSet.getOrNull(position) ?: return views
views.setTextViewText(R.id.textView_title, item.title) views.setTextViewText(R.id.textView_title, item.title)
runCatching { runCatching {
coil.executeBlocking( coilLazy.get().executeBlocking(
ImageRequest.Builder(context) ImageRequest.Builder(context)
.data(item.coverUrl) .data(item.coverUrl)
.size(coverSize) .size(coverSize)

@ -31,7 +31,6 @@ class ShelfWidgetProvider : BaseAppWidgetProvider() {
} else { } else {
views.setInt(R.id.widget_root, "setBackgroundResource", R.drawable.bg_appwidget_root) views.setInt(R.id.widget_root, "setBackgroundResource", R.drawable.bg_appwidget_root)
} }
// TODO security
val adapter = Intent(context, ShelfWidgetService::class.java) val adapter = Intent(context, ShelfWidgetService::class.java)
adapter.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, config.widgetId) adapter.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, config.widgetId)
adapter.data = Uri.parse(adapter.toUri(Intent.URI_INTENT_SCHEME)) adapter.data = Uri.parse(adapter.toUri(Intent.URI_INTENT_SCHEME))

@ -4,9 +4,11 @@ import android.appwidget.AppWidgetManager
import android.content.Intent import android.content.Intent
import android.widget.RemoteViewsService import android.widget.RemoteViewsService
import coil.ImageLoader import coil.ImageLoader
import dagger.Lazy
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject import org.koitharu.kotatsu.core.prefs.AppSettings
import org.koitharu.kotatsu.favourites.domain.FavouritesRepository import org.koitharu.kotatsu.favourites.domain.FavouritesRepository
import javax.inject.Inject
@AndroidEntryPoint @AndroidEntryPoint
class ShelfWidgetService : RemoteViewsService() { class ShelfWidgetService : RemoteViewsService() {
@ -15,13 +17,16 @@ class ShelfWidgetService : RemoteViewsService() {
lateinit var favouritesRepository: FavouritesRepository lateinit var favouritesRepository: FavouritesRepository
@Inject @Inject
lateinit var coil: ImageLoader lateinit var settings: AppSettings
@Inject
lateinit var coilLazy: Lazy<ImageLoader>
override fun onGetViewFactory(intent: Intent): RemoteViewsFactory { override fun onGetViewFactory(intent: Intent): RemoteViewsFactory {
val widgetId = intent.getIntExtra( val widgetId = intent.getIntExtra(
AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID,
) )
return ShelfListFactory(applicationContext, favouritesRepository, coil, widgetId) return ShelfListFactory(applicationContext, favouritesRepository, coilLazy, settings, widgetId)
} }
} }

Loading…
Cancel
Save