Skip to content

Commit

Permalink
1.0.10
Browse files Browse the repository at this point in the history
+ Fully custom controls

Took 1 hour 27 minutes
  • Loading branch information
JulesPvx committed May 30, 2024
1 parent e6ec107 commit 1d3107f
Show file tree
Hide file tree
Showing 8 changed files with 464 additions and 28 deletions.
89 changes: 89 additions & 0 deletions app/src/main/java/fr/angel/soundtap/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ import fr.angel.soundtap.data.enums.HapticFeedbackLevel
import fr.angel.soundtap.data.enums.WorkingMode
import fr.angel.soundtap.data.models.BottomSheetState
import fr.angel.soundtap.data.settings.customization.ControlMediaAction
import fr.angel.soundtap.data.settings.customization.CustomControlMediaAction
import fr.angel.soundtap.data.settings.customization.CustomizationSettings
import fr.angel.soundtap.data.settings.customization.MediaAction
import fr.angel.soundtap.data.settings.settings.AppSettings
import fr.angel.soundtap.data.settings.stats.StatsSettings
import fr.angel.soundtap.navigation.Screens
import fr.angel.soundtap.service.SleepTimerService
import fr.angel.soundtap.service.SoundTapAccessibilityService
import javax.inject.Inject
import kotlin.random.Random
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
Expand Down Expand Up @@ -263,4 +266,90 @@ class MainViewModel
),
)
}

fun toggleControlMediaAction(action: CustomControlMediaAction) {
viewModelScope.launch {
customizationSettingsDataStore.updateData { settings ->
val newAction = action.copy(enabled = !action.enabled)
settings.copy(
customMediaActions =
settings.customMediaActions.map {
if (it.id == action.id) newAction else it
},
)
}
}
}

fun changeControlMediaAction(controlMediaAction: CustomControlMediaAction) {
showBottomSheet(
BottomSheetState.EditControlMediaAction(
displayName = "Edit custom control action",
onSetAction = { newAction ->
viewModelScope.launch {
customizationSettingsDataStore.updateData { settings ->
val newControlMediaAction = controlMediaAction.copy(action = newAction)
settings.copy(
customMediaActions =
settings.customMediaActions.map {
if (it.id == controlMediaAction.id) newControlMediaAction else it
},
)
}
}
},
),
)
}

fun addCustomControlMediaAction() {
viewModelScope.launch {
customizationSettingsDataStore.updateData { settings ->
val newAction =
CustomControlMediaAction(
id = settings.customMediaActions.size + 100 * Random.nextInt(),
action = MediaAction.NEXT,
enabled = false,
)
settings.copy(
customMediaActions =
settings.customMediaActions.toMutableList().apply {
add(newAction)
},
)
}
}
}

fun removeCustomControlMediaAction(action: CustomControlMediaAction) {
viewModelScope.launch {
customizationSettingsDataStore.updateData { settings ->
settings.copy(
customMediaActions = settings.customMediaActions.filter { it.id != action.id },
)
}
}
}

fun editCustomControlMediaActionSequence(controlMediaAction: CustomControlMediaAction) {
showBottomSheet(
BottomSheetState.EditControlMediaActionSequence(
displayName = "Edit custom control action sequence",
customControlMediaAction = controlMediaAction,
onSetSequence = { newSequence ->
viewModelScope.launch {
customizationSettingsDataStore.updateData { settings ->
val newControlMediaAction = controlMediaAction.copy(eventsSequenceList = newSequence)
settings.copy(
customMediaActions =
settings.customMediaActions.map {
if (it.id == controlMediaAction.id) newControlMediaAction else it
},
)
}
}
},
),
)
}
}
143 changes: 143 additions & 0 deletions app/src/main/java/fr/angel/soundtap/data/models/BottomSheetState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,41 @@

package fr.angel.soundtap.data.models

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.rounded.ArrowForward
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.FilledTonalButton
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.toMutableStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import fr.angel.soundtap.data.settings.customization.CustomControlMediaAction
import fr.angel.soundtap.data.settings.customization.HardwareButtonsEvent
import fr.angel.soundtap.data.settings.customization.MediaAction

sealed class BottomSheetState(
Expand Down Expand Up @@ -143,4 +163,127 @@ sealed class BottomSheetState(
}
},
)

