Skip to content

Commit

Permalink
Fix BPM zero (#5)
Browse files Browse the repository at this point in the history
This fixes a bug, where the user could set the BPM to zero and cause an
ArythmeticException (division by zero)
  • Loading branch information
Kwasow committed Aug 26, 2023
1 parent 0accff1 commit c9b6c3c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 50 deletions.
12 changes: 6 additions & 6 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 12 additions & 43 deletions app/src/main/java/com/kwasow/musekit/fragments/MetronomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class MetronomeFragment : Fragment() {
isBound = true
metronomeService.connectTicker(binding.sliderBeat)

updateBpmText()
updateBpm()
setupSoundPicker()
}

Expand Down Expand Up @@ -85,51 +85,20 @@ class MetronomeFragment : Fragment() {
}
}

binding.buttonPlus5.setOnClickListener {
if (isBound) {
metronomeService.bpm += 5
updateBpmText()
}
}

binding.buttonPlus2.setOnClickListener {
if (isBound) {
metronomeService.bpm += 2
updateBpmText()
}
}

binding.buttonPlus1.setOnClickListener {
if (isBound) {
metronomeService.bpm += 1
updateBpmText()
}
}

binding.buttonMinus5.setOnClickListener {
if (isBound) {
metronomeService.bpm -= 5
updateBpmText()
}
}

binding.buttonMinus2.setOnClickListener {
if (isBound) {
metronomeService.bpm -= 2
updateBpmText()
}
}
binding.buttonPlus5.setOnClickListener { updateBpm(5) }
binding.buttonPlus2.setOnClickListener { updateBpm(2) }
binding.buttonPlus1.setOnClickListener { updateBpm(1) }

binding.buttonMinus1.setOnClickListener {
if (isBound) {
metronomeService.bpm -= 1
updateBpmText()
}
}
binding.buttonMinus5.setOnClickListener { updateBpm(-5) }
binding.buttonMinus2.setOnClickListener { updateBpm(-2) }
binding.buttonMinus1.setOnClickListener { updateBpm(-1) }
}

private fun updateBpmText() {
if (isBound) binding.textBpm.text = metronomeService.bpm.toString()
private fun updateBpm(by: Int = 0) {
if (isBound) {
metronomeService.bpm += by
binding.textBpm.text = metronomeService.bpm.toString()
}
}

private fun setupSoundPicker() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class MetronomeService : Service(), Runnable {
}
var bpm = 60
set(value) {
field = value.coerceAtMost(300)
field = value.coerceIn(30..300)
}

private var soundId by Delegates.notNull<Int>()
Expand Down

0 comments on commit c9b6c3c

Please sign in to comment.