Fix animated emojis in composable views

This commit is contained in:
Adolfo Santiago 2022-05-21 11:29:36 +02:00
parent a26c078731
commit 81abedb89b
No known key found for this signature in database
GPG key ID: 244D6F9A317B4A65
4 changed files with 451 additions and 276 deletions

View file

@ -15,38 +15,48 @@
package com.keylesspalace.tusky.adapter package com.keylesspalace.tusky.adapter
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.ImageView import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
import com.github.penfeizhou.animation.glide.AnimationDecoderOption
import com.keylesspalace.tusky.R import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.entity.Emoji import com.keylesspalace.tusky.entity.Emoji
import java.util.*
class EmojiAdapter(emojiList: List<Emoji>, private val onEmojiSelectedListener: OnEmojiSelectedListener) : RecyclerView.Adapter<EmojiAdapter.EmojiHolder>() { class EmojiAdapter(
private val emojiList : List<Emoji> emojiList: List<Emoji>,
private val onEmojiSelectedListener: OnEmojiSelectedListener,
private val animateEmojis: Boolean
) : RecyclerView.Adapter<EmojiAdapter.EmojiHolder>() {
private val emojis: List<Emoji>
init { init {
this.emojiList = emojiList.filter { emoji -> emoji.visibleInPicker == null || emoji.visibleInPicker } this.emojis =
.sortedBy { it.shortcode.toLowerCase(Locale.ROOT) } emojiList.filter { emoji -> emoji.visibleInPicker == null || emoji.visibleInPicker }
.sortedBy { it.shortcode.lowercase() }
} }
override fun getItemCount(): Int { override fun getItemCount(): Int {
return emojiList.size return emojis.size
} }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EmojiHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EmojiHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_emoji_button, parent, false) as ImageView val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_emoji_button, parent, false) as ImageView
return EmojiHolder(view) return EmojiHolder(view)
} }
override fun onBindViewHolder(viewHolder: EmojiHolder, position: Int) { override fun onBindViewHolder(viewHolder: EmojiHolder, position: Int) {
val emoji = emojiList[position] val emoji = emojis[position]
Glide.with(viewHolder.emojiImageView) Glide.with(viewHolder.emojiImageView)
.load(emoji.url) .load(emoji.url)
.into(viewHolder.emojiImageView) .set(AnimationDecoderOption.DISABLE_ANIMATION_GIF_DECODER, !animateEmojis)
.set(AnimationDecoderOption.DISABLE_ANIMATION_WEBP_DECODER, !animateEmojis)
.set(AnimationDecoderOption.DISABLE_ANIMATION_APNG_DECODER, !animateEmojis)
.into(viewHolder.emojiImageView)
viewHolder.emojiImageView.setOnClickListener { viewHolder.emojiImageView.setOnClickListener {
onEmojiSelectedListener.onEmojiSelected(emoji.shortcode) onEmojiSelectedListener.onEmojiSelected(emoji.shortcode)

View file

@ -55,6 +55,7 @@ class AnnouncementsActivity : BottomSheetActivity(), AnnouncementActionListener,
private val viewModel: AnnouncementsViewModel by viewModels { viewModelFactory } private val viewModel: AnnouncementsViewModel by viewModels { viewModelFactory }
private lateinit var preferences: SharedPreferences
private lateinit var adapter: AnnouncementAdapter private lateinit var adapter: AnnouncementAdapter
private val picker by lazy { EmojiPicker(this) } private val picker by lazy { EmojiPicker(this) }
@ -89,7 +90,7 @@ class AnnouncementsActivity : BottomSheetActivity(), AnnouncementActionListener,
val divider = DividerItemDecoration(this, DividerItemDecoration.VERTICAL) val divider = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
announcementsList.addItemDecoration(divider) announcementsList.addItemDecoration(divider)
val preferences: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(this) preferences = PreferenceManager.getDefaultSharedPreferences(this)
val wellbeingEnabled = preferences.getBoolean(PrefKeys.WELLBEING_HIDE_STATS_POSTS, false) val wellbeingEnabled = preferences.getBoolean(PrefKeys.WELLBEING_HIDE_STATS_POSTS, false)
adapter = AnnouncementAdapter(emptyList(), this, wellbeingEnabled) adapter = AnnouncementAdapter(emptyList(), this, wellbeingEnabled)
@ -128,7 +129,11 @@ class AnnouncementsActivity : BottomSheetActivity(), AnnouncementActionListener,
viewModel.emojis.observe(this) { viewModel.emojis.observe(this) {
it?.let { list -> it?.let { list ->
picker.adapter = EmojiAdapter(list, this) picker.adapter = EmojiAdapter(
list,
this,
preferences.getBoolean(PrefKeys.ANIMATE_CUSTOM_EMOJIS, false)
)
} }
} }

View file

@ -167,9 +167,11 @@ class ComposeActivity : BaseActivity(),
private val maxUploadMediaNumber = 4 private val maxUploadMediaNumber = 4
private var mediaCount = 0 private var mediaCount = 0
private lateinit var preferences: SharedPreferences
public override fun onCreate(savedInstanceState: Bundle?) { public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
val preferences = PreferenceManager.getDefaultSharedPreferences(this) preferences = PreferenceManager.getDefaultSharedPreferences(this)
val theme = preferences.getString("appTheme", ThemeUtils.APP_THEME_DEFAULT) val theme = preferences.getString("appTheme", ThemeUtils.APP_THEME_DEFAULT)
if(theme == "black") { if(theme == "black") {
setTheme(R.style.TuskyDialogActivityBlackTheme) setTheme(R.style.TuskyDialogActivityBlackTheme)
@ -1447,7 +1449,11 @@ class ComposeActivity : BaseActivity(),
private fun setEmojiList(emojiList: List<Emoji>?) { private fun setEmojiList(emojiList: List<Emoji>?) {
if(emojiList != null) { if(emojiList != null) {
binding.emojiView.adapter = EmojiAdapter(emojiList, this@ComposeActivity) binding.emojiView.adapter = EmojiAdapter(
emojiList,
this@ComposeActivity,
preferences.getBoolean(PrefKeys.ANIMATE_CUSTOM_EMOJIS, false)
)
enableButton(binding.composeEmojiButton, true, emojiList.isNotEmpty()) enableButton(binding.composeEmojiButton, true, emojiList.isNotEmpty())
} }
} }