Minor cleanups

Pulling out some of the smaller changes that aren't related to the manga controller changes in #7244
This commit is contained in:
arkon 2022-06-04 16:01:49 -04:00
parent 5ea03fad87
commit 7fdbf40cd2
25 changed files with 94 additions and 79 deletions

View File

@ -0,0 +1,3 @@
package eu.kanade.data.chapter
class NoChaptersException : Exception()

View File

@ -24,7 +24,7 @@ import androidx.compose.ui.unit.dp
import androidx.core.graphics.drawable.toBitmap
import coil.compose.AsyncImage
import eu.kanade.domain.source.model.Source
import eu.kanade.presentation.util.bitmapPainterResource
import eu.kanade.presentation.util.rememberResourceBitmapPainter
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.extension.model.Extension
import eu.kanade.tachiyomi.util.lang.withIOContext
@ -67,7 +67,7 @@ fun ExtensionIcon(
model = extension.iconUrl,
contentDescription = "",
placeholder = ColorPainter(Color(0x1F888888)),
error = bitmapPainterResource(id = R.drawable.cover_error),
error = rememberResourceBitmapPainter(id = R.drawable.cover_error),
modifier = modifier
.clip(RoundedCornerShape(4.dp))
.then(defaultModifier),

View File

@ -11,7 +11,7 @@ import androidx.compose.ui.graphics.painter.ColorPainter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import eu.kanade.presentation.util.bitmapPainterResource
import eu.kanade.presentation.util.rememberResourceBitmapPainter
import eu.kanade.tachiyomi.R
enum class MangaCover(private val ratio: Float) {
@ -28,7 +28,7 @@ enum class MangaCover(private val ratio: Float) {
AsyncImage(
model = data,
placeholder = ColorPainter(CoverPlaceholderColor),
error = bitmapPainterResource(id = R.drawable.cover_error),
error = rememberResourceBitmapPainter(id = R.drawable.cover_error),
contentDescription = contentDescription,
modifier = modifier
.aspectRatio(ratio)

View File

@ -3,15 +3,21 @@ package eu.kanade.presentation.components
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.google.accompanist.swiperefresh.SwipeRefreshState
import com.google.accompanist.swiperefresh.SwipeRefreshIndicator as AccompanistSwipeRefreshIndicator
@Composable
fun SwipeRefreshIndicator(state: SwipeRefreshState, refreshTrigger: Dp) {
fun SwipeRefreshIndicator(
state: SwipeRefreshState,
refreshTriggerDistance: Dp,
refreshingOffset: Dp = 16.dp,
) {
AccompanistSwipeRefreshIndicator(
state = state,
refreshTriggerDistance = refreshTrigger,
refreshTriggerDistance = refreshTriggerDistance,
backgroundColor = MaterialTheme.colorScheme.primary,
contentColor = MaterialTheme.colorScheme.onPrimary,
refreshingOffset = refreshingOffset,
)
}

View File

@ -4,9 +4,11 @@ import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.calculateEndPadding
import androidx.compose.foundation.layout.calculateStartPadding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.ui.platform.LocalLayoutDirection
@Composable
@ReadOnlyComposable
operator fun PaddingValues.plus(other: PaddingValues): PaddingValues {
val layoutDirection = LocalLayoutDirection.current
return PaddingValues(

View File

@ -4,6 +4,8 @@ import android.content.res.Resources
import androidx.annotation.DrawableRes
import androidx.annotation.PluralsRes
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.painter.BitmapPainter
import androidx.compose.ui.platform.LocalContext
@ -18,6 +20,7 @@ import androidx.core.graphics.drawable.toBitmap
* @return the string data associated with the resource
*/
@Composable
@ReadOnlyComposable
fun quantityStringResource(@PluralsRes id: Int, quantity: Int): String {
val context = LocalContext.current
return context.resources.getQuantityString(id, quantity, quantity)
@ -32,6 +35,7 @@ fun quantityStringResource(@PluralsRes id: Int, quantity: Int): String {
* @return the string data associated with the resource
*/
@Composable
@ReadOnlyComposable
fun quantityStringResource(@PluralsRes id: Int, quantity: Int, vararg formatArgs: Any): String {
val context = LocalContext.current
return context.resources.getQuantityString(id, quantity, *formatArgs)
@ -46,9 +50,11 @@ fun quantityStringResource(@PluralsRes id: Int, quantity: Int, vararg formatArgs
* @return the bitmap associated with the resource
*/
@Composable
fun bitmapPainterResource(@DrawableRes id: Int): BitmapPainter {
fun rememberResourceBitmapPainter(@DrawableRes id: Int): BitmapPainter {
val context = LocalContext.current
return remember(id) {
val drawable = ContextCompat.getDrawable(context, id)
?: throw Resources.NotFoundException()
return BitmapPainter(drawable.toBitmap().asImageBitmap())
BitmapPainter(drawable.toBitmap().asImageBitmap())
}
}

View File

@ -2,6 +2,7 @@ package eu.kanade.tachiyomi.data.backup
import android.content.Context
import android.net.Uri
import eu.kanade.data.chapter.NoChaptersException
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Chapter
@ -9,7 +10,6 @@ import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.database.models.Track
import eu.kanade.tachiyomi.data.track.TrackManager
import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.util.chapter.NoChaptersException
import eu.kanade.tachiyomi.util.system.createFileInCacheDir
import kotlinx.coroutines.Job
import uy.kohesive.injekt.injectLazy

View File

@ -175,7 +175,7 @@ class FullBackupManager(context: Context) : AbstractBackupManager(context) {
// Check if user wants track information in backup
if (options and BACKUP_TRACK_MASK == BACKUP_TRACK) {
val tracks = db.getTracks(manga).executeAsBlocking()
val tracks = db.getTracks(manga.id).executeAsBlocking()
if (tracks.isNotEmpty()) {
mangaObject.tracking = tracks.map { BackupTracking.copyFrom(it) }
}
@ -318,7 +318,7 @@ class FullBackupManager(context: Context) : AbstractBackupManager(context) {
tracks.map { it.manga_id = manga.id!! }
// Get tracks from database
val dbTracks = db.getTracks(manga).executeAsBlocking()
val dbTracks = db.getTracks(manga.id).executeAsBlocking()
val trackToUpdate = mutableListOf<Track>()
tracks.forEach { track ->

View File

@ -34,11 +34,11 @@ class CoverCache(private val context: Context) {
/**
* Returns the cover from cache.
*
* @param manga the manga.
* @param mangaThumbnailUrl thumbnail url for the manga.
* @return cover image.
*/
fun getCoverFile(manga: Manga): File? {
return manga.thumbnail_url?.let {
fun getCoverFile(mangaThumbnailUrl: String?): File? {
return mangaThumbnailUrl?.let {
File(cacheDir, DiskUtil.hashKeyForDisk(it))
}
}
@ -46,11 +46,11 @@ class CoverCache(private val context: Context) {
/**
* Returns the custom cover from cache.
*
* @param manga the manga.
* @param mangaId the manga id.
* @return cover image.
*/
fun getCustomCoverFile(manga: Manga): File {
return File(customCoverCacheDir, DiskUtil.hashKeyForDisk(manga.id.toString()))
fun getCustomCoverFile(mangaId: Long?): File {
return File(customCoverCacheDir, DiskUtil.hashKeyForDisk(mangaId.toString()))
}
/**
@ -62,7 +62,7 @@ class CoverCache(private val context: Context) {
*/
@Throws(IOException::class)
fun setCustomCoverToCache(manga: Manga, inputStream: InputStream) {
getCustomCoverFile(manga).outputStream().use {
getCustomCoverFile(manga.id).outputStream().use {
inputStream.copyTo(it)
}
}
@ -77,12 +77,12 @@ class CoverCache(private val context: Context) {
fun deleteFromCache(manga: Manga, deleteCustomCover: Boolean = false): Int {
var deleted = 0
getCoverFile(manga)?.let {
getCoverFile(manga.thumbnail_url)?.let {
if (it.exists() && it.delete()) ++deleted
}
if (deleteCustomCover) {
if (deleteCustomCover(manga)) ++deleted
if (deleteCustomCover(manga.id)) ++deleted
}
return deleted
@ -91,11 +91,11 @@ class CoverCache(private val context: Context) {
/**
* Delete custom cover of the manga from the cache
*
* @param manga the manga.
* @param mangaId the manga id.
* @return whether the cover was deleted.
*/
fun deleteCustomCover(manga: Manga): Boolean {
return getCustomCoverFile(manga).let {
fun deleteCustomCover(mangaId: Long?): Boolean {
return getCustomCoverFile(mangaId).let {
it.exists() && it.delete()
}
}

View File

@ -57,7 +57,7 @@ class MangaCoverFetcher(
override suspend fun fetch(): FetchResult {
// Use custom cover if exists
val useCustomCover = options.parameters.value(USE_CUSTOM_COVER) ?: true
val customCoverFile = coverCache.getCustomCoverFile(manga)
val customCoverFile = coverCache.getCustomCoverFile(manga.id)
if (useCustomCover && customCoverFile.exists()) {
return fileLoader(customCoverFile)
}
@ -82,7 +82,7 @@ class MangaCoverFetcher(
private suspend fun httpLoader(): FetchResult {
// Only cache separately if it's a library item
val libraryCoverCacheFile = if (manga.favorite) {
coverCache.getCoverFile(manga) ?: error("No cover specified")
coverCache.getCoverFile(manga.thumbnail_url) ?: error("No cover specified")
} else {
null
}

View File

@ -19,13 +19,13 @@ interface TrackQueries : DbProvider {
)
.prepare()
fun getTracks(manga: Manga) = db.get()
fun getTracks(mangaId: Long?) = db.get()
.listOfObjects(Track::class.java)
.withQuery(
Query.builder()
.table(TrackTable.TABLE)
.where("${TrackTable.COL_MANGA_ID} = ?")
.whereArgs(manga.id)
.whereArgs(mangaId)
.build(),
)
.prepare()

View File

@ -102,8 +102,9 @@ class DownloadManager(
downloader.clearQueue(isNotification)
}
fun startDownloadNow(chapter: Chapter) {
val download = downloader.queue.find { it.chapter.id == chapter.id } ?: return
fun startDownloadNow(chapterId: Long?) {
if (chapterId == null) return
val download = downloader.queue.find { it.chapter.id == chapterId } ?: return
val queue = downloader.queue.toMutableList()
queue.remove(download)
queue.add(0, download)

View File

@ -6,9 +6,12 @@ import eu.kanade.tachiyomi.source.model.Page
import eu.kanade.tachiyomi.source.online.HttpSource
import rx.subjects.PublishSubject
class Download(val source: HttpSource, val manga: Manga, val chapter: Chapter) {
var pages: List<Page>? = null
data class Download(
val source: HttpSource,
val manga: Manga,
val chapter: Chapter,
var pages: List<Page>? = null,
) {
@Volatile
@Transient

View File

@ -6,6 +6,7 @@ import android.content.Intent
import android.os.IBinder
import android.os.PowerManager
import androidx.core.content.ContextCompat
import eu.kanade.data.chapter.NoChaptersException
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.cache.CoverCache
import eu.kanade.tachiyomi.data.database.DatabaseHelper
@ -31,7 +32,6 @@ import eu.kanade.tachiyomi.source.model.SManga
import eu.kanade.tachiyomi.source.model.toMangaInfo
import eu.kanade.tachiyomi.source.model.toSChapter
import eu.kanade.tachiyomi.source.model.toSManga
import eu.kanade.tachiyomi.util.chapter.NoChaptersException
import eu.kanade.tachiyomi.util.chapter.syncChaptersWithSource
import eu.kanade.tachiyomi.util.chapter.syncChaptersWithTrackServiceTwoWay
import eu.kanade.tachiyomi.util.lang.withIOContext
@ -500,7 +500,7 @@ class LibraryUpdateService(
}
private suspend fun updateTrackings(manga: LibraryManga, loggedServices: List<TrackService>) {
db.getTracks(manga).executeAsBlocking()
db.getTracks(manga.id).executeAsBlocking()
.map { track ->
supervisorScope {
async {

View File

@ -30,7 +30,7 @@ class DelayedTrackingUpdateJob(context: Context, workerParams: WorkerParameters)
withContext(Dispatchers.IO) {
val tracks = delayedTrackingStore.getItems().mapNotNull {
val manga = db.getManga(it.mangaId).executeAsBlocking() ?: return@withContext
db.getTracks(manga).executeAsBlocking()
db.getTracks(manga.id).executeAsBlocking()
.find { track -> track.id == it.trackId }
?.also { track ->
track.last_chapter_read = it.lastChapterRead

View File

@ -16,10 +16,6 @@ object MigrationFlags {
private const val TRACK = 0b0100
private const val CUSTOM_COVER = 0b1000
private const val CHAPTERS2 = 0x1
private const val CATEGORIES2 = 0x2
private const val TRACK2 = 0x4
private val coverCache: CoverCache by injectLazy()
private val db: DatabaseHelper = Injekt.get()
@ -53,7 +49,7 @@ object MigrationFlags {
val titles = arrayOf(R.string.chapters, R.string.categories).toMutableList()
if (manga != null) {
db.inTransaction {
if (db.getTracks(manga).executeAsBlocking().isNotEmpty()) {
if (db.getTracks(manga.id).executeAsBlocking().isNotEmpty()) {
titles.add(R.string.track)
}

View File

@ -150,7 +150,7 @@ class SearchPresenter(
// Update track
if (migrateTracks) {
val tracksToUpdate = db.getTracks(prevManga).executeAsBlocking().mapNotNull { track ->
val tracksToUpdate = db.getTracks(prevManga.id).executeAsBlocking().mapNotNull { track ->
track.id = null
track.manga_id = manga.id!!
@ -183,7 +183,7 @@ class SearchPresenter(
// Update custom cover
if (migrateCustomCover) {
coverCache.setCustomCoverToCache(manga, coverCache.getCustomCoverFile(prevManga).inputStream())
coverCache.setCustomCoverToCache(manga, coverCache.getCustomCoverFile(prevManga.id).inputStream())
}
// SearchPresenter#networkToLocalManga may have updated the manga title,

View File

@ -467,7 +467,7 @@ class LibraryController(
override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
when (item.itemId) {
R.id.action_move_to_category -> showChangeMangaCategoriesDialog()
R.id.action_move_to_category -> showMangaCategoriesDialog()
R.id.action_download_unread -> downloadUnreadChapters()
R.id.action_mark_as_read -> markReadStatus(true)
R.id.action_mark_as_unread -> markReadStatus(false)
@ -540,7 +540,7 @@ class LibraryController(
/**
* Move the selected manga to a list of categories.
*/
private fun showChangeMangaCategoriesDialog() {
private fun showMangaCategoriesDialog() {
// Create a copy of selected manga
val mangas = selectedMangas.toList()

View File

@ -34,6 +34,7 @@ import com.google.android.material.snackbar.Snackbar
import dev.chrisbanes.insetter.applyInsetter
import eu.davidea.flexibleadapter.FlexibleAdapter
import eu.davidea.flexibleadapter.SelectableAdapter
import eu.kanade.data.chapter.NoChaptersException
import eu.kanade.domain.history.model.HistoryWithRelations
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.cache.CoverCache
@ -84,7 +85,6 @@ import eu.kanade.tachiyomi.ui.reader.ReaderActivity
import eu.kanade.tachiyomi.ui.recent.history.HistoryController
import eu.kanade.tachiyomi.ui.recent.updates.UpdatesController
import eu.kanade.tachiyomi.ui.webview.WebViewActivity
import eu.kanade.tachiyomi.util.chapter.NoChaptersException
import eu.kanade.tachiyomi.util.hasCustomCover
import eu.kanade.tachiyomi.util.lang.launchIO
import eu.kanade.tachiyomi.util.lang.launchUI
@ -149,7 +149,6 @@ class MangaController :
private val preferences: PreferencesHelper by injectLazy()
private val coverCache: CoverCache by injectLazy()
private val sourceManager: SourceManager by injectLazy()
private var mangaInfoAdapter: MangaInfoHeaderAdapter? = null
private var chaptersHeaderAdapter: MangaChaptersHeaderAdapter? = null

View File

@ -185,7 +185,7 @@ class MangaPresenter(
return Observable.just(0)
}
return db.getTracks(manga).asRxObservable()
return db.getTracks(manga.id).asRxObservable()
.map { tracks ->
val loggedServices = trackManager.services.filter { it.isLogged }.map { it.id }
tracks.filter { it.sync_id in loggedServices }
@ -335,7 +335,7 @@ class MangaPresenter(
fun deleteCustomCover(manga: Manga) {
Observable
.fromCallable {
coverCache.deleteCustomCover(manga)
coverCache.deleteCustomCover(manga.id)
manga.updateCoverLastModified(db)
coverCache.clearMemoryCache()
}
@ -514,7 +514,7 @@ class MangaPresenter(
}
fun startDownloadingNow(chapter: Chapter) {
downloadManager.startDownloadNow(chapter)
downloadManager.startDownloadNow(chapter.id)
}
/**
@ -720,7 +720,7 @@ class MangaPresenter(
private fun fetchTrackers() {
trackSubscription?.let { remove(it) }
trackSubscription = db.getTracks(manga)
trackSubscription = db.getTracks(manga.id)
.asRxObservable()
.map { tracks ->
loggedServices.map { service ->

View File

@ -85,9 +85,9 @@ class ChaptersSettingsSheet(
private val unread = Item.TriStateGroup(R.string.action_filter_unread, this)
private val bookmarked = Item.TriStateGroup(R.string.action_filter_bookmarked, this)
override val header = null
override val header: Item? = null
override val items = listOf(downloaded, unread, bookmarked)
override val footer = null
override val footer: Item? = null
override fun initModels() {
if (presenter.forceDownloaded()) {
@ -138,9 +138,9 @@ class ChaptersSettingsSheet(
private val chapterNum = Item.MultiSort(R.string.sort_by_number, this)
private val uploadDate = Item.MultiSort(R.string.sort_by_upload_date, this)
override val header = null
override val header: Item? = null
override val items = listOf(source, uploadDate, chapterNum)
override val footer = null
override val footer: Item? = null
override fun initModels() {
val sorting = presenter.manga.sorting
@ -200,9 +200,9 @@ class ChaptersSettingsSheet(
private val displayTitle = Item.Radio(R.string.show_title, this)
private val displayChapterNum = Item.Radio(R.string.show_chapter_number, this)
override val header = null
override val header: Item? = null
override val items = listOf(displayTitle, displayChapterNum)
override val footer = null
override val footer: Item? = null
override fun initModels() {
val mode = presenter.manga.displayMode

View File

@ -151,7 +151,7 @@ class ReaderPresenter(
private var hasTrackers: Boolean = false
private val checkTrackers: (Manga) -> Unit = { manga ->
val tracks = db.getTracks(manga).executeAsBlocking()
val tracks = db.getTracks(manga.id).executeAsBlocking()
hasTrackers = tracks.size > 0
}
@ -755,7 +755,7 @@ class ReaderPresenter(
val context = Injekt.get<Application>()
launchIO {
db.getTracks(manga).executeAsBlocking()
db.getTracks(manga.id).executeAsBlocking()
.mapNotNull { track ->
val service = trackManager.getService(track.sync_id)
if (service != null && service.isLogged && chapterRead > track.last_chapter_read) {

View File

@ -143,7 +143,7 @@ class UpdatesPresenter : BasePresenter<UpdatesController>() {
}
fun startDownloadingNow(chapter: Chapter) {
downloadManager.startDownloadNow(chapter)
downloadManager.startDownloadNow(chapter.id)
}
/**

View File

@ -37,7 +37,7 @@ fun Manga.prepUpdateCover(coverCache: CoverCache, remoteManga: SManga, refreshSa
}
fun Manga.hasCustomCover(coverCache: CoverCache): Boolean {
return coverCache.getCustomCoverFile(this).exists()
return coverCache.getCustomCoverFile(id).exists()
}
fun Manga.removeCovers(coverCache: CoverCache) {

View File

@ -1,5 +1,6 @@
package eu.kanade.tachiyomi.util.chapter
import eu.kanade.data.chapter.NoChaptersException
import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.database.models.Manga
@ -171,5 +172,3 @@ private fun shouldUpdateDbChapter(dbChapter: Chapter, sourceChapter: Chapter): B
dbChapter.chapter_number != sourceChapter.chapter_number ||
dbChapter.source_order != sourceChapter.source_order
}
class NoChaptersException : Exception()