Refactor skipping checkable state animation

This commit is contained in:
Koitharu
2023-05-08 19:46:17 +03:00
parent c0544e25af
commit 023605e246
5 changed files with 19 additions and 20 deletions

View File

@@ -25,6 +25,7 @@ import org.koitharu.kotatsu.favourites.ui.categories.FavouriteCategoriesActivity
import org.koitharu.kotatsu.parsers.model.SortOrder
import org.koitharu.kotatsu.utils.ext.getDisplayMessage
import org.koitharu.kotatsu.utils.ext.getSerializableCompat
import org.koitharu.kotatsu.utils.ext.setChecked
import com.google.android.material.R as materialR
@AndroidEntryPoint
@@ -112,10 +113,8 @@ class FavouritesCategoryEditActivity :
selectedSortOrder = category?.order
val sortText = getString((category?.order ?: SortOrder.NEWEST).titleRes)
binding.editSort.setText(sortText, false)
binding.switchTracker.isChecked = category?.isTrackingEnabled ?: true
binding.switchTracker.jumpDrawablesToCurrentState()
binding.switchShelf.isChecked = category?.isVisibleInLibrary ?: true
binding.switchShelf.jumpDrawablesToCurrentState()
binding.switchTracker.setChecked(category?.isTrackingEnabled ?: true, false)
binding.switchShelf.setChecked(category?.isVisibleInLibrary ?: true, false)
}
private fun onError(e: Throwable) {

View File

@@ -4,6 +4,7 @@ import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.databinding.ItemSourceLocaleBinding
import org.koitharu.kotatsu.settings.onboard.model.SourceLocale
import org.koitharu.kotatsu.utils.ext.setChecked
import org.koitharu.kotatsu.utils.ext.textAndVisible
fun sourceLocaleAD(
@@ -19,9 +20,6 @@ fun sourceLocaleAD(
bind { payloads ->
binding.textViewTitle.text = item.title ?: getString(R.string.different_languages)
binding.textViewDescription.textAndVisible = item.summary
binding.switchToggle.isChecked = item.isChecked
if (payloads.isEmpty()) {
binding.switchToggle.jumpDrawablesToCurrentState()
}
binding.switchToggle.setChecked(item.isChecked, payloads.isNotEmpty())
}
}

View File

@@ -19,6 +19,7 @@ import org.koitharu.kotatsu.databinding.FragmentToolsBinding
import org.koitharu.kotatsu.download.ui.list.DownloadsActivity
import org.koitharu.kotatsu.settings.SettingsActivity
import org.koitharu.kotatsu.settings.about.AppUpdateDialog
import org.koitharu.kotatsu.utils.ext.setChecked
@AndroidEntryPoint
class ToolsFragment :
@@ -42,10 +43,7 @@ class ToolsFragment :
binding.memoryUsageView.setManageButtonOnClickListener(this)
viewModel.isIncognitoModeEnabled.observe(viewLifecycleOwner) {
if (binding.switchIncognito.isChecked != it) {
binding.switchIncognito.isChecked = it
binding.switchIncognito.jumpDrawablesToCurrentState()
}
binding.switchIncognito.setChecked(it, false)
}
viewModel.storageUsage.observe(viewLifecycleOwner) {
binding.memoryUsageView.bind(it)

View File

@@ -10,6 +10,7 @@ import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.databinding.ItemCategoryCheckableMultipleBinding
import org.koitharu.kotatsu.databinding.ItemShelfSectionDraggableBinding
import org.koitharu.kotatsu.shelf.domain.ShelfSection
import org.koitharu.kotatsu.utils.ext.setChecked
@SuppressLint("ClickableViewAccessibility")
fun shelfSectionAD(
@@ -42,10 +43,7 @@ fun shelfSectionAD(
bind { payloads ->
binding.textViewTitle.setText(item.section.titleResId)
binding.switchToggle.isChecked = item.isChecked
if (payloads.isEmpty()) {
binding.switchToggle.jumpDrawablesToCurrentState()
}
binding.switchToggle.setChecked(item.isChecked, payloads.isNotEmpty())
}
}
@@ -65,10 +63,7 @@ fun shelfCategoryAD(
bind { payloads ->
binding.root.text = item.title
binding.root.isChecked = item.isChecked
if (payloads.isEmpty()) {
binding.root.jumpDrawablesToCurrentState()
}
binding.root.setChecked(item.isChecked, payloads.isNotEmpty())
}
}

View File

@@ -7,6 +7,7 @@ import android.view.View.MeasureSpec
import android.view.ViewGroup
import android.view.ViewParent
import android.view.inputmethod.InputMethodManager
import android.widget.Checkable
import androidx.core.view.children
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
@@ -192,3 +193,11 @@ fun View.measureDimension(desiredSize: Int, measureSpec: Int): Int {
}
return result
}
fun <V> V.setChecked(checked: Boolean, animate: Boolean) where V : View, V : Checkable {
val skipAnimation = !animate && checked != isChecked
isChecked = checked
if (skipAnimation) {
jumpDrawablesToCurrentState()
}
}