fix: sorting extensions order
This commit is contained in:
parent
15abcd77d0
commit
c2f108bf44
12 changed files with 146 additions and 82 deletions
|
@ -4,13 +4,19 @@ import android.util.Log
|
|||
import ani.dantotsu.Lazier
|
||||
import ani.dantotsu.parsers.novel.DynamicNovelParser
|
||||
import ani.dantotsu.parsers.novel.NovelExtension
|
||||
import ani.dantotsu.settings.saving.PrefManager
|
||||
import ani.dantotsu.settings.saving.PrefName
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.first
|
||||
|
||||
object NovelSources : NovelReadSources() {
|
||||
override var list: List<Lazier<BaseParser>> = emptyList()
|
||||
var pinnedNovelSources: List<String> = emptyList()
|
||||
|
||||
suspend fun init(fromExtensions: StateFlow<List<NovelExtension.Installed>>) {
|
||||
pinnedNovelSources = PrefManager.getNullableVal<List<String>>(PrefName.NovelSourcesOrder, null)
|
||||
?: emptyList()
|
||||
|
||||
// Initialize with the first value from StateFlow
|
||||
val initialExtensions = fromExtensions.first()
|
||||
list = createParsersFromExtensions(initialExtensions) + Lazier(
|
||||
|
@ -20,13 +26,25 @@ object NovelSources : NovelReadSources() {
|
|||
|
||||
// Update as StateFlow emits new values
|
||||
fromExtensions.collect { extensions ->
|
||||
list = createParsersFromExtensions(extensions) + Lazier(
|
||||
list = sortPinnedNovelSources(
|
||||
createParsersFromExtensions(extensions),
|
||||
pinnedNovelSources
|
||||
) + Lazier(
|
||||
{ OfflineNovelParser() },
|
||||
"Downloaded"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun performReorderNovelSources() {
|
||||
//remove the downloaded source from the list to avoid duplicates
|
||||
list = list.filter { it.name != "Downloaded" }
|
||||
list = sortPinnedNovelSources(list, pinnedNovelSources) + Lazier(
|
||||
{ OfflineNovelParser() },
|
||||
"Downloaded"
|
||||
)
|
||||
}
|
||||
|
||||
private fun createParsersFromExtensions(extensions: List<NovelExtension.Installed>): List<Lazier<BaseParser>> {
|
||||
Log.d("NovelSources", "createParsersFromExtensions")
|
||||
Log.d("NovelSources", extensions.toString())
|
||||
|
@ -35,4 +53,17 @@ object NovelSources : NovelReadSources() {
|
|||
Lazier({ DynamicNovelParser(extension) }, name)
|
||||
}
|
||||
}
|
||||
|
||||
private fun sortPinnedNovelSources(
|
||||
parsers: List<Lazier<BaseParser>>,
|
||||
pinnedSources: List<String>
|
||||
): List<Lazier<BaseParser>> {
|
||||
val pinnedSourcesMap = parsers.filter { pinnedSources.contains(it.name) }
|
||||
.associateBy { it.name }
|
||||
val orderedPinnedSources = pinnedSources.mapNotNull { name ->
|
||||
pinnedSourcesMap[name]
|
||||
}
|
||||
val unpinnedSources = parsers.filterNot { pinnedSources.contains(it.name) }
|
||||
return orderedPinnedSources + unpinnedSources
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue