mirror of
https://github.com/Aviortheking/Neo-Store.git
synced 2025-04-23 19:32:16 +00:00
Revert: Revert Back to RxJava for Now
This commit is contained in:
parent
bc44c9de15
commit
ed49230d4e
@ -29,34 +29,27 @@ import com.looker.droidify.utility.extension.android.notificationManager
|
|||||||
import com.looker.droidify.utility.extension.resources.getColorFromAttr
|
import com.looker.droidify.utility.extension.resources.getColorFromAttr
|
||||||
import com.looker.droidify.utility.extension.text.formatSize
|
import com.looker.droidify.utility.extension.text.formatSize
|
||||||
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
|
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
|
||||||
|
import io.reactivex.rxjava3.core.Observable
|
||||||
import io.reactivex.rxjava3.disposables.Disposable
|
import io.reactivex.rxjava3.disposables.Disposable
|
||||||
import io.reactivex.rxjava3.schedulers.Schedulers.io
|
import io.reactivex.rxjava3.schedulers.Schedulers
|
||||||
import kotlinx.coroutines.*
|
import io.reactivex.rxjava3.subjects.PublishSubject
|
||||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
|
||||||
import kotlinx.coroutines.flow.SharedFlow
|
|
||||||
import kotlinx.coroutines.flow.asSharedFlow
|
|
||||||
import kotlinx.coroutines.flow.collect
|
|
||||||
import java.lang.ref.WeakReference
|
import java.lang.ref.WeakReference
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
import kotlin.math.roundToInt
|
import kotlin.math.roundToInt
|
||||||
|
|
||||||
class SyncService : ConnectionService<SyncService.Binder>() {
|
class SyncService : ConnectionService<SyncService.Binder>() {
|
||||||
private val scope = CoroutineScope(Dispatchers.Default)
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val ACTION_CANCEL = "${BuildConfig.APPLICATION_ID}.intent.action.CANCEL"
|
private const val ACTION_CANCEL = "${BuildConfig.APPLICATION_ID}.intent.action.CANCEL"
|
||||||
|
|
||||||
private val mutableStateSubject = MutableSharedFlow<State>()
|
private val stateSubject = PublishSubject.create<State>()
|
||||||
private val mutableFinishSubject = MutableSharedFlow<Unit>()
|
private val finishSubject = PublishSubject.create<Unit>()
|
||||||
|
|
||||||
private val stateSubject = mutableStateSubject.asSharedFlow()
|
|
||||||
private val finishSubject = mutableFinishSubject.asSharedFlow()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private sealed class State {
|
private sealed class State {
|
||||||
data class Connecting(val name: String) : State()
|
data class Connecting(val name: String) : State()
|
||||||
data class Syncing(
|
data class Syncing(
|
||||||
val name: String, val stage: RepositoryUpdater.Stage,
|
val name: String, val stage: RepositoryUpdater.Stage,
|
||||||
val read: Long, val total: Long?,
|
val read: Long, val total: Long?
|
||||||
) : State()
|
) : State()
|
||||||
|
|
||||||
object Finishing : State()
|
object Finishing : State()
|
||||||
@ -65,7 +58,7 @@ class SyncService : ConnectionService<SyncService.Binder>() {
|
|||||||
private class Task(val repositoryId: Long, val manual: Boolean)
|
private class Task(val repositoryId: Long, val manual: Boolean)
|
||||||
private data class CurrentTask(
|
private data class CurrentTask(
|
||||||
val task: Task?, val disposable: Disposable,
|
val task: Task?, val disposable: Disposable,
|
||||||
val hasUpdates: Boolean, val lastState: State,
|
val hasUpdates: Boolean, val lastState: State
|
||||||
)
|
)
|
||||||
|
|
||||||
private enum class Started { NO, AUTO, MANUAL }
|
private enum class Started { NO, AUTO, MANUAL }
|
||||||
@ -79,7 +72,7 @@ class SyncService : ConnectionService<SyncService.Binder>() {
|
|||||||
enum class SyncRequest { AUTO, MANUAL, FORCE }
|
enum class SyncRequest { AUTO, MANUAL, FORCE }
|
||||||
|
|
||||||
inner class Binder : android.os.Binder() {
|
inner class Binder : android.os.Binder() {
|
||||||
val finish: SharedFlow<Unit>
|
val finish: Observable<Unit>
|
||||||
get() = finishSubject
|
get() = finishSubject
|
||||||
|
|
||||||
private fun sync(ids: List<Long>, request: SyncRequest) {
|
private fun sync(ids: List<Long>, request: SyncRequest) {
|
||||||
@ -159,6 +152,8 @@ class SyncService : ConnectionService<SyncService.Binder>() {
|
|||||||
private val binder = Binder()
|
private val binder = Binder()
|
||||||
override fun onBind(intent: Intent): Binder = binder
|
override fun onBind(intent: Intent): Binder = binder
|
||||||
|
|
||||||
|
private var stateDisposable: Disposable? = null
|
||||||
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
|
|
||||||
@ -175,17 +170,17 @@ class SyncService : ConnectionService<SyncService.Binder>() {
|
|||||||
)
|
)
|
||||||
.let(notificationManager::createNotificationChannel)
|
.let(notificationManager::createNotificationChannel)
|
||||||
}
|
}
|
||||||
scope.launch(Dispatchers.Default) {
|
|
||||||
stateSubject.collect {
|
stateDisposable = stateSubject
|
||||||
publishForegroundState(false,
|
.sample(500L, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread())
|
||||||
it)
|
.subscribe { publishForegroundState(false, it) }
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
scope.cancel()
|
|
||||||
|
stateDisposable?.dispose()
|
||||||
|
stateDisposable = null
|
||||||
cancelTasks { true }
|
cancelTasks { true }
|
||||||
cancelCurrentTask { true }
|
cancelCurrentTask { true }
|
||||||
}
|
}
|
||||||
@ -354,20 +349,18 @@ class SyncService : ConnectionService<SyncService.Binder>() {
|
|||||||
lateinit var disposable: Disposable
|
lateinit var disposable: Disposable
|
||||||
disposable = RepositoryUpdater
|
disposable = RepositoryUpdater
|
||||||
.update(this, repository, unstable) { stage, progress, total ->
|
.update(this, repository, unstable) { stage, progress, total ->
|
||||||
scope.launch(Dispatchers.Default) {
|
if (!disposable.isDisposed) {
|
||||||
if (!disposable.isDisposed) {
|
stateSubject.onNext(
|
||||||
mutableStateSubject.emit(
|
State.Syncing(
|
||||||
State.Syncing(
|
repository.name,
|
||||||
repository.name,
|
stage,
|
||||||
stage,
|
progress,
|
||||||
progress,
|
total
|
||||||
total
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
}
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.observeOn(io())
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
.subscribe { result, throwable ->
|
.subscribe { result, throwable ->
|
||||||
currentTask = null
|
currentTask = null
|
||||||
throwable?.printStackTrace()
|
throwable?.printStackTrace()
|
||||||
@ -398,7 +391,7 @@ class SyncService : ConnectionService<SyncService.Binder>() {
|
|||||||
.toList()
|
.toList()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.subscribeOn(io())
|
.subscribeOn(Schedulers.io())
|
||||||
.observeOn(AndroidSchedulers.mainThread())
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
.subscribe { result, throwable ->
|
.subscribe { result, throwable ->
|
||||||
throwable?.printStackTrace()
|
throwable?.printStackTrace()
|
||||||
@ -411,7 +404,7 @@ class SyncService : ConnectionService<SyncService.Binder>() {
|
|||||||
}
|
}
|
||||||
currentTask = CurrentTask(null, disposable, true, State.Finishing)
|
currentTask = CurrentTask(null, disposable, true, State.Finishing)
|
||||||
} else {
|
} else {
|
||||||
scope.launch(Dispatchers.Main) { mutableFinishSubject.emit(Unit) }
|
finishSubject.onNext(Unit)
|
||||||
val needStop = started == Started.MANUAL
|
val needStop = started == Started.MANUAL
|
||||||
started = Started.NO
|
started = Started.NO
|
||||||
if (needStop) {
|
if (needStop) {
|
||||||
@ -479,20 +472,23 @@ class SyncService : ConnectionService<SyncService.Binder>() {
|
|||||||
|
|
||||||
class Job : JobService() {
|
class Job : JobService() {
|
||||||
private var syncParams: JobParameters? = null
|
private var syncParams: JobParameters? = null
|
||||||
|
private var syncDisposable: Disposable? = null
|
||||||
private val syncConnection =
|
private val syncConnection =
|
||||||
Connection(SyncService::class.java, onBind = { connection, binder ->
|
Connection(SyncService::class.java, onBind = { connection, binder ->
|
||||||
MainScope().launch {
|
syncDisposable = binder.finish.subscribe {
|
||||||
binder.finish.collect {
|
val params = syncParams
|
||||||
val params = syncParams
|
if (params != null) {
|
||||||
if (params != null) {
|
syncParams = null
|
||||||
syncParams = null
|
syncDisposable?.dispose()
|
||||||
connection.unbind(this@Job)
|
syncDisposable = null
|
||||||
jobFinished(params, false)
|
connection.unbind(this)
|
||||||
}
|
jobFinished(params, false)
|
||||||
}
|
}
|
||||||
binder.sync(SyncRequest.AUTO)
|
|
||||||
}
|
}
|
||||||
|
binder.sync(SyncRequest.AUTO)
|
||||||
}, onUnbind = { _, binder ->
|
}, onUnbind = { _, binder ->
|
||||||
|
syncDisposable?.dispose()
|
||||||
|
syncDisposable = null
|
||||||
binder.cancelAuto()
|
binder.cancelAuto()
|
||||||
val params = syncParams
|
val params = syncParams
|
||||||
if (params != null) {
|
if (params != null) {
|
||||||
@ -509,6 +505,8 @@ class SyncService : ConnectionService<SyncService.Binder>() {
|
|||||||
|
|
||||||
override fun onStopJob(params: JobParameters): Boolean {
|
override fun onStopJob(params: JobParameters): Boolean {
|
||||||
syncParams = null
|
syncParams = null
|
||||||
|
syncDisposable?.dispose()
|
||||||
|
syncDisposable = null
|
||||||
val reschedule = syncConnection.binder?.cancelAuto() == true
|
val reschedule = syncConnection.binder?.cancelAuto() == true
|
||||||
syncConnection.unbind(this)
|
syncConnection.unbind(this)
|
||||||
return reschedule
|
return reschedule
|
||||||
|
Loading…
x
Reference in New Issue
Block a user