Replace more usages of Kotlin synthetic views

This commit is contained in:
arkon
2020-11-28 14:56:57 -05:00
parent aa98cd0da0
commit 322d66d282
9 changed files with 88 additions and 102 deletions

View File

@@ -2,6 +2,7 @@ package eu.kanade.tachiyomi.widget
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.animation.Animation
import android.view.animation.AnimationUtils
@@ -11,8 +12,7 @@ import androidx.annotation.MenuRes
import androidx.appcompat.view.ActionMode
import androidx.core.view.isVisible
import eu.kanade.tachiyomi.R
import kotlinx.android.synthetic.main.common_action_toolbar.view.common_action_menu
import kotlinx.android.synthetic.main.common_action_toolbar.view.common_action_toolbar
import eu.kanade.tachiyomi.databinding.CommonActionToolbarBinding
/**
* A toolbar holding only menu items.
@@ -20,23 +20,25 @@ import kotlinx.android.synthetic.main.common_action_toolbar.view.common_action_t
class ActionToolbar @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
FrameLayout(context, attrs) {
private val binding: CommonActionToolbarBinding
init {
inflate(context, R.layout.common_action_toolbar, this)
binding = CommonActionToolbarBinding.inflate(LayoutInflater.from(context), this, true)
}
/**
* Remove menu items and remove listener.
*/
fun destroy() {
common_action_menu.menu.clear()
common_action_menu.setOnMenuItemClickListener(null)
binding.commonActionMenu.menu.clear()
binding.commonActionMenu.setOnMenuItemClickListener(null)
}
/**
* Gets a menu item if found.
*/
fun findItem(@IdRes itemId: Int): MenuItem? {
return common_action_menu.menu.findItem(itemId)
return binding.commonActionMenu.menu.findItem(itemId)
}
/**
@@ -44,14 +46,14 @@ class ActionToolbar @JvmOverloads constructor(context: Context, attrs: Attribute
*/
fun show(mode: ActionMode, @MenuRes menuRes: Int, listener: (item: MenuItem?) -> Boolean) {
// Avoid re-inflating the menu
if (common_action_menu.menu.size() == 0) {
mode.menuInflater.inflate(menuRes, common_action_menu.menu)
common_action_menu.setOnMenuItemClickListener { listener(it) }
if (binding.commonActionMenu.menu.size() == 0) {
mode.menuInflater.inflate(menuRes, binding.commonActionMenu.menu)
binding.commonActionMenu.setOnMenuItemClickListener { listener(it) }
}
common_action_toolbar.isVisible = true
binding.commonActionToolbar.isVisible = true
val bottomAnimation = AnimationUtils.loadAnimation(context, R.anim.enter_from_bottom)
common_action_toolbar.startAnimation(bottomAnimation)
binding.commonActionToolbar.startAnimation(bottomAnimation)
}
/**
@@ -62,10 +64,10 @@ class ActionToolbar @JvmOverloads constructor(context: Context, attrs: Attribute
bottomAnimation.setAnimationListener(
object : SimpleAnimationListener() {
override fun onAnimationEnd(animation: Animation) {
common_action_toolbar.isVisible = false
binding.commonActionToolbar.isVisible = false
}
}
)
common_action_toolbar.startAnimation(bottomAnimation)
binding.commonActionToolbar.startAnimation(bottomAnimation)
}
}

View File

@@ -2,29 +2,30 @@ package eu.kanade.tachiyomi.widget
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import androidx.annotation.StringRes
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.util.view.inflate
import kotlinx.android.synthetic.main.common_dialog_with_checkbox.view.checkbox_option
import kotlinx.android.synthetic.main.common_dialog_with_checkbox.view.description
import eu.kanade.tachiyomi.databinding.CommonDialogWithCheckboxBinding
class DialogCheckboxView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
LinearLayout(context, attrs) {
private val binding: CommonDialogWithCheckboxBinding
init {
addView(inflate(R.layout.common_dialog_with_checkbox))
binding = CommonDialogWithCheckboxBinding.inflate(LayoutInflater.from(context), this, false)
addView(binding.root)
}
fun setDescription(@StringRes id: Int) {
description.text = context.getString(id)
binding.description.text = context.getString(id)
}
fun setOptionDescription(@StringRes id: Int) {
checkbox_option.text = context.getString(id)
binding.checkboxOption.text = context.getString(id)
}
fun isChecked(): Boolean {
return checkbox_option.isChecked
return binding.checkboxOption.isChecked
}
}

View File

@@ -3,15 +3,10 @@ package eu.kanade.tachiyomi.widget
import android.content.Context
import android.text.SpannableStringBuilder
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.util.view.inflate
import kotlinx.android.synthetic.main.download_custom_amount.view.btn_decrease
import kotlinx.android.synthetic.main.download_custom_amount.view.btn_decrease_10
import kotlinx.android.synthetic.main.download_custom_amount.view.btn_increase
import kotlinx.android.synthetic.main.download_custom_amount.view.btn_increase_10
import kotlinx.android.synthetic.main.download_custom_amount.view.myNumber
import eu.kanade.tachiyomi.databinding.DownloadCustomAmountBinding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
@@ -26,8 +21,6 @@ import timber.log.Timber
class DialogCustomDownloadView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
LinearLayout(context, attrs) {
private val scope = CoroutineScope(Job() + Dispatchers.Main)
/**
* Current amount of custom download chooser.
*/
@@ -44,44 +37,43 @@ class DialogCustomDownloadView @JvmOverloads constructor(context: Context, attrs
*/
private var max = 0
private val scope = CoroutineScope(Job() + Dispatchers.Main)
private val binding: DownloadCustomAmountBinding
init {
// Add view to stack
addView(inflate(R.layout.download_custom_amount))
binding = DownloadCustomAmountBinding.inflate(LayoutInflater.from(context), this, false)
addView(binding.root)
}
/**
* Called when view is added
*
* @param child
*/
override fun onViewAdded(child: View) {
super.onViewAdded(child)
// Set download count to 0.
myNumber.text = SpannableStringBuilder(getAmount(0).toString())
binding.myNumber.text = SpannableStringBuilder(getAmount(0).toString())
// When user presses button decrease amount by 10.
btn_decrease_10.setOnClickListener {
myNumber.text = SpannableStringBuilder(getAmount(amount - 10).toString())
binding.btnDecrease10.setOnClickListener {
binding.myNumber.text = SpannableStringBuilder(getAmount(amount - 10).toString())
}
// When user presses button increase amount by 10.
btn_increase_10.setOnClickListener {
myNumber.text = SpannableStringBuilder(getAmount(amount + 10).toString())
binding.btnIncrease10.setOnClickListener {
binding.myNumber.text = SpannableStringBuilder(getAmount(amount + 10).toString())
}
// When user presses button decrease amount by 1.
btn_decrease.setOnClickListener {
myNumber.text = SpannableStringBuilder(getAmount(amount - 1).toString())
binding.btnDecrease.setOnClickListener {
binding.myNumber.text = SpannableStringBuilder(getAmount(amount - 1).toString())
}
// When user presses button increase amount by 1.
btn_increase.setOnClickListener {
myNumber.text = SpannableStringBuilder(getAmount(amount + 1).toString())
binding.btnIncrease.setOnClickListener {
binding.myNumber.text = SpannableStringBuilder(getAmount(amount + 1).toString())
}
// When user inputs custom number set amount equal to input.
myNumber.textChanges()
binding.myNumber.textChanges()
.onEach {
try {
amount = getAmount(Integer.parseInt(it.toString()))

View File

@@ -2,22 +2,22 @@ package eu.kanade.tachiyomi.widget
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import android.widget.RelativeLayout
import androidx.annotation.StringRes
import androidx.appcompat.widget.AppCompatButton
import androidx.core.view.isVisible
import eu.kanade.tachiyomi.R
import kotlinx.android.synthetic.main.common_view_empty.view.actions_container
import kotlinx.android.synthetic.main.common_view_empty.view.text_face
import kotlinx.android.synthetic.main.common_view_empty.view.text_label
import eu.kanade.tachiyomi.databinding.CommonViewEmptyBinding
import kotlin.random.Random
class EmptyView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
RelativeLayout(context, attrs) {
private val binding: CommonViewEmptyBinding
init {
inflate(context, R.layout.common_view_empty, this)
binding = CommonViewEmptyBinding.inflate(LayoutInflater.from(context), this, true)
}
/**
@@ -36,10 +36,10 @@ class EmptyView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
}
fun show(message: String, actions: List<Action>? = null) {
text_face.text = getRandomErrorFace()
text_label.text = message
binding.textFace.text = getRandomErrorFace()
binding.textLabel.text = message
actions_container.removeAllViews()
binding.actionsContainer.removeAllViews()
if (!actions.isNullOrEmpty()) {
actions.forEach {
val button = AppCompatButton(context).apply {
@@ -52,7 +52,7 @@ class EmptyView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
setOnClickListener(it.listener)
}
actions_container.addView(button)
binding.actionsContainer.addView(button)
}
}