AquaStream/app/src/main/java/com/lagradost/cloudstream3/utils/Coroutines.kt

67 lines
1.8 KiB
Kotlin

package com.lagradost.cloudstream3.utils
import android.os.Handler
import android.os.Looper
import com.lagradost.cloudstream3.mvvm.launchSafe
import com.lagradost.cloudstream3.mvvm.logError
import kotlinx.coroutines.*
import java.util.Collections.synchronizedList
object Coroutines {
fun <T> T.main(work: suspend ((T) -> Unit)): Job {
val value = this
return CoroutineScope(Dispatchers.Main).launchSafe {
work(value)
}
}
fun <T> T.ioSafe(work: suspend (CoroutineScope.(T) -> Unit)): Job {
val value = this
return CoroutineScope(Dispatchers.IO).launchSafe {
work(value)
}
}
suspend fun <T, V> V.ioWorkSafe(work: suspend (CoroutineScope.(V) -> T)): T? {
val value = this
return withContext(Dispatchers.IO) {
try {
work(value)
} catch (e: Exception) {
logError(e)
null
}
}
}
suspend fun <T, V> V.ioWork(work: suspend (CoroutineScope.(V) -> T)): T {
val value = this
return withContext(Dispatchers.IO) {
work(value)
}
}
suspend fun <T, V> V.mainWork(work: suspend (CoroutineScope.(V) -> T)): T {
val value = this
return withContext(Dispatchers.Main) {
work(value)
}
}
fun runOnMainThread(work: (() -> Unit)) {
val mainHandler = Handler(Looper.getMainLooper())
mainHandler.post {
work()
}
}
/**
* Safe to add and remove how you want
* If you want to iterate over the list then you need to do:
* synchronized(allProviders) { code here }
**/
fun <T> threadSafeListOf(vararg items: T): MutableList<T> {
return synchronizedList(items.toMutableList())
}
}