Skip to content

Commit

Permalink
Version 0.3.0
Browse files Browse the repository at this point in the history
- Added ability to passthrough touch events
- Changed OnEnd trigger to be before view is removed
- Added the ability to not show start animation aka duration 0
- Added debounce click listener
  • Loading branch information
NicholasMata committed Sep 20, 2021
1 parent 522e30c commit 0f8ae3f
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 38 deletions.
16 changes: 0 additions & 16 deletions .idea/codeStyles/Project.xml

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

3 changes: 1 addition & 2 deletions .idea/gradle.xml

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

8 changes: 8 additions & 0 deletions .idea/misc.xml

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

12 changes: 0 additions & 12 deletions .idea/runConfigurations.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.lifecycle.ViewModelProviders
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
Expand All @@ -14,8 +15,10 @@ import com.matadesigns.spotlight.SpotlightBuilder
import com.matadesigns.spotlight.abstraction.SpotlightListener
import com.matadesigns.spotlight.config.SpotlightDismissType
import com.matadesigns.spotlight.config.SpotlightMessageGravity
import com.matadesigns.spotlightandroid.MainActivity
import com.matadesigns.spotlightandroid.R
import kotlinx.android.synthetic.main.main_fragment.view.*
import com.matadesigns.spotlight.utils.*

class MainFragment : Fragment() {

Expand Down Expand Up @@ -82,9 +85,11 @@ class MainFragment : Fragment() {
topLeft -> {
builder
.setTargetView(topRight)
.setPassThrough(true)
}
topRight -> {
builder
.setPassThrough(false)
.setTitle("PULL DOWN")
.setDescription("to refresh the value")
.setMessageLayout(R.layout.spotlight_skippable_message_view)
Expand All @@ -106,7 +111,7 @@ class MainFragment : Fragment() {
.setTargetView(center)
}
}
builder.build().startSpotlight()
builder.build().startSpotlight(animate = false)
}

override fun onStart(targetView: View?) {
Expand All @@ -121,7 +126,11 @@ class MainFragment : Fragment() {
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProvider(this).get(MainViewModel::class.java)
// TODO: Use the ViewModel
requireView().message_top_right.isClickable = true
requireView().message_top_right.setOnClickListenerWithDebounce({
Log.i("DebounceOnClickListener", "Clicked")
builder.current?.endSpotlight()
})
}

}
2 changes: 1 addition & 1 deletion spotlight/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ android {
minSdkVersion 24
targetSdkVersion 30
versionCode 3
versionName "0.2.9"
versionName "0.3.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ open class SpotlightBuilder(private var context: Context) {
private var _title: CharSequence = ""
private var _description: CharSequence = ""
private var _styler: SpotlightStyler = SimpleStyler(context)
private var _passThrough = false
public var current: SpotlightView? = null
private set

fun setTitle(text: CharSequence): SpotlightBuilder {
_title = text
Expand All @@ -39,6 +42,11 @@ open class SpotlightBuilder(private var context: Context) {
return this
}

fun setPassThrough(passThrough: Boolean): SpotlightBuilder {
_passThrough = passThrough
return this
}

fun setInset(size: Int): SpotlightBuilder {
val actualSize = (size * context.resources.displayMetrics.density).toInt()
this._insetTop = actualSize
Expand Down Expand Up @@ -79,7 +87,8 @@ open class SpotlightBuilder(private var context: Context) {
}

fun build(): SpotlightView {
return SpotlightView(context).also {
val current = SpotlightView(context).also {
it.passThrough = this._passThrough
it.dismissType = this._dismissType
it.styler = this._styler
it.listener = this._listener
Expand All @@ -94,5 +103,7 @@ open class SpotlightBuilder(private var context: Context) {
it.title = this._title
it.description = this._description
}
this.current = current
return current
}
}
18 changes: 14 additions & 4 deletions spotlight/src/main/java/com/matadesigns/spotlight/SpotlightView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import android.widget.FrameLayout
import android.widget.TextView
import androidx.annotation.LayoutRes
import androidx.core.graphics.toRect
import androidx.core.graphics.toRectF
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import com.matadesigns.spotlight.abstraction.*
Expand All @@ -33,7 +34,6 @@ import com.matadesigns.spotlight.utils.SpotlightMath
open class SpotlightView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

var backgroundPaint = Paint()
protected set
var targetPaint = Paint(Paint.ANTI_ALIAS_FLAG)
Expand Down Expand Up @@ -115,6 +115,7 @@ open class SpotlightView @JvmOverloads constructor(
}
var listener: SpotlightListener? = null
var dismissType: SpotlightDismissType = SpotlightDismissType.targetView
var passThrough = false
var startAnimation: Animation = AlphaAnimation(0.0f, 1.0f).also {
it.duration = 400
it.fillAfter = true
Expand Down Expand Up @@ -202,10 +203,10 @@ open class SpotlightView @JvmOverloads constructor(
}
}

fun startSpotlight(viewGroup: ViewGroup? = null) {
fun startSpotlight(viewGroup: ViewGroup? = null, animate: Boolean = true) {
layoutViews()

if(viewGroup != null) {
if (viewGroup != null) {
viewGroup.addView(this)
} else {
val context = this.context
Expand All @@ -221,15 +222,18 @@ open class SpotlightView @JvmOverloads constructor(
}
}
}
if (!animate) {
startAnimation.duration = 0
}
this.startAnimation(startAnimation)
listener?.onStart(targetView)
}

fun endSpotlight() {
listener?.onEnd(targetView)
(messageView as? SpotlightMessage)?.spotlightView = null
(targetView as? SpotlightTarget)?.spotlightView = null
(parent as? ViewGroup)?.removeView(this)
listener?.onEnd(targetView)
}

fun setTargetViewPosition() {
Expand Down Expand Up @@ -283,6 +287,12 @@ open class SpotlightView @JvmOverloads constructor(

override fun onTouchEvent(event: MotionEvent?): Boolean {
if (event == null) return false

if (passThrough && dismissType == SpotlightDismissType.targetView) {
val isInsideTarget = targetRect.toRectF().contains(event.x, event.y)
return !isInsideTarget
}

val x = event.x
val y = event.y
val targetView = targetView
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.matadesigns.spotlight.utils

import android.os.SystemClock
import android.util.Log
import android.view.View

class DebounceOnClickListener(var debounceTime: Long, var action: (View) -> Unit): View.OnClickListener {
private var clickTime: Long = 0

override fun onClick(v: View) {
val lastClickTime = clickTime
clickTime = SystemClock.elapsedRealtime()
if (SystemClock.elapsedRealtime() - lastClickTime < debounceTime) {
Log.d("DebounceOnClickListener", "Blocked Click")
return
}
action(v)
}
}

fun View.setOnClickListenerWithDebounce(action: (View) -> Unit, debounceTime: Long = 600L) {
this.setOnClickListener(DebounceOnClickListener(debounceTime, action))
}

0 comments on commit 0f8ae3f

Please sign in to comment.