refactor: Reduce dependant towards RecentsPresenter

This commit is contained in:
Ahmad Ansori Palembani 2025-01-01 14:02:57 +07:00
parent 42dd857d94
commit fa84ce8fe8
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6
3 changed files with 79 additions and 22 deletions

View file

@ -40,9 +40,13 @@ import eu.kanade.tachiyomi.util.system.launchIO
import java.util.Calendar
import java.util.Date
import kotlin.math.min
import kotlin.math.roundToLong
import kotlinx.coroutines.MainScope
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy
import yokai.domain.manga.models.cover
import yokai.domain.recents.interactor.GetRecents
class UpdatesGridGlanceWidget : GlanceAppWidget() {
private val app: Application by injectLazy()
@ -64,6 +68,33 @@ class UpdatesGridGlanceWidget : GlanceAppWidget() {
}
}
// FIXME: Don't depends on RecentsPresenter
private suspend fun getUpdates(customAmount: Int = 0, getRecents: GetRecents = Injekt.get()): List<Pair<Manga, Long>> {
return getRecents
.awaitUpdates(
limit = when {
customAmount > 0 -> (customAmount * 1.5).roundToLong()
else -> 25L
}
)
.mapNotNull {
when {
it.chapter.read || it.chapter.id == null -> RecentsPresenter.getNextChapter(it.manga)
it.history.id == null -> RecentsPresenter.getFirstUpdatedChapter(it.manga, it.chapter)
else -> it.chapter
} ?: return@mapNotNull null
it
}
.asSequence()
.distinctBy { it.manga.id }
.sortedByDescending { it.history.last_read }
// nChapterItems + nAdditionalItems + cReadingItems
.take((RecentsPresenter.UPDATES_CHAPTER_LIMIT * 2) + RecentsPresenter.UPDATES_READING_LIMIT_LOWER)
.filter { it.manga.id != null }
.map { it.manga to it.history.last_read }
.toList()
}
fun loadData(list: List<Pair<Manga, Long>>? = null) {
coroutineScope.launchIO {
// Don't show anything when lock is active
@ -80,7 +111,7 @@ class UpdatesGridGlanceWidget : GlanceAppWidget() {
.flatMap { manager.getAppWidgetSizes(it) }
.maxBy { it.height.value * it.width.value }
.calculateRowAndColumnCount()
val processList = list ?: RecentsPresenter.getRecentManga(customAmount = min(50, rowCount * columnCount))
val processList = list ?: getUpdates(customAmount = min(50, rowCount * columnCount))
data = prepareList(processList, rowCount * columnCount)
ids.forEach { update(app, it) }

View file

@ -379,20 +379,22 @@ class RecentsPresenter(
f2.second.date_fetch.compareTo(f1.second.date_fetch)
}
}
.take(4).map {
RecentMangaItem(it.first, it.second, newChaptersHeader)
}.toMutableList()
.take(UPDATES_CHAPTER_LIMIT)
.map { RecentMangaItem(it.first, it.second, newChaptersHeader) }
.toMutableList()
val cReadingItems =
pairs.filter { it.first.history.id != null }.take(9 - nChaptersItems.size).map {
RecentMangaItem(it.first, it.second, continueReadingHeader)
}.toMutableList()
pairs.filter { it.first.history.id != null }
.take(UPDATES_READING_LIMIT_UPPER - nChaptersItems.size)
.map { RecentMangaItem(it.first, it.second, continueReadingHeader) }
.toMutableList()
if (nChaptersItems.isNotEmpty()) {
nChaptersItems.add(RecentMangaItem(header = newChaptersHeader))
}
if (cReadingItems.isNotEmpty()) {
cReadingItems.add(RecentMangaItem(header = continueReadingHeader))
}
val nAdditionsItems = pairs.filter { it.first.chapter.id == null }.take(4)
val nAdditionsItems = pairs.filter { it.first.chapter.id == null }
.take(UPDATES_CHAPTER_LIMIT)
.map { RecentMangaItem(it.first, it.second, newAdditionsHeader) }
listOf(nChaptersItems, cReadingItems, nAdditionsItems).sortedByDescending {
it.firstOrNull()?.mch?.history?.last_read ?: 0L
@ -483,20 +485,6 @@ class RecentsPresenter(
return Triple(sortedChapters, firstChapter, extraCount)
}
private suspend fun getNextChapter(manga: Manga): Chapter? {
val mangaId = manga.id ?: return null
val chapters = getChapter.awaitUnread(mangaId, true)
return ChapterSort(manga, chapterFilter, preferences).getNextChapter(chapters, false)
}
private suspend fun getFirstUpdatedChapter(manga: Manga, chapter: Chapter): Chapter? {
val mangaId = manga.id ?: return null
val chapters = getChapter.awaitUnread(mangaId, true)
return chapters.sortedWith(ChapterSort(manga, chapterFilter, preferences).sortComparator(true)).find {
abs(it.date_fetch - chapter.date_fetch) <= TimeUnit.HOURS.toMillis(12)
}
}
override fun onDestroy() {
super.onDestroy()
lastRecents = recentItems
@ -727,6 +715,31 @@ class RecentsPresenter(
var SHORT_LIMIT = 25
private set
suspend fun getNextChapter(
manga: Manga,
getChapter: GetChapter = Injekt.get(),
chapterFilter: ChapterFilter = Injekt.get(),
preferences: PreferencesHelper = Injekt.get(),
): Chapter? {
val mangaId = manga.id ?: return null
val chapters = getChapter.awaitUnread(mangaId, true)
return ChapterSort(manga, chapterFilter, preferences).getNextChapter(chapters, false)
}
suspend fun getFirstUpdatedChapter(
manga: Manga,
chapter: Chapter,
getChapter: GetChapter = Injekt.get(),
chapterFilter: ChapterFilter = Injekt.get(),
preferences: PreferencesHelper = Injekt.get(),
): Chapter? {
val mangaId = manga.id ?: return null
val chapters = getChapter.awaitUnread(mangaId, true)
return chapters.sortedWith(ChapterSort(manga, chapterFilter, preferences).sortComparator(true)).find {
abs(it.date_fetch - chapter.date_fetch) <= TimeUnit.HOURS.toMillis(12)
}
}
suspend fun getRecentManga(includeRead: Boolean = false, customAmount: Int = 0): List<Pair<Manga, Long>> {
val presenter = RecentsPresenter()
presenter.viewType = RecentsViewType.UngroupedAll
@ -741,5 +754,9 @@ class RecentsPresenter(
.filter { it.mch.manga.id != null }
.map { it.mch.manga to it.mch.history.last_read }
}
const val UPDATES_CHAPTER_LIMIT = 4
const val UPDATES_READING_LIMIT_UPPER = 9
const val UPDATES_READING_LIMIT_LOWER = 5
}
}

View file

@ -55,4 +55,13 @@ class GetRecents(
return historyRepository.getRecentsAll(includeRead, filterScanlators, search, limit, actualOffset)
}
suspend fun awaitUpdates(limit: Long = 0L): List<MangaChapterHistory> =
historyRepository.getRecentsAll(
includeRead = false,
filterScanlators = true,
search = "",
limit = limit,
offset = 0L
)
}