From f293670c14302bbb871c7e24a1e7b85675efd484 Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Sun, 21 Mar 2021 12:42:28 +0100 Subject: [PATCH] migrating to ViewBinding part 6: the final cleanup (#2117) --- app/build.gradle | 5 +- .../components/compose/ComposeActivity.kt | 4 +- .../report/adapter/StatusViewHolder.kt | 67 +++++++++---------- .../report/adapter/StatusesAdapter.kt | 22 +++--- .../com/keylesspalace/tusky/db/DraftEntity.kt | 2 +- .../keylesspalace/tusky/entity/Attachment.kt | 2 +- .../com/keylesspalace/tusky/entity/Emoji.kt | 2 +- .../keylesspalace/tusky/entity/NewStatus.kt | 2 +- .../tusky/service/SendTootService.kt | 2 +- .../tusky/viewdata/AttachmentViewData.kt | 2 +- build.gradle | 4 +- 11 files changed, 52 insertions(+), 62 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 583abb11d..dc240af0e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,7 +1,7 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' -apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' +apply plugin: 'kotlin-parcelize' apply from: "../instance-build.gradle" @@ -64,9 +64,6 @@ android { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } - androidExtensions { - experimental = true - } buildFeatures { viewBinding true } diff --git a/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt b/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt index 5c5b35328..649ed1420 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt @@ -76,10 +76,10 @@ import com.mikepenz.iconics.IconicsDrawable import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial import com.mikepenz.iconics.utils.colorInt import com.mikepenz.iconics.utils.sizeDp -import kotlinx.android.parcel.Parcelize +import kotlinx.parcelize.Parcelize import java.io.File import java.io.IOException -import java.util.* +import java.util.Locale import javax.inject.Inject import kotlin.math.max import kotlin.math.min diff --git a/app/src/main/java/com/keylesspalace/tusky/components/report/adapter/StatusViewHolder.kt b/app/src/main/java/com/keylesspalace/tusky/components/report/adapter/StatusViewHolder.kt index 8201de2e5..88a246f40 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/report/adapter/StatusViewHolder.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/report/adapter/StatusViewHolder.kt @@ -21,6 +21,7 @@ import android.view.View import androidx.recyclerview.widget.RecyclerView import com.keylesspalace.tusky.R import com.keylesspalace.tusky.components.report.model.StatusViewState +import com.keylesspalace.tusky.databinding.ItemReportStatusBinding import com.keylesspalace.tusky.entity.Emoji import com.keylesspalace.tusky.entity.Status import com.keylesspalace.tusky.interfaces.LinkListener @@ -28,16 +29,15 @@ import com.keylesspalace.tusky.util.* import com.keylesspalace.tusky.util.StatusViewHelper.Companion.COLLAPSE_INPUT_FILTER import com.keylesspalace.tusky.util.StatusViewHelper.Companion.NO_INPUT_FILTER import com.keylesspalace.tusky.viewdata.toViewData -import kotlinx.android.synthetic.main.item_report_status.view.* import java.util.* class StatusViewHolder( - itemView: View, + private val binding: ItemReportStatusBinding, private val statusDisplayOptions: StatusDisplayOptions, private val viewState: StatusViewState, private val adapterHandler: AdapterHandler, private val getStatusForPosition: (Int) -> Status? -) : RecyclerView.ViewHolder(itemView) { +) : RecyclerView.ViewHolder(binding.root) { private val mediaViewHeight = itemView.context.resources.getDimensionPixelSize(R.dimen.status_media_preview_height) private val statusViewHelper = StatusViewHelper(itemView) @@ -56,16 +56,16 @@ class StatusViewHolder( } init { - itemView.statusSelection.setOnCheckedChangeListener { _, isChecked -> + binding.statusSelection.setOnCheckedChangeListener { _, isChecked -> status()?.let { status -> adapterHandler.setStatusChecked(status, isChecked) } } - itemView.status_media_preview_container.clipToOutline = true + binding.statusMediaPreviewContainer.clipToOutline = true } fun bind(status: Status) { - itemView.statusSelection.isChecked = adapterHandler.isStatusChecked(status.id) + binding.statusSelection.isChecked = adapterHandler.isStatusChecked(status.id) updateTextView() @@ -86,18 +86,18 @@ class StatusViewHolder( if (status.spoilerText.isBlank()) { setTextVisible(true, status.content, status.mentions, status.emojis, adapterHandler) - itemView.statusContentWarningButton.hide() - itemView.statusContentWarningDescription.hide() + binding.statusContentWarningButton.hide() + binding.statusContentWarningDescription.hide() } else { - val emojiSpoiler = status.spoilerText.emojify(status.emojis, itemView.statusContentWarningDescription, statusDisplayOptions.animateEmojis) - itemView.statusContentWarningDescription.text = emojiSpoiler - itemView.statusContentWarningDescription.show() - itemView.statusContentWarningButton.show() + val emojiSpoiler = status.spoilerText.emojify(status.emojis, binding.statusContentWarningDescription, statusDisplayOptions.animateEmojis) + binding.statusContentWarningDescription.text = emojiSpoiler + binding.statusContentWarningDescription.show() + binding.statusContentWarningButton.show() setContentWarningButtonText(viewState.isContentShow(status.id, true)) - itemView.statusContentWarningButton.setOnClickListener { + binding.statusContentWarningButton.setOnClickListener { status()?.let { status -> val contentShown = viewState.isContentShow(status.id, true) - itemView.statusContentWarningDescription.invalidate() + binding.statusContentWarningDescription.invalidate() viewState.setContentShow(status.id, !contentShown) setTextVisible(!contentShown, status.content, status.mentions, status.emojis, adapterHandler) setContentWarningButtonText(!contentShown) @@ -110,9 +110,9 @@ class StatusViewHolder( private fun setContentWarningButtonText(contentShown: Boolean) { if(contentShown) { - itemView.statusContentWarningButton.setText(R.string.status_content_warning_show_less) + binding.statusContentWarningButton.setText(R.string.status_content_warning_show_less) } else { - itemView.statusContentWarningButton.setText(R.string.status_content_warning_show_more) + binding.statusContentWarningButton.setText(R.string.status_content_warning_show_more) } } @@ -122,26 +122,26 @@ class StatusViewHolder( emojis: List, listener: LinkListener) { if (expanded) { - val emojifiedText = content.emojify(emojis, itemView.statusContent, statusDisplayOptions.animateEmojis) - LinkHelper.setClickableText(itemView.statusContent, emojifiedText, mentions, listener) + val emojifiedText = content.emojify(emojis, binding.statusContent, statusDisplayOptions.animateEmojis) + LinkHelper.setClickableText(binding.statusContent, emojifiedText, mentions, listener) } else { - LinkHelper.setClickableMentions(itemView.statusContent, mentions, listener) + LinkHelper.setClickableMentions(binding.statusContent, mentions, listener) } - if (itemView.statusContent.text.isNullOrBlank()) { - itemView.statusContent.hide() + if (binding.statusContent.text.isNullOrBlank()) { + binding.statusContent.hide() } else { - itemView.statusContent.show() + binding.statusContent.show() } } private fun setCreatedAt(createdAt: Date?) { if (statusDisplayOptions.useAbsoluteTime) { - itemView.timestampInfo.text = statusViewHelper.getAbsoluteTime(createdAt) + binding.timestampInfo.text = statusViewHelper.getAbsoluteTime(createdAt) } else { - itemView.timestampInfo.text = if (createdAt != null) { + binding.timestampInfo.text = if (createdAt != null) { val then = createdAt.time val now = System.currentTimeMillis() - TimestampUtils.getRelativeTimeSpanString(itemView.timestampInfo.context, then, now) + TimestampUtils.getRelativeTimeSpanString(binding.timestampInfo.context, then, now) } else { // unknown minutes~ "?m" @@ -149,28 +149,27 @@ class StatusViewHolder( } } - private fun setupCollapsedState(collapsible: Boolean, collapsed: Boolean, expanded: Boolean, spoilerText: String) { /* input filter for TextViews have to be set before text */ if (collapsible && (expanded || TextUtils.isEmpty(spoilerText))) { - itemView.buttonToggleContent.setOnClickListener{ + binding.buttonToggleContent.setOnClickListener{ status()?.let { status -> viewState.setCollapsed(status.id, !collapsed) updateTextView() } } - itemView.buttonToggleContent.show() + binding.buttonToggleContent.show() if (collapsed) { - itemView.buttonToggleContent.setText(R.string.status_content_show_more) - itemView.statusContent.filters = COLLAPSE_INPUT_FILTER + binding.buttonToggleContent.setText(R.string.status_content_show_more) + binding.statusContent.filters = COLLAPSE_INPUT_FILTER } else { - itemView.buttonToggleContent.setText(R.string.status_content_show_less) - itemView.statusContent.filters = NO_INPUT_FILTER + binding.buttonToggleContent.setText(R.string.status_content_show_less) + binding.statusContent.filters = NO_INPUT_FILTER } } else { - itemView.buttonToggleContent.hide() - itemView.statusContent.filters = NO_INPUT_FILTER + binding.buttonToggleContent.hide() + binding.statusContent.filters = NO_INPUT_FILTER } } diff --git a/app/src/main/java/com/keylesspalace/tusky/components/report/adapter/StatusesAdapter.kt b/app/src/main/java/com/keylesspalace/tusky/components/report/adapter/StatusesAdapter.kt index 34817ca0d..b66ac4f3c 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/report/adapter/StatusesAdapter.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/report/adapter/StatusesAdapter.kt @@ -20,8 +20,8 @@ import android.view.ViewGroup import androidx.paging.PagedListAdapter import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.RecyclerView -import com.keylesspalace.tusky.R import com.keylesspalace.tusky.components.report.model.StatusViewState +import com.keylesspalace.tusky.databinding.ItemReportStatusBinding import com.keylesspalace.tusky.entity.Status import com.keylesspalace.tusky.util.StatusDisplayOptions @@ -29,29 +29,25 @@ class StatusesAdapter( private val statusDisplayOptions: StatusDisplayOptions, private val statusViewState: StatusViewState, private val adapterHandler: AdapterHandler -) : PagedListAdapter(STATUS_COMPARATOR) { +) : PagedListAdapter(STATUS_COMPARATOR) { private val statusForPosition: (Int) -> Status? = { position: Int -> if (position != RecyclerView.NO_POSITION) getItem(position) else null } - - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { - val view = LayoutInflater.from(parent.context) - .inflate(R.layout.item_report_status, parent, false) - return StatusViewHolder(view, statusDisplayOptions, statusViewState, adapterHandler, + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StatusViewHolder { + val binding = ItemReportStatusBinding.inflate(LayoutInflater.from(parent.context), parent, false) + return StatusViewHolder(binding, statusDisplayOptions, statusViewState, adapterHandler, statusForPosition) } - override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { + override fun onBindViewHolder(holder: StatusViewHolder, position: Int) { getItem(position)?.let { status -> - (holder as? StatusViewHolder)?.bind(status) + holder.bind(status) } - } companion object { - val STATUS_COMPARATOR = object : DiffUtil.ItemCallback() { override fun areContentsTheSame(oldItem: Status, newItem: Status): Boolean = oldItem == newItem @@ -59,7 +55,5 @@ class StatusesAdapter( override fun areItemsTheSame(oldItem: Status, newItem: Status): Boolean = oldItem.id == newItem.id } - } - -} \ No newline at end of file +} diff --git a/app/src/main/java/com/keylesspalace/tusky/db/DraftEntity.kt b/app/src/main/java/com/keylesspalace/tusky/db/DraftEntity.kt index be1eca589..184ff2c30 100644 --- a/app/src/main/java/com/keylesspalace/tusky/db/DraftEntity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/db/DraftEntity.kt @@ -23,7 +23,7 @@ import androidx.room.PrimaryKey import androidx.room.TypeConverters import com.keylesspalace.tusky.entity.NewPoll import com.keylesspalace.tusky.entity.Status -import kotlinx.android.parcel.Parcelize +import kotlinx.parcelize.Parcelize @Entity @TypeConverters(Converters::class) diff --git a/app/src/main/java/com/keylesspalace/tusky/entity/Attachment.kt b/app/src/main/java/com/keylesspalace/tusky/entity/Attachment.kt index 1de7bd787..3e14519ac 100644 --- a/app/src/main/java/com/keylesspalace/tusky/entity/Attachment.kt +++ b/app/src/main/java/com/keylesspalace/tusky/entity/Attachment.kt @@ -22,7 +22,7 @@ import com.google.gson.JsonElement import com.google.gson.JsonParseException import com.google.gson.annotations.JsonAdapter import com.google.gson.annotations.SerializedName -import kotlinx.android.parcel.Parcelize +import kotlinx.parcelize.Parcelize @Parcelize data class Attachment( diff --git a/app/src/main/java/com/keylesspalace/tusky/entity/Emoji.kt b/app/src/main/java/com/keylesspalace/tusky/entity/Emoji.kt index fe7a22c73..42bb99e93 100644 --- a/app/src/main/java/com/keylesspalace/tusky/entity/Emoji.kt +++ b/app/src/main/java/com/keylesspalace/tusky/entity/Emoji.kt @@ -17,7 +17,7 @@ package com.keylesspalace.tusky.entity import android.os.Parcelable import com.google.gson.annotations.SerializedName -import kotlinx.android.parcel.Parcelize +import kotlinx.parcelize.Parcelize @Parcelize data class Emoji( diff --git a/app/src/main/java/com/keylesspalace/tusky/entity/NewStatus.kt b/app/src/main/java/com/keylesspalace/tusky/entity/NewStatus.kt index ebc979f36..16cbc6a7c 100644 --- a/app/src/main/java/com/keylesspalace/tusky/entity/NewStatus.kt +++ b/app/src/main/java/com/keylesspalace/tusky/entity/NewStatus.kt @@ -17,7 +17,7 @@ package com.keylesspalace.tusky.entity import android.os.Parcelable import com.google.gson.annotations.SerializedName -import kotlinx.android.parcel.Parcelize +import kotlinx.parcelize.Parcelize data class NewStatus( val status: String, diff --git a/app/src/main/java/com/keylesspalace/tusky/service/SendTootService.kt b/app/src/main/java/com/keylesspalace/tusky/service/SendTootService.kt index b54a941b1..c6b07bb72 100644 --- a/app/src/main/java/com/keylesspalace/tusky/service/SendTootService.kt +++ b/app/src/main/java/com/keylesspalace/tusky/service/SendTootService.kt @@ -28,7 +28,7 @@ import com.keylesspalace.tusky.entity.Status import com.keylesspalace.tusky.network.MastodonApi import com.keylesspalace.tusky.util.SaveTootHelper import dagger.android.AndroidInjection -import kotlinx.android.parcel.Parcelize +import kotlinx.parcelize.Parcelize import retrofit2.Call import retrofit2.Callback import retrofit2.Response diff --git a/app/src/main/java/com/keylesspalace/tusky/viewdata/AttachmentViewData.kt b/app/src/main/java/com/keylesspalace/tusky/viewdata/AttachmentViewData.kt index 52eb31ac5..a7b2bffc7 100644 --- a/app/src/main/java/com/keylesspalace/tusky/viewdata/AttachmentViewData.kt +++ b/app/src/main/java/com/keylesspalace/tusky/viewdata/AttachmentViewData.kt @@ -3,7 +3,7 @@ package com.keylesspalace.tusky.viewdata import android.os.Parcelable import com.keylesspalace.tusky.entity.Attachment import com.keylesspalace.tusky.entity.Status -import kotlinx.android.parcel.Parcelize +import kotlinx.parcelize.Parcelize @Parcelize data class AttachmentViewData( diff --git a/build.gradle b/build.gradle index 75d0217a4..d6ef9eec0 100644 --- a/build.gradle +++ b/build.gradle @@ -1,11 +1,11 @@ buildscript { - ext.kotlin_version = '1.4.21' + ext.kotlin_version = '1.4.31' repositories { google() jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:4.1.1' + classpath 'com.android.tools.build:gradle:4.1.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } }