Skip to content

Commit fa34fd5

Browse files
committed
Merge branch 'copilot/refactor-volume-actions' into develop
2 parents 8fb08f3 + 3b4104a commit fa34fd5

File tree

14 files changed

+424
-119
lines changed

14 files changed

+424
-119
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +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+
- #991 Consolidated volume and stream actions.
2021
- #1066 Action to mute/unmute microphone.
2122

2223
## Removed

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

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -98,46 +98,40 @@ sealed class ActionData : Comparable<ActionData> {
9898

9999
@Serializable
100100
sealed class Volume : ActionData() {
101-
sealed class Stream : Volume() {
102-
abstract val volumeStream: VolumeStream
103-
abstract val showVolumeUi: Boolean
101+
@Serializable
102+
data class Up(
103+
val showVolumeUi: Boolean,
104+
val volumeStream: VolumeStream? = null,
105+
) : Volume() {
106+
override val id = ActionId.VOLUME_UP
104107

105108
override fun compareTo(other: ActionData) = when (other) {
106-
is Stream -> compareValuesBy(
109+
is Up -> compareValuesBy(
107110
this,
108111
other,
109-
{ it.id },
112+
{ it.showVolumeUi },
110113
{ it.volumeStream },
111114
)
112-
113115
else -> super.compareTo(other)
114116
}
115-
116-
@Serializable
117-
data class Increase(
118-
override val showVolumeUi: Boolean,
119-
override val volumeStream: VolumeStream,
120-
) : Stream() {
121-
override val id = ActionId.VOLUME_INCREASE_STREAM
122-
}
123-
124-
@Serializable
125-
data class Decrease(
126-
override val showVolumeUi: Boolean,
127-
override val volumeStream: VolumeStream,
128-
) : Stream() {
129-
override val id = ActionId.VOLUME_DECREASE_STREAM
130-
}
131117
}
132118

133119
@Serializable
134-
data class Up(val showVolumeUi: Boolean) : Volume() {
135-
override val id = ActionId.VOLUME_UP
136-
}
137-
138-
@Serializable
139-
data class Down(val showVolumeUi: Boolean) : Volume() {
120+
data class Down(
121+
val showVolumeUi: Boolean,
122+
val volumeStream: VolumeStream? = null,
123+
) : Volume() {
140124
override val id = ActionId.VOLUME_DOWN
125+
126+
override fun compareTo(other: ActionData) = when (other) {
127+
is Down -> compareValuesBy(
128+
this,
129+
other,
130+
{ it.showVolumeUi },
131+
{ it.volumeStream },
132+
)
133+
else -> super.compareTo(other)
134+
}
141135
}
142136

143137
@Serializable

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

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -283,12 +283,13 @@ object ActionDataEntityMapper {
283283
val showVolumeUi =
284284
entity.flags.hasFlag(ActionEntity.ACTION_FLAG_SHOW_VOLUME_UI)
285285

286+
// Convert old stream actions to new volume up/down with stream parameter
286287
when (actionId) {
287288
ActionId.VOLUME_INCREASE_STREAM ->
288-
ActionData.Volume.Stream.Increase(showVolumeUi, stream)
289+
ActionData.Volume.Up(showVolumeUi, stream)
289290

290291
ActionId.VOLUME_DECREASE_STREAM ->
291-
ActionData.Volume.Stream.Decrease(showVolumeUi, stream)
292+
ActionData.Volume.Down(showVolumeUi, stream)
292293

293294
else -> throw Exception("don't know how to create system action for $actionId")
294295
}
@@ -303,9 +304,18 @@ object ActionDataEntityMapper {
303304
val showVolumeUi =
304305
entity.flags.hasFlag(ActionEntity.ACTION_FLAG_SHOW_VOLUME_UI)
305306

307+
// For VOLUME_UP and VOLUME_DOWN, optionally read the stream type
308+
val volumeStream = if (actionId == ActionId.VOLUME_UP || actionId == ActionId.VOLUME_DOWN) {
309+
entity.extras.getData(ActionEntity.EXTRA_STREAM_TYPE).then {
310+
VOLUME_STREAM_MAP.getKey(it)?.success() ?: null.success()
311+
}.valueOrNull()
312+
} else {
313+
null
314+
}
315+
306316
when (actionId) {
307-
ActionId.VOLUME_UP -> ActionData.Volume.Up(showVolumeUi)
308-
ActionId.VOLUME_DOWN -> ActionData.Volume.Down(showVolumeUi)
317+
ActionId.VOLUME_UP -> ActionData.Volume.Up(showVolumeUi, volumeStream)
318+
ActionId.VOLUME_DOWN -> ActionData.Volume.Down(showVolumeUi, volumeStream)
309319
ActionId.VOLUME_TOGGLE_MUTE -> ActionData.Volume.ToggleMute(
310320
showVolumeUi,
311321
)
@@ -734,7 +744,6 @@ object ActionDataEntityMapper {
734744
var flags = 0
735745

736746
val showVolumeUiFlag = when (data) {
737-
is ActionData.Volume.Stream -> data.showVolumeUi
738747
is ActionData.Volume.Up -> data.showVolumeUi
739748
is ActionData.Volume.Down -> data.showVolumeUi
740749
is ActionData.Volume.Mute -> data.showVolumeUi
@@ -919,12 +928,31 @@ object ActionDataEntityMapper {
919928

920929
is ActionData.Volume ->
921930
when (data) {
922-
is ActionData.Volume.Stream -> listOf(
923-
EntityExtra(
924-
ActionEntity.EXTRA_STREAM_TYPE,
925-
VOLUME_STREAM_MAP[data.volumeStream]!!,
926-
),
927-
)
931+
is ActionData.Volume.Up -> buildList {
932+
if (data.volumeStream != null) {
933+
VOLUME_STREAM_MAP[data.volumeStream]?.let { streamValue ->
934+
add(
935+
EntityExtra(
936+
ActionEntity.EXTRA_STREAM_TYPE,
937+
streamValue,
938+
),
939+
)
940+
}
941+
}
942+
}
943+
944+
is ActionData.Volume.Down -> buildList {
945+
if (data.volumeStream != null) {
946+
VOLUME_STREAM_MAP[data.volumeStream]?.let { streamValue ->
947+
add(
948+
EntityExtra(
949+
ActionEntity.EXTRA_STREAM_TYPE,
950+
streamValue,
951+
),
952+
)
953+
}
954+
}
955+
}
928956

929957
else -> emptyList()
930958
}

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/ActionUiHelper.kt

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -114,34 +114,20 @@ class ActionUiHelper(
114114
val string: String
115115

116116
when (action) {
117-
is ActionData.Volume.Stream -> {
118-
val streamString = getString(
119-
VolumeStreamStrings.getLabel(action.volumeStream),
120-
)
121-
117+
is ActionData.Volume.Down -> {
122118
if (action.showVolumeUi) {
123119
hasShowVolumeUiFlag = true
124120
}
125121

126-
string = when (action) {
127-
is ActionData.Volume.Stream.Decrease -> getString(
122+
string = if (action.volumeStream != null) {
123+
val streamString = getString(VolumeStreamStrings.getLabel(action.volumeStream))
124+
getString(
128125
R.string.action_decrease_stream_formatted,
129126
streamString,
130127
)
131-
132-
is ActionData.Volume.Stream.Increase -> getString(
133-
R.string.action_increase_stream_formatted,
134-
streamString,
135-
)
136-
}
137-
}
138-
139-
is ActionData.Volume.Down -> {
140-
if (action.showVolumeUi) {
141-
hasShowVolumeUiFlag = true
128+
} else {
129+
getString(R.string.action_volume_down)
142130
}
143-
144-
string = getString(R.string.action_volume_down)
145131
}
146132

147133
is ActionData.Volume.Mute -> {
@@ -173,7 +159,15 @@ class ActionUiHelper(
173159
hasShowVolumeUiFlag = true
174160
}
175161

176-
string = getString(R.string.action_volume_up)
162+
string = if (action.volumeStream != null) {
163+
val streamString = getString(VolumeStreamStrings.getLabel(action.volumeStream))
164+
getString(
165+
R.string.action_increase_stream_formatted,
166+
streamString,
167+
)
168+
} else {
169+
getString(R.string.action_volume_up)
170+
}
177171
}
178172

179173
ActionData.Volume.CycleRingerMode -> {

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -916,8 +916,6 @@ fun ActionData.isEditable(): Boolean = when (this) {
916916
is ActionData.Volume.Mute,
917917
is ActionData.Volume.UnMute,
918918
is ActionData.Volume.ToggleMute,
919-
is ActionData.Volume.Stream.Increase,
920-
is ActionData.Volume.Stream.Decrease,
921919
is ActionData.Volume.SetRingerMode,
922920
is ActionData.DoNotDisturb.Enable,
923921
is ActionData.DoNotDisturb.Toggle,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ fun HandleActionBottomSheets(delegate: CreateActionDelegate) {
5555
ChangeFlashlightStrengthActionBottomSheet(delegate)
5656
HttpRequestBottomSheet(delegate)
5757
SmsActionBottomSheet(delegate)
58+
VolumeActionBottomSheet(delegate)
5859
}
5960

6061
@Composable

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ class ConfigActionsUseCaseImpl @Inject constructor(
217217
}
218218
}
219219

220-
if (data is ActionData.Volume.Down || data is ActionData.Volume.Up || data is ActionData.Volume.Stream) {
220+
if (data is ActionData.Volume.Down || data is ActionData.Volume.Up) {
221221
repeat = true
222222
}
223223

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ class ConfigActionsViewModel @Inject constructor(
8282
combine(config.keyMap, actionOptionsUid, transform = ::buildOptionsState)
8383
.stateIn(viewModelScope, SharingStarted.Lazily, null)
8484

85+
private var editedActionUid: String? = null
86+
8587
private val actionErrorSnapshot: StateFlow<ActionErrorSnapshot?> =
8688
displayAction.actionErrorSnapshot.stateIn(viewModelScope, SharingStarted.Lazily, null)
8789

@@ -99,7 +101,7 @@ class ConfigActionsViewModel @Inject constructor(
99101

100102
viewModelScope.launch {
101103
createActionDelegate.actionResult.filterNotNull().collect { action ->
102-
val actionUid = actionOptionsUid.value ?: return@collect
104+
val actionUid = editedActionUid ?: return@collect
103105
config.setActionData(actionUid, action)
104106
actionOptionsUid.update { null }
105107
}
@@ -186,6 +188,7 @@ class ConfigActionsViewModel @Inject constructor(
186188
viewModelScope.launch {
187189
// Clear the bottom sheet so navigating back with predicted-back works
188190
actionOptionsUid.update { null }
191+
editedActionUid = actionUid
189192

190193
val keyMap = config.keyMap.first().dataOrNull() ?: return@launch
191194

0 commit comments

Comments
 (0)