-
Notifications
You must be signed in to change notification settings - Fork 6
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
EmojiPicker: autosize #199
Changes from 12 commits
b3bf92a
b008cfd
c2561cf
713824b
53d5779
e6df0f3
21430d8
000e8c2
dfd266f
6a024a8
6649ade
4988824
78ffcf3
ba15ab1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,18 +8,21 @@ import android.view.ViewGroup | |
import android.widget.TextView | ||
import androidx.coordinatorlayout.widget.CoordinatorLayout | ||
import androidx.core.content.ContextCompat | ||
import androidx.emoji.text.EmojiCompat | ||
import androidx.recyclerview.widget.GridLayoutManager | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.automattic.photoeditor.PhotoEditor | ||
import com.automattic.portkey.R | ||
import com.automattic.portkey.compose.hideStatusBar | ||
import com.automattic.portkey.util.getDisplayPixelWidth | ||
import com.google.android.material.bottomsheet.BottomSheetBehavior | ||
import com.google.android.material.bottomsheet.BottomSheetDialogFragment | ||
import kotlinx.android.synthetic.main.fragment_bottom_sticker_emoji_dialog.view.* | ||
import kotlinx.android.synthetic.main.row_emoji.view.* | ||
|
||
class EmojiPickerFragment : BottomSheetDialogFragment() { | ||
private var listener: EmojiListener? = null | ||
private var emojiViewWidth: Int? = null | ||
|
||
private val bottomSheetBehaviorCallback = object : BottomSheetBehavior.BottomSheetCallback() { | ||
override fun onStateChanged(bottomSheet: View, newState: Int) { | ||
|
@@ -44,6 +47,25 @@ class EmojiPickerFragment : BottomSheetDialogFragment() { | |
override fun onResume() { | ||
super.onResume() | ||
dialog?.window?.let { hideStatusBar(it) } | ||
context?.let { | ||
emojiViewWidth = getEmojiViewWidthForScreenWidthAndColumns(getDisplayPixelWidth(it), COLUMNS) | ||
} | ||
} | ||
|
||
private fun getEmojiViewWidthForScreenWidthAndColumns(screenWidth: Int, columns: Int): Int { | ||
// let's calculate the emoji view width here, given the dialog may be displayed while the user goes | ||
// out to change the fontSize manually in Settings -> Accessibility | ||
val itemPadding = resources.getDimension(R.dimen.emoji_picker_item_padding) / resources.displayMetrics.density | ||
val itemMargin = resources.getDimension(R.dimen.emoji_picker_item_margin) / resources.displayMetrics.density | ||
val startEndMargin = | ||
resources.getDimension(R.dimen.emoji_picker_whole_list_side_margin) / resources.displayMetrics.density | ||
|
||
val wholeListTakenSpace = (itemPadding * 2) + (itemMargin * 2) + (startEndMargin * 2) | ||
|
||
val remainingScreenOperatingSpace = screenWidth - wholeListTakenSpace | ||
val maxEmojiWidth = remainingScreenOperatingSpace / columns | ||
|
||
return maxEmojiWidth.toInt() | ||
} | ||
|
||
@SuppressLint("RestrictedApi") | ||
|
@@ -71,11 +93,36 @@ class EmojiPickerFragment : BottomSheetDialogFragment() { | |
inner class EmojiAdapter(internal val emojiList: List<String>) : RecyclerView.Adapter<EmojiAdapter.ViewHolder>() { | ||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | ||
val view = LayoutInflater.from(parent.context).inflate(R.layout.row_emoji, parent, false) | ||
emojiViewWidth?.let { | ||
val params = view.getLayoutParams() | ||
// Changes the height and width to the specified dp | ||
params.height = it | ||
params.width = it | ||
view.setLayoutParams(params) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, can use property access syntax: view.layoutParams = params There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 done in ba15ab1 |
||
} | ||
return ViewHolder(view) | ||
} | ||
|
||
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | ||
holder.txtEmojiRef.text = emojiList[position] | ||
// use EmojiCompat to process the string and make sure we have an emoji that can be rendered | ||
EmojiCompat.get().registerInitCallback(object : EmojiCompat.InitCallback() { | ||
override fun onInitialized() { | ||
EmojiCompat.get().unregisterInitCallback(this) | ||
|
||
val regularTextView = holder.txtEmojiRef | ||
if (regularTextView != null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary null check here - There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 addressed in 78ffcf3 |
||
val compat = EmojiCompat.get() | ||
regularTextView.text = compat.process(emojiList[position]) | ||
} | ||
} | ||
|
||
override fun onFailed(throwable: Throwable?) { | ||
EmojiCompat.get().unregisterInitCallback(this) | ||
|
||
// just fallback to setting the text | ||
holder.txtEmojiRef.text = emojiList[position] | ||
} | ||
}) | ||
} | ||
|
||
override fun getItemCount(): Int { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,19 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:background="@android:color/transparent" | ||
android:orientation="vertical"> | ||
android:background="@android:color/transparent"> | ||
|
||
|
||
<androidx.constraintlayout.widget.ConstraintLayout | ||
<androidx.recyclerview.widget.RecyclerView | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
android:id="@+id/rvEmoji" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:layout_marginEnd="16dp" | ||
android:layout_marginStart="16dp"> | ||
|
||
<TextView | ||
android:id="@+id/txtClose" | ||
android:layout_width="wrap_content" | ||
android:layout_height="?attr/actionBarSize" | ||
android:layout_marginStart="8dp" | ||
android:gravity="center" | ||
android:text="Close" | ||
android:textAppearance="@style/TextAppearance.AppCompat.Large.Inverse" | ||
android:visibility="gone" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
|
||
<View | ||
android:id="@+id/lineView" | ||
android:layout_width="match_parent" | ||
android:layout_height="2px" | ||
android:background="@android:color/white" | ||
android:visibility="gone" | ||
app:layout_constraintTop_toBottomOf="@+id/txtClose" /> | ||
|
||
<androidx.recyclerview.widget.RecyclerView | ||
android:id="@+id/rvEmoji" | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:layout_margin="8dp" | ||
app:layout_behavior="@string/bottom_sheet_behavior" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@+id/lineView" /> | ||
|
||
<TextView | ||
android:id="@+id/txtDone" | ||
android:layout_width="wrap_content" | ||
android:layout_height="?attr/actionBarSize" | ||
android:layout_marginEnd="8dp" | ||
android:gravity="center" | ||
android:text="Done" | ||
android:textAppearance="@style/TextAppearance.AppCompat.Large.Inverse" | ||
android:visibility="gone" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
|
||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> | ||
|
||
</LinearLayout> | ||
android:layout_height="wrap_content" | ||
android:layout_centerHorizontal="true" | ||
android:layout_marginEnd="@dimen/emoji_picker_whole_list_side_margin" | ||
android:layout_marginStart="@dimen/emoji_picker_whole_list_side_margin" | ||
/> | ||
</RelativeLayout> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,12 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
<TextView xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content"> | ||
|
||
<androidx.emoji.widget.EmojiTextView | ||
android:id="@+id/txtEmoji" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_margin="4dp" | ||
android:padding="2dp" | ||
android:textColor="#FFFFFF" | ||
android:textSize="35sp"/> | ||
</LinearLayout> | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
android:id="@+id/txtEmoji" | ||
android:layout_width="@dimen/emoji_picker_initial_size" | ||
android:layout_height="@dimen/emoji_picker_initial_size" | ||
android:layout_margin="@dimen/emoji_picker_item_margin" | ||
android:padding="@dimen/emoji_picker_item_padding" | ||
android:textColor="#FFFFFF" | ||
android:textAlignment="center" | ||
app:autoSizeTextType="uniform" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can use property access syntax here and silence an IDE warning:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 ba15ab1