Skip to content

Commit

Permalink
Don't observe downloads when leaving to reader activity
Browse files Browse the repository at this point in the history
  • Loading branch information
nonproto committed Feb 22, 2024
1 parent ef438ac commit 1c70fad
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.kanade.tachiyomi.ui.base.controller

import android.app.Activity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
Expand Down Expand Up @@ -31,6 +32,16 @@ abstract class BaseCoroutineController<VB : ViewBinding, PS : BaseCoroutinePrese
@Suppress("UNCHECKED_CAST")
private fun <View> BaseCoroutinePresenter<View>.takeView(view: Any) = attachView(view as? View)

override fun onActivityPaused(activity: Activity) {
super.onActivityPaused(activity)
presenter.onPause()
}

override fun onActivityResumed(activity: Activity) {
super.onActivityResumed(activity)
presenter.onResume()
}

override fun onDestroyView(view: View) {
super.onDestroyView(view)
presenter.onDestroy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import kotlinx.coroutines.isActive

open class BaseCoroutinePresenter<T> {
var presenterScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
var pausablePresenterScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
private var weakView: WeakReference<T>? = null
protected val view: T?
get() = weakView?.get()
Expand All @@ -23,12 +24,24 @@ open class BaseCoroutinePresenter<T> {
if (!presenterScope.isActive) {
presenterScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
}
if (!pausablePresenterScope.isActive) {
pausablePresenterScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
}
}

open fun onCreate() {}

open fun onPause() {
pausablePresenterScope.cancel()
}

open fun onResume() {
pausablePresenterScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
}

open fun onDestroy() {
presenterScope.cancel()
pausablePresenterScope.cancel()
weakView = null
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package eu.kanade.tachiyomi.ui.manga

import android.app.Activity
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
Expand Down Expand Up @@ -328,11 +327,6 @@ class MangaDetailController(private val mangaId: Long) :
}
}

override fun onActivityResumed(activity: Activity) {
super.onActivityResumed(activity)
presenter.resume()
}

companion object {
const val MANGA_EXTRA = "manga"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ class MangaDetailPresenter(
private val _currentManga = MutableStateFlow<Manga?>(null)
val manga: StateFlow<Manga?> = _currentManga.asStateFlow()

val downloadsScope = presenterScope

private fun currentManga(): Manga {
if (_currentManga.value == null) {
val dbManga = db.getManga(mangaId).executeAsBlocking()
Expand Down Expand Up @@ -1926,27 +1928,28 @@ class MangaDetailPresenter(
}

/**
* This method request updates after the actitivy resumed (usually after a return from the
* This method request updates after the activity resumed (usually after a return from the
* reader)
*/
fun resume() {
override fun onResume() {
presenterScope.launch {
updateMangaFlow()
updateChapterFlows()
updateTrackingFlows()
observeDownloads()
}
}

private fun observeDownloads() {
presenterScope.launchIO {
pausablePresenterScope.launchIO {
downloadManager
.statusFlow()
.filter { it.manga.id == currentManga().id }
.catch { error -> TimberKt.e(error) }
.collect { updateDownloadState(it) }
}

presenterScope.launchIO {
pausablePresenterScope.launchIO {
downloadManager
.progressFlow()
.filter { it.manga.id == currentManga().id }
Expand Down

0 comments on commit 1c70fad

Please sign in to comment.