Refactor and optimization
This commit is contained in:
@@ -10,7 +10,7 @@ import androidx.lifecycle.LifecycleOwner
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
|
||||
class LifecycleAwareServiceConnection private constructor(
|
||||
class LifecycleAwareServiceConnection(
|
||||
private val host: Activity,
|
||||
) : ServiceConnection, DefaultLifecycleObserver {
|
||||
|
||||
@@ -31,19 +31,15 @@ class LifecycleAwareServiceConnection private constructor(
|
||||
super.onDestroy(owner)
|
||||
host.unbindService(this)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
fun bindService(
|
||||
host: Activity,
|
||||
lifecycleOwner: LifecycleOwner,
|
||||
service: Intent,
|
||||
flags: Int,
|
||||
): LifecycleAwareServiceConnection {
|
||||
val connection = LifecycleAwareServiceConnection(host)
|
||||
host.bindService(service, connection, flags)
|
||||
lifecycleOwner.lifecycle.addObserver(connection)
|
||||
return connection
|
||||
}
|
||||
}
|
||||
fun Activity.bindServiceWithLifecycle(
|
||||
owner: LifecycleOwner,
|
||||
service: Intent,
|
||||
flags: Int
|
||||
): LifecycleAwareServiceConnection {
|
||||
val connection = LifecycleAwareServiceConnection(this)
|
||||
bindService(service, connection, flags)
|
||||
owner.lifecycle.addObserver(connection)
|
||||
return connection
|
||||
}
|
||||
@@ -9,7 +9,11 @@ import android.net.Uri
|
||||
import android.os.Build
|
||||
import androidx.activity.result.ActivityResultLauncher
|
||||
import androidx.core.app.ActivityOptionsCompat
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.coroutineScope
|
||||
import androidx.work.CoroutineWorker
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||
import kotlin.coroutines.resume
|
||||
|
||||
@@ -55,4 +59,11 @@ fun <I> ActivityResultLauncher<I>.tryLaunch(input: I, options: ActivityOptionsCo
|
||||
return runCatching {
|
||||
launch(input, options)
|
||||
}.isSuccess
|
||||
}
|
||||
|
||||
fun Lifecycle.postDelayed(runnable: Runnable, delay: Long) {
|
||||
coroutineScope.launch {
|
||||
delay(delay)
|
||||
runnable.run()
|
||||
}
|
||||
}
|
||||
@@ -3,15 +3,6 @@ package org.koitharu.kotatsu.utils.ext
|
||||
import androidx.lifecycle.LifecycleCoroutineScope
|
||||
import androidx.lifecycle.ProcessLifecycleOwner
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import kotlinx.coroutines.CoroutineExceptionHandler
|
||||
import org.koitharu.kotatsu.BuildConfig
|
||||
|
||||
val IgnoreErrors
|
||||
get() = CoroutineExceptionHandler { _, e ->
|
||||
if (BuildConfig.DEBUG) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
val processLifecycleScope: LifecycleCoroutineScope
|
||||
inline get() = ProcessLifecycleOwner.get().lifecycleScope
|
||||
@@ -4,6 +4,7 @@ import androidx.lifecycle.LifecycleOwner
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.liveData
|
||||
import kotlinx.coroutines.Deferred
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
import kotlin.coroutines.EmptyCoroutineContext
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
Reference in New Issue
Block a user