diff --git a/app/src/main/kotlin/com/vanniktech/emoji/sample/CustomViewActivity.kt b/app/src/main/kotlin/com/vanniktech/emoji/sample/CustomViewActivity.kt index aeb519cc35..2621150bd7 100644 --- a/app/src/main/kotlin/com/vanniktech/emoji/sample/CustomViewActivity.kt +++ b/app/src/main/kotlin/com/vanniktech/emoji/sample/CustomViewActivity.kt @@ -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() } } diff --git a/app/src/main/kotlin/com/vanniktech/emoji/sample/MainActivity.kt b/app/src/main/kotlin/com/vanniktech/emoji/sample/MainActivity.kt index 7c36cf849c..cb00a8ba39 100644 --- a/app/src/main/kotlin/com/vanniktech/emoji/sample/MainActivity.kt +++ b/app/src/main/kotlin/com/vanniktech/emoji/sample/MainActivity.kt @@ -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) diff --git a/app/src/main/kotlin/com/vanniktech/emoji/sample/MainDialog.kt b/app/src/main/kotlin/com/vanniktech/emoji/sample/MainDialog.kt index d5d1f50da1..2e32b0e869 100644 --- a/app/src/main/kotlin/com/vanniktech/emoji/sample/MainDialog.kt +++ b/app/src/main/kotlin/com/vanniktech/emoji/sample/MainDialog.kt @@ -54,16 +54,17 @@ class MainDialog : DialogFragment() { val emojiButton = result.findViewById(R.id.main_dialog_emoji) val sendButton = result.findViewById(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) diff --git a/emoji/src/main/kotlin/com/vanniktech/emoji/EmojiPopup.kt b/emoji/src/main/kotlin/com/vanniktech/emoji/EmojiPopup.kt index 6de67ce111..8421312658 100644 --- a/emoji/src/main/kotlin/com/vanniktech/emoji/EmojiPopup.kt +++ b/emoji/src/main/kotlin/com/vanniktech/emoji/EmojiPopup.kt @@ -47,27 +47,31 @@ 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 { @@ -75,23 +79,24 @@ class EmojiPopup internal constructor( } 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() @@ -132,9 +137,7 @@ class EmojiPopup internal constructor( if (!isKeyboardOpen) { isKeyboardOpen = true - if (onSoftKeyboardOpenListener != null) { - onSoftKeyboardOpenListener!!.onKeyboardOpen(keyboardHeight) - } + onSoftKeyboardOpenListener?.onKeyboardOpen(keyboardHeight) } if (isPendingOpen) { @@ -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?) { @@ -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 @@ -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 @@ -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 @@ -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 @@ -317,43 +329,54 @@ 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 @@ -361,6 +384,7 @@ class EmojiPopup internal constructor( * @return builder For building the [EmojiPopup]. */ @CheckResult + @Deprecated("Please use EmojiPopup constructor directly") fun fromRootView(rootView: View): Builder { return Builder(rootView) }