@OptIn(ExperimentalLayoutApi::class)
data class EditControlMediaActionSequence(
override val displayName: String? = "Edit Action Sequence",
override val onDismiss: (() -> Unit)? = null,
val customControlMediaAction: CustomControlMediaAction,
val onSetSequence: (List<HardwareButtonsEvent>) -> Unit,
) : BottomSheetState(
displayName = displayName,
content = { state, hide ->
val sequence = remember { customControlMediaAction.eventsSequenceList.toMutableStateList() }

Column(
modifier =
Modifier
.fillMaxWidth()
.verticalScroll(rememberScrollState()),
verticalArrangement = Arrangement.spacedBy(16.dp),
) {
Text(
text = "Create a sequence of hardware button events to trigger the action",
style = MaterialTheme.typography.labelMedium,
)
FlowRow(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(4.dp),
verticalArrangement = Arrangement.spacedBy(4.dp),
) {
HardwareButtonsEvent.entries.forEach { event ->
Box(
modifier =
Modifier
.size(48.dp)
.clip(MaterialTheme.shapes.medium)
.background(MaterialTheme.colorScheme.surfaceContainerHigh)
.clickable(onClick = { sequence.add(event) }),
contentAlignment = Alignment.Center,
) {
Icon(
imageVector = event.icon,
contentDescription = null,
)
}
}
}

HorizontalDivider()

Text(
text = "The current sequence is:",
style = MaterialTheme.typography.labelMedium,
)
FlowRow(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(4.dp),
verticalArrangement = Arrangement.spacedBy(4.dp),
) {
if (sequence.isEmpty()) {
Text(
text = "No events in the sequence",
style = MaterialTheme.typography.bodyMedium,
fontWeight = FontWeight.Bold,
)
}
sequence.forEachIndexed { index, event ->
Row(
horizontalArrangement = Arrangement.spacedBy(4.dp),
verticalAlignment = Alignment.CenterVertically,
) {
if (index != 0) {
Icon(
modifier = Modifier.size(16.dp),
imageVector = Icons.AutoMirrored.Rounded.ArrowForward,
contentDescription = null,
)
}
Box(
modifier =
Modifier
.size(48.dp)
.clip(MaterialTheme.shapes.medium)
.background(MaterialTheme.colorScheme.surfaceContainerHigh)
.clickable(onClick = { sequence.remove(event) }),
contentAlignment = Alignment.Center,
) {
Icon(
imageVector = event.icon,
contentDescription = null,
)
}
}
}
}

Row(
modifier =
Modifier
.fillMaxWidth()
.padding(top = 16.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp),
) {
FilledTonalButton(
onClick = {
hide()
state.onDismiss?.invoke()
},
) {
Text("Cancel")
}
Button(
modifier = Modifier.weight(1f),
onClick = {
hide()
onSetSequence(sequence)
state.onDismiss?.invoke()
},
) {
Text("Done")
}
}
}
},
)
}
27 changes: 14 additions & 13 deletions app/src/main/java/fr/angel/soundtap/data/models/Song.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
/*
* Copyright 2024 Angel Studio
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* * Copyright (c) 2024 Angel Studio
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package fr.angel.soundtap.data.models

Expand Down Expand Up @@ -60,7 +62,6 @@ data class Song(
fun isSimilar(song: Song): Boolean {
return title == song.title &&
artist == song.artist &&
album == song.album &&
duration == song.duration
album == song.album
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package fr.angel.soundtap.data.settings.customization

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Block
import androidx.compose.material.icons.rounded.FastForward
import androidx.compose.material.icons.rounded.FastRewind
import androidx.compose.material.icons.rounded.KeyboardArrowDown
Expand Down Expand Up @@ -82,6 +83,7 @@ data class CustomizationSettings(
enabled = true,
action = MediaAction.PLAY_PAUSE,
),
val customMediaActions: List<CustomControlMediaAction> = listOf(),
) {
companion object {
const val ACTION_PLAY_PAUSE = "play_pause"
Expand All @@ -100,6 +102,7 @@ enum class MediaAction(
val icon: ImageVector,
val title: String,
) {
NONE("", Icons.Rounded.Block, "None"),
PLAY_PAUSE(ACTION_PLAY_PAUSE, Icons.Rounded.ToggleOn, "Play/Pause"),
NEXT(ACTION_NEXT, Icons.Rounded.SkipNext, "Next"),
PREVIOUS(ACTION_PREVIOUS, Icons.Rounded.SkipPrevious, "Previous"),
Expand All @@ -110,6 +113,15 @@ enum class MediaAction(
PAUSE(ACTION_PAUSE, Icons.Rounded.Pause, "Pause"),
}

enum class HardwareButtonsEvent(
val title: String,
val icon: ImageVector,
) {
VOLUME_UP("Volume up", Icons.Rounded.KeyboardArrowUp),
VOLUME_DOWN("Volume down", Icons.Rounded.KeyboardArrowDown),
BOTH_VOLUME("Both volume", Icons.Rounded.UnfoldMoreDouble),
}

@Serializable
data class ControlMediaAction(
val id: Int,
Expand All @@ -118,3 +130,12 @@ data class ControlMediaAction(
val action: MediaAction,
@Transient val icon: ImageVector? = null,
)

@Serializable
data class CustomControlMediaAction(
val id: Int,
val enabled: Boolean,
val action: MediaAction,
@Transient val icon: ImageVector? = null,
val eventsSequenceList: List<HardwareButtonsEvent> = listOf(),
)
Loading

0 comments on commit 1d3107f

Please sign in to comment.