mirror of
https://github.com/mihonapp/mihon.git
synced 2025-11-15 05:27:28 +01:00
Use Compose in Migrate tab (#7008)
* Use Compose in Migrate tab * Add missing header * Remove unused files * Fix build after rebase * Changes from review comments
This commit is contained in:
@@ -10,6 +10,8 @@ import eu.kanade.domain.history.interactor.RemoveHistoryByMangaId
|
||||
import eu.kanade.domain.history.repository.HistoryRepository
|
||||
import eu.kanade.domain.source.interactor.DisableSource
|
||||
import eu.kanade.domain.source.interactor.GetEnabledSources
|
||||
import eu.kanade.domain.source.interactor.GetSourcesWithFavoriteCount
|
||||
import eu.kanade.domain.source.interactor.SetMigrateSorting
|
||||
import eu.kanade.domain.source.interactor.ToggleSourcePin
|
||||
import eu.kanade.domain.source.repository.SourceRepository
|
||||
import uy.kohesive.injekt.api.InjektModule
|
||||
@@ -29,9 +31,11 @@ class DomainModule : InjektModule {
|
||||
addFactory { RemoveHistoryById(get()) }
|
||||
addFactory { RemoveHistoryByMangaId(get()) }
|
||||
|
||||
addSingletonFactory<SourceRepository> { SourceRepositoryImpl(get()) }
|
||||
addSingletonFactory<SourceRepository> { SourceRepositoryImpl(get(), get()) }
|
||||
addFactory { GetEnabledSources(get(), get()) }
|
||||
addFactory { DisableSource(get()) }
|
||||
addFactory { ToggleSourcePin(get()) }
|
||||
addFactory { GetSourcesWithFavoriteCount(get(), get()) }
|
||||
addFactory { SetMigrateSorting(get()) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package eu.kanade.domain.source.interactor
|
||||
|
||||
import eu.kanade.domain.source.model.Source
|
||||
import eu.kanade.domain.source.repository.SourceRepository
|
||||
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import java.text.Collator
|
||||
import java.util.*
|
||||
import kotlin.Comparator
|
||||
|
||||
class GetSourcesWithFavoriteCount(
|
||||
private val repository: SourceRepository,
|
||||
private val preferences: PreferencesHelper
|
||||
) {
|
||||
|
||||
fun subscribe(): Flow<List<Pair<Source, Long>>> {
|
||||
return combine(
|
||||
preferences.migrationSortingDirection().asFlow(),
|
||||
preferences.migrationSortingMode().asFlow(),
|
||||
repository.getSourcesWithFavoriteCount()
|
||||
) { direction, mode, list ->
|
||||
list.sortedWith(sortFn(direction, mode))
|
||||
}
|
||||
}
|
||||
|
||||
private fun sortFn(
|
||||
direction: SetMigrateSorting.Direction,
|
||||
sorting: SetMigrateSorting.Mode
|
||||
): java.util.Comparator<Pair<Source, Long>> {
|
||||
val locale = Locale.getDefault()
|
||||
val collator = Collator.getInstance(locale).apply {
|
||||
strength = Collator.PRIMARY
|
||||
}
|
||||
val sortFn: (Pair<Source, Long>, Pair<Source, Long>) -> Int = { a, b ->
|
||||
val id1 = a.first.name.toLongOrNull()
|
||||
val id2 = b.first.name.toLongOrNull()
|
||||
when (sorting) {
|
||||
SetMigrateSorting.Mode.ALPHABETICAL -> {
|
||||
collator.compare(a.first.name.lowercase(locale), b.first.name.lowercase(locale))
|
||||
}
|
||||
SetMigrateSorting.Mode.TOTAL -> {
|
||||
when {
|
||||
id1 != null && id2 != null -> a.second.compareTo(b.second)
|
||||
id1 != null && id2 == null -> -1
|
||||
id2 != null && id1 == null -> 1
|
||||
else -> a.second.compareTo(b.second)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return when (direction) {
|
||||
SetMigrateSorting.Direction.ASCENDING -> Comparator(sortFn)
|
||||
SetMigrateSorting.Direction.DESCENDING -> Collections.reverseOrder(sortFn)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package eu.kanade.domain.source.interactor
|
||||
|
||||
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
|
||||
|
||||
class SetMigrateSorting(
|
||||
private val preferences: PreferencesHelper
|
||||
) {
|
||||
|
||||
fun await(mode: Mode, isAscending: Boolean) {
|
||||
val direction = if (isAscending) Direction.ASCENDING else Direction.DESCENDING
|
||||
preferences.migrationSortingDirection().set(direction)
|
||||
preferences.migrationSortingMode().set(mode)
|
||||
}
|
||||
|
||||
enum class Mode {
|
||||
ALPHABETICAL,
|
||||
TOTAL;
|
||||
}
|
||||
|
||||
enum class Direction {
|
||||
ASCENDING,
|
||||
DESCENDING;
|
||||
}
|
||||
}
|
||||
@@ -6,4 +6,6 @@ import kotlinx.coroutines.flow.Flow
|
||||
interface SourceRepository {
|
||||
|
||||
fun getSources(): Flow<List<Source>>
|
||||
|
||||
fun getSourcesWithFavoriteCount(): Flow<List<Pair<Source, Long>>>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user