commit 964c199e30b93cec4850bee9a81383ee142e52c1
parent d5e0150dad4d44133476b2e43675b593cc610210
Author: massi <mdsiboldi@gmail.com>
Date: Thu, 11 Jul 2024 14:20:17 -0700
collect consts
Diffstat:
1 file changed, 27 insertions(+), 35 deletions(-)
diff --git a/app/src/main/java/com/example/fretboardtrainer/MainActivity.kt b/app/src/main/java/com/example/fretboardtrainer/MainActivity.kt
@@ -59,8 +59,17 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
-val FRET_COUNT = 13; // open + 12 frets
-val STRING_COUNT = 6;
+data object Constants {
+ val STRINGS = listOf("E", "B", "G", "D", "A", "E");
+ val FRETS = listOf(
+ "open", "1", "2", "3", "4", "5", "6",
+ "7", "8", "9", "10", "11", "12"
+ );
+ val NOTES = listOf(
+ "A", "A♯/B♭", "B", "C", "C♯/D♭", "D",
+ "D♯/E♭", "E", "F", "F♯/G♭", "G", "G♯/A♭"
+ )
+}
val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")
@@ -90,33 +99,17 @@ fun weightForFret(fretIdx: Int): Float {
return weight;
}
-val NOTES = arrayOf(
- "A", "A♯/B♭", "B", "C", "C♯/D♭", "D", "D♯/E♭", "E", "F", "F♯/G♭", "G", "G♯/A♭"
-)
-val STRINGS = arrayOf("E", "B", "G", "D", "A", "E");
-val FRETS = arrayOf(
- "open", "1", "2", "3", "4", "5",
- "6", "7", "8", "9", "10", "11", "12"
-);
fun noteName(note: List<Int>): String {
- val startIdx = when (note[1]) {
- 0 -> 7 // E
- 1 -> 2 // B
- 2 -> 10 // G
- 3 -> 5 // D
- 4 -> 0 // A
- 5 -> 7 // E
- else -> -1
- }
- return NOTES[(startIdx + note[0]) % NOTES.size]
+ val startIdx = Constants.NOTES.indexOf(Constants.STRINGS[note[1]])
+ return Constants.NOTES[(startIdx + note[0]) % Constants.NOTES.size]
}
// TODO: persist string and fret settings across app launches.
data class FretfretState(
val showSettings: Boolean = false,
- val stringSettings: List<Boolean> = List(STRING_COUNT) { true },
- val fretSettings: List<Boolean> = List(FRET_COUNT) { true },
+ val stringSettings: List<Boolean> = List(Constants.STRINGS.size) { true },
+ val fretSettings: List<Boolean> = List(Constants.FRETS.size) { true },
val note: List<Int>? = null,
val lastNote: List<Int>? = null,
)
@@ -124,8 +117,7 @@ data class FretfretState(
val STRING_KEY = byteArrayPreferencesKey("string_key")
val FRET_KEY = byteArrayPreferencesKey("fret_key")
-
-class FretfretViewModel(application: Application) : AndroidViewModel(application) {
+class MainViewModel(application: Application) : AndroidViewModel(application) {
private val _uiState = MutableStateFlow(FretfretState())
val uiState = _uiState.asStateFlow()
@@ -137,13 +129,13 @@ class FretfretViewModel(application: Application) : AndroidViewModel(application
stringSettings = application.dataStore.data.map { prefs ->
when (prefs[STRING_KEY]) {
is ByteArray -> decodeBoolList(prefs[STRING_KEY]!!)
- else -> List(STRING_COUNT) { true }
+ else -> List(Constants.STRINGS.size) { true }
}
}.first(),
fretSettings = application.dataStore.data.map { prefs ->
when (prefs[FRET_KEY]) {
is ByteArray -> decodeBoolList(prefs[FRET_KEY]!!)
- else -> List(FRET_COUNT) { true }
+ else -> List(Constants.FRETS.size) { true }
}
}.first(),
)
@@ -166,7 +158,7 @@ class FretfretViewModel(application: Application) : AndroidViewModel(application
if (!res.any { it }) {
currentState
}
- else {
+ else {
currentState.copy(
stringSettings = res.toList()
)
@@ -263,7 +255,7 @@ class MainActivity : ComponentActivity() {
@OptIn(ExperimentalMaterial3Api::class)
@Composable
-fun Fretfret(viewModel: FretfretViewModel = viewModel()) {
+fun Fretfret(viewModel: MainViewModel = viewModel()) {
val uiState by viewModel.uiState.collectAsState()
FretboardTrainerTheme {
Scaffold(
@@ -303,7 +295,7 @@ fun Fretfret(viewModel: FretfretViewModel = viewModel()) {
)
if (uiState.showSettings) {
Row(horizontalArrangement = Arrangement.SpaceBetween) {
- repeat(FRET_COUNT) { n ->
+ repeat(Constants.FRETS.size) { n ->
Column(
modifier = Modifier
.weight(weightForFret(n)),
@@ -313,7 +305,7 @@ fun Fretfret(viewModel: FretfretViewModel = viewModel()) {
.padding(end = 15.dp, start = 12.dp),
checked = uiState.fretSettings[n],
onCheckedChange = { viewModel.toggleFret(n) })
- Text(FRETS[n])
+ Text(Constants.FRETS[n])
}
}
}
@@ -352,10 +344,10 @@ fun Fretfret(viewModel: FretfretViewModel = viewModel()) {
@Composable
fun StringSettings(settings: List<Boolean>, toggle: (n: Int) -> Unit) {
Column {
- repeat(STRING_COUNT) { n ->
+ repeat(Constants.STRINGS.size) { n ->
Row(verticalAlignment = Alignment.CenterVertically) {
- Text(STRINGS[n])
- Checkbox(modifier = Modifier.height((200 / STRING_COUNT).dp),
+ Text(Constants.STRINGS[n])
+ Checkbox(modifier = Modifier.height((200 / Constants.STRINGS.size).dp),
checked = settings[n],
onCheckedChange = { toggle(n) })
}
@@ -379,7 +371,7 @@ fun Fretboard(modifier: Modifier = Modifier, note: List<Int>?) {
.fillMaxWidth()
.height(200.dp)
) {
- repeat(FRET_COUNT) { n: Int ->
+ repeat(Constants.FRETS.size) { n: Int ->
var weight = 24 - n * 1.0f
if (n == 0) {
weight /= 2
@@ -418,7 +410,7 @@ fun TopBar() {
@Composable
fun Strings(fretIdx: Int, noteAt: Int?, color: Color, openStringColor: Color) {
Column(modifier = Modifier.fillMaxSize()) {
- repeat(STRING_COUNT) { n: Int ->
+ repeat(Constants.STRINGS.size) { n: Int ->
Box(
modifier = Modifier
.fillMaxWidth()