From 8fbc6aa29baa4fc08cfbc6e374bb974042388ab6 Mon Sep 17 00:00:00 2001 From: Jobobby04 Date: Mon, 30 Nov 2020 20:34:48 -0500 Subject: [PATCH] Convert intercept activity to a stateflow --- .../exh/ui/intercept/InterceptActivity.kt | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/exh/ui/intercept/InterceptActivity.kt b/app/src/main/java/exh/ui/intercept/InterceptActivity.kt index 584de727d..566316eb9 100755 --- a/app/src/main/java/exh/ui/intercept/InterceptActivity.kt +++ b/app/src/main/java/exh/ui/intercept/InterceptActivity.kt @@ -17,13 +17,14 @@ import eu.kanade.tachiyomi.ui.manga.MangaController import exh.GalleryAddEvent import exh.GalleryAdder import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch -import rx.Subscription -import rx.android.schedulers.AndroidSchedulers -import rx.subjects.BehaviorSubject class InterceptActivity : BaseActivity() { - private var statusSubscription: Subscription? = null + private var statusJob: Job? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -56,10 +57,9 @@ class InterceptActivity : BaseActivity() { override fun onStart() { super.onStart() - statusSubscription?.unsubscribe() - statusSubscription = status - .observeOn(AndroidSchedulers.mainThread()) - .subscribe { + statusJob?.cancel() + statusJob = status + .onEach { when (it) { is InterceptResult.Success -> { binding.interceptProgress.isVisible = false @@ -87,22 +87,23 @@ class InterceptActivity : BaseActivity() { } } } + .launchIn(scope) } override fun onStop() { super.onStop() - statusSubscription?.unsubscribe() + statusJob?.cancel() } private val galleryAdder = GalleryAdder() - val status: BehaviorSubject = BehaviorSubject.create(InterceptResult.Idle) + val status: MutableStateFlow = MutableStateFlow(InterceptResult.Idle) @Synchronized fun loadGallery(gallery: String) { // Do not load gallery if already loading if (status.value is InterceptResult.Idle) { - status.onNext(InterceptResult.Loading) + status.value = InterceptResult.Loading val sources = galleryAdder.pickSource(gallery) if (sources.size > 1) { MaterialDialog(this) @@ -123,14 +124,12 @@ class InterceptActivity : BaseActivity() { scope.launch(Dispatchers.IO) { val result = galleryAdder.addGallery(this@InterceptActivity, gallery, forceSource = source) - status.onNext( - when (result) { - is GalleryAddEvent.Success -> result.manga.id?.let { - InterceptResult.Success(it) - } ?: InterceptResult.Failure(this@InterceptActivity.getString(R.string.manga_id_is_null)) - is GalleryAddEvent.Fail -> InterceptResult.Failure(result.logMessage) - } - ) + status.value = when (result) { + is GalleryAddEvent.Success -> result.manga.id?.let { + InterceptResult.Success(it) + } ?: InterceptResult.Failure(this@InterceptActivity.getString(R.string.manga_id_is_null)) + is GalleryAddEvent.Fail -> InterceptResult.Failure(result.logMessage) + } } } }