Skip to content

Commit

Permalink
Provide a disposable that can be used to remove the observer by the c…
Browse files Browse the repository at this point in the history
…aller; Bump version code (#10)
  • Loading branch information
ruimendesM authored Jul 25, 2023
1 parent a1cb1e9 commit 5d129f4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ buildscript {
ext.buildtools_version = '33.0.0'
ext.corektx_version = '1.10.1'
ext.compilesdk_version = 33
ext.lib_version = '0.3.0'
ext.lib_version = '0.3.1'

repositories {
google()
Expand Down
16 changes: 10 additions & 6 deletions firely-lib/src/main/java/com/busbud/android/firely/LiveVariable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,10 @@ package com.busbud.android.firely

import android.util.Log
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.google.firebase.remoteconfig.ConfigUpdate
import com.google.firebase.remoteconfig.ConfigUpdateListener
import com.google.firebase.remoteconfig.ConfigUpdateListenerRegistration
import com.google.firebase.remoteconfig.FirebaseRemoteConfigException
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlin.reflect.KClass
import kotlin.reflect.cast

Expand All @@ -55,7 +50,10 @@ class LiveVariable<T : Any>(
else -> throw ClassCastException("Unsupported")
}

fun observeRealTime(lifecycleOwner: LifecycleOwner, onError: (() -> Unit)? = null) {
fun observeRealTime(
lifecycleOwner: LifecycleOwner,
onError: (() -> Unit)? = null
): LiveVariableDisposable {
val listenerRegistration =
internalFirely.addConfigUpdateListener(object : ConfigUpdateListener {
override fun onUpdate(configUpdate: ConfigUpdate) {
Expand All @@ -76,5 +74,11 @@ class LiveVariable<T : Any>(
super.onDestroy(owner)
}
})

return object : LiveVariableDisposable {
override fun dispose() {
listenerRegistration.remove()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.busbud.android.firely

interface LiveVariableDisposable {
fun dispose()
}

0 comments on commit 5d129f4

Please sign in to comment.