mirror of
https://github.com/mihonapp/mihon.git
synced 2025-07-01 13:37:50 +02:00
Library search is now asynchronous
Library will now display progress bar while loading More optimizations to library search
This commit is contained in:
@ -28,8 +28,11 @@ private inline fun <reified T> delegatedSourceId(): Long {
|
||||
}!!.value.sourceId
|
||||
}
|
||||
|
||||
fun isLewdSource(source: Long) = source in 6900..6999 || SourceManager.DELEGATED_SOURCES.filter {
|
||||
// Used to speed up isLewdSource
|
||||
private val lewdDelegatedSourceIds = SourceManager.DELEGATED_SOURCES.filter {
|
||||
it.value.newSourceClass in DELEGATED_LEWD_SOURCES
|
||||
}.any {
|
||||
it.value.sourceId == source
|
||||
}
|
||||
}.map { it.value.sourceId }.sorted()
|
||||
|
||||
// This method MUST be fast!
|
||||
fun isLewdSource(source: Long) = source in 6900..6999
|
||||
|| lewdDelegatedSourceIds.binarySearch(source) >= 0
|
||||
|
55
app/src/main/java/exh/ui/LoadingTools.kt
Normal file
55
app/src/main/java/exh/ui/LoadingTools.kt
Normal file
@ -0,0 +1,55 @@
|
||||
package exh.ui
|
||||
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import java.util.*
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
import kotlin.coroutines.EmptyCoroutineContext
|
||||
|
||||
typealias LoadingHandle = String
|
||||
|
||||
/**
|
||||
* Class used to manage loader UIs
|
||||
*/
|
||||
class LoaderManager(parentContext: CoroutineContext = EmptyCoroutineContext): CoroutineScope {
|
||||
override val coroutineContext = Dispatchers.Main + parentContext
|
||||
|
||||
private val openLoadingHandles = mutableListOf<LoadingHandle>()
|
||||
var loadingChangeListener: (suspend (newIsLoading: Boolean) -> Unit)? = null
|
||||
|
||||
fun openProgressBar(): LoadingHandle {
|
||||
val (handle, shouldUpdateLoadingStatus) = synchronized(this) {
|
||||
val handle = UUID.randomUUID().toString()
|
||||
openLoadingHandles += handle
|
||||
handle to (openLoadingHandles.size == 1)
|
||||
}
|
||||
|
||||
if(shouldUpdateLoadingStatus) {
|
||||
launch {
|
||||
updateLoadingStatus(true)
|
||||
}
|
||||
}
|
||||
|
||||
return handle
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun closeProgressBar(handle: LoadingHandle?) {
|
||||
if(handle == null) return
|
||||
|
||||
val shouldUpdateLoadingStatus = synchronized(this) {
|
||||
openLoadingHandles.remove(handle) && openLoadingHandles.isEmpty()
|
||||
}
|
||||
|
||||
if(shouldUpdateLoadingStatus) {
|
||||
launch {
|
||||
updateLoadingStatus(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun updateLoadingStatus(newStatus: Boolean) {
|
||||
loadingChangeListener?.invoke(newStatus)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user