Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ import android.content.res.ColorStateList
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import android.widget.ImageView
import androidx.appcompat.widget.SwitchCompat
import androidx.core.content.withStyledAttributes
import com.google.android.material.color.MaterialColors
import com.ichi2.anki.R
import com.ichi2.anki.databinding.PermissionsItemBinding
import com.ichi2.anki.utils.ext.usingStyledAttributes
import com.ichi2.ui.FixedTextView
import com.ichi2.utils.Permissions
import timber.log.Timber

Expand Down Expand Up @@ -56,7 +54,7 @@ class PermissionsItem(
context: Context,
attrs: AttributeSet,
) : FrameLayout(context, attrs) {
private val switch: SwitchCompat
val binding = PermissionsItemBinding.inflate(LayoutInflater.from(context), this, true)

/**
* The value of either app:permissions or app:permission.
Expand All @@ -65,15 +63,12 @@ class PermissionsItem(
val areGranted get() = Permissions.hasAllPermissions(context, permissions)

init {
LayoutInflater.from(context).inflate(R.layout.permissions_item, this, true)

switch =
findViewById<SwitchCompat>(R.id.switch_widget).apply {
isEnabled = true
setOnCheckedChangeListener { button, _ ->
button.isChecked = areGranted
}
binding.switchWidget.apply {
isEnabled = true
setOnCheckedChangeListener { button, _ ->
button.isChecked = areGranted
}
}

permissions =
context.usingStyledAttributes(attrs, R.styleable.PermissionItem) {
Expand All @@ -83,13 +78,13 @@ class PermissionsItem(
}

context.withStyledAttributes(attrs, R.styleable.PermissionItem) {
findViewById<FixedTextView>(R.id.title).text = getText(R.styleable.PermissionItem_permissionTitle)
findViewById<FixedTextView>(R.id.summary).text = getText(R.styleable.PermissionItem_permissionSummary)
binding.title.text = getText(R.styleable.PermissionItem_permissionTitle)
binding.summary.text = getText(R.styleable.PermissionItem_permissionSummary)

val icon = getDrawable(R.styleable.PermissionItem_permissionIcon)
icon?.let {
val color = MaterialColors.getColor(this@PermissionsItem, android.R.attr.colorControlNormal)
findViewById<ImageView>(R.id.icon).apply {
binding.icon.apply {
setImageDrawable(it)
imageTintList = ColorStateList.valueOf(color)
}
Expand All @@ -100,7 +95,7 @@ class PermissionsItem(
permissionsRequested?.invoke(areGranted)
}

switch.setOnClickListener {
binding.switchWidget.setOnClickListener {
Timber.i("Permission switch clicked, requesting permissions: $areGranted")
permissionsRequested?.invoke(areGranted)
}
Expand All @@ -118,7 +113,7 @@ class PermissionsItem(
* or uncheck if not
*/
fun updateSwitchCheckedStatus() {
switch.isChecked = areGranted
binding.switchWidget.isChecked = areGranted
}

/**
Expand Down