From 113a6530d9f04cef482d536f52f576d636c9f8a6 Mon Sep 17 00:00:00 2001 From: this-Aditya Date: Sat, 19 Oct 2024 23:37:10 +0530 Subject: [PATCH] Updated MainActivity to react of flows instead of callbacks --- .../org/radarbase/android/MainActivity.kt | 56 ++++++++++++++++--- .../android/util/BluetoothEnforcer.kt | 4 +- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/radar-commons-android/src/main/java/org/radarbase/android/MainActivity.kt b/radar-commons-android/src/main/java/org/radarbase/android/MainActivity.kt index 4df61d134..5be81bcb9 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/MainActivity.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/MainActivity.kt @@ -23,9 +23,13 @@ import android.os.Process import androidx.annotation.CallSuper import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat +import androidx.lifecycle.lifecycleScope import androidx.localbroadcastmanager.content.LocalBroadcastManager import kotlinx.coroutines.flow.StateFlow import com.google.firebase.analytics.FirebaseAnalytics +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach +import kotlinx.coroutines.launch import org.radarbase.android.RadarApplication.Companion.radarApp import org.radarbase.android.RadarApplication.Companion.radarConfig import org.radarbase.android.RadarConfiguration.Companion.PROJECT_ID_KEY @@ -37,12 +41,12 @@ import org.radarbase.android.RadarService.Companion.ACTION_PROVIDERS_UPDATED import org.radarbase.android.RadarService.Companion.EXTRA_PERMISSIONS import org.radarbase.android.auth.* import org.radarbase.android.auth.AuthService -import org.radarbase.android.config.CombinedRadarConfig import org.radarbase.android.util.* import org.slf4j.LoggerFactory import kotlin.time.Duration import kotlin.time.Duration.Companion.milliseconds +typealias ServiceStateReactor = (IRadarBinder) -> Unit /** Base MainActivity class. It manages the services to collect the data and starts up a view. To * create an application, extend this class and override the abstract methods. */ @@ -70,6 +74,8 @@ abstract class MainActivity : AppCompatActivity(), LoginListener { protected lateinit var configuration: RadarConfiguration private var connectionsUpdatedReceiver: BroadcastRegistration? = null + private var serviceActionBinder: IRadarBinder? = null + protected open val requestPermissionTimeout: Duration get() = REQUEST_PERMISSION_TIMEOUT @@ -82,6 +88,14 @@ abstract class MainActivity : AppCompatActivity(), LoginListener { val projectId: String? get() = configuration.latestConfig.optString(PROJECT_ID_KEY) + private val serviceBoundActions: MutableList = mutableListOf( + IRadarBinder::startScanning, + {binder -> view?.onRadarServiceBound(binder)}, + ) + private val serviceUnboundActions: MutableList = mutableListOf( + IRadarBinder::stopScanning + ) + @CallSuper override fun onSaveInstanceState(savedInstanceState: Bundle) { super.onSaveInstanceState(savedInstanceState) @@ -99,14 +113,42 @@ abstract class MainActivity : AppCompatActivity(), LoginListener { savedInstanceState?.also { permissionHandler.restoreInstanceState(it) } - radarConnection = ManagedServiceConnection(this@MainActivity, radarApp.radarService).apply { - bindFlags = Context.BIND_ABOVE_CLIENT or Context.BIND_AUTO_CREATE - onBoundListeners += IRadarBinder::startScanning - onBoundListeners += { binder -> view?.onRadarServiceBound(binder) } - onUnboundListeners += IRadarBinder::stopScanning + radarConnection = ManagedServiceConnection( + this, + radarApp.radarService, + IRadarBinder::class.java + ).apply { + bindFlags = Context.BIND_AUTO_CREATE or Context.BIND_ABOVE_CLIENT + } + + lifecycleScope.launch { +// repeatOnLifecycle(Lifecycle.State.CREATED) + radarConnection.state + .onEach { bindState: BindState -> + when (bindState) { + is ManagedServiceConnection.BoundService -> { + serviceActionBinder = bindState.binder + .also { binder -> + serviceBoundActions.forEach { action -> + action(binder) + } + } + } + + is ManagedServiceConnection.Unbound -> { + serviceActionBinder?.also { binder -> + serviceUnboundActions.forEach { action -> + action(binder) + } + serviceActionBinder = null + } + } + } + }.launchIn(this) } - bluetoothEnforcer = BluetoothEnforcer(this, radarConnection) + + bluetoothEnforcer = BluetoothEnforcer(this, radarConnection, serviceBoundActions) authConnection = AuthServiceConnection(this, this).apply { onBoundListeners += { binder -> binder.applyState { diff --git a/radar-commons-android/src/main/java/org/radarbase/android/util/BluetoothEnforcer.kt b/radar-commons-android/src/main/java/org/radarbase/android/util/BluetoothEnforcer.kt index e4b05873b..dfae572be 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/util/BluetoothEnforcer.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/util/BluetoothEnforcer.kt @@ -12,6 +12,7 @@ import org.radarbase.android.IRadarBinder import org.radarbase.android.RadarApplication.Companion.radarConfig import org.radarbase.android.RadarConfiguration.Companion.ENABLE_BLUETOOTH_REQUESTS import org.radarbase.android.RadarService +import org.radarbase.android.ServiceStateReactor import org.radarbase.android.util.BluetoothStateReceiver.Companion.bluetoothIsEnabled import org.slf4j.LoggerFactory import java.util.concurrent.TimeUnit @@ -19,6 +20,7 @@ import java.util.concurrent.TimeUnit class BluetoothEnforcer( private val context: ComponentActivity, private val radarConnection: ManagedServiceConnection, + private val serviceBoundActions: MutableList ) { private val handler = Handler(Looper.getMainLooper()) private var isRequestingBluetooth = false @@ -55,7 +57,7 @@ class BluetoothEnforcer( config.reset(ENABLE_BLUETOOTH_REQUESTS) } - radarConnection.onBoundListeners += { + serviceBoundActions += { updateNeedsBluetooth(it.needsBluetooth()) } enableBluetoothRequests = ChangeRunner(