Skip to content

Commit 46c15f9

Browse files
committed
Support position attribute
1 parent 78b74a6 commit 46c15f9

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

library/src/main/java/co/kyash/targetinstructions/AbstractTargetBuilder.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ abstract class AbstractTargetBuilder<T : AbstractTargetBuilder<T, S>, S : Target
2525
internal var highlightPaddingRight = 0f
2626
internal var highlightPaddingBottom = 0f
2727

28+
internal var positionType: Target.Position? = null
29+
2830
internal var startDelayMillis = 0L
2931

3032
internal abstract fun self(): T
@@ -60,6 +62,11 @@ abstract class AbstractTargetBuilder<T : AbstractTargetBuilder<T, S>, S : Target
6062
}
6163
}
6264

65+
fun setPositionType(positionType: Target.Position): T {
66+
this.positionType = positionType
67+
return self()
68+
}
69+
6370
fun setStartDelayMillis(startDelayMillis: Long): T {
6471
this.startDelayMillis = startDelayMillis
6572
return self()

library/src/main/java/co/kyash/targetinstructions/targets/SimpleTarget.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class SimpleTarget(
2828
override val messageView: View,
2929
override val targetView: View? = null,
3030
override val delay: Long = 0L,
31+
override var positionType: Position? = null,
3132
private val messageAnimationDuration: Long = 300L,
3233
private val messageInterpolator: Interpolator,
3334
private val listener: OnStateChangedListener?
@@ -43,11 +44,11 @@ class SimpleTarget(
4344
highlightPaddingBottom,
4445
messageView,
4546
targetView,
46-
delay
47+
delay,
48+
positionType
4749
) {
4850

4951
override fun showImmediately() {
50-
val positionType = decideMessagePositionType()
5152
val container = messageView.findViewById<LinearLayout>(R.id.container)
5253
val topCaret = container.findViewById<ImageView>(R.id.top_caret)
5354
val bottomCaret = container.findViewById<ImageView>(R.id.bottom_caret)
@@ -166,6 +167,7 @@ class SimpleTarget(
166167
messageView = messageView,
167168
targetView = targetView,
168169
delay = startDelayMillis,
170+
positionType = positionType,
169171
messageAnimationDuration = messageAnimationDuration,
170172
messageInterpolator = messageInterpolator,
171173
listener = listener

library/src/main/java/co/kyash/targetinstructions/targets/Target.kt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,22 @@ abstract class Target(
1919
open val highlightPaddingBottom: Float,
2020
open val messageView: View,
2121
open val targetView: View? = null,
22-
open val delay: Long = 0L
22+
open val delay: Long = 0L,
23+
open var positionType: Position? = null
2324
) {
2425

2526
private val targetPaint = Paint().apply {
2627
xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR)
2728
}
2829

29-
internal enum class Position {
30-
ABOVE,
31-
BELOW
32-
}
33-
3430
internal fun show() {
3531
Handler().postDelayed({
3632
updateCoordinate()
33+
34+
if (positionType == null) {
35+
positionType = decideMessagePositionType()
36+
}
37+
3738
showImmediately()
3839
}, delay)
3940
}
@@ -68,7 +69,7 @@ abstract class Target(
6869

6970
internal open fun canClick(): Boolean = true
7071

71-
internal fun decideMessagePositionType(): Position {
72+
private fun decideMessagePositionType(): Position {
7273
val screenSize = Point()
7374
(messageView.context.getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay.getSize(screenSize)
7475
return if (top > screenSize.y - (top + height)) Position.ABOVE else Position.BELOW
@@ -82,4 +83,9 @@ abstract class Target(
8283
fun onClosed()
8384
}
8485

86+
enum class Position {
87+
ABOVE,
88+
BELOW
89+
}
90+
8591
}

0 commit comments

Comments
 (0)