Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate EmojiPopup.Builder in favor of EmojiPopup constructor. #731

Merged
merged 1 commit into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ class CustomViewActivity : AppCompatActivity() {
}
}

val emojiPopup = EmojiPopup.Builder.fromRootView(this)
.setKeyboardAnimationStyle(R.style.emoji_fade_animation_style)
.setPageTransformer(PageTransformer())
.build(binding.editText)
val emojiPopup = EmojiPopup(
rootView = this,
keyboardAnimationStyle = R.style.emoji_fade_animation_style,
editText = binding.editText,
)
binding.editText.installDisableKeyboardInput(emojiPopup)
binding.button.setOnClickListener { binding.editText.requestFocus() }
}
Expand Down
25 changes: 14 additions & 11 deletions app/src/main/kotlin/com/vanniktech/emoji/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,20 @@ class MainActivity : AppCompatActivity() {
chatAdapter = ChatAdapter()
setUpShowcaseButtons()

emojiPopup = EmojiPopup.Builder.fromRootView(binding.rootView)
.setOnEmojiBackspaceClickListener { Timber.d(TAG, "Clicked on Backspace") }
.setOnEmojiClickListener { emoji -> Timber.d(TAG, "Clicked on Emoji " + emoji.unicode) }
.setOnEmojiPopupShownListener { binding.chatEmoji.setImageResource(R.drawable.ic_keyboard) }
.setOnSoftKeyboardOpenListener { px -> Timber.d(TAG, "Opened soft keyboard with height $px") }
.setOnEmojiPopupDismissListener { binding.chatEmoji.setImageResource(R.drawable.emoji_ios_category_smileysandpeople) }
.setOnSoftKeyboardCloseListener { Timber.d(TAG, "Closed soft keyboard") }
.setKeyboardAnimationStyle(R.style.emoji_fade_animation_style)
.setPageTransformer(PageTransformer())
// .setRecentEmoji(NoRecentEmoji.INSTANCE) // Uncomment this to hide recent emojis.
.build(binding.chatEditText)
emojiPopup = EmojiPopup(
rootView = binding.rootView,
editText = binding.chatEditText,
onEmojiBackspaceClickListener = { Timber.d(TAG, "Clicked on Backspace") },
onEmojiClickListener = { emoji -> Timber.d(TAG, "Clicked on Emoji " + emoji.unicode) },
onEmojiPopupShownListener = { binding.chatEmoji.setImageResource(R.drawable.ic_keyboard) },
onSoftKeyboardOpenListener = { px -> Timber.d(TAG, "Opened soft keyboard with height $px") },
onEmojiPopupDismissListener = { binding.chatEmoji.setImageResource(R.drawable.emoji_ios_category_smileysandpeople) },
onSoftKeyboardCloseListener = { Timber.d(TAG, "Closed soft keyboard") },
keyboardAnimationStyle = R.style.emoji_fade_animation_style,
pageTransformer = PageTransformer(),
// searchEmoji = NoSearchEmoji, // Uncomment this to hide search emojis.
// recentEmoji = NoRecentEmoji, // Uncomment this to hide recent emojis.
)

binding.chatSend.setColorFilter(ContextCompat.getColor(this, R.color.colorPrimary), PorterDuff.Mode.SRC_IN)
binding.chatEmoji.setColorFilter(ContextCompat.getColor(this, R.color.colorPrimary), PorterDuff.Mode.SRC_IN)
Expand Down
21 changes: 11 additions & 10 deletions app/src/main/kotlin/com/vanniktech/emoji/sample/MainDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,17 @@ class MainDialog : DialogFragment() {
val emojiButton = result.findViewById<ImageButton>(R.id.main_dialog_emoji)
val sendButton = result.findViewById<ImageView>(R.id.main_dialog_send)

val emojiPopup = EmojiPopup.Builder.fromRootView(rootView)
.setOnEmojiBackspaceClickListener { Timber.d(TAG, "Clicked on Backspace") }
.setOnEmojiClickListener { emoji: Emoji -> Timber.d(TAG, "Clicked on Emoji " + emoji.unicode) }
.setOnEmojiPopupShownListener { emojiButton.setImageResource(R.drawable.ic_keyboard) }
.setOnSoftKeyboardOpenListener { px -> Timber.d(TAG, "Opened soft keyboard with height $px") }
.setOnEmojiPopupDismissListener { emojiButton.setImageResource(R.drawable.emoji_ios_category_smileysandpeople) }
.setOnSoftKeyboardCloseListener { Timber.d(TAG, "Closed soft keyboard") }
.setKeyboardAnimationStyle(R.style.emoji_fade_animation_style)
.setPageTransformer(PageTransformer())
.build(editText)
val emojiPopup = EmojiPopup(
rootView = rootView,
editText = editText,
onEmojiBackspaceClickListener = { Timber.d(TAG, "Clicked on Backspace") },
onEmojiClickListener = { emoji: Emoji -> Timber.d(TAG, "Clicked on Emoji " + emoji.unicode) },
onEmojiPopupShownListener = { emojiButton.setImageResource(R.drawable.ic_keyboard) },
onSoftKeyboardOpenListener = { px -> Timber.d(TAG, "Opened soft keyboard with height $px") },
onEmojiPopupDismissListener = { emojiButton.setImageResource(R.drawable.emoji_ios_category_smileysandpeople) },
onSoftKeyboardCloseListener = { Timber.d(TAG, "Closed soft keyboard") },
keyboardAnimationStyle = R.style.emoji_fade_animation_style,
)

emojiButton.setColorFilter(ContextCompat.getColor(context, R.color.colorPrimary), PorterDuff.Mode.SRC_IN)
sendButton.setColorFilter(ContextCompat.getColor(context, R.color.colorPrimary), PorterDuff.Mode.SRC_IN)
Expand Down
126 changes: 75 additions & 51 deletions emoji/src/main/kotlin/com/vanniktech/emoji/EmojiPopup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,51 +47,56 @@ import com.vanniktech.emoji.listeners.OnSoftKeyboardOpenListener
import java.lang.ref.WeakReference
import kotlin.math.max

class EmojiPopup internal constructor(
builder: Builder,
class EmojiPopup(
rootView: View,
private val editText: EditText,
internal val theming: EmojiTheming = EmojiTheming(),
recentEmoji: RecentEmoji = RecentEmojiManager(rootView.context),
internal val searchEmoji: SearchEmoji = SearchEmojiManager(),
variantEmoji: VariantEmoji = VariantEmojiManager(rootView.context),
pageTransformer: ViewPager.PageTransformer? = null,
@StyleRes keyboardAnimationStyle: Int = 0,
private var popupWindowHeight: Int = 0,
private val onEmojiPopupShownListener: OnEmojiPopupShownListener? = null,
private val onSoftKeyboardCloseListener: OnSoftKeyboardCloseListener? = null,
private val onSoftKeyboardOpenListener: OnSoftKeyboardOpenListener? = null,
private val onEmojiBackspaceClickListener: OnEmojiBackspaceClickListener? = null,
private val onEmojiClickListener: OnEmojiClickListener? = null,
private val onEmojiPopupDismissListener: OnEmojiPopupDismissListener? = null,
) : EmojiResultReceiver.Receiver {
internal val rootView: View = builder.rootView.rootView
internal val context: Activity = Utils.asActivity(builder.rootView.context)
internal val rootView: View = rootView.rootView
internal val context: Activity = Utils.asActivity(rootView.context)
private val emojiView: EmojiView = EmojiView(context)
private val popupWindow: PopupWindow = PopupWindow(context)
internal val theming = builder.theming
internal val searchEmoji = builder.searchEmoji
private var isPendingOpen = false
private var isKeyboardOpen = false
private var globalKeyboardHeight = 0
private var delay = 0
internal var onEmojiPopupShownListener: OnEmojiPopupShownListener? = null
internal var onSoftKeyboardCloseListener: OnSoftKeyboardCloseListener? = null
internal var onSoftKeyboardOpenListener: OnSoftKeyboardOpenListener? = null
internal var onEmojiBackspaceClickListener: OnEmojiBackspaceClickListener? = null
internal var onEmojiClickListener: OnEmojiClickListener? = null
internal var onEmojiPopupDismissListener: OnEmojiPopupDismissListener? = null
private var popupWindowHeight = 0
private var originalImeOptions = -1
private val emojiResultReceiver = EmojiResultReceiver(Handler(Looper.getMainLooper()))
private val onDismissListener = PopupWindow.OnDismissListener {
onEmojiPopupDismissListener?.onEmojiPopupDismiss()
}

init {
verifyInstalled()
emojiView.setUp(
rootView,
onEmojiClickListener,
onEmojiBackspaceClickListener,
editText,
builder.theming,
builder.recentEmoji,
builder.searchEmoji,
builder.variantEmoji,
builder.pageTransformer
theming,
recentEmoji,
searchEmoji,
variantEmoji,
pageTransformer,
)
popupWindow.contentView = emojiView
popupWindow.inputMethodMode = PopupWindow.INPUT_METHOD_NOT_NEEDED
popupWindow.setBackgroundDrawable(BitmapDrawable(context.resources, null as Bitmap?)) // To avoid borders and overdraw.
popupWindow.setOnDismissListener(onDismissListener)
if (builder.keyboardAnimationStyle != 0) {
popupWindow.animationStyle = builder.keyboardAnimationStyle
if (keyboardAnimationStyle != 0) {
popupWindow.animationStyle = keyboardAnimationStyle
}

// Root view might already be laid out in which case we need to manually call start()
Expand Down Expand Up @@ -132,9 +137,7 @@ class EmojiPopup internal constructor(

if (!isKeyboardOpen) {
isKeyboardOpen = true
if (onSoftKeyboardOpenListener != null) {
onSoftKeyboardOpenListener!!.onKeyboardOpen(keyboardHeight)
}
onSoftKeyboardOpenListener?.onKeyboardOpen(keyboardHeight)
}

if (isPendingOpen) {
Expand Down Expand Up @@ -210,9 +213,7 @@ class EmojiPopup internal constructor(
Utils.getProperHeight(context) + popupWindowHeight
)
}, delay.toLong())
if (onEmojiPopupShownListener != null) {
onEmojiPopupShownListener!!.onEmojiPopupShown()
}
onEmojiPopupShownListener?.onEmojiPopupShown()
}

override fun onReceiveResult(resultCode: Int, data: Bundle?) {
Expand All @@ -221,52 +222,60 @@ class EmojiPopup internal constructor(
}
}

@Suppress("DEPRECATION")
@Deprecated("Please use EmojiPopup constructor directly")
class Builder private constructor(val rootView: View) {
@StyleRes var keyboardAnimationStyle = 0
var theming = EmojiTheming()
var pageTransformer: ViewPager.PageTransformer? = null
var onEmojiPopupShownListener: OnEmojiPopupShownListener? = null
var onSoftKeyboardCloseListener: OnSoftKeyboardCloseListener? = null
var onSoftKeyboardOpenListener: OnSoftKeyboardOpenListener? = null
var onEmojiBackspaceClickListener: OnEmojiBackspaceClickListener? = null
var onEmojiClickListener: OnEmojiClickListener? = null
var onEmojiPopupDismissListener: OnEmojiPopupDismissListener? = null
var recentEmoji: RecentEmoji
var searchEmoji: SearchEmoji
var variantEmoji: VariantEmoji
var popupWindowHeight = 0
@StyleRes private var keyboardAnimationStyle = 0
private var theming = EmojiTheming()
private var pageTransformer: ViewPager.PageTransformer? = null
private var onEmojiPopupShownListener: OnEmojiPopupShownListener? = null
private var onSoftKeyboardCloseListener: OnSoftKeyboardCloseListener? = null
private var onSoftKeyboardOpenListener: OnSoftKeyboardOpenListener? = null
private var onEmojiBackspaceClickListener: OnEmojiBackspaceClickListener? = null
private var onEmojiClickListener: OnEmojiClickListener? = null
private var onEmojiPopupDismissListener: OnEmojiPopupDismissListener? = null
private var recentEmoji: RecentEmoji
private var searchEmoji: SearchEmoji
private var variantEmoji: VariantEmoji
private var popupWindowHeight = 0

@CheckResult
@Deprecated("Please use EmojiPopup constructor directly")
fun setOnSoftKeyboardCloseListener(listener: OnSoftKeyboardCloseListener?): Builder {
onSoftKeyboardCloseListener = listener
return this
}

@CheckResult
@Deprecated("Please use EmojiPopup constructor directly")
fun setOnEmojiClickListener(listener: OnEmojiClickListener?): Builder {
onEmojiClickListener = listener
return this
}

@CheckResult
@Deprecated("Please use EmojiPopup constructor directly")
fun setOnSoftKeyboardOpenListener(listener: OnSoftKeyboardOpenListener?): Builder {
onSoftKeyboardOpenListener = listener
return this
}

@CheckResult
@Deprecated("Please use EmojiPopup constructor directly")
fun setOnEmojiPopupShownListener(listener: OnEmojiPopupShownListener?): Builder {
onEmojiPopupShownListener = listener
return this
}

@CheckResult
@Deprecated("Please use EmojiPopup constructor directly")
fun setOnEmojiPopupDismissListener(listener: OnEmojiPopupDismissListener?): Builder {
onEmojiPopupDismissListener = listener
return this
}

@CheckResult
@Deprecated("Please use EmojiPopup constructor directly")
fun setOnEmojiBackspaceClickListener(listener: OnEmojiBackspaceClickListener?): Builder {
onEmojiBackspaceClickListener = listener
return this
Expand All @@ -281,6 +290,7 @@ class EmojiPopup internal constructor(
* @since 0.7.0
*/
@CheckResult
@Deprecated("Please use EmojiPopup constructor directly")
fun setPopupWindowHeight(windowHeight: Int): Builder {
popupWindowHeight = max(windowHeight, 0)
return this
Expand All @@ -293,6 +303,7 @@ class EmojiPopup internal constructor(
* @since 0.2.0
*/
@CheckResult
@Deprecated("Please use EmojiPopup constructor directly")
fun setRecentEmoji(recent: RecentEmoji): Builder {
recentEmoji = recent
return this
Expand All @@ -305,6 +316,7 @@ class EmojiPopup internal constructor(
* @since 0.10.0
*/
@CheckResult
@Deprecated("Please use EmojiPopup constructor directly")
fun setSearchEmoji(search: SearchEmoji): Builder {
searchEmoji = search
return this
Expand All @@ -317,50 +329,62 @@ class EmojiPopup internal constructor(
* @since 0.5.0
*/
@CheckResult
@Deprecated("Please use EmojiPopup constructor directly")
fun setVariantEmoji(variant: VariantEmoji): Builder {
variantEmoji = variant
return this
}

@CheckResult
@Deprecated("Please use EmojiPopup constructor directly")
fun setTheming(theming: EmojiTheming): Builder {
this.theming = theming
return this
}

@CheckResult
@Deprecated("Please use EmojiPopup constructor directly")
fun setKeyboardAnimationStyle(@StyleRes animation: Int): Builder {
keyboardAnimationStyle = animation
return this
}

@CheckResult
@Deprecated("Please use EmojiPopup constructor directly")
fun setPageTransformer(transformer: ViewPager.PageTransformer?): Builder {
pageTransformer = transformer
return this
}

@CheckResult
fun build(editText: EditText): EmojiPopup {
verifyInstalled()
val emojiPopup = EmojiPopup(this, editText)
emojiPopup.onSoftKeyboardCloseListener = onSoftKeyboardCloseListener
emojiPopup.onEmojiClickListener = onEmojiClickListener
emojiPopup.onSoftKeyboardOpenListener = onSoftKeyboardOpenListener
emojiPopup.onEmojiPopupShownListener = onEmojiPopupShownListener
emojiPopup.onEmojiPopupDismissListener = onEmojiPopupDismissListener
emojiPopup.onEmojiBackspaceClickListener = onEmojiBackspaceClickListener
emojiPopup.popupWindowHeight = Math.max(popupWindowHeight, 0)
return emojiPopup
}
@Deprecated("Please use EmojiPopup constructor directly")
fun build(editText: EditText): EmojiPopup = EmojiPopup(
rootView = rootView,
editText = editText,
theming = theming,
recentEmoji = recentEmoji,
searchEmoji = searchEmoji,
variantEmoji = variantEmoji,
pageTransformer = pageTransformer,
keyboardAnimationStyle = keyboardAnimationStyle,
onEmojiPopupShownListener = onEmojiPopupShownListener,
onSoftKeyboardCloseListener = onSoftKeyboardCloseListener,
onSoftKeyboardOpenListener = onSoftKeyboardOpenListener,
onEmojiBackspaceClickListener = onEmojiBackspaceClickListener,
onEmojiClickListener = onEmojiClickListener,
onEmojiPopupDismissListener = onEmojiPopupDismissListener,
popupWindowHeight = max(popupWindowHeight, 0),
)

@Deprecated("Please use EmojiPopup constructor directly")
companion object {
/**
* @param rootView The root View of your layout.xml which will be used for calculating the height
* of the keyboard.
* @return builder For building the [EmojiPopup].
*/
@CheckResult
@Deprecated("Please use EmojiPopup constructor directly")
fun fromRootView(rootView: View): Builder {
return Builder(rootView)
}
Expand Down