Skip to content

Commit

Permalink
Convert MainDialog class to Kotlin. (#663)
Browse files Browse the repository at this point in the history
vanniktech authored Apr 28, 2022
1 parent 08f7379 commit 005d73b
Showing 3 changed files with 92 additions and 110 deletions.
Original file line number Diff line number Diff line change
@@ -139,7 +139,7 @@ public class MainActivity extends AppCompatActivity {
final Button dialogButton = findViewById(R.id.dialog_button);
dialogButton.setOnClickListener(ignore -> {
emojiPopup.dismiss();
MainDialog.show(this);
MainDialog.Companion.show(this);
});
final Button button = findViewById(R.id.main_activity_material_button);
button.setText("Switch between Emoji Provider \uD83D\uDE18\uD83D\uDE02\uD83E\uDD8C");
109 changes: 0 additions & 109 deletions app/src/main/java/com/vanniktech/emoji/sample/MainDialog.java

This file was deleted.

91 changes: 91 additions & 0 deletions app/src/main/java/com/vanniktech/emoji/sample/MainDialog.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (C) 2016 - Niklas Baudy, Ruben Gees, Mario Đanić and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.vanniktech.emoji.sample

import android.app.Dialog
import android.graphics.PorterDuff
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.EditText
import android.widget.ImageButton
import android.widget.ImageView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.RecyclerView
import com.vanniktech.emoji.EmojiPopup
import com.vanniktech.emoji.emoji.Emoji
import com.vanniktech.emoji.material.MaterialEmojiLayoutFactory

// We don't care about duplicated code in the sample.
class MainDialog : DialogFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
layoutInflater.factory2 = MaterialEmojiLayoutFactory(null)
super.onCreate(savedInstanceState)
}

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return AlertDialog.Builder(requireContext())
.setView(buildView())
.create()
}

private fun buildView(): View? {
val context = requireContext()
val result = View.inflate(context, R.layout.dialog_main, null)
val editText = result.findViewById<EditText>(R.id.main_dialog_chat_bottom_message_edittext)
val rootView = result.findViewById<View>(R.id.main_dialog_root_view)
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 { Log.d(TAG, "Clicked on Backspace") }
.setOnEmojiClickListener { emoji: Emoji -> Log.d(TAG, "Clicked on Emoji " + emoji.unicode) }
.setOnEmojiPopupShownListener { emojiButton.setImageResource(R.drawable.ic_keyboard) }
.setOnSoftKeyboardOpenListener { px -> Log.d(TAG, "Opened soft keyboard with height $px") }
.setOnEmojiPopupDismissListener { emojiButton.setImageResource(R.drawable.emoji_ios_category_smileysandpeople) }
.setOnSoftKeyboardCloseListener { Log.d(TAG, "Closed soft keyboard") }
.setKeyboardAnimationStyle(R.style.emoji_fade_animation_style)
.setPageTransformer(PageTransformer())
.build(editText)

emojiButton.setColorFilter(ContextCompat.getColor(context, R.color.colorPrimary), PorterDuff.Mode.SRC_IN)
sendButton.setColorFilter(ContextCompat.getColor(context, R.color.colorPrimary), PorterDuff.Mode.SRC_IN)
val chatAdapter = ChatAdapter()
emojiButton.setOnClickListener { emojiPopup.toggle() }
sendButton.setOnClickListener {
val text = editText.text.toString().trim { it <= ' ' }
if (text.isNotEmpty()) {
chatAdapter.add(text)
editText.setText("")
}
}
val recyclerView: RecyclerView = result.findViewById(R.id.main_dialog_recycler_view)
recyclerView.adapter = chatAdapter
return rootView
}

internal companion object {
const val TAG = "MainDialog"

fun show(activity: AppCompatActivity) {
MainDialog().show(activity.supportFragmentManager, TAG)
}
}
}

0 comments on commit 005d73b

Please sign in to comment.