Skip to content

Commit

Permalink
Put activity observer directly in module
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianDevel committed Feb 9, 2024
1 parent c588718 commit 1bf0835
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class InAppUpdateManager(
onInstallSuccess: (() -> Unit)? = null,
) : BaseInAppUpdateManager(activity) {

init {
observeLifecycle()
}

override fun installDownloadedUpdate() = Unit

override fun checkUpdateIsAvailable() {
Expand Down
12 changes: 8 additions & 4 deletions Stores/src/main/java/BaseInAppUpdateManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,27 @@ import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner

abstract class BaseInAppUpdateManager(activity: FragmentActivity) : DefaultLifecycleObserver {
abstract class BaseInAppUpdateManager(private val activity: FragmentActivity) : DefaultLifecycleObserver {

var onInAppUpdateUiChange: ((Boolean) -> Unit)? = null
var onFDroidResult: ((Boolean) -> Unit)? = null

protected val localSettings = StoresLocalSettings.getInstance(activity)

abstract fun installDownloadedUpdate()

protected abstract fun checkUpdateIsAvailable()

override fun onStart(owner: LifecycleOwner) {
super.onStart(owner)

handleUpdates()
localSettings.appUpdateLaunches++
}

abstract fun installDownloadedUpdate()

protected abstract fun checkUpdateIsAvailable()
protected fun BaseInAppUpdateManager.observeLifecycle() {
activity.lifecycle.addObserver(observer = this)
}

private fun handleUpdates() = with(localSettings) {
if (appUpdateLaunches != 0 && (isUserWantingUpdates || appUpdateLaunches % 10 == 0)) checkUpdateIsAvailable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ class InAppUpdateManager(
}
}

init {
observeLifecycle()
}

override fun onCreate(owner: LifecycleOwner) {
super.onCreate(owner)
observeAppUpdateDownload()
Expand Down

0 comments on commit 1bf0835

Please sign in to comment.