Skip to content

Commit

Permalink
Tap tempo on button press
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr0oked committed Jun 24, 2023
1 parent b5d5945 commit fb21865
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions app/src/main/java/com/bobek/metronome/MetronomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.bobek.metronome

import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
Expand Down Expand Up @@ -60,12 +61,34 @@ class MetronomeFragment : Fragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initViews()
initViewModel()
initBinding()
registerTickReceiver()
setupMenu()
}

@SuppressLint("ClickableViewAccessibility")
private fun initViews() {
binding.content.tapTempoButton.setOnTouchListener { view, event -> tapTempoOnTouchListener(view, event) }
}

private fun tapTempoOnTouchListener(view: View, event: MotionEvent): Boolean {
when (event.action) {
MotionEvent.ACTION_DOWN -> {
view.isPressed = true
view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY)
view.performClick()
}

MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
view.isPressed = false
}
}

return true
}

private fun initViewModel() {
viewModel.playing.observe(viewLifecycleOwner) { playing ->
if (playing) keepScreenOn() else doNotKeepScreenOn()
Expand Down Expand Up @@ -155,6 +178,7 @@ class MetronomeFragment : Fragment() {
showSettings()
true
}

else -> false
}
}
Expand Down

0 comments on commit fb21865

Please sign in to comment.