|
@@ -22,6 +22,7 @@ import kotlinx.coroutines.channels.awaitClose
|
|
|
import kotlinx.coroutines.flow.callbackFlow
|
|
import kotlinx.coroutines.flow.callbackFlow
|
|
|
import kotlinx.coroutines.flow.catch
|
|
import kotlinx.coroutines.flow.catch
|
|
|
import kotlinx.coroutines.flow.collect
|
|
import kotlinx.coroutines.flow.collect
|
|
|
|
|
+import kotlinx.coroutines.flow.conflate
|
|
|
import kotlinx.coroutines.flow.consumeAsFlow
|
|
import kotlinx.coroutines.flow.consumeAsFlow
|
|
|
import kotlinx.coroutines.flow.flowOn
|
|
import kotlinx.coroutines.flow.flowOn
|
|
|
import kotlinx.coroutines.flow.map
|
|
import kotlinx.coroutines.flow.map
|
|
@@ -36,6 +37,7 @@ class ImportBookViewModel(application: Application) : BaseViewModel(application)
|
|
|
var sort = context.getPrefInt(PreferKey.localBookImportSort)
|
|
var sort = context.getPrefInt(PreferKey.localBookImportSort)
|
|
|
var dataCallback: DataCallback? = null
|
|
var dataCallback: DataCallback? = null
|
|
|
var dataFlowStart: (() -> Unit)? = null
|
|
var dataFlowStart: (() -> Unit)? = null
|
|
|
|
|
+ var filterKey: String? = null
|
|
|
val dataFlow = callbackFlow<List<ImportBook>> {
|
|
val dataFlow = callbackFlow<List<ImportBook>> {
|
|
|
|
|
|
|
|
val list = Collections.synchronizedList(ArrayList<ImportBook>())
|
|
val list = Collections.synchronizedList(ArrayList<ImportBook>())
|
|
@@ -62,16 +64,6 @@ class ImportBookViewModel(application: Application) : BaseViewModel(application)
|
|
|
trySend(emptyList())
|
|
trySend(emptyList())
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- override fun screen(key: String?) {
|
|
|
|
|
- if (key.isNullOrBlank()) {
|
|
|
|
|
- trySend(list)
|
|
|
|
|
- } else {
|
|
|
|
|
- trySend(
|
|
|
|
|
- list.filter { it.name.contains(key) }
|
|
|
|
|
- )
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
override fun upAdapter() {
|
|
override fun upAdapter() {
|
|
|
trySend(list)
|
|
trySend(list)
|
|
|
}
|
|
}
|
|
@@ -85,13 +77,18 @@ class ImportBookViewModel(application: Application) : BaseViewModel(application)
|
|
|
dataCallback = null
|
|
dataCallback = null
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- }.map { docList ->
|
|
|
|
|
|
|
+ }.conflate().map { docList ->
|
|
|
|
|
+ val docList = docList.toList()
|
|
|
|
|
+ val filterKey = filterKey
|
|
|
|
|
+ val skipFilter = filterKey.isNullOrBlank()
|
|
|
val comparator = when (sort) {
|
|
val comparator = when (sort) {
|
|
|
2 -> compareBy<ImportBook>({ !it.isDir }, { -it.lastModified })
|
|
2 -> compareBy<ImportBook>({ !it.isDir }, { -it.lastModified })
|
|
|
1 -> compareBy({ !it.isDir }, { -it.size })
|
|
1 -> compareBy({ !it.isDir }, { -it.size })
|
|
|
else -> compareBy { !it.isDir }
|
|
else -> compareBy { !it.isDir }
|
|
|
} then compareBy(AlphanumComparator) { it.name }
|
|
} then compareBy(AlphanumComparator) { it.name }
|
|
|
- docList.sortedWith(comparator)
|
|
|
|
|
|
|
+ docList.asSequence().filter {
|
|
|
|
|
+ skipFilter || it.name.contains(filterKey)
|
|
|
|
|
+ }.sortedWith(comparator).toList()
|
|
|
}.flowOn(IO)
|
|
}.flowOn(IO)
|
|
|
|
|
|
|
|
fun addToBookshelf(bookList: HashSet<ImportBook>, finally: () -> Unit) {
|
|
fun addToBookshelf(bookList: HashSet<ImportBook>, finally: () -> Unit) {
|
|
@@ -164,7 +161,8 @@ class ImportBookViewModel(application: Application) : BaseViewModel(application)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
fun updateCallBackFlow(filterKey: String?) {
|
|
fun updateCallBackFlow(filterKey: String?) {
|
|
|
- dataCallback?.screen(filterKey)
|
|
|
|
|
|
|
+ this.filterKey = filterKey
|
|
|
|
|
+ dataCallback?.upAdapter()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
interface DataCallback {
|
|
interface DataCallback {
|
|
@@ -175,8 +173,6 @@ class ImportBookViewModel(application: Application) : BaseViewModel(application)
|
|
|
|
|
|
|
|
fun clear()
|
|
fun clear()
|
|
|
|
|
|
|
|
- fun screen(key: String?)
|
|
|
|
|
-
|
|
|
|
|
fun upAdapter()
|
|
fun upAdapter()
|
|
|
|
|
|
|
|
}
|
|
}
|