rely on gestureDetector at recognizing weekly view clicks

This commit is contained in:
tibbi 2020-03-22 19:50:39 +01:00
parent c9bde11ca4
commit dcbcaaf789
1 changed files with 30 additions and 34 deletions

View File

@ -5,10 +5,7 @@ import android.content.res.Resources
import android.graphics.drawable.ColorDrawable import android.graphics.drawable.ColorDrawable
import android.os.Bundle import android.os.Bundle
import android.util.Range import android.util.Range
import android.view.LayoutInflater import android.view.*
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView import android.widget.ImageView
import android.widget.RelativeLayout import android.widget.RelativeLayout
import android.widget.TextView import android.widget.TextView
@ -37,14 +34,12 @@ import org.joda.time.Days
import java.util.* import java.util.*
class WeekFragment : Fragment(), WeeklyCalendar { class WeekFragment : Fragment(), WeeklyCalendar {
private val CLICK_DURATION_THRESHOLD = 150
private val PLUS_FADEOUT_DELAY = 5000L private val PLUS_FADEOUT_DELAY = 5000L
var listener: WeekFragmentListener? = null var listener: WeekFragmentListener? = null
private var weekTimestamp = 0L private var weekTimestamp = 0L
private var rowHeight = 0f private var rowHeight = 0f
private var todayColumnIndex = -1 private var todayColumnIndex = -1
private var clickStartTime = 0L
private var primaryColor = 0 private var primaryColor = 0
private var lastHash = 0 private var lastHash = 0
private var mWasDestroyed = false private var mWasDestroyed = false
@ -166,46 +161,47 @@ class WeekFragment : Fragment(), WeeklyCalendar {
(0..6).map { getColumnWithId(it) } (0..6).map { getColumnWithId(it) }
.forEachIndexed { index, layout -> .forEachIndexed { index, layout ->
layout.removeAllViews() layout.removeAllViews()
val gestureDetector = getViewGestureDetector(layout, index)
layout.setOnTouchListener { view, motionEvent -> layout.setOnTouchListener { view, motionEvent ->
checkGridClick(motionEvent, index, layout) gestureDetector.onTouchEvent(motionEvent)
true true
} }
} }
} }
private fun checkGridClick(event: MotionEvent, index: Int, view: ViewGroup) { private fun getViewGestureDetector(view: ViewGroup, index: Int): GestureDetector {
when (event.action) { val gestureDetector = GestureDetector(context, object : GestureDetector.SimpleOnGestureListener() {
MotionEvent.ACTION_DOWN -> clickStartTime = System.currentTimeMillis() override fun onSingleTapUp(event: MotionEvent): Boolean {
MotionEvent.ACTION_UP -> { selectedGrid?.animation?.cancel()
if (System.currentTimeMillis() - clickStartTime < CLICK_DURATION_THRESHOLD) { selectedGrid?.beGone()
selectedGrid?.animation?.cancel()
selectedGrid?.beGone()
val hour = (event.y / rowHeight).toInt() val hour = (event.y / rowHeight).toInt()
selectedGrid = (inflater.inflate(R.layout.week_grid_item, null, false) as ImageView).apply { selectedGrid = (inflater.inflate(R.layout.week_grid_item, null, false) as ImageView).apply {
view.addView(this) view.addView(this)
background = ColorDrawable(primaryColor) background = ColorDrawable(primaryColor)
layoutParams.width = view.width layoutParams.width = view.width
layoutParams.height = rowHeight.toInt() layoutParams.height = rowHeight.toInt()
y = hour * rowHeight y = hour * rowHeight
applyColorFilter(primaryColor.getContrastColor()) applyColorFilter(primaryColor.getContrastColor())
setOnClickListener { setOnClickListener {
val timestamp = weekTimestamp + index * DAY_SECONDS + hour * 60 * 60 val timestamp = weekTimestamp + index * DAY_SECONDS + hour * 60 * 60
Intent(context, EventActivity::class.java).apply { Intent(context, EventActivity::class.java).apply {
putExtra(NEW_EVENT_START_TS, timestamp) putExtra(NEW_EVENT_START_TS, timestamp)
putExtra(NEW_EVENT_SET_HOUR_DURATION, true) putExtra(NEW_EVENT_SET_HOUR_DURATION, true)
startActivity(this) startActivity(this)
}
}
animate().setStartDelay(PLUS_FADEOUT_DELAY).alpha(0f).withEndAction {
beGone()
} }
} }
animate().setStartDelay(PLUS_FADEOUT_DELAY).alpha(0f).withEndAction {
beGone()
}
} }
return super.onSingleTapUp(event)
} }
} })
return gestureDetector
} }
override fun updateWeeklyCalendar(events: ArrayList<Event>) { override fun updateWeeklyCalendar(events: ArrayList<Event>) {