mirror of
https://github.com/Aviortheking/Neo-Store.git
synced 2025-04-23 19:32:16 +00:00
Improve: Move away from RxJava
Syncing is fast but looks less responsive (Will fix later)
This commit is contained in:
parent
83a9efcfd7
commit
6fbc94dca8
@ -29,20 +29,22 @@ 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
|
import io.reactivex.rxjava3.schedulers.Schedulers
|
||||||
import io.reactivex.rxjava3.subjects.PublishSubject
|
import kotlinx.coroutines.*
|
||||||
|
import kotlinx.coroutines.flow.*
|
||||||
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>() {
|
||||||
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 stateSubject = PublishSubject.create<State>()
|
private val mutableStateSubject = MutableSharedFlow<State>()
|
||||||
private val finishSubject = PublishSubject.create<Unit>()
|
private val mutableFinishState = MutableSharedFlow<Unit>()
|
||||||
|
|
||||||
|
private val stateSubject = mutableStateSubject.asSharedFlow()
|
||||||
|
private val finishState = mutableFinishState.asSharedFlow()
|
||||||
}
|
}
|
||||||
|
|
||||||
private sealed class State {
|
private sealed class State {
|
||||||
@ -63,6 +65,8 @@ class SyncService : ConnectionService<SyncService.Binder>() {
|
|||||||
|
|
||||||
private enum class Started { NO, AUTO, MANUAL }
|
private enum class Started { NO, AUTO, MANUAL }
|
||||||
|
|
||||||
|
private val scope = CoroutineScope(Dispatchers.Default)
|
||||||
|
|
||||||
private var started = Started.NO
|
private var started = Started.NO
|
||||||
private val tasks = mutableListOf<Task>()
|
private val tasks = mutableListOf<Task>()
|
||||||
private var currentTask: CurrentTask? = null
|
private var currentTask: CurrentTask? = null
|
||||||
@ -72,8 +76,8 @@ 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: Observable<Unit>
|
val finish: SharedFlow<Unit>
|
||||||
get() = finishSubject
|
get() = finishState
|
||||||
|
|
||||||
private fun sync(ids: List<Long>, request: SyncRequest) {
|
private fun sync(ids: List<Long>, request: SyncRequest) {
|
||||||
val cancelledTask =
|
val cancelledTask =
|
||||||
@ -152,8 +156,6 @@ 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()
|
||||||
|
|
||||||
@ -171,16 +173,11 @@ class SyncService : ConnectionService<SyncService.Binder>() {
|
|||||||
.let(notificationManager::createNotificationChannel)
|
.let(notificationManager::createNotificationChannel)
|
||||||
}
|
}
|
||||||
|
|
||||||
stateDisposable = stateSubject
|
stateSubject.onEach { publishForegroundState(false, it) }.launchIn(scope)
|
||||||
.sample(500L, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread())
|
|
||||||
.subscribe { publishForegroundState(false, it) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
|
|
||||||
stateDisposable?.dispose()
|
|
||||||
stateDisposable = null
|
|
||||||
cancelTasks { true }
|
cancelTasks { true }
|
||||||
cancelCurrentTask { true }
|
cancelCurrentTask { true }
|
||||||
}
|
}
|
||||||
@ -350,7 +347,8 @@ class SyncService : ConnectionService<SyncService.Binder>() {
|
|||||||
disposable = RepositoryUpdater
|
disposable = RepositoryUpdater
|
||||||
.update(this, repository, unstable) { stage, progress, total ->
|
.update(this, repository, unstable) { stage, progress, total ->
|
||||||
if (!disposable.isDisposed) {
|
if (!disposable.isDisposed) {
|
||||||
stateSubject.onNext(
|
scope.launch {
|
||||||
|
mutableStateSubject.emit(
|
||||||
State.Syncing(
|
State.Syncing(
|
||||||
repository.name,
|
repository.name,
|
||||||
stage,
|
stage,
|
||||||
@ -360,6 +358,7 @@ class SyncService : ConnectionService<SyncService.Binder>() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
.observeOn(AndroidSchedulers.mainThread())
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
.subscribe { result, throwable ->
|
.subscribe { result, throwable ->
|
||||||
currentTask = null
|
currentTask = null
|
||||||
@ -404,7 +403,7 @@ class SyncService : ConnectionService<SyncService.Binder>() {
|
|||||||
}
|
}
|
||||||
currentTask = CurrentTask(null, disposable, true, State.Finishing)
|
currentTask = CurrentTask(null, disposable, true, State.Finishing)
|
||||||
} else {
|
} else {
|
||||||
finishSubject.onNext(Unit)
|
scope.launch { mutableFinishState.emit(Unit) }
|
||||||
val needStop = started == Started.MANUAL
|
val needStop = started == Started.MANUAL
|
||||||
started = Started.NO
|
started = Started.NO
|
||||||
if (needStop) {
|
if (needStop) {
|
||||||
@ -471,25 +470,24 @@ class SyncService : ConnectionService<SyncService.Binder>() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class Job : JobService() {
|
class Job : JobService() {
|
||||||
|
private val jobScope = CoroutineScope(Dispatchers.Default)
|
||||||
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 ->
|
||||||
syncDisposable = binder.finish.subscribe {
|
jobScope.launch {
|
||||||
|
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
|
|
||||||
connection.unbind(this)
|
|
||||||
jobFinished(params, false)
|
jobFinished(params, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
binder.sync(SyncRequest.AUTO)
|
binder.sync(SyncRequest.AUTO)
|
||||||
}, onUnbind = { _, binder ->
|
}, onUnbind = { _, binder ->
|
||||||
syncDisposable?.dispose()
|
|
||||||
syncDisposable = null
|
|
||||||
binder.cancelAuto()
|
binder.cancelAuto()
|
||||||
|
jobScope.cancel()
|
||||||
val params = syncParams
|
val params = syncParams
|
||||||
if (params != null) {
|
if (params != null) {
|
||||||
syncParams = null
|
syncParams = null
|
||||||
@ -505,8 +503,7 @@ class SyncService : ConnectionService<SyncService.Binder>() {
|
|||||||
|
|
||||||
override fun onStopJob(params: JobParameters): Boolean {
|
override fun onStopJob(params: JobParameters): Boolean {
|
||||||
syncParams = null
|
syncParams = null
|
||||||
syncDisposable?.dispose()
|
jobScope.cancel()
|
||||||
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