migrating to ViewBinding part 1: Dialogs + Views (#2091)

This commit is contained in:
Konrad Pozniak 2021-03-07 19:04:22 +01:00 committed by GitHub
parent e7c514c28e
commit 5167b8578e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 43 additions and 47 deletions

View file

@ -23,8 +23,8 @@ import android.view.WindowManager
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import com.keylesspalace.tusky.R import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.adapter.AddPollOptionsAdapter import com.keylesspalace.tusky.adapter.AddPollOptionsAdapter
import com.keylesspalace.tusky.databinding.DialogAddPollBinding
import com.keylesspalace.tusky.entity.NewPoll import com.keylesspalace.tusky.entity.NewPoll
import kotlinx.android.synthetic.main.dialog_add_poll.view.*
fun showAddPollDialog( fun showAddPollDialog(
context: Context, context: Context,
@ -34,12 +34,12 @@ fun showAddPollDialog(
onUpdatePoll: (NewPoll) -> Unit onUpdatePoll: (NewPoll) -> Unit
) { ) {
val view = LayoutInflater.from(context).inflate(R.layout.dialog_add_poll, null) val binding = DialogAddPollBinding.inflate(LayoutInflater.from(context))
val dialog = AlertDialog.Builder(context) val dialog = AlertDialog.Builder(context)
.setIcon(R.drawable.ic_poll_24dp) .setIcon(R.drawable.ic_poll_24dp)
.setTitle(R.string.create_poll_title) .setTitle(R.string.create_poll_title)
.setView(view) .setView(binding.root)
.setNegativeButton(android.R.string.cancel, null) .setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(android.R.string.ok, null) .setPositiveButton(android.R.string.ok, null)
.create() .create()
@ -48,7 +48,7 @@ fun showAddPollDialog(
options = poll?.options?.toMutableList() ?: mutableListOf("", ""), options = poll?.options?.toMutableList() ?: mutableListOf("", ""),
maxOptionLength = maxOptionLength, maxOptionLength = maxOptionLength,
onOptionRemoved = { valid -> onOptionRemoved = { valid ->
view.addChoiceButton.isEnabled = true binding.addChoiceButton.isEnabled = true
dialog.getButton(AlertDialog.BUTTON_POSITIVE).isEnabled = valid dialog.getButton(AlertDialog.BUTTON_POSITIVE).isEnabled = valid
}, },
onOptionChanged = { valid -> onOptionChanged = { valid ->
@ -56,9 +56,9 @@ fun showAddPollDialog(
} }
) )
view.pollChoices.adapter = adapter binding.pollChoices.adapter = adapter
view.addChoiceButton.setOnClickListener { binding.addChoiceButton.setOnClickListener {
if (adapter.itemCount < maxOptionCount) { if (adapter.itemCount < maxOptionCount) {
adapter.addChoice() adapter.addChoice()
} }
@ -71,14 +71,14 @@ fun showAddPollDialog(
it <= poll?.expiresIn ?: 0 it <= poll?.expiresIn ?: 0
} }
view.pollDurationSpinner.setSelection(pollDurationId) binding.pollDurationSpinner.setSelection(pollDurationId)
view.multipleChoicesCheckBox.isChecked = poll?.multiple ?: false binding.multipleChoicesCheckBox.isChecked = poll?.multiple ?: false
dialog.setOnShowListener { dialog.setOnShowListener {
val button = dialog.getButton(AlertDialog.BUTTON_POSITIVE) val button = dialog.getButton(AlertDialog.BUTTON_POSITIVE)
button.setOnClickListener { button.setOnClickListener {
val selectedPollDurationId = view.pollDurationSpinner.selectedItemPosition val selectedPollDurationId = binding.pollDurationSpinner.selectedItemPosition
val pollDuration = context.resources val pollDuration = context.resources
.getIntArray(R.array.poll_duration_values)[selectedPollDurationId] .getIntArray(R.array.poll_duration_values)[selectedPollDurationId]
@ -86,7 +86,7 @@ fun showAddPollDialog(
onUpdatePoll(NewPoll( onUpdatePoll(NewPoll(
options = adapter.pollOptions, options = adapter.pollOptions,
expiresIn = pollDuration, expiresIn = pollDuration,
multiple = view.multipleChoicesCheckBox.isChecked multiple = binding.multipleChoicesCheckBox.isChecked
)) ))
dialog.dismiss() dialog.dismiss()
@ -97,5 +97,4 @@ fun showAddPollDialog(
// make the dialog focusable so the keyboard does not stay behind it // make the dialog focusable so the keyboard does not stay behind it
dialog.window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM) dialog.window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
} }

View file

@ -17,11 +17,12 @@ package com.keylesspalace.tusky.components.compose.view
import android.content.Context import android.content.Context
import android.util.AttributeSet import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout import android.widget.LinearLayout
import com.keylesspalace.tusky.R import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.adapter.PreviewPollOptionsAdapter import com.keylesspalace.tusky.adapter.PreviewPollOptionsAdapter
import com.keylesspalace.tusky.databinding.ViewPollPreviewBinding
import com.keylesspalace.tusky.entity.NewPoll import com.keylesspalace.tusky.entity.NewPoll
import kotlinx.android.synthetic.main.view_poll_preview.view.*
class PollPreviewView @JvmOverloads constructor( class PollPreviewView @JvmOverloads constructor(
context: Context?, context: Context?,
@ -29,11 +30,11 @@ class PollPreviewView @JvmOverloads constructor(
defStyleAttr: Int = 0) defStyleAttr: Int = 0)
: LinearLayout(context, attrs, defStyleAttr) { : LinearLayout(context, attrs, defStyleAttr) {
val adapter = PreviewPollOptionsAdapter() private val adapter = PreviewPollOptionsAdapter()
private val binding = ViewPollPreviewBinding.inflate(LayoutInflater.from(context), this)
init { init {
inflate(context, R.layout.view_poll_preview, this)
orientation = VERTICAL orientation = VERTICAL
setBackgroundResource(R.drawable.card_frame) setBackgroundResource(R.drawable.card_frame)
@ -42,8 +43,7 @@ class PollPreviewView @JvmOverloads constructor(
setPadding(padding, padding, padding, padding) setPadding(padding, padding, padding, padding)
pollPreviewOptions.adapter = adapter binding.pollPreviewOptions.adapter = adapter
} }
fun setPoll(poll: NewPoll){ fun setPoll(poll: NewPoll){
@ -52,13 +52,11 @@ class PollPreviewView @JvmOverloads constructor(
val pollDurationId = resources.getIntArray(R.array.poll_duration_values).indexOfLast { val pollDurationId = resources.getIntArray(R.array.poll_duration_values).indexOfLast {
it <= poll.expiresIn it <= poll.expiresIn
} }
pollDurationPreview.text = resources.getStringArray(R.array.poll_duration_names)[pollDurationId] binding.pollDurationPreview.text = resources.getStringArray(R.array.poll_duration_names)[pollDurationId]
} }
override fun setOnClickListener(l: OnClickListener?) { override fun setOnClickListener(l: OnClickListener?) {
super.setOnClickListener(l) super.setOnClickListener(l)
adapter.setOnClickListener(l) adapter.setOnClickListener(l)
} }
} }

View file

@ -3,14 +3,14 @@ package com.keylesspalace.tusky.view
import android.content.Context import android.content.Context
import android.util.AttributeSet import android.util.AttributeSet
import android.view.Gravity import android.view.Gravity
import android.view.LayoutInflater
import android.view.View import android.view.View
import android.widget.LinearLayout import android.widget.LinearLayout
import androidx.annotation.DrawableRes import androidx.annotation.DrawableRes
import androidx.annotation.StringRes import androidx.annotation.StringRes
import com.keylesspalace.tusky.R import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.databinding.ViewBackgroundMessageBinding
import com.keylesspalace.tusky.util.visible import com.keylesspalace.tusky.util.visible
import kotlinx.android.synthetic.main.view_background_message.view.*
/** /**
* This view is used for screens with downloadable content which may fail. * This view is used for screens with downloadable content which may fail.
@ -22,8 +22,9 @@ class BackgroundMessageView @JvmOverloads constructor(
defStyleAttr: Int = 0 defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) { ) : LinearLayout(context, attrs, defStyleAttr) {
private val binding = ViewBackgroundMessageBinding.inflate(LayoutInflater.from(context), this)
init { init {
View.inflate(context, R.layout.view_background_message, this)
gravity = Gravity.CENTER_HORIZONTAL gravity = Gravity.CENTER_HORIZONTAL
orientation = VERTICAL orientation = VERTICAL
@ -36,11 +37,14 @@ class BackgroundMessageView @JvmOverloads constructor(
* Setup image, message and button. * Setup image, message and button.
* If [clickListener] is `null` then the button will be hidden. * If [clickListener] is `null` then the button will be hidden.
*/ */
fun setup(@DrawableRes imageRes: Int, @StringRes messageRes: Int, fun setup(
clickListener: ((v: View) -> Unit)? = null) { @DrawableRes imageRes: Int,
messageTextView.setText(messageRes) @StringRes messageRes: Int,
imageView.setImageResource(imageRes) clickListener: ((v: View) -> Unit)? = null
button.setOnClickListener(clickListener) ) {
button.visible(clickListener != null) binding.messageTextView.setText(messageRes)
binding.imageView.setImageResource(imageRes)
binding.button.setOnClickListener(clickListener)
binding.button.visible(clickListener != null)
} }
} }

View file

@ -17,12 +17,13 @@ package com.keylesspalace.tusky.view
import android.content.Context import android.content.Context
import android.util.AttributeSet import android.util.AttributeSet
import android.view.LayoutInflater
import com.google.android.material.card.MaterialCardView import com.google.android.material.card.MaterialCardView
import com.keylesspalace.tusky.R import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.databinding.CardLicenseBinding
import com.keylesspalace.tusky.util.LinkHelper import com.keylesspalace.tusky.util.LinkHelper
import com.keylesspalace.tusky.util.ThemeUtils import com.keylesspalace.tusky.util.ThemeUtils
import com.keylesspalace.tusky.util.hide import com.keylesspalace.tusky.util.hide
import kotlinx.android.synthetic.main.card_license.view.*
class LicenseCard class LicenseCard
@JvmOverloads constructor( @JvmOverloads constructor(
@ -32,7 +33,7 @@ class LicenseCard
) : MaterialCardView(context, attrs, defStyleAttr) { ) : MaterialCardView(context, attrs, defStyleAttr) {
init { init {
inflate(context, R.layout.card_license, this) val binding = CardLicenseBinding.inflate(LayoutInflater.from(context), this)
setCardBackgroundColor(ThemeUtils.getColor(context, R.attr.colorSurface)) setCardBackgroundColor(ThemeUtils.getColor(context, R.attr.colorSurface))
@ -43,12 +44,12 @@ class LicenseCard
val link: String? = a.getString(R.styleable.LicenseCard_link) val link: String? = a.getString(R.styleable.LicenseCard_link)
a.recycle() a.recycle()
licenseCardName.text = name binding.licenseCardName.text = name
licenseCardLicense.text = license binding.licenseCardLicense.text = license
if(link.isNullOrBlank()) { if(link.isNullOrBlank()) {
licenseCardLink.hide() binding.licenseCardLink.hide()
} else { } else {
licenseCardLink.text = link binding.licenseCardLink.text = link
setOnClickListener { LinkHelper.openLink(link, context) } setOnClickListener { LinkHelper.openLink(link, context) }
} }

View file

@ -3,29 +3,24 @@
package com.keylesspalace.tusky.view package com.keylesspalace.tusky.view
import android.app.Activity import android.app.Activity
import android.widget.CheckBox
import android.widget.Spinner
import android.widget.TextView
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import com.keylesspalace.tusky.R import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.databinding.DialogMuteAccountBinding
fun showMuteAccountDialog( fun showMuteAccountDialog(
activity: Activity, activity: Activity,
accountUsername: String, accountUsername: String,
onOk: (notifications: Boolean, duration: Int) -> Unit onOk: (notifications: Boolean, duration: Int) -> Unit
) { ) {
val view = activity.layoutInflater.inflate(R.layout.dialog_mute_account, null) val binding = DialogMuteAccountBinding.inflate(activity.layoutInflater)
(view.findViewById(R.id.warning) as TextView).text = binding.warning.text = activity.getString(R.string.dialog_mute_warning, accountUsername)
activity.getString(R.string.dialog_mute_warning, accountUsername) binding.checkbox.isChecked = true
val checkbox: CheckBox = view.findViewById(R.id.checkbox)
checkbox.isChecked = true
AlertDialog.Builder(activity) AlertDialog.Builder(activity)
.setView(view) .setView(binding.root)
.setPositiveButton(android.R.string.ok) { _, _ -> .setPositiveButton(android.R.string.ok) { _, _ ->
val spinner: Spinner = view.findViewById(R.id.duration)
val durationValues = activity.resources.getIntArray(R.array.mute_duration_values) val durationValues = activity.resources.getIntArray(R.array.mute_duration_values)
onOk(checkbox.isChecked, durationValues[spinner.selectedItemPosition]) onOk(binding.checkbox.isChecked, durationValues[binding.duration.selectedItemPosition])
} }
.setNegativeButton(android.R.string.cancel, null) .setNegativeButton(android.R.string.cancel, null)
.show() .show()

View file

@ -23,7 +23,6 @@
android:text="@string/dialog_mute_hide_notifications"/> android:text="@string/dialog_mute_hide_notifications"/>
<TextView <TextView
android:id="@+id/duration_label"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingTop="20dp" android:paddingTop="20dp"