Convert intercept activity to a stateflow

This commit is contained in:
Jobobby04 2020-11-30 20:34:48 -05:00
parent ee18f94788
commit 8fbc6aa29b

View File

@ -17,13 +17,14 @@ import eu.kanade.tachiyomi.ui.manga.MangaController
import exh.GalleryAddEvent import exh.GalleryAddEvent
import exh.GalleryAdder import exh.GalleryAdder
import kotlinx.coroutines.Dispatchers 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 kotlinx.coroutines.launch
import rx.Subscription
import rx.android.schedulers.AndroidSchedulers
import rx.subjects.BehaviorSubject
class InterceptActivity : BaseActivity<EhActivityInterceptBinding>() { class InterceptActivity : BaseActivity<EhActivityInterceptBinding>() {
private var statusSubscription: Subscription? = null private var statusJob: Job? = null
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -56,10 +57,9 @@ class InterceptActivity : BaseActivity<EhActivityInterceptBinding>() {
override fun onStart() { override fun onStart() {
super.onStart() super.onStart()
statusSubscription?.unsubscribe() statusJob?.cancel()
statusSubscription = status statusJob = status
.observeOn(AndroidSchedulers.mainThread()) .onEach {
.subscribe {
when (it) { when (it) {
is InterceptResult.Success -> { is InterceptResult.Success -> {
binding.interceptProgress.isVisible = false binding.interceptProgress.isVisible = false
@ -87,22 +87,23 @@ class InterceptActivity : BaseActivity<EhActivityInterceptBinding>() {
} }
} }
} }
.launchIn(scope)
} }
override fun onStop() { override fun onStop() {
super.onStop() super.onStop()
statusSubscription?.unsubscribe() statusJob?.cancel()
} }
private val galleryAdder = GalleryAdder() private val galleryAdder = GalleryAdder()
val status: BehaviorSubject<InterceptResult> = BehaviorSubject.create(InterceptResult.Idle) val status: MutableStateFlow<InterceptResult> = MutableStateFlow(InterceptResult.Idle)
@Synchronized @Synchronized
fun loadGallery(gallery: String) { fun loadGallery(gallery: String) {
// Do not load gallery if already loading // Do not load gallery if already loading
if (status.value is InterceptResult.Idle) { if (status.value is InterceptResult.Idle) {
status.onNext(InterceptResult.Loading) status.value = InterceptResult.Loading
val sources = galleryAdder.pickSource(gallery) val sources = galleryAdder.pickSource(gallery)
if (sources.size > 1) { if (sources.size > 1) {
MaterialDialog(this) MaterialDialog(this)
@ -123,14 +124,12 @@ class InterceptActivity : BaseActivity<EhActivityInterceptBinding>() {
scope.launch(Dispatchers.IO) { scope.launch(Dispatchers.IO) {
val result = galleryAdder.addGallery(this@InterceptActivity, gallery, forceSource = source) val result = galleryAdder.addGallery(this@InterceptActivity, gallery, forceSource = source)
status.onNext( status.value = when (result) {
when (result) {
is GalleryAddEvent.Success -> result.manga.id?.let { is GalleryAddEvent.Success -> result.manga.id?.let {
InterceptResult.Success(it) InterceptResult.Success(it)
} ?: InterceptResult.Failure(this@InterceptActivity.getString(R.string.manga_id_is_null)) } ?: InterceptResult.Failure(this@InterceptActivity.getString(R.string.manga_id_is_null))
is GalleryAddEvent.Fail -> InterceptResult.Failure(result.logMessage) is GalleryAddEvent.Fail -> InterceptResult.Failure(result.logMessage)
} }
)
} }
} }
} }