diff --git a/app/src/main/java/com/lukas/music/song/chords/Chord.kt b/app/src/main/java/com/lukas/music/song/chords/Chord.kt index db45fdd..b823fbb 100644 --- a/app/src/main/java/com/lukas/music/song/chords/Chord.kt +++ b/app/src/main/java/com/lukas/music/song/chords/Chord.kt @@ -19,6 +19,10 @@ interval = Interval(value) } var interval = Interval(note) + set(value) { + field = value + note = value.distance + } fun getNotes(root: Note): Array { return Array(chordType.notes.size) { root + note + chordType.notes[it] } diff --git a/app/src/main/java/com/lukas/music/song/chords/Chord.kt b/app/src/main/java/com/lukas/music/song/chords/Chord.kt index db45fdd..b823fbb 100644 --- a/app/src/main/java/com/lukas/music/song/chords/Chord.kt +++ b/app/src/main/java/com/lukas/music/song/chords/Chord.kt @@ -19,6 +19,10 @@ interval = Interval(value) } var interval = Interval(note) + set(value) { + field = value + note = value.distance + } fun getNotes(root: Note): Array { return Array(chordType.notes.size) { root + note + chordType.notes[it] } diff --git a/app/src/main/java/com/lukas/music/song/chords/Interval.kt b/app/src/main/java/com/lukas/music/song/chords/Interval.kt index 2726787..d112805 100644 --- a/app/src/main/java/com/lukas/music/song/chords/Interval.kt +++ b/app/src/main/java/com/lukas/music/song/chords/Interval.kt @@ -10,7 +10,7 @@ package com.lukas.music.song.chords -class Interval(private val distance: Int) { +class Interval(val distance: Int) { val name: IntervalName = when (distance) { 0 -> IntervalName.UNISON 1, 2 -> IntervalName.SECOND @@ -32,7 +32,7 @@ return name.toString() } - enum class IntervalName(val distance: Int, val romanVersion: String) { + enum class IntervalName(private val distance: Int, val romanVersion: String) { UNISON(0, "I"), SECOND(1, "II"), THIRD(3, "III"), diff --git a/app/src/main/java/com/lukas/music/song/chords/Chord.kt b/app/src/main/java/com/lukas/music/song/chords/Chord.kt index db45fdd..b823fbb 100644 --- a/app/src/main/java/com/lukas/music/song/chords/Chord.kt +++ b/app/src/main/java/com/lukas/music/song/chords/Chord.kt @@ -19,6 +19,10 @@ interval = Interval(value) } var interval = Interval(note) + set(value) { + field = value + note = value.distance + } fun getNotes(root: Note): Array { return Array(chordType.notes.size) { root + note + chordType.notes[it] } diff --git a/app/src/main/java/com/lukas/music/song/chords/Interval.kt b/app/src/main/java/com/lukas/music/song/chords/Interval.kt index 2726787..d112805 100644 --- a/app/src/main/java/com/lukas/music/song/chords/Interval.kt +++ b/app/src/main/java/com/lukas/music/song/chords/Interval.kt @@ -10,7 +10,7 @@ package com.lukas.music.song.chords -class Interval(private val distance: Int) { +class Interval(val distance: Int) { val name: IntervalName = when (distance) { 0 -> IntervalName.UNISON 1, 2 -> IntervalName.SECOND @@ -32,7 +32,7 @@ return name.toString() } - enum class IntervalName(val distance: Int, val romanVersion: String) { + enum class IntervalName(private val distance: Int, val romanVersion: String) { UNISON(0, "I"), SECOND(1, "II"), THIRD(3, "III"), diff --git a/app/src/main/java/com/lukas/music/ui/fragments/EditChordFragment.kt b/app/src/main/java/com/lukas/music/ui/fragments/EditChordFragment.kt index a311a6c..9415cca 100644 --- a/app/src/main/java/com/lukas/music/ui/fragments/EditChordFragment.kt +++ b/app/src/main/java/com/lukas/music/ui/fragments/EditChordFragment.kt @@ -10,13 +10,10 @@ package com.lukas.music.ui.fragments -import android.R import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.AdapterView -import android.widget.ArrayAdapter import androidx.fragment.app.DialogFragment import com.lukas.music.databinding.FragmentEditChordBinding import com.lukas.music.song.Scale @@ -24,8 +21,10 @@ import com.lukas.music.song.chords.Chord import com.lukas.music.song.chords.ChordType import com.lukas.music.song.chords.Interval +import com.lukas.music.util.setup -class EditChordFragment(val chord: Chord, val songFragment: SongFragment) : DialogFragment() { +class EditChordFragment(val chord: Chord, private val songFragment: SongFragment) : + DialogFragment() { lateinit var binding: FragmentEditChordBinding override fun onCreateView( @@ -45,24 +44,9 @@ val pitches = if (songFragment.displayChordNames) { Array(Scale.MAJOR.steps.size) { (Song.currentSong.root + Scale.MAJOR.steps[it]).noteName.toString() } } else Interval.IntervalName.NAMES - val pitchAdapter = ArrayAdapter( - binding.root.context, - R.layout.simple_spinner_dropdown_item, pitches - ) - binding.pitchSpinner.adapter = pitchAdapter - binding.pitchSpinner.setSelection(chord.interval.name.ordinal) - binding.pitchSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { - override fun onItemSelected( - parent: AdapterView<*>?, - view: View?, - position: Int, - id: Long - ) { - chord.note = Scale.MAJOR.steps[position] - songFragment.updateChords() - } - - override fun onNothingSelected(parent: AdapterView<*>?) {} + binding.pitchSpinner.setup(pitches, chord.interval.name.ordinal) { + chord.note = Scale.MAJOR.steps[it] + songFragment.updateChords() } } @@ -71,32 +55,17 @@ for (chordType in ChordType.VALUES) { values += chordType.toString() } - val modifierAdapter = ArrayAdapter( - binding.root.context, - R.layout.simple_spinner_dropdown_item, values - ) - binding.typeSpinner.adapter = modifierAdapter - binding.typeSpinner.setSelection( - if (chord.chordType == Scale.MAJOR.chordTypes[chord.interval.name.ordinal]) - 0 + binding.typeSpinner.setup( + values, + if (chord.chordType == Scale.MAJOR.chordTypes[chord.interval.name.ordinal]) 0 else chord.chordType.ordinal + 1 - ) - binding.typeSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { - override fun onItemSelected( - parent: AdapterView<*>?, - view: View?, - position: Int, - id: Long - ) { - if (position == 0) { - chord.chordType = Scale.MAJOR.chordTypes[chord.interval.name.ordinal] - } else { - chord.chordType = ChordType.VALUES[position - 1] - } - songFragment.updateChords() + ) { + if (it == 0) { + chord.chordType = Scale.MAJOR.chordTypes[chord.interval.name.ordinal] + } else { + chord.chordType = ChordType.VALUES[it - 1] } - - override fun onNothingSelected(parent: AdapterView<*>?) {} + songFragment.updateChords() } } } \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/song/chords/Chord.kt b/app/src/main/java/com/lukas/music/song/chords/Chord.kt index db45fdd..b823fbb 100644 --- a/app/src/main/java/com/lukas/music/song/chords/Chord.kt +++ b/app/src/main/java/com/lukas/music/song/chords/Chord.kt @@ -19,6 +19,10 @@ interval = Interval(value) } var interval = Interval(note) + set(value) { + field = value + note = value.distance + } fun getNotes(root: Note): Array { return Array(chordType.notes.size) { root + note + chordType.notes[it] } diff --git a/app/src/main/java/com/lukas/music/song/chords/Interval.kt b/app/src/main/java/com/lukas/music/song/chords/Interval.kt index 2726787..d112805 100644 --- a/app/src/main/java/com/lukas/music/song/chords/Interval.kt +++ b/app/src/main/java/com/lukas/music/song/chords/Interval.kt @@ -10,7 +10,7 @@ package com.lukas.music.song.chords -class Interval(private val distance: Int) { +class Interval(val distance: Int) { val name: IntervalName = when (distance) { 0 -> IntervalName.UNISON 1, 2 -> IntervalName.SECOND @@ -32,7 +32,7 @@ return name.toString() } - enum class IntervalName(val distance: Int, val romanVersion: String) { + enum class IntervalName(private val distance: Int, val romanVersion: String) { UNISON(0, "I"), SECOND(1, "II"), THIRD(3, "III"), diff --git a/app/src/main/java/com/lukas/music/ui/fragments/EditChordFragment.kt b/app/src/main/java/com/lukas/music/ui/fragments/EditChordFragment.kt index a311a6c..9415cca 100644 --- a/app/src/main/java/com/lukas/music/ui/fragments/EditChordFragment.kt +++ b/app/src/main/java/com/lukas/music/ui/fragments/EditChordFragment.kt @@ -10,13 +10,10 @@ package com.lukas.music.ui.fragments -import android.R import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.AdapterView -import android.widget.ArrayAdapter import androidx.fragment.app.DialogFragment import com.lukas.music.databinding.FragmentEditChordBinding import com.lukas.music.song.Scale @@ -24,8 +21,10 @@ import com.lukas.music.song.chords.Chord import com.lukas.music.song.chords.ChordType import com.lukas.music.song.chords.Interval +import com.lukas.music.util.setup -class EditChordFragment(val chord: Chord, val songFragment: SongFragment) : DialogFragment() { +class EditChordFragment(val chord: Chord, private val songFragment: SongFragment) : + DialogFragment() { lateinit var binding: FragmentEditChordBinding override fun onCreateView( @@ -45,24 +44,9 @@ val pitches = if (songFragment.displayChordNames) { Array(Scale.MAJOR.steps.size) { (Song.currentSong.root + Scale.MAJOR.steps[it]).noteName.toString() } } else Interval.IntervalName.NAMES - val pitchAdapter = ArrayAdapter( - binding.root.context, - R.layout.simple_spinner_dropdown_item, pitches - ) - binding.pitchSpinner.adapter = pitchAdapter - binding.pitchSpinner.setSelection(chord.interval.name.ordinal) - binding.pitchSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { - override fun onItemSelected( - parent: AdapterView<*>?, - view: View?, - position: Int, - id: Long - ) { - chord.note = Scale.MAJOR.steps[position] - songFragment.updateChords() - } - - override fun onNothingSelected(parent: AdapterView<*>?) {} + binding.pitchSpinner.setup(pitches, chord.interval.name.ordinal) { + chord.note = Scale.MAJOR.steps[it] + songFragment.updateChords() } } @@ -71,32 +55,17 @@ for (chordType in ChordType.VALUES) { values += chordType.toString() } - val modifierAdapter = ArrayAdapter( - binding.root.context, - R.layout.simple_spinner_dropdown_item, values - ) - binding.typeSpinner.adapter = modifierAdapter - binding.typeSpinner.setSelection( - if (chord.chordType == Scale.MAJOR.chordTypes[chord.interval.name.ordinal]) - 0 + binding.typeSpinner.setup( + values, + if (chord.chordType == Scale.MAJOR.chordTypes[chord.interval.name.ordinal]) 0 else chord.chordType.ordinal + 1 - ) - binding.typeSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { - override fun onItemSelected( - parent: AdapterView<*>?, - view: View?, - position: Int, - id: Long - ) { - if (position == 0) { - chord.chordType = Scale.MAJOR.chordTypes[chord.interval.name.ordinal] - } else { - chord.chordType = ChordType.VALUES[position - 1] - } - songFragment.updateChords() + ) { + if (it == 0) { + chord.chordType = Scale.MAJOR.chordTypes[chord.interval.name.ordinal] + } else { + chord.chordType = ChordType.VALUES[it - 1] } - - override fun onNothingSelected(parent: AdapterView<*>?) {} + songFragment.updateChords() } } } \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/ui/fragments/EditInstrumentFragment.kt b/app/src/main/java/com/lukas/music/ui/fragments/EditInstrumentFragment.kt index 67d2ca9..16aeaca 100644 --- a/app/src/main/java/com/lukas/music/ui/fragments/EditInstrumentFragment.kt +++ b/app/src/main/java/com/lukas/music/ui/fragments/EditInstrumentFragment.kt @@ -10,21 +10,19 @@ package com.lukas.music.ui.fragments -import android.R import android.os.Bundle import android.text.Editable import android.text.TextWatcher import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.AdapterView -import android.widget.ArrayAdapter import androidx.fragment.app.DialogFragment import com.lukas.music.databinding.FragmentEditInstrumentBinding import com.lukas.music.instruments.Instrument import com.lukas.music.instruments.Waveform import com.lukas.music.ui.adapters.InstrumentViewHolder import com.lukas.music.util.setup +import com.lukas.music.util.smartSetup class EditInstrumentFragment( private val instrument: Instrument, @@ -48,24 +46,7 @@ viewHolder.instrument = viewHolder.instrument } }) - val adapter = ArrayAdapter( - binding.root.context, - R.layout.simple_spinner_dropdown_item, Waveform.VALUES - ) - binding.waveformSelection.adapter = adapter - binding.waveformSelection.onItemSelectedListener = - object : AdapterView.OnItemSelectedListener { - override fun onItemSelected( - parent: AdapterView<*>?, - view: View?, - position: Int, - id: Long - ) { - instrument.waveform = Waveform.VALUES[position] - } - - override fun onNothingSelected(parent: AdapterView<*>?) {} - } + binding.waveformSelection.smartSetup(Waveform.VALUES, instrument::waveform) binding.volumeSeek.setup(0, 100, 30) { binding.volumeText.text = "volume: $it%" instrument.volume = it.toFloat() / 100f diff --git a/app/src/main/java/com/lukas/music/song/chords/Chord.kt b/app/src/main/java/com/lukas/music/song/chords/Chord.kt index db45fdd..b823fbb 100644 --- a/app/src/main/java/com/lukas/music/song/chords/Chord.kt +++ b/app/src/main/java/com/lukas/music/song/chords/Chord.kt @@ -19,6 +19,10 @@ interval = Interval(value) } var interval = Interval(note) + set(value) { + field = value + note = value.distance + } fun getNotes(root: Note): Array { return Array(chordType.notes.size) { root + note + chordType.notes[it] } diff --git a/app/src/main/java/com/lukas/music/song/chords/Interval.kt b/app/src/main/java/com/lukas/music/song/chords/Interval.kt index 2726787..d112805 100644 --- a/app/src/main/java/com/lukas/music/song/chords/Interval.kt +++ b/app/src/main/java/com/lukas/music/song/chords/Interval.kt @@ -10,7 +10,7 @@ package com.lukas.music.song.chords -class Interval(private val distance: Int) { +class Interval(val distance: Int) { val name: IntervalName = when (distance) { 0 -> IntervalName.UNISON 1, 2 -> IntervalName.SECOND @@ -32,7 +32,7 @@ return name.toString() } - enum class IntervalName(val distance: Int, val romanVersion: String) { + enum class IntervalName(private val distance: Int, val romanVersion: String) { UNISON(0, "I"), SECOND(1, "II"), THIRD(3, "III"), diff --git a/app/src/main/java/com/lukas/music/ui/fragments/EditChordFragment.kt b/app/src/main/java/com/lukas/music/ui/fragments/EditChordFragment.kt index a311a6c..9415cca 100644 --- a/app/src/main/java/com/lukas/music/ui/fragments/EditChordFragment.kt +++ b/app/src/main/java/com/lukas/music/ui/fragments/EditChordFragment.kt @@ -10,13 +10,10 @@ package com.lukas.music.ui.fragments -import android.R import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.AdapterView -import android.widget.ArrayAdapter import androidx.fragment.app.DialogFragment import com.lukas.music.databinding.FragmentEditChordBinding import com.lukas.music.song.Scale @@ -24,8 +21,10 @@ import com.lukas.music.song.chords.Chord import com.lukas.music.song.chords.ChordType import com.lukas.music.song.chords.Interval +import com.lukas.music.util.setup -class EditChordFragment(val chord: Chord, val songFragment: SongFragment) : DialogFragment() { +class EditChordFragment(val chord: Chord, private val songFragment: SongFragment) : + DialogFragment() { lateinit var binding: FragmentEditChordBinding override fun onCreateView( @@ -45,24 +44,9 @@ val pitches = if (songFragment.displayChordNames) { Array(Scale.MAJOR.steps.size) { (Song.currentSong.root + Scale.MAJOR.steps[it]).noteName.toString() } } else Interval.IntervalName.NAMES - val pitchAdapter = ArrayAdapter( - binding.root.context, - R.layout.simple_spinner_dropdown_item, pitches - ) - binding.pitchSpinner.adapter = pitchAdapter - binding.pitchSpinner.setSelection(chord.interval.name.ordinal) - binding.pitchSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { - override fun onItemSelected( - parent: AdapterView<*>?, - view: View?, - position: Int, - id: Long - ) { - chord.note = Scale.MAJOR.steps[position] - songFragment.updateChords() - } - - override fun onNothingSelected(parent: AdapterView<*>?) {} + binding.pitchSpinner.setup(pitches, chord.interval.name.ordinal) { + chord.note = Scale.MAJOR.steps[it] + songFragment.updateChords() } } @@ -71,32 +55,17 @@ for (chordType in ChordType.VALUES) { values += chordType.toString() } - val modifierAdapter = ArrayAdapter( - binding.root.context, - R.layout.simple_spinner_dropdown_item, values - ) - binding.typeSpinner.adapter = modifierAdapter - binding.typeSpinner.setSelection( - if (chord.chordType == Scale.MAJOR.chordTypes[chord.interval.name.ordinal]) - 0 + binding.typeSpinner.setup( + values, + if (chord.chordType == Scale.MAJOR.chordTypes[chord.interval.name.ordinal]) 0 else chord.chordType.ordinal + 1 - ) - binding.typeSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { - override fun onItemSelected( - parent: AdapterView<*>?, - view: View?, - position: Int, - id: Long - ) { - if (position == 0) { - chord.chordType = Scale.MAJOR.chordTypes[chord.interval.name.ordinal] - } else { - chord.chordType = ChordType.VALUES[position - 1] - } - songFragment.updateChords() + ) { + if (it == 0) { + chord.chordType = Scale.MAJOR.chordTypes[chord.interval.name.ordinal] + } else { + chord.chordType = ChordType.VALUES[it - 1] } - - override fun onNothingSelected(parent: AdapterView<*>?) {} + songFragment.updateChords() } } } \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/ui/fragments/EditInstrumentFragment.kt b/app/src/main/java/com/lukas/music/ui/fragments/EditInstrumentFragment.kt index 67d2ca9..16aeaca 100644 --- a/app/src/main/java/com/lukas/music/ui/fragments/EditInstrumentFragment.kt +++ b/app/src/main/java/com/lukas/music/ui/fragments/EditInstrumentFragment.kt @@ -10,21 +10,19 @@ package com.lukas.music.ui.fragments -import android.R import android.os.Bundle import android.text.Editable import android.text.TextWatcher import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.AdapterView -import android.widget.ArrayAdapter import androidx.fragment.app.DialogFragment import com.lukas.music.databinding.FragmentEditInstrumentBinding import com.lukas.music.instruments.Instrument import com.lukas.music.instruments.Waveform import com.lukas.music.ui.adapters.InstrumentViewHolder import com.lukas.music.util.setup +import com.lukas.music.util.smartSetup class EditInstrumentFragment( private val instrument: Instrument, @@ -48,24 +46,7 @@ viewHolder.instrument = viewHolder.instrument } }) - val adapter = ArrayAdapter( - binding.root.context, - R.layout.simple_spinner_dropdown_item, Waveform.VALUES - ) - binding.waveformSelection.adapter = adapter - binding.waveformSelection.onItemSelectedListener = - object : AdapterView.OnItemSelectedListener { - override fun onItemSelected( - parent: AdapterView<*>?, - view: View?, - position: Int, - id: Long - ) { - instrument.waveform = Waveform.VALUES[position] - } - - override fun onNothingSelected(parent: AdapterView<*>?) {} - } + binding.waveformSelection.smartSetup(Waveform.VALUES, instrument::waveform) binding.volumeSeek.setup(0, 100, 30) { binding.volumeText.text = "volume: $it%" instrument.volume = it.toFloat() / 100f diff --git a/app/src/main/java/com/lukas/music/util/UIUtil.kt b/app/src/main/java/com/lukas/music/util/UIUtil.kt index 5bb150a..b033fb0 100644 --- a/app/src/main/java/com/lukas/music/util/UIUtil.kt +++ b/app/src/main/java/com/lukas/music/util/UIUtil.kt @@ -10,8 +10,8 @@ package com.lukas.music.util -import android.widget.Button -import android.widget.SeekBar +import android.view.View +import android.widget.* import androidx.core.content.ContextCompat import com.lukas.music.R import kotlin.reflect.KMutableProperty0 @@ -49,4 +49,63 @@ setBackgroundColor( ContextCompat.getColor(context, if (target.get()) activeColor else R.color.gray_0x60) ) +} + +fun Spinner.setup( + items: Array, + initialIndex: Int, + callback: (Int) -> Unit = {}, +) { + val arrayAdapter = ArrayAdapter( + context, + android.R.layout.simple_spinner_dropdown_item, items + ) + spinnerSetupMain(arrayAdapter, initialIndex, callback) +} + +fun Spinner.setup( + items: List, + initialIndex: Int, + callback: (Int) -> Unit = {}, +) { + val arrayAdapter = ArrayAdapter( + context, + android.R.layout.simple_spinner_dropdown_item, items + ) + spinnerSetupMain(arrayAdapter, initialIndex, callback) +} + +private fun Spinner.spinnerSetupMain( + arrayAdapter: ArrayAdapter, + initialIndex: Int, + callback: (Int) -> Unit +) { + adapter = arrayAdapter + setSelection(initialIndex) + onItemSelectedListener = object : AdapterView.OnItemSelectedListener { + override fun onItemSelected( + parent: AdapterView<*>?, + view: View?, + position: Int, + id: Long + ) { + callback(position) + } + + override fun onNothingSelected(parent: AdapterView<*>?) {} + } +} + +fun > Spinner.smartSetup( + items: Array, + target: KMutableProperty0, + callback: (Int) -> Unit = {} +) { + val arrayAdapter = ArrayAdapter( + context, + android.R.layout.simple_spinner_dropdown_item, items + ) + spinnerSetupMain(arrayAdapter, target.get().ordinal) { + target.set(items[it]) + } } \ No newline at end of file