Use Material3 Switch and cleanup

This commit is contained in:
Iamlooker 2022-06-29 19:47:15 +05:30
parent c7ba399b53
commit c599cbc75f
No known key found for this signature in database
GPG Key ID: 16F53B972BAECA48
4 changed files with 18 additions and 165 deletions

View File

@ -4,20 +4,22 @@ import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.material.Switch import androidx.compose.material3.Switch
import androidx.compose.material3.SwitchDefaults
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
@Composable @Composable
fun SwitchPreference( fun SwitchPreference(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
text: String, text: String,
initSelected: Boolean, initSelected: () -> Boolean,
onCheckedChanged: (Boolean) -> Unit onCheckedChanged: (Boolean) -> Unit
) { ) {
Row( Row(
@ -26,14 +28,16 @@ fun SwitchPreference(
.fillMaxWidth(), .fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically verticalAlignment = Alignment.CenterVertically
) { ) {
val (selected, select) = remember { mutableStateOf(initSelected) } val (selected, select) = remember { mutableStateOf(initSelected()) }
Text(text = text) Text(text = text)
Spacer(modifier = Modifier.weight(1f)) Spacer(modifier = Modifier.weight(1f))
Switch( Switch(
checked = selected, checked = selected,
colors = SwitchDefaults.colors(uncheckedBorderColor = Color.Transparent),
onCheckedChange = { onCheckedChange = {
select(it) select(it)
onCheckedChanged(it) onCheckedChanged(it)
}) }
)
} }
} }

View File

@ -453,23 +453,27 @@ class AppSheetX() : FullscreenBottomSheetDialogFragment(), Callbacks {
} }
item { item {
AnimatedVisibility(visible = product.canUpdate(installed)) { AnimatedVisibility(visible = product.canUpdate(installed)) {
SwitchPreference(text = stringResource(id = R.string.ignore_this_update), SwitchPreference(
initSelected = extras?.ignoredVersion == product.versionCode, text = stringResource(id = R.string.ignore_this_update),
initSelected = { extras?.ignoredVersion == product.versionCode },
onCheckedChanged = { onCheckedChanged = {
viewModel.setIgnoredVersion( viewModel.setIgnoredVersion(
product.packageName, product.packageName,
if (it) product.versionCode else 0 if (it) product.versionCode else 0
) )
}) }
)
} }
} }
item { item {
AnimatedVisibility(visible = installed != null) { AnimatedVisibility(visible = installed != null) {
SwitchPreference(text = stringResource(id = R.string.ignore_all_updates), SwitchPreference(
initSelected = extras?.ignoreUpdates == true, text = stringResource(id = R.string.ignore_all_updates),
initSelected = { extras?.ignoreUpdates == true },
onCheckedChanged = { onCheckedChanged = {
viewModel.setIgnoreUpdates(product.packageName, it) viewModel.setIgnoreUpdates(product.packageName, it)
}) }
)
} }
} }
if (Preferences[Preferences.Key.ShowScreenshots]) { if (Preferences[Preferences.Key.ShowScreenshots]) {

View File

@ -1,36 +0,0 @@
package com.looker.droidify.widget
import android.database.Cursor
import androidx.recyclerview.widget.RecyclerView
abstract class CursorRecyclerAdapter<VT : Enum<VT>, VH : RecyclerView.ViewHolder> :
EnumRecyclerAdapter<VT, VH>() {
init {
super.setHasStableIds(true)
}
private var rowIdIndex = 0
var cursor: Cursor? = null
set(value) {
if (field != value) {
field?.close()
field = value
rowIdIndex = value?.getColumnIndexOrThrow("_id") ?: 0
notifyDataSetChanged()
}
}
final override fun setHasStableIds(hasStableIds: Boolean) {
throw UnsupportedOperationException()
}
override fun getItemCount(): Int = cursor?.count ?: 0
override fun getItemId(position: Int): Long = moveTo(position).getLong(rowIdIndex)
fun moveTo(position: Int): Cursor {
val cursor = cursor!!
cursor.moveToPosition(position)
return cursor
}
}

View File

@ -1,119 +0,0 @@
package com.looker.droidify.widget
import android.content.Context
import android.graphics.Canvas
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.looker.droidify.R
import com.looker.droidify.utility.extension.resources.getDrawableFromAttr
import kotlin.math.roundToInt
class DividerItemDecoration(
context: Context,
private val configure: (
context: Context,
position: Int, configuration: Configuration,
) -> Unit,
) : RecyclerView.ItemDecoration() {
interface Configuration {
fun set(needDivider: Boolean, toTop: Boolean, paddingStart: Int, paddingEnd: Int)
}
private class ConfigurationHolder : Configuration {
var needDivider = false
var toTop = false
var paddingStart = 0
var paddingEnd = 0
override fun set(needDivider: Boolean, toTop: Boolean, paddingStart: Int, paddingEnd: Int) {
this.needDivider = needDivider
this.toTop = toTop
this.paddingStart = paddingStart
this.paddingEnd = paddingEnd
}
}
private val View.configuration: ConfigurationHolder
get() = getTag(R.id.divider_configuration) as? ConfigurationHolder ?: run {
val configuration = ConfigurationHolder()
setTag(R.id.divider_configuration, configuration)
configuration
}
private val divider = context.getDrawableFromAttr(android.R.attr.listDivider)
private val bounds = Rect()
private fun draw(
c: Canvas,
configuration: ConfigurationHolder,
view: View,
top: Int,
width: Int,
rtl: Boolean,
) {
val divider = divider
val left = if (rtl) configuration.paddingEnd else configuration.paddingStart
val right = width - (if (rtl) configuration.paddingStart else configuration.paddingEnd)
val translatedTop = top + view.translationY.roundToInt()
divider.alpha = (view.alpha * 0xff).toInt()
divider.setBounds(left, translatedTop, right, translatedTop + divider.intrinsicHeight)
divider.draw(c)
}
override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
val divider = divider
val bounds = bounds
val rtl = parent.layoutDirection == View.LAYOUT_DIRECTION_RTL
for (i in 0 until parent.childCount) {
val view = parent.getChildAt(i)
val configuration = view.configuration
if (configuration.needDivider) {
val position = parent.getChildAdapterPosition(view)
if (position == parent.adapter!!.itemCount - 1) {
parent.getDecoratedBoundsWithMargins(view, bounds)
draw(c, configuration, view, bounds.bottom, parent.width, rtl)
} else {
val toTopView = if (configuration.toTop && position >= 0)
parent.findViewHolderForAdapterPosition(position + 1)?.itemView else null
if (toTopView != null) {
parent.getDecoratedBoundsWithMargins(toTopView, bounds)
draw(
c,
configuration,
toTopView,
bounds.top - divider.intrinsicHeight,
parent.width,
rtl
)
} else {
parent.getDecoratedBoundsWithMargins(view, bounds)
draw(
c,
configuration,
view,
bounds.bottom - divider.intrinsicHeight,
parent.width,
rtl
)
}
}
}
}
}
override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State,
) {
val configuration = view.configuration
val position = parent.getChildAdapterPosition(view)
if (position >= 0) {
configure(view.context, position, configuration)
}
val needDivider = position < parent.adapter!!.itemCount - 1 && configuration.needDivider
outRect.set(0, 0, 0, if (needDivider) divider.intrinsicHeight else 0)
}
}