Skip to content

Commit

Permalink
Added custom vibration option in LongPopupClickListener
Browse files Browse the repository at this point in the history
Bug fixes in LongPopupClickListener
  • Loading branch information
Prem Suman committed Dec 27, 2018
1 parent 831d4e9 commit 9f71db8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class SelectionActivity : BaseCardActivity() {
blurPopupZoomBtn.onDebouncingClick { pushPopup(SampleBlurPopup.newInstance(R.anim.zoom_in, R.anim.zoom_out)) }

LongPopupClickListener.inside(this)
.withVibration()
.withPopup(SampleBlurPopup.newInstance())
.setOn(longPressBlurPopupBtn)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.prembros.facilis.util

import android.view.*
import android.view.MotionEvent.*
import android.view.View
import com.prembros.facilis.activity.BaseCardActivity
import com.prembros.facilis.dialog.BaseBlurPopup
import org.jetbrains.anko.toast
Expand All @@ -9,27 +10,40 @@ class LongPopupClickListener private constructor(private val activity: BaseCardA

private lateinit var baseBlurPopup: BaseBlurPopup
private var isLongPressStarted: Boolean = false
private var isVibrationEnabled: Boolean = false
private var vibrationDurationMillis: Long = DEFAULT_VIBRATION_DURATION

companion object {
private const val DEFAULT_VIBRATION_DURATION = 50L
fun inside(activity: BaseCardActivity): LongPopupClickListener = LongPopupClickListener(activity)
}

fun withVibration(durationMillis: Long = DEFAULT_VIBRATION_DURATION): LongPopupClickListener {
isVibrationEnabled = true
vibrationDurationMillis = durationMillis
return this
}

fun withPopup(baseBlurPopup: BaseBlurPopup): LongPopupClickListener {
this.baseBlurPopup = baseBlurPopup
return this
}

fun setOn(view: View) {
view.setOnLongClickListener {
isLongPressStarted = true
activity.vibrate(40)
activity.pushPopup(baseBlurPopup)
true
if (!isLongPressStarted) {
isLongPressStarted = true
activity.vibrate(vibrationDurationMillis)
activity.pushPopup(baseBlurPopup)
return@setOnLongClickListener true
} else isLongPressStarted = false
false
}
view.setOnTouchListener { _, event ->
when (event.action) {
MotionEvent.ACTION_UP -> {
ACTION_UP, ACTION_CANCEL, ACTION_OUTSIDE -> {
if (isLongPressStarted) {
isLongPressStarted = false
activity.onBackPressed()
return@setOnTouchListener true
} else activity.toast("Long press for the popup")
Expand Down

0 comments on commit 9f71db8

Please sign in to comment.