Skip to content

Commit

Permalink
Refactor and fix control states
Browse files Browse the repository at this point in the history
  • Loading branch information
ReneeVandervelde committed Feb 20, 2022
1 parent 6c1a323 commit 42ddfde
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.inkapplications.ack.data.AfskModulationConfiguration
import com.inkapplications.ack.data.AprsAccess
import com.inkapplications.ack.data.ConnectionConfiguration
import com.inkapplications.ack.structures.*
import com.inkapplications.android.extensions.control.ControlState
import inkapplications.spondee.measure.Meters
import inkapplications.spondee.measure.Miles
import inkapplications.spondee.scalar.WholePercentage
Expand Down Expand Up @@ -38,16 +39,37 @@ class CaptureEvents @Inject constructor(
private val internetListenState = MutableStateFlow(false)
private val transmitState = MutableStateFlow(false)

val screenState = audioListenState
.map { CaptureScreenViewModel(recordingEnabled = it) }
.combine(internetListenState) { viewModel, state ->
viewModel.copy(internetServiceEnabled = state)
private val recordingControlState = audioListenState.map { listening ->
if (listening) ControlState.Enabled else ControlState.Disabled
}

private val internetServiceControlState = settings.observeString(connectionSettings.callsign)
.combine(internetListenState) { callsign, state ->
when {
callsign.isBlank() -> ControlState.Hidden
state -> ControlState.Enabled
else -> ControlState.Disabled
}
}
.combine(settings.observeString(connectionSettings.callsign)) { viewModel, callsign ->
viewModel.copy(internetServiceVisible = callsign.isNotBlank())

private val transmitControlState = settings.observeString(connectionSettings.callsign)
.combine(transmitState) { callsign, state ->
when {
callsign.isBlank() -> ControlState.Hidden
state -> ControlState.Enabled
else -> ControlState.Disabled
}
}

val screenState = recordingControlState
.combine(internetServiceControlState) { recording, internet ->
CaptureScreenViewModel(
recordingState = recording,
internetServiceState = internet,
)
}
.combine(transmitState) { viewModel, state ->
viewModel.copy(transmitState = state)
.combine(transmitControlState) { viewModel, transmit ->
viewModel.copy(transmitState = transmit)
}

suspend fun listenForPackets() {
Expand Down Expand Up @@ -135,7 +157,7 @@ class CaptureEvents @Inject constructor(
)
)

aprs.transmitAudioPacket(packet, EncodingConfig(compression = EncodingPreference.Barred), prototype.afskConfiguration)
aprs.transmitAudioPacket(packet, EncodingConfig(compression = EncodingPreference.Disfavored), prototype.afskConfiguration)
delay(prototype.minRate)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import com.inkapplications.ack.android.capture.map.MapViewModel
import com.inkapplications.ack.android.trackNavigation
import com.inkapplications.ack.android.ui.theme.AprsScreen
import com.inkapplications.ack.android.ui.theme.AprsTheme
import com.inkapplications.android.extensions.control.ControlState
import com.inkapplications.android.extensions.control.whenDisabled
import com.inkapplications.android.extensions.control.whenEnabled
import kimchi.Kimchi

@Composable
Expand All @@ -40,10 +43,9 @@ fun CaptureScreen(

Column {
CaptureAppBar(
recordingEnabled = captureScreenState.value.recordingEnabled,
internetServiceVisible = captureScreenState.value.internetServiceVisible,
internetServiceEnabled = captureScreenState.value.internetServiceEnabled,
transmitEnabled = captureScreenState.value.transmitState,
recordingState = captureScreenState.value.recordingState,
internetState = captureScreenState.value.internetServiceState,
transmitState = captureScreenState.value.transmitState,
controller = controller,
)
Box(
Expand Down Expand Up @@ -105,10 +107,9 @@ fun CaptureScreen(

@Composable
fun CaptureAppBar(
recordingEnabled: Boolean,
internetServiceVisible: Boolean,
internetServiceEnabled: Boolean,
transmitEnabled: Boolean,
recordingState: ControlState,
internetState: ControlState,
transmitState: ControlState,
controller: CaptureNavController,
) {
TopAppBar(
Expand All @@ -118,7 +119,7 @@ fun CaptureAppBar(
backgroundColor = AprsTheme.colors.surface,
contentColor = contentColorFor(AprsTheme.colors.surface),
actions = {
if (transmitEnabled) {
transmitState.whenEnabled {
IconButton(
onClick = controller::onTransmitDisableClick,
) {
Expand All @@ -128,7 +129,8 @@ fun CaptureAppBar(
tint = AprsTheme.colors.brand,
)
}
} else {
}
transmitState.whenDisabled {
IconButton(
onClick = controller::onTransmitEnableClick,
) {
Expand All @@ -138,7 +140,7 @@ fun CaptureAppBar(
)
}
}
if (recordingEnabled) {
recordingState.whenEnabled {
IconButton(
onClick = controller::onRecordingDisableClick,
) {
Expand All @@ -148,7 +150,8 @@ fun CaptureAppBar(
tint = AprsTheme.colors.brand,
)
}
} else {
}
recordingState.whenDisabled {
IconButton(
onClick = controller::onRecordingEnableClick
) {
Expand All @@ -158,27 +161,25 @@ fun CaptureAppBar(
)
}
}
when {
internetServiceVisible && internetServiceEnabled -> {
IconButton(
onClick = controller::onInternetServiceDisableClick,
) {
Icon(
Icons.Default.Cloud,
contentDescription = "Disable APRS-IS",
tint = AprsTheme.colors.brand,
)
}
internetState.whenEnabled {
IconButton(
onClick = controller::onInternetServiceDisableClick,
) {
Icon(
Icons.Default.Cloud,
contentDescription = "Disable APRS-IS",
tint = AprsTheme.colors.brand,
)
}
internetServiceVisible && !internetServiceEnabled -> {
IconButton(
onClick = controller::onInternetServiceEnableClick,
) {
Icon(
Icons.Default.CloudOff,
contentDescription = "Enable APRS-IS"
)
}
}
internetState.whenDisabled {
IconButton(
onClick = controller::onInternetServiceEnableClick,
) {
Icon(
Icons.Default.CloudOff,
contentDescription = "Enable APRS-IS"
)
}
}
IconButton(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.inkapplications.ack.android.capture

import com.inkapplications.android.extensions.control.ControlState

data class CaptureScreenViewModel(
val recordingEnabled: Boolean = false,
val internetServiceEnabled: Boolean = false,
val internetServiceVisible: Boolean = false,
val transmitState: Boolean = false,
val recordingState: ControlState = ControlState.Hidden,
val internetServiceState: ControlState = ControlState.Hidden,
val transmitState: ControlState = ControlState.Hidden,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.inkapplications.android.extensions.control

import androidx.compose.runtime.Composable

enum class ControlState {
Enabled,
Disabled,
Hidden,
}

@Composable
inline fun ControlState.whenEnabled(action: @Composable () -> Unit) {
if (this == ControlState.Enabled) action()
}

@Composable
inline fun ControlState.whenDisabled(action: @Composable () -> Unit) {
if (this == ControlState.Disabled) action()
}

@Composable
inline fun ControlState.whenHidden(action: @Composable () -> Unit) {
if (this == ControlState.Hidden) action()
}

@Composable
inline fun ControlState.whenVisible(action: @Composable () -> Unit) {
if (this != ControlState.Hidden) action()
}

0 comments on commit 42ddfde

Please sign in to comment.