fretfret

learn the notes on a guitar from the comfort of your android phone
Log | Files | Refs

commit 30eb372f190fcd0153f69f6bde05897f97cc559c
parent 6f3b8e57f3964cfe8a3cfce1cd1fe31bf7c8f78d
Author: massi <mdsiboldi@gmail.com>
Date:   Thu, 11 Jul 2024 14:34:01 -0700

cleanup, constants and magic number management

Diffstat:
Mapp/src/main/java/com/example/fretboardtrainer/MainActivity.kt | 44+++++++++++++++++++++-----------------------
1 file changed, 21 insertions(+), 23 deletions(-)

diff --git a/app/src/main/java/com/example/fretboardtrainer/MainActivity.kt b/app/src/main/java/com/example/fretboardtrainer/MainActivity.kt @@ -69,6 +69,7 @@ data object Constants { "A", "A♯/B♭", "B", "C", "C♯/D♭", "D", "D♯/E♭", "E", "F", "F♯/G♭", "G", "G♯/A♭" ) + const val FRETBOARD_HEIGHT = 200 } val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings") @@ -225,7 +226,6 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { fun toggleSettings() { val show = !uiState.value.showSettings _uiState.update { currentState -> - // TODO: keep note if updated settings allow it? var note = currentState.note var lastNote = currentState.lastNote if (!show && !isValidNote(note)) { @@ -342,7 +342,9 @@ fun StringSettings(settings: List<Boolean>, toggle: (n: Int) -> Unit) { repeat(Constants.STRINGS.size) { n -> Row(verticalAlignment = Alignment.CenterVertically) { Text(Constants.STRINGS[n]) - Checkbox(modifier = Modifier.height((200 / Constants.STRINGS.size).dp), + Checkbox(modifier = Modifier.height( + (Constants.FRETBOARD_HEIGHT / Constants.STRINGS.size).dp + ), checked = settings[n], onCheckedChange = { toggle(n) }) } @@ -360,18 +362,16 @@ fun Fretboard(modifier: Modifier = Modifier, note: List<Int>?) { ) { val lineColor = MaterialTheme.colorScheme.onSurfaceVariant; val dotColor = MaterialTheme.colorScheme.outline; - val openStringColor = dotColor; + val openStringColor = dotColor Row( modifier = modifier .fillMaxWidth() - .height(200.dp) + .height(Constants.FRETBOARD_HEIGHT.dp) ) { repeat(Constants.FRETS.size) { n: Int -> - var weight = 24 - n * 1.0f - if (n == 0) { - weight /= 2 - } - val fretWidth = if (n == 0) 12.dp else 6.dp + val weight = weightForFret(n); + val nutWidth = 12.dp + val fretWidth = if (n == 0) nutWidth else 6.dp Box( modifier = Modifier .fillMaxHeight() @@ -398,12 +398,9 @@ fun Fretboard(modifier: Modifier = Modifier, note: List<Int>?) { } @Composable -fun TopBar() { - Text("TOP BAR"); -} - -@Composable fun Strings(fretIdx: Int, noteAt: Int?, color: Color, openStringColor: Color) { + val noteIndicatorDiameter = 20.dp + val stringThickness = 2.dp Column(modifier = Modifier.fillMaxSize()) { repeat(Constants.STRINGS.size) { n: Int -> Box( @@ -412,14 +409,14 @@ fun Strings(fretIdx: Int, noteAt: Int?, color: Color, openStringColor: Color) { .weight(1.0f), contentAlignment = Alignment.Center ) { Divider( - modifier = Modifier.fillMaxWidth(), thickness = 2.dp, + modifier = Modifier.fillMaxWidth(), thickness = stringThickness, color = if (fretIdx == 0) openStringColor else color ) if (noteAt == n) { Box( modifier = Modifier - .width(20.dp) - .height(20.dp) + .width(noteIndicatorDiameter) + .height(noteIndicatorDiameter) .background( color = color, shape = CircleShape ) @@ -432,6 +429,7 @@ fun Strings(fretIdx: Int, noteAt: Int?, color: Color, openStringColor: Color) { @Composable fun Dots(fretIdx: Int, color: Color) { + val circleDiameter = 10.dp when (fretIdx) { in intArrayOf(3, 5, 7, 9) -> { Box( @@ -439,8 +437,8 @@ fun Dots(fretIdx: Int, color: Color) { ) { Box( modifier = Modifier - .width(10.dp) - .height(10.dp) + .width(circleDiameter) + .height(circleDiameter) .background(color, CircleShape) ) } @@ -456,15 +454,15 @@ fun Dots(fretIdx: Int, color: Color) { ) { Box( modifier = Modifier - .width(10.dp) - .height(10.dp) + .width(circleDiameter) + .height(circleDiameter) .absoluteOffset(y = 16.dp) .background(color, CircleShape) ) Box( modifier = Modifier - .width(10.dp) - .height(10.dp) + .width(circleDiameter) + .height(circleDiameter) .absoluteOffset(y = (-16).dp) .background(color, CircleShape) )