Skip to content

Commit 3a6ac16

Browse files
Copilotsds100
andcommitted
Refactor volume action state management and deprecate stream actions
- Consolidated volumeUpActionState and volumeDownActionState into single volumeActionState - Added actionId to VolumeActionBottomSheetState to determine which action to create - Updated bottom sheet to use actionId for dynamic title selection - Merged onDoneConfigVolumeUpClick and onDoneConfigVolumeDownClick into single handler - Deprecated VOLUME_INCREASE_STREAM and VOLUME_DECREASE_STREAM ActionIds - Deprecated Volume.Stream classes (kept for backward compatibility) - Updated CHANGELOG wording to "Consolidated volume and stream actions" Co-authored-by: sds100 <[email protected]>
1 parent 99e7780 commit 3a6ac16

File tree

6 files changed

+41
-63
lines changed

6 files changed

+41
-63
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
- #727 Actions to send SMS messages: "Send SMS" and "Compose SMS"
1818
- #1819 Explain how to enable the accessibility service restricted setting
1919
- #661 Action to execute shell commands.
20-
- Volume up/down actions now allow selecting a specific audio stream (e.g., music, notifications) to control, consolidating the functionality of increase/decrease stream actions.
20+
- Consolidated volume and stream actions.
2121

2222
## Removed
2323

base/src/main/java/io/github/sds100/keymapper/base/actions/ActionData.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ sealed class ActionData : Comparable<ActionData> {
9898

9999
@Serializable
100100
sealed class Volume : ActionData() {
101+
@Deprecated("Use Volume.Up or Volume.Down with volumeStream parameter instead")
101102
sealed class Stream : Volume() {
102103
abstract val volumeStream: VolumeStream
103104
abstract val showVolumeUi: Boolean

base/src/main/java/io/github/sds100/keymapper/base/actions/ActionId.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ enum class ActionId {
4545
VOLUME_UP,
4646
VOLUME_DOWN,
4747
VOLUME_SHOW_DIALOG,
48+
@Deprecated("Use VOLUME_DOWN with volumeStream parameter instead")
4849
VOLUME_DECREASE_STREAM,
50+
@Deprecated("Use VOLUME_UP with volumeStream parameter instead")
4951
VOLUME_INCREASE_STREAM,
5052
CYCLE_RINGER_MODE,
5153
CHANGE_RINGER_MODE,

base/src/main/java/io/github/sds100/keymapper/base/actions/ChooseActionScreen.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ fun HandleActionBottomSheets(delegate: CreateActionDelegate) {
5555
ChangeFlashlightStrengthActionBottomSheet(delegate)
5656
HttpRequestBottomSheet(delegate)
5757
SmsActionBottomSheet(delegate)
58-
VolumeUpActionBottomSheet(delegate)
59-
VolumeDownActionBottomSheet(delegate)
58+
VolumeActionBottomSheet(delegate)
6059
}
6160

6261
@Composable

base/src/main/java/io/github/sds100/keymapper/base/actions/CreateActionDelegate.kt

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ class CreateActionDelegate(
5454

5555
var httpRequestBottomSheetState: ActionData.HttpRequest? by mutableStateOf(null)
5656
var smsActionBottomSheetState: SmsActionBottomSheetState? by mutableStateOf(null)
57-
var volumeUpActionState: VolumeActionBottomSheetState? by mutableStateOf(null)
58-
var volumeDownActionState: VolumeActionBottomSheetState? by mutableStateOf(null)
57+
var volumeActionState: VolumeActionBottomSheetState? by mutableStateOf(null)
5958

6059
init {
6160
coroutineScope.launch {
@@ -162,26 +161,21 @@ class CreateActionDelegate(
162161
actionResult.update { action }
163162
}
164163

165-
fun onDoneConfigVolumeUpClick() {
166-
volumeUpActionState?.also { state ->
167-
val action = ActionData.Volume.Up(
168-
showVolumeUi = state.showVolumeUi,
169-
volumeStream = state.volumeStream,
170-
)
171-
172-
volumeUpActionState = null
173-
actionResult.update { action }
174-
}
175-
}
176-
177-
fun onDoneConfigVolumeDownClick() {
178-
volumeDownActionState?.also { state ->
179-
val action = ActionData.Volume.Down(
180-
showVolumeUi = state.showVolumeUi,
181-
volumeStream = state.volumeStream,
182-
)
164+
fun onDoneConfigVolumeClick() {
165+
volumeActionState?.also { state ->
166+
val action = when (state.actionId) {
167+
ActionId.VOLUME_UP -> ActionData.Volume.Up(
168+
showVolumeUi = state.showVolumeUi,
169+
volumeStream = state.volumeStream,
170+
)
171+
ActionId.VOLUME_DOWN -> ActionData.Volume.Down(
172+
showVolumeUi = state.showVolumeUi,
173+
volumeStream = state.volumeStream,
174+
)
175+
else -> return
176+
}
183177

184-
volumeDownActionState = null
178+
volumeActionState = null
185179
actionResult.update { action }
186180
}
187181
}
@@ -291,7 +285,8 @@ class CreateActionDelegate(
291285

292286
ActionId.VOLUME_UP -> {
293287
val oldVolumeUpData = oldData as? ActionData.Volume.Up
294-
volumeUpActionState = VolumeActionBottomSheetState(
288+
volumeActionState = VolumeActionBottomSheetState(
289+
actionId = ActionId.VOLUME_UP,
295290
volumeStream = oldVolumeUpData?.volumeStream,
296291
showVolumeUi = oldVolumeUpData?.showVolumeUi ?: false,
297292
)
@@ -300,7 +295,8 @@ class CreateActionDelegate(
300295

301296
ActionId.VOLUME_DOWN -> {
302297
val oldVolumeDownData = oldData as? ActionData.Volume.Down
303-
volumeDownActionState = VolumeActionBottomSheetState(
298+
volumeActionState = VolumeActionBottomSheetState(
299+
actionId = ActionId.VOLUME_DOWN,
304300
volumeStream = oldVolumeDownData?.volumeStream,
305301
showVolumeUi = oldVolumeDownData?.showVolumeUi ?: false,
306302
)

base/src/main/java/io/github/sds100/keymapper/base/actions/VolumeActionBottomSheet.kt

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -47,58 +47,35 @@ import kotlinx.coroutines.launch
4747

4848
@OptIn(ExperimentalMaterial3Api::class)
4949
@Composable
50-
fun VolumeUpActionBottomSheet(delegate: CreateActionDelegate) {
50+
fun VolumeActionBottomSheet(delegate: CreateActionDelegate) {
5151
val scope = rememberCoroutineScope()
5252
val sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)
5353

54-
if (delegate.volumeUpActionState != null) {
55-
VolumeActionBottomSheet(
56-
sheetState = sheetState,
57-
onDismissRequest = {
58-
delegate.volumeUpActionState = null
59-
},
60-
state = delegate.volumeUpActionState!!,
61-
title = stringResource(R.string.action_volume_up),
62-
onSelectStream = {
63-
delegate.volumeUpActionState = delegate.volumeUpActionState?.copy(volumeStream = it)
64-
},
65-
onToggleShowVolumeUi = {
66-
delegate.volumeUpActionState = delegate.volumeUpActionState?.copy(showVolumeUi = it)
67-
},
68-
onDoneClick = {
69-
scope.launch {
70-
sheetState.hide()
71-
delegate.onDoneConfigVolumeUpClick()
72-
}
73-
},
74-
)
75-
}
76-
}
77-
78-
@OptIn(ExperimentalMaterial3Api::class)
79-
@Composable
80-
fun VolumeDownActionBottomSheet(delegate: CreateActionDelegate) {
81-
val scope = rememberCoroutineScope()
82-
val sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)
54+
if (delegate.volumeActionState != null) {
55+
val state = delegate.volumeActionState!!
56+
val title = when (state.actionId) {
57+
ActionId.VOLUME_UP -> stringResource(R.string.action_volume_up)
58+
ActionId.VOLUME_DOWN -> stringResource(R.string.action_volume_down)
59+
else -> ""
60+
}
8361

84-
if (delegate.volumeDownActionState != null) {
8562
VolumeActionBottomSheet(
8663
sheetState = sheetState,
8764
onDismissRequest = {
88-
delegate.volumeDownActionState = null
65+
delegate.volumeActionState = null
8966
},
90-
state = delegate.volumeDownActionState!!,
91-
title = stringResource(R.string.action_volume_down),
67+
state = state,
68+
title = title,
9269
onSelectStream = {
93-
delegate.volumeDownActionState = delegate.volumeDownActionState?.copy(volumeStream = it)
70+
delegate.volumeActionState = delegate.volumeActionState?.copy(volumeStream = it)
9471
},
9572
onToggleShowVolumeUi = {
96-
delegate.volumeDownActionState = delegate.volumeDownActionState?.copy(showVolumeUi = it)
73+
delegate.volumeActionState = delegate.volumeActionState?.copy(showVolumeUi = it)
9774
},
9875
onDoneClick = {
9976
scope.launch {
10077
sheetState.hide()
101-
delegate.onDoneConfigVolumeDownClick()
78+
delegate.onDoneConfigVolumeClick()
10279
}
10380
},
10481
)
@@ -220,6 +197,7 @@ private fun VolumeActionBottomSheet(
220197
}
221198

222199
data class VolumeActionBottomSheetState(
200+
val actionId: ActionId,
223201
val volumeStream: VolumeStream?,
224202
val showVolumeUi: Boolean,
225203
)
@@ -238,6 +216,7 @@ private fun PreviewVolumeActionBottomSheet() {
238216
var state by remember {
239217
mutableStateOf(
240218
VolumeActionBottomSheetState(
219+
actionId = ActionId.VOLUME_UP,
241220
volumeStream = VolumeStream.MUSIC,
242221
showVolumeUi = true,
243222
),
@@ -269,6 +248,7 @@ private fun PreviewVolumeActionBottomSheetDefaultStream() {
269248
var state by remember {
270249
mutableStateOf(
271250
VolumeActionBottomSheetState(
251+
actionId = ActionId.VOLUME_DOWN,
272252
volumeStream = null,
273253
showVolumeUi = false,
274254
),

0 commit comments

Comments
 (0)