migrate adapters to viewbinding

This commit is contained in:
fatih ergin 2023-08-17 00:21:18 +03:00
parent ee2422b3c3
commit 1972063ca6
3 changed files with 26 additions and 29 deletions

View File

@ -19,12 +19,12 @@ import com.simplemobiletools.commons.interfaces.RefreshRecyclerViewListener
import com.simplemobiletools.commons.interfaces.StartReorderDragListener import com.simplemobiletools.commons.interfaces.StartReorderDragListener
import com.simplemobiletools.commons.views.MyRecyclerView import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.keyboard.R import com.simplemobiletools.keyboard.R
import com.simplemobiletools.keyboard.databinding.ItemClipInActivityBinding
import com.simplemobiletools.keyboard.dialogs.AddOrEditClipDialog import com.simplemobiletools.keyboard.dialogs.AddOrEditClipDialog
import com.simplemobiletools.keyboard.extensions.clipsDB import com.simplemobiletools.keyboard.extensions.clipsDB
import com.simplemobiletools.keyboard.helpers.ClipsHelper import com.simplemobiletools.keyboard.helpers.ClipsHelper
import com.simplemobiletools.keyboard.models.Clip import com.simplemobiletools.keyboard.models.Clip
import kotlinx.android.synthetic.main.item_clip_in_activity.view.* import java.util.Collections
import java.util.*
class ClipsActivityAdapter( class ClipsActivityAdapter(
activity: BaseSimpleActivity, var items: ArrayList<Clip>, recyclerView: MyRecyclerView, val listener: RefreshRecyclerViewListener, itemClick: (Any) -> Unit activity: BaseSimpleActivity, var items: ArrayList<Clip>, recyclerView: MyRecyclerView, val listener: RefreshRecyclerViewListener, itemClick: (Any) -> Unit
@ -98,7 +98,7 @@ class ClipsActivityAdapter(
wasClipMoved = false wasClipMoved = false
} }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = createViewHolder(R.layout.item_clip_in_activity, parent) override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = createViewHolder(ItemClipInActivityBinding.inflate(layoutInflater, parent, false).root)
override fun onBindViewHolder(holder: ViewHolder, position: Int) { override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = items[position] val item = items[position]
@ -154,15 +154,15 @@ class ClipsActivityAdapter(
} }
val isSelected = selectedKeys.contains(clip.id!!.toInt()) val isSelected = selectedKeys.contains(clip.id!!.toInt())
view.apply { ItemClipInActivityBinding.bind(view).apply {
setupViewBackground(activity) root.setupViewBackground(activity)
clip_value.text = clip.value clipValue.text = clip.value
clip_value.setTextColor(textColor) clipValue.setTextColor(textColor)
clip_drag_handle.applyColorFilter(textColor) clipDragHandle.applyColorFilter(textColor)
clip_drag_handle.beVisibleIf(selectedKeys.isNotEmpty()) clipDragHandle.beVisibleIf(selectedKeys.isNotEmpty())
clip_holder.isSelected = isSelected clipHolder.isSelected = isSelected
clip_drag_handle.setOnTouchListener { v, event -> clipDragHandle.setOnTouchListener { v, event ->
if (event.action == MotionEvent.ACTION_DOWN) { if (event.action == MotionEvent.ACTION_DOWN) {
startReorderDragListener.requestDrag(holder) startReorderDragListener.requestDrag(holder)
} }

View File

@ -12,6 +12,8 @@ import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.keyboard.R import com.simplemobiletools.keyboard.R
import com.simplemobiletools.keyboard.databinding.ItemClipOnKeyboardBinding
import com.simplemobiletools.keyboard.databinding.ItemSectionLabelBinding
import com.simplemobiletools.keyboard.extensions.config import com.simplemobiletools.keyboard.extensions.config
import com.simplemobiletools.keyboard.extensions.getCurrentClip import com.simplemobiletools.keyboard.extensions.getCurrentClip
import com.simplemobiletools.keyboard.extensions.getStrokeColor import com.simplemobiletools.keyboard.extensions.getStrokeColor
@ -22,8 +24,6 @@ import com.simplemobiletools.keyboard.interfaces.RefreshClipsListener
import com.simplemobiletools.keyboard.models.Clip import com.simplemobiletools.keyboard.models.Clip
import com.simplemobiletools.keyboard.models.ClipsSectionLabel import com.simplemobiletools.keyboard.models.ClipsSectionLabel
import com.simplemobiletools.keyboard.models.ListItem import com.simplemobiletools.keyboard.models.ListItem
import kotlinx.android.synthetic.main.item_clip_on_keyboard.view.*
import kotlinx.android.synthetic.main.item_section_label.view.*
class ClipsKeyboardAdapter( class ClipsKeyboardAdapter(
val context: Context, var items: ArrayList<ListItem>, val refreshClipsListener: RefreshClipsListener, val context: Context, var items: ArrayList<ListItem>, val refreshClipsListener: RefreshClipsListener,
@ -36,13 +36,12 @@ class ClipsKeyboardAdapter(
private var backgroundColor = context.getProperBackgroundColor() private var backgroundColor = context.getProperBackgroundColor()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val layoutId = when (viewType) { val binding = when (viewType) {
ITEM_SECTION_LABEL -> R.layout.item_section_label ITEM_SECTION_LABEL -> ItemSectionLabelBinding.inflate(layoutInflater, parent, false)
else -> R.layout.item_clip_on_keyboard else -> ItemClipOnKeyboardBinding.inflate(layoutInflater, parent, false)
} }
val view = layoutInflater.inflate(layoutId, parent, false) return ViewHolder(binding.root)
return ViewHolder(view)
} }
override fun onBindViewHolder(holder: ViewHolder, position: Int) { override fun onBindViewHolder(holder: ViewHolder, position: Int) {
@ -65,13 +64,13 @@ class ClipsKeyboardAdapter(
} }
private fun setupClip(view: View, clip: Clip) { private fun setupClip(view: View, clip: Clip) {
view.apply { ItemClipOnKeyboardBinding.bind(view).apply {
val rippleBg = clip_holder.background as RippleDrawable val rippleBg = clipHolder.background as RippleDrawable
val layerDrawable = rippleBg.findDrawableByLayerId(R.id.clipboard_background_holder) as LayerDrawable val layerDrawable = rippleBg.findDrawableByLayerId(R.id.clipboard_background_holder) as LayerDrawable
layerDrawable.findDrawableByLayerId(R.id.clipboard_background_stroke).applyColorFilter(context.getStrokeColor()) layerDrawable.findDrawableByLayerId(R.id.clipboard_background_stroke).applyColorFilter(context.getStrokeColor())
layerDrawable.findDrawableByLayerId(R.id.clipboard_background_shape).applyColorFilter(backgroundColor) layerDrawable.findDrawableByLayerId(R.id.clipboard_background_shape).applyColorFilter(backgroundColor)
clip_value.apply { clipValue.apply {
text = clip.value text = clip.value
removeUnderlines() removeUnderlines()
setTextColor(textColor) setTextColor(textColor)
@ -81,13 +80,13 @@ class ClipsKeyboardAdapter(
@SuppressLint("UseCompatLoadingForDrawables") @SuppressLint("UseCompatLoadingForDrawables")
private fun setupSection(view: View, sectionLabel: ClipsSectionLabel) { private fun setupSection(view: View, sectionLabel: ClipsSectionLabel) {
view.apply { ItemSectionLabelBinding.bind(view).apply {
clips_section_label.apply { clipsSectionLabel.apply {
text = sectionLabel.value text = sectionLabel.value
setTextColor(textColor) setTextColor(textColor)
} }
clips_section_icon.apply { clipsSectionIcon.apply {
applyColorFilter(textColor) applyColorFilter(textColor)
if (sectionLabel.isCurrent) { if (sectionLabel.isCurrent) {

View File

@ -6,15 +6,13 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.emoji2.text.EmojiCompat import androidx.emoji2.text.EmojiCompat
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.simplemobiletools.keyboard.R import com.simplemobiletools.keyboard.databinding.ItemEmojiBinding
import kotlinx.android.synthetic.main.item_emoji.view.*
class EmojisAdapter(val context: Context, var items: List<String>, val itemClick: (emoji: String) -> Unit) : RecyclerView.Adapter<EmojisAdapter.ViewHolder>() { class EmojisAdapter(val context: Context, var items: List<String>, val itemClick: (emoji: String) -> Unit) : RecyclerView.Adapter<EmojisAdapter.ViewHolder>() {
private val layoutInflater = LayoutInflater.from(context) private val layoutInflater = LayoutInflater.from(context)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EmojisAdapter.ViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EmojisAdapter.ViewHolder {
val layoutId = R.layout.item_emoji val view = ItemEmojiBinding.inflate(layoutInflater, parent, false).root
val view = layoutInflater.inflate(layoutId, parent, false)
return ViewHolder(view) return ViewHolder(view)
} }
@ -29,7 +27,7 @@ class EmojisAdapter(val context: Context, var items: List<String>, val itemClick
private fun setupEmoji(view: View, emoji: String) { private fun setupEmoji(view: View, emoji: String) {
val processed = EmojiCompat.get().process(emoji) val processed = EmojiCompat.get().process(emoji)
view.emoji_value.text = processed ItemEmojiBinding.bind(view).emojiValue.text = processed
} }
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {