Skip to content

Commit

Permalink
Updated MainActivity to react of flows instead of callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
this-Aditya committed Oct 19, 2024
1 parent e14d5c8 commit 113a653
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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. */
Expand Down Expand Up @@ -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

Expand All @@ -82,6 +88,14 @@ abstract class MainActivity : AppCompatActivity(), LoginListener {
val projectId: String?
get() = configuration.latestConfig.optString(PROJECT_ID_KEY)

private val serviceBoundActions: MutableList<ServiceStateReactor> = mutableListOf(
IRadarBinder::startScanning,
{binder -> view?.onRadarServiceBound(binder)},
)
private val serviceUnboundActions: MutableList<ServiceStateReactor> = mutableListOf(
IRadarBinder::stopScanning
)

@CallSuper
override fun onSaveInstanceState(savedInstanceState: Bundle) {
super.onSaveInstanceState(savedInstanceState)
Expand All @@ -99,14 +113,42 @@ abstract class MainActivity : AppCompatActivity(), LoginListener {

savedInstanceState?.also { permissionHandler.restoreInstanceState(it) }

radarConnection = ManagedServiceConnection<IRadarBinder>(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<IRadarBinder> ->
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ 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

class BluetoothEnforcer(
private val context: ComponentActivity,
private val radarConnection: ManagedServiceConnection<IRadarBinder>,
private val serviceBoundActions: MutableList<ServiceStateReactor>
) {
private val handler = Handler(Looper.getMainLooper())
private var isRequestingBluetooth = false
Expand Down Expand Up @@ -55,7 +57,7 @@ class BluetoothEnforcer(
config.reset(ENABLE_BLUETOOTH_REQUESTS)
}

radarConnection.onBoundListeners += {
serviceBoundActions += {
updateNeedsBluetooth(it.needsBluetooth())
}
enableBluetoothRequests = ChangeRunner(
Expand Down

0 comments on commit 113a653

Please sign in to comment.