Implement ViewBinding

This commit is contained in:
Moez Bhatti
2020-03-13 22:47:55 -04:00
parent 582a823876
commit 37d8c45379
77 changed files with 1212 additions and 1201 deletions
@@ -31,10 +31,9 @@ import com.moez.QKSMS.common.util.Colors
import com.moez.QKSMS.common.util.DateFormatter
import com.moez.QKSMS.common.util.extensions.resolveThemeColor
import com.moez.QKSMS.common.util.extensions.setTint
import com.moez.QKSMS.databinding.ConversationListItemBinding
import com.moez.QKSMS.model.Conversation
import com.moez.QKSMS.util.PhoneNumberUtils
import kotlinx.android.synthetic.main.conversation_list_item.*
import kotlinx.android.synthetic.main.conversation_list_item.view.*
import javax.inject.Inject
class ConversationsAdapter @Inject constructor(
@@ -43,64 +42,62 @@ class ConversationsAdapter @Inject constructor(
private val dateFormatter: DateFormatter,
private val navigator: Navigator,
private val phoneNumberUtils: PhoneNumberUtils
) : QkRealmAdapter<Conversation>() {
) : QkRealmAdapter<Conversation, ConversationListItemBinding>() {
init {
// This is how we access the threadId for the swipe actions
setHasStableIds(true)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val view = layoutInflater.inflate(R.layout.conversation_list_item, parent, false)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder<ConversationListItemBinding> {
return QkViewHolder(parent, ConversationListItemBinding::inflate).apply {
if (viewType == 1) {
val textColorPrimary = parent.context.resolveThemeColor(android.R.attr.textColorPrimary)
if (viewType == 1) {
val textColorPrimary = parent.context.resolveThemeColor(android.R.attr.textColorPrimary)
binding.title.setTypeface(binding.title.typeface, Typeface.BOLD)
view.title.setTypeface(view.title.typeface, Typeface.BOLD)
binding.snippet.setTypeface(binding.snippet.typeface, Typeface.BOLD)
binding.snippet.setTextColor(textColorPrimary)
binding.snippet.maxLines = 5
view.snippet.setTypeface(view.snippet.typeface, Typeface.BOLD)
view.snippet.setTextColor(textColorPrimary)
view.snippet.maxLines = 5
binding.unread.isVisible = true
view.unread.isVisible = true
binding.date.setTypeface(binding.date.typeface, Typeface.BOLD)
binding.date.setTextColor(textColorPrimary)
}
view.date.setTypeface(view.date.typeface, Typeface.BOLD)
view.date.setTextColor(textColorPrimary)
}
return QkViewHolder(view).apply {
view.setOnClickListener {
binding.root.setOnClickListener {
val conversation = getItem(adapterPosition) ?: return@setOnClickListener
when (toggleSelection(conversation.id, false)) {
true -> view.isActivated = isSelected(conversation.id)
true -> binding.root.isActivated = isSelected(conversation.id)
false -> navigator.showConversation(conversation.id)
}
}
view.setOnLongClickListener {
binding.root.setOnLongClickListener {
val conversation = getItem(adapterPosition) ?: return@setOnLongClickListener true
toggleSelection(conversation.id)
view.isActivated = isSelected(conversation.id)
binding.root.isActivated = isSelected(conversation.id)
true
}
}
}
override fun onBindViewHolder(holder: QkViewHolder, position: Int) {
override fun onBindViewHolder(holder: QkViewHolder<ConversationListItemBinding>, position: Int) {
val conversation = getItem(position) ?: return
holder.containerView.isActivated = isSelected(conversation.id)
holder.binding.root.isActivated = isSelected(conversation.id)
holder.avatars.recipients = conversation.recipients
holder.title.collapseEnabled = conversation.recipients.size > 1
holder.title.text = conversation.getTitle()
holder.date.text = conversation.date.takeIf { it > 0 }?.let(dateFormatter::getConversationTimestamp)
holder.snippet.text = when {
holder.binding.avatars.recipients = conversation.recipients
holder.binding.title.collapseEnabled = conversation.recipients.size > 1
holder.binding.title.text = conversation.getTitle()
holder.binding.date.text = conversation.date.takeIf { it > 0 }?.let(dateFormatter::getConversationTimestamp)
holder.binding.snippet.text = when {
conversation.draft.isNotEmpty() -> context.getString(R.string.main_draft, conversation.draft)
conversation.me -> context.getString(R.string.main_sender_you, conversation.snippet)
else -> conversation.snippet
}
holder.pinned.isVisible = conversation.pinned
holder.binding.pinned.isVisible = conversation.pinned
// If the last message wasn't incoming, then the colour doesn't really matter anyway
val lastMessage = conversation.lastMessage
@@ -111,7 +108,7 @@ class ConversationsAdapter @Inject constructor(
}
}
holder.unread.setTint(colors.theme(recipient).theme)
holder.binding.unread.setTint(colors.theme(recipient).theme)
}
override fun getItemId(position: Int): Long {