Refactor manga list model mapping
parent
c14d39c456
commit
607785dcd4
@ -1,60 +0,0 @@
|
|||||||
package org.koitharu.kotatsu.list.domain
|
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import androidx.annotation.ColorRes
|
|
||||||
import dagger.Reusable
|
|
||||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
|
||||||
import org.koitharu.kotatsu.R
|
|
||||||
import org.koitharu.kotatsu.core.prefs.AppSettings
|
|
||||||
import org.koitharu.kotatsu.history.data.HistoryRepository
|
|
||||||
import org.koitharu.kotatsu.history.data.PROGRESS_NONE
|
|
||||||
import org.koitharu.kotatsu.parsers.model.MangaTag
|
|
||||||
import org.koitharu.kotatsu.tracker.domain.TrackingRepository
|
|
||||||
import javax.inject.Inject
|
|
||||||
|
|
||||||
@Reusable
|
|
||||||
class ListExtraProvider @Inject constructor(
|
|
||||||
@ApplicationContext context: Context,
|
|
||||||
private val settings: AppSettings,
|
|
||||||
private val trackingRepository: TrackingRepository,
|
|
||||||
private val historyRepository: HistoryRepository,
|
|
||||||
) {
|
|
||||||
|
|
||||||
private val dict by lazy {
|
|
||||||
context.resources.openRawResource(R.raw.tags_redlist).use {
|
|
||||||
val set = HashSet<String>()
|
|
||||||
it.bufferedReader().forEachLine { x ->
|
|
||||||
val line = x.trim()
|
|
||||||
if (line.isNotEmpty()) {
|
|
||||||
set.add(line)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
set
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun getCounter(mangaId: Long): Int {
|
|
||||||
return if (settings.isTrackerEnabled) {
|
|
||||||
trackingRepository.getNewChaptersCount(mangaId)
|
|
||||||
} else {
|
|
||||||
0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun getProgress(mangaId: Long): Float {
|
|
||||||
return if (settings.isReadingIndicatorsEnabled) {
|
|
||||||
historyRepository.getProgress(mangaId)
|
|
||||||
} else {
|
|
||||||
PROGRESS_NONE
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@ColorRes
|
|
||||||
fun getTagTint(tag: MangaTag): Int {
|
|
||||||
return if (tag.title.lowercase() in dict) {
|
|
||||||
R.color.warning
|
|
||||||
} else {
|
|
||||||
0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -0,0 +1,129 @@
|
|||||||
|
package org.koitharu.kotatsu.list.domain
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import androidx.annotation.ColorRes
|
||||||
|
import androidx.collection.MutableScatterSet
|
||||||
|
import androidx.collection.ScatterSet
|
||||||
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||||
|
import org.koitharu.kotatsu.R
|
||||||
|
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||||
|
import org.koitharu.kotatsu.core.prefs.ListMode
|
||||||
|
import org.koitharu.kotatsu.core.ui.widgets.ChipsView
|
||||||
|
import org.koitharu.kotatsu.favourites.domain.FavouritesRepository
|
||||||
|
import org.koitharu.kotatsu.history.data.HistoryRepository
|
||||||
|
import org.koitharu.kotatsu.history.data.PROGRESS_NONE
|
||||||
|
import org.koitharu.kotatsu.list.ui.model.MangaCompactListModel
|
||||||
|
import org.koitharu.kotatsu.list.ui.model.MangaDetailedListModel
|
||||||
|
import org.koitharu.kotatsu.list.ui.model.MangaGridModel
|
||||||
|
import org.koitharu.kotatsu.list.ui.model.MangaListModel
|
||||||
|
import org.koitharu.kotatsu.parsers.model.Manga
|
||||||
|
import org.koitharu.kotatsu.parsers.model.MangaTag
|
||||||
|
import org.koitharu.kotatsu.tracker.domain.TrackingRepository
|
||||||
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
class MangaListMapper @Inject constructor(
|
||||||
|
@ApplicationContext context: Context,
|
||||||
|
private val settings: AppSettings,
|
||||||
|
private val trackingRepository: TrackingRepository,
|
||||||
|
private val historyRepository: HistoryRepository,
|
||||||
|
private val favouritesRepository: FavouritesRepository,
|
||||||
|
) {
|
||||||
|
|
||||||
|
private val dict by lazy { readTagsDict(context) }
|
||||||
|
|
||||||
|
suspend fun toListModelList(manga: Collection<Manga>, mode: ListMode): List<MangaListModel> = manga.map {
|
||||||
|
toListModel(it, mode)
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun toListModelList(
|
||||||
|
destination: MutableCollection<in MangaListModel>,
|
||||||
|
manga: Collection<Manga>,
|
||||||
|
mode: ListMode
|
||||||
|
) = manga.mapTo(destination) {
|
||||||
|
toListModel(it, mode)
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun toListModel(manga: Manga, mode: ListMode): MangaListModel = when (mode) {
|
||||||
|
ListMode.LIST -> toCompactListModel(manga)
|
||||||
|
ListMode.DETAILED_LIST -> toDetailedListModel(manga)
|
||||||
|
ListMode.GRID -> toGridModel(manga)
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun toCompactListModel(manga: Manga) = MangaCompactListModel(
|
||||||
|
id = manga.id,
|
||||||
|
title = manga.title,
|
||||||
|
subtitle = manga.tags.joinToString(", ") { it.title },
|
||||||
|
coverUrl = manga.coverUrl,
|
||||||
|
manga = manga,
|
||||||
|
counter = getCounter(manga.id),
|
||||||
|
progress = getProgress(manga.id),
|
||||||
|
)
|
||||||
|
|
||||||
|
suspend fun toDetailedListModel(manga: Manga) = MangaDetailedListModel(
|
||||||
|
id = manga.id,
|
||||||
|
title = manga.title,
|
||||||
|
subtitle = manga.altTitle,
|
||||||
|
coverUrl = manga.coverUrl,
|
||||||
|
manga = manga,
|
||||||
|
counter = getCounter(manga.id),
|
||||||
|
progress = getProgress(manga.id),
|
||||||
|
tags = mapTags(manga.tags),
|
||||||
|
)
|
||||||
|
|
||||||
|
suspend fun toGridModel(manga: Manga) = MangaGridModel(
|
||||||
|
id = manga.id,
|
||||||
|
title = manga.title,
|
||||||
|
coverUrl = manga.coverUrl,
|
||||||
|
manga = manga,
|
||||||
|
counter = getCounter(manga.id),
|
||||||
|
progress = getProgress(manga.id),
|
||||||
|
)
|
||||||
|
|
||||||
|
fun mapTags(tags: Collection<MangaTag>) = tags.map {
|
||||||
|
ChipsView.ChipModel(
|
||||||
|
tint = getTagTint(it),
|
||||||
|
title = it.title,
|
||||||
|
data = it,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun getCounter(mangaId: Long): Int {
|
||||||
|
return if (settings.isTrackerEnabled) {
|
||||||
|
trackingRepository.getNewChaptersCount(mangaId)
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun getProgress(mangaId: Long): Float {
|
||||||
|
return if (settings.isReadingIndicatorsEnabled) {
|
||||||
|
historyRepository.getProgress(mangaId)
|
||||||
|
} else {
|
||||||
|
PROGRESS_NONE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ColorRes
|
||||||
|
private fun getTagTint(tag: MangaTag): Int {
|
||||||
|
return if (tag.title.lowercase() in dict) {
|
||||||
|
R.color.warning
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun readTagsDict(context: Context): ScatterSet<String> =
|
||||||
|
context.resources.openRawResource(R.raw.tags_redlist).use {
|
||||||
|
val set = MutableScatterSet<String>()
|
||||||
|
it.bufferedReader().forEachLine { x ->
|
||||||
|
val line = x.trim()
|
||||||
|
if (line.isNotEmpty()) {
|
||||||
|
set.add(line)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
set.trim()
|
||||||
|
set
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,13 @@
|
|||||||
|
package org.koitharu.kotatsu.list.ui.model
|
||||||
|
|
||||||
|
import org.koitharu.kotatsu.parsers.model.Manga
|
||||||
|
|
||||||
|
data class MangaCompactListModel(
|
||||||
|
override val id: Long,
|
||||||
|
override val title: String,
|
||||||
|
val subtitle: String,
|
||||||
|
override val coverUrl: String,
|
||||||
|
override val manga: Manga,
|
||||||
|
override val counter: Int,
|
||||||
|
override val progress: Float,
|
||||||
|
) : MangaListModel()
|
||||||
@ -1,31 +0,0 @@
|
|||||||
package org.koitharu.kotatsu.list.ui.model
|
|
||||||
|
|
||||||
import org.koitharu.kotatsu.list.ui.ListModelDiffCallback
|
|
||||||
import org.koitharu.kotatsu.parsers.model.Manga
|
|
||||||
import org.koitharu.kotatsu.parsers.model.MangaSource
|
|
||||||
|
|
||||||
sealed class MangaItemModel : ListModel {
|
|
||||||
|
|
||||||
abstract val id: Long
|
|
||||||
abstract val manga: Manga
|
|
||||||
abstract val title: String
|
|
||||||
abstract val coverUrl: String
|
|
||||||
abstract val counter: Int
|
|
||||||
abstract val progress: Float
|
|
||||||
|
|
||||||
val source: MangaSource
|
|
||||||
get() = manga.source
|
|
||||||
|
|
||||||
override fun areItemsTheSame(other: ListModel): Boolean {
|
|
||||||
return other is MangaItemModel && other.javaClass == javaClass && id == other.id
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getChangePayload(previousState: ListModel): Any? {
|
|
||||||
return when {
|
|
||||||
previousState !is MangaItemModel -> super.getChangePayload(previousState)
|
|
||||||
progress != previousState.progress -> ListModelDiffCallback.PAYLOAD_PROGRESS_CHANGED
|
|
||||||
counter != previousState.counter -> ListModelDiffCallback.PAYLOAD_ANYTHING_CHANGED
|
|
||||||
else -> null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,13 +1,31 @@
|
|||||||
package org.koitharu.kotatsu.list.ui.model
|
package org.koitharu.kotatsu.list.ui.model
|
||||||
|
|
||||||
|
import org.koitharu.kotatsu.list.ui.ListModelDiffCallback
|
||||||
import org.koitharu.kotatsu.parsers.model.Manga
|
import org.koitharu.kotatsu.parsers.model.Manga
|
||||||
|
import org.koitharu.kotatsu.parsers.model.MangaSource
|
||||||
|
|
||||||
data class MangaListModel(
|
sealed class MangaListModel : ListModel {
|
||||||
override val id: Long,
|
|
||||||
override val title: String,
|
abstract val id: Long
|
||||||
val subtitle: String,
|
abstract val manga: Manga
|
||||||
override val coverUrl: String,
|
abstract val title: String
|
||||||
override val manga: Manga,
|
abstract val coverUrl: String
|
||||||
override val counter: Int,
|
abstract val counter: Int
|
||||||
override val progress: Float,
|
abstract val progress: Float
|
||||||
) : MangaItemModel()
|
|
||||||
|
val source: MangaSource
|
||||||
|
get() = manga.source
|
||||||
|
|
||||||
|
override fun areItemsTheSame(other: ListModel): Boolean {
|
||||||
|
return other is MangaListModel && other.javaClass == javaClass && id == other.id
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getChangePayload(previousState: ListModel): Any? {
|
||||||
|
return when {
|
||||||
|
previousState !is MangaListModel -> super.getChangePayload(previousState)
|
||||||
|
progress != previousState.progress -> ListModelDiffCallback.PAYLOAD_PROGRESS_CHANGED
|
||||||
|
counter != previousState.counter -> ListModelDiffCallback.PAYLOAD_ANYTHING_CHANGED
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue