Skip to content

Commit

Permalink
Initialize BalloonPersistence lazily and move supportRtlLayoutFactor …
Browse files Browse the repository at this point in the history
…into the builder
  • Loading branch information
skydoves committed Nov 29, 2020
1 parent 5f6a95f commit 30917cf
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
49 changes: 39 additions & 10 deletions balloon/src/main/java/com/skydoves/balloon/Balloon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,16 @@ import com.skydoves.balloon.overlay.BalloonOverlayShape
@DslMarker
internal annotation class BalloonDsl

/** creates an instance of [Balloon] by [Balloon.Builder] using kotlin dsl. */
/**
* Creates an instance of the [Balloon] by scope of the [Balloon.Builder] using kotlin dsl.
*
* @param context A context for creating resources of the [Balloon].
* @param block A dsl scope lambda from the [Balloon.Builder].
* */
@MainThread
@BalloonDsl
@JvmSynthetic
inline fun createBalloon(context: Context, block: Balloon.Builder.() -> Unit): Balloon =
inline fun createBalloon(context: Context, crossinline block: Balloon.Builder.() -> Unit): Balloon =
Balloon.Builder(context).apply(block).build()

/** Balloon implements showing and dismissing text popup with arrow and animations. */
Expand All @@ -92,19 +98,35 @@ class Balloon(
private val builder: Builder
) : LifecycleObserver {

/** A main content view of the popup. */
private val binding: LayoutBalloonLibrarySkydovesBinding =
LayoutBalloonLibrarySkydovesBinding.inflate(LayoutInflater.from(context), null, false)

/** An overlay view of the background for highlighting the popup and an anchor. */
private val overlayBinding: LayoutBalloonOverlayLibrarySkydovesBinding =
LayoutBalloonOverlayLibrarySkydovesBinding.inflate(LayoutInflater.from(context), null, false)

/** A main content window of the popup. */
private val bodyWindow: PopupWindow

/** An overlay window of the background popup. */
private val overlayWindow: PopupWindow

/** Denotes the popup is showing or not. */
var isShowing = false
private set

/** Denotes the popup is already destroyed internally. */
private var destroyed: Boolean = false

/** Interface definition for a callback to be invoked when a balloon view is initialized. */
var onBalloonInitializedListener: OnBalloonInitializedListener? =
builder.onBalloonInitializedListener
private var supportRtlLayoutFactor: Int = LTR.unaryMinus(builder.isRtlSupport)
private val balloonPersistence = BalloonPersistence.getInstance(context)

/** A persistence helper for showing the popup a limited number of times. */
private val balloonPersistence: BalloonPersistence by lazy(LazyThreadSafetyMode.NONE) {
BalloonPersistence.getInstance(context)
}

init {
this.bodyWindow = PopupWindow(
Expand Down Expand Up @@ -481,7 +503,7 @@ class Balloon(
show(anchor) {
bodyWindow.showAsDropDown(
anchor,
supportRtlLayoutFactor * ((anchor.measuredWidth / 2) - (getMeasureWidth() / 2)),
builder.supportRtlLayoutFactor * ((anchor.measuredWidth / 2) - (getMeasureWidth() / 2)),
-getMeasureHeight() - (anchor.measuredHeight / 2)
)
}
Expand Down Expand Up @@ -541,7 +563,7 @@ class Balloon(
show(anchor) {
bodyWindow.showAsDropDown(
anchor,
supportRtlLayoutFactor * ((anchor.measuredWidth / 2) - (getMeasureWidth() / 2)),
builder.supportRtlLayoutFactor * ((anchor.measuredWidth / 2) - (getMeasureWidth() / 2)),
-getMeasureHeight() - anchor.measuredHeight
)
}
Expand All @@ -561,7 +583,7 @@ class Balloon(
show(anchor) {
bodyWindow.showAsDropDown(
anchor,
supportRtlLayoutFactor * ((anchor.measuredWidth / 2) - (getMeasureWidth() / 2) + xOff),
builder.supportRtlLayoutFactor * ((anchor.measuredWidth / 2) - (getMeasureWidth() / 2) + xOff),
-getMeasureHeight() - anchor.measuredHeight + yOff
)
}
Expand All @@ -581,7 +603,7 @@ class Balloon(
show(anchor) {
bodyWindow.showAsDropDown(
anchor,
supportRtlLayoutFactor * ((anchor.measuredWidth / 2) - (getMeasureWidth() / 2)),
builder.supportRtlLayoutFactor * ((anchor.measuredWidth / 2) - (getMeasureWidth() / 2)),
0
)
}
Expand All @@ -601,7 +623,7 @@ class Balloon(
show(anchor) {
bodyWindow.showAsDropDown(
anchor,
supportRtlLayoutFactor * ((anchor.measuredWidth / 2) - (getMeasureWidth() / 2) + xOff),
builder.supportRtlLayoutFactor * ((anchor.measuredWidth / 2) - (getMeasureWidth() / 2) + xOff),
yOff
)
}
Expand Down Expand Up @@ -1193,6 +1215,10 @@ class Balloon(
@set:JvmSynthetic
var isRtlSupport: Boolean = false

@JvmField
@set:JvmSynthetic
var supportRtlLayoutFactor: Int = LTR.unaryMinus(isRtlSupport)

@JvmField
@set:JvmSynthetic
var isFocusable: Boolean = true
Expand Down Expand Up @@ -1662,7 +1688,10 @@ class Balloon(
fun setShowTime(value: Int): Builder = apply { this.showTimes = value }

/** sets flag for enabling rtl support */
fun isRtlSupport(value: Boolean): Builder = apply { this.isRtlSupport = value }
fun isRtlSupport(value: Boolean): Builder = apply {
this.supportRtlLayoutFactor = LTR.unaryMinus(value)
this.isRtlSupport = value
}

/**
* sets isFocusable option to the body window.
Expand Down
4 changes: 3 additions & 1 deletion balloon/src/main/java/com/skydoves/balloon/IconForm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import android.graphics.Color
import android.graphics.drawable.Drawable
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.annotation.MainThread
import androidx.annotation.Px
import com.skydoves.balloon.extensions.contextColor
import com.skydoves.balloon.extensions.dp2Px
Expand All @@ -31,9 +32,10 @@ import com.skydoves.balloon.extensions.dp2Px
internal annotation class IconFormDsl

/** creates an instance of [IconForm] from [IconForm.Builder] using kotlin dsl. */
@MainThread
@IconFormDsl
@JvmSynthetic
inline fun iconForm(context: Context, block: IconForm.Builder.() -> Unit): IconForm =
inline fun iconForm(context: Context, crossinline block: IconForm.Builder.() -> Unit): IconForm =
IconForm.Builder(context).apply(block).build()

/**
Expand Down
4 changes: 3 additions & 1 deletion balloon/src/main/java/com/skydoves/balloon/TextForm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import android.text.method.MovementMethod
import android.view.Gravity
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.annotation.MainThread
import androidx.annotation.StringRes
import com.skydoves.balloon.annotations.Sp
import com.skydoves.balloon.extensions.contextColor
Expand All @@ -33,9 +34,10 @@ import com.skydoves.balloon.extensions.contextColor
internal annotation class TextFormDsl

/** creates an instance of [TextForm] from [TextForm.Builder] using kotlin dsl. */
@MainThread
@TextFormDsl
@JvmSynthetic
inline fun textForm(context: Context, block: TextForm.Builder.() -> Unit): TextForm =
inline fun textForm(context: Context, crossinline block: TextForm.Builder.() -> Unit): TextForm =
TextForm.Builder(context).apply(block).build()

/**
Expand Down

0 comments on commit 30917cf

Please sign in to comment.