commit c2494b8256b8ee80088709e1c991d52a21bf3e04
parent 70dcfe08e8a5d7cbacb065d030c585c152db78c7
Author: massi <mdsiboldi@gmail.com>
Date: Thu, 11 Jul 2024 14:40:26 -0700
note dataclass
Diffstat:
1 file changed, 18 insertions(+), 13 deletions(-)
diff --git a/app/src/main/java/com/example/fretboardtrainer/MainActivity.kt b/app/src/main/java/com/example/fretboardtrainer/MainActivity.kt
@@ -101,18 +101,23 @@ fun weightForFret(fretIdx: Int): Float {
}
-fun noteName(note: List<Int>): String {
- val startIdx = Constants.NOTES.indexOf(Constants.STRINGS[note[1]])
- return Constants.NOTES[(startIdx + note[0]) % Constants.NOTES.size]
+fun noteName(note: Note): String {
+ val startIdx = Constants.NOTES.indexOf(Constants.STRINGS[note.string])
+ return Constants.NOTES[(startIdx + note.fret) % Constants.NOTES.size]
}
+data class Note(
+ val string: Int,
+ val fret: Int
+)
+
// TODO: persist string and fret settings across app launches.
data class FretfretState(
val showSettings: Boolean = false,
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,
+ val note: Note? = null,
+ val lastNote: Note? = null,
)
val STRING_KEY = byteArrayPreferencesKey("string_key")
@@ -182,15 +187,15 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
viewModelScope.launch { updateSettings() }
}
- private fun pickNote(): List<Int> {
+ private fun pickNote(): Note {
// find all true values in settings
// randomly pick one of those
- val notes = mutableSetOf<List<Int>>();
+ val notes = mutableSetOf<Note>();
for (stringIdx in uiState.value.stringSettings.indices) {
if (uiState.value.stringSettings[stringIdx]) {
for (fretIdx in uiState.value.fretSettings.indices) {
if (uiState.value.fretSettings[fretIdx]) {
- notes.add(listOf(fretIdx, stringIdx));
+ notes.add(Note(fret = fretIdx, string = stringIdx));
}
}
}
@@ -218,9 +223,9 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
}
}
- private fun isValidNote(note: List<Int>?): Boolean {
- return note != null && uiState.value.stringSettings[note[0]] &&
- uiState.value.fretSettings[note[1]]
+ private fun isValidNote(note: Note?): Boolean {
+ return note != null && uiState.value.stringSettings[note.string] &&
+ uiState.value.fretSettings[note.fret]
}
fun toggleSettings() {
@@ -353,7 +358,7 @@ fun StringSettings(settings: List<Boolean>, toggle: (n: Int) -> Unit) {
}
@Composable
-fun Fretboard(modifier: Modifier = Modifier, note: List<Int>?) {
+fun Fretboard(modifier: Modifier = Modifier, note: Note?) {
Surface(
color = MaterialTheme.colorScheme.surfaceVariant,
tonalElevation = 0.dp,
@@ -380,7 +385,7 @@ fun Fretboard(modifier: Modifier = Modifier, note: List<Int>?) {
Dots(n, color = dotColor)
Strings(
n,
- noteAt = if (note != null && n == note[0]) note[1] else null,
+ noteAt = if (note != null && n == note.fret) note.string else null,
color = lineColor,
openStringColor = openStringColor
)