mirror of https://github.com/tasks/tasks
Convert priority control set to compose
parent
1ca6076005
commit
708e2b14e6
@ -1,77 +1,97 @@
|
|||||||
package org.tasks.ui
|
package org.tasks.ui
|
||||||
|
|
||||||
import android.content.res.ColorStateList
|
|
||||||
import android.os.Bundle
|
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import androidx.appcompat.widget.AppCompatRadioButton
|
import androidx.compose.foundation.layout.*
|
||||||
|
import androidx.compose.material.MaterialTheme
|
||||||
|
import androidx.compose.material.RadioButton
|
||||||
|
import androidx.compose.material.RadioButtonDefaults
|
||||||
|
import androidx.compose.material.Text
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.collectAsState
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.platform.ComposeView
|
||||||
|
import androidx.compose.ui.res.colorResource
|
||||||
|
import androidx.compose.ui.res.dimensionResource
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import com.google.android.material.composethemeadapter.MdcTheme
|
||||||
import com.todoroo.astrid.data.Task
|
import com.todoroo.astrid.data.Task
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import org.tasks.R
|
import org.tasks.R
|
||||||
import org.tasks.databinding.ControlSetPriorityBinding
|
|
||||||
import org.tasks.themes.ColorProvider
|
|
||||||
import javax.inject.Inject
|
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class PriorityControlSet : TaskEditControlFragment() {
|
class PriorityControlSet : TaskEditControlFragment() {
|
||||||
@Inject lateinit var colorProvider: ColorProvider
|
|
||||||
|
|
||||||
private lateinit var priorityHigh: AppCompatRadioButton
|
|
||||||
private lateinit var priorityMedium: AppCompatRadioButton
|
|
||||||
private lateinit var priorityLow: AppCompatRadioButton
|
|
||||||
private lateinit var priorityNone: AppCompatRadioButton
|
|
||||||
|
|
||||||
private fun onPriorityChanged() {
|
|
||||||
viewModel.priority = getPriority()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun createView(savedInstanceState: Bundle?) {
|
|
||||||
when (viewModel.priority) {
|
|
||||||
0 -> priorityHigh.isChecked = true
|
|
||||||
1 -> priorityMedium.isChecked = true
|
|
||||||
2 -> priorityLow.isChecked = true
|
|
||||||
else -> priorityNone.isChecked = true
|
|
||||||
}
|
|
||||||
tintRadioButton(priorityHigh, 0)
|
|
||||||
tintRadioButton(priorityMedium, 1)
|
|
||||||
tintRadioButton(priorityLow, 2)
|
|
||||||
tintRadioButton(priorityNone, 3)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun bind(parent: ViewGroup?) =
|
override fun bind(parent: ViewGroup?) =
|
||||||
ControlSetPriorityBinding.inflate(layoutInflater, parent, true).let {
|
(parent?.findViewById(R.id.compose_view) as ComposeView).apply {
|
||||||
priorityHigh = it.priorityHigh.apply {
|
setContent {
|
||||||
setOnClickListener { onPriorityChanged() }
|
MdcTheme {
|
||||||
}
|
val priority = viewModel.priority.collectAsState()
|
||||||
priorityMedium = it.priorityMedium.apply {
|
PriorityRow(
|
||||||
setOnClickListener { onPriorityChanged() }
|
selected = priority.value,
|
||||||
|
onClick = { viewModel.priority.value = it })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
priorityLow = it.priorityLow.apply {
|
|
||||||
setOnClickListener { onPriorityChanged() }
|
|
||||||
}
|
|
||||||
priorityNone = it.priorityNone.apply {
|
|
||||||
setOnClickListener { onPriorityChanged() }
|
|
||||||
}
|
|
||||||
it.root
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override val icon = R.drawable.ic_outline_flag_24px
|
override val icon = R.drawable.ic_outline_flag_24px
|
||||||
|
|
||||||
|
override val rootLayout = R.layout.control_set_template_compose
|
||||||
|
|
||||||
override fun controlId() = TAG
|
override fun controlId() = TAG
|
||||||
|
|
||||||
private fun tintRadioButton(radioButton: AppCompatRadioButton, priority: Int) {
|
companion object {
|
||||||
val color = colorProvider.getPriorityColor(priority, true)
|
const val TAG = R.string.TEA_ctrl_importance_pref
|
||||||
radioButton.buttonTintList = ColorStateList(arrayOf(intArrayOf(-android.R.attr.state_checked), intArrayOf(android.R.attr.state_checked)), intArrayOf(color, color))
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Task.Priority
|
@Composable
|
||||||
private fun getPriority() = when {
|
fun PriorityRow(
|
||||||
priorityHigh.isChecked -> Task.Priority.HIGH
|
selected: Int,
|
||||||
priorityMedium.isChecked -> Task.Priority.MEDIUM
|
onClick: (Int) -> Unit = {}
|
||||||
priorityLow.isChecked -> Task.Priority.LOW
|
) {
|
||||||
else -> Task.Priority.NONE
|
Row(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.wrapContentHeight()
|
||||||
|
.padding(
|
||||||
|
top = dimensionResource(id = R.dimen.half_keyline_first),
|
||||||
|
bottom = dimensionResource(id = R.dimen.half_keyline_first),
|
||||||
|
end = 16.dp
|
||||||
|
),
|
||||||
|
verticalAlignment = Alignment.CenterVertically
|
||||||
|
) {
|
||||||
|
Text(
|
||||||
|
text = stringResource(id = R.string.TEA_importance_label),
|
||||||
|
style = MaterialTheme.typography.body1,
|
||||||
|
)
|
||||||
|
Spacer(modifier = Modifier.weight(1f))
|
||||||
|
PriorityButton(priority = Task.Priority.HIGH, selected = selected, onClick = onClick)
|
||||||
|
PriorityButton(priority = Task.Priority.MEDIUM, selected = selected, onClick = onClick)
|
||||||
|
PriorityButton(priority = Task.Priority.LOW, selected = selected, onClick = onClick)
|
||||||
|
PriorityButton(priority = Task.Priority.NONE, selected = selected, onClick = onClick)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
@Composable
|
||||||
const val TAG = R.string.TEA_ctrl_importance_pref
|
fun PriorityButton(
|
||||||
|
@Task.Priority priority: Int,
|
||||||
|
selected: Int,
|
||||||
|
onClick: (Int) -> Unit,
|
||||||
|
) {
|
||||||
|
val color = when (priority) {
|
||||||
|
in Int.MIN_VALUE..Task.Priority.HIGH -> colorResource(id = R.color.red_500)
|
||||||
|
Task.Priority.MEDIUM -> colorResource(id = R.color.amber_500)
|
||||||
|
Task.Priority.LOW -> colorResource(id = R.color.blue_500)
|
||||||
|
else -> colorResource(R.color.grey_500)
|
||||||
}
|
}
|
||||||
|
RadioButton(
|
||||||
|
selected = priority == selected,
|
||||||
|
onClick = { onClick(priority) },
|
||||||
|
colors = RadioButtonDefaults.colors(
|
||||||
|
selectedColor = color,
|
||||||
|
unselectedColor = color,
|
||||||
|
),
|
||||||
|
)
|
||||||
}
|
}
|
@ -1,48 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:gravity="center_vertical">
|
|
||||||
|
|
||||||
<RadioGroup
|
|
||||||
android:id="@+id/priority_group"
|
|
||||||
android:layout_alignParentEnd="true"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_gravity="end"
|
|
||||||
android:paddingStart="@dimen/keyline_first"
|
|
||||||
android:paddingEnd="0dp"
|
|
||||||
android:gravity="end"
|
|
||||||
android:orientation="horizontal">
|
|
||||||
|
|
||||||
<androidx.appcompat.widget.AppCompatRadioButton
|
|
||||||
android:id="@+id/priority_none"
|
|
||||||
style="@style/priority_button" />
|
|
||||||
|
|
||||||
<androidx.appcompat.widget.AppCompatRadioButton
|
|
||||||
android:id="@+id/priority_low"
|
|
||||||
style="@style/priority_button" />
|
|
||||||
|
|
||||||
<androidx.appcompat.widget.AppCompatRadioButton
|
|
||||||
android:id="@+id/priority_medium"
|
|
||||||
style="@style/priority_button" />
|
|
||||||
|
|
||||||
<androidx.appcompat.widget.AppCompatRadioButton
|
|
||||||
android:id="@+id/priority_high"
|
|
||||||
style="@style/priority_button" />
|
|
||||||
</RadioGroup>
|
|
||||||
|
|
||||||
<TextView
|
|
||||||
android:id="@+id/text"
|
|
||||||
style="@style/TaskEditTextPrimary"
|
|
||||||
android:layout_alignParentStart="true"
|
|
||||||
android:layout_toStartOf="@id/priority_group"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:singleLine="true"
|
|
||||||
android:ellipsize="end"
|
|
||||||
android:gravity="start|center_vertical"
|
|
||||||
android:text="@string/TEA_importance_label" />
|
|
||||||
|
|
||||||
|
|
||||||
</RelativeLayout>
|
|
@ -0,0 +1,30 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
|
android:id="@+id/content"
|
||||||
|
android:layout_width="fill_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:background="?attr/selectableItemBackground"
|
||||||
|
android:orientation="horizontal">
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/icon"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="top"
|
||||||
|
android:alpha="@dimen/alpha_secondary"
|
||||||
|
android:paddingStart="@dimen/keyline_first"
|
||||||
|
android:paddingTop="20dp"
|
||||||
|
android:paddingEnd="@dimen/keyline_second"
|
||||||
|
android:paddingBottom="20dp"
|
||||||
|
android:scaleType="center"
|
||||||
|
app:tint="@color/icon_tint"
|
||||||
|
tools:ignore="ContentDescription" />
|
||||||
|
|
||||||
|
<androidx.compose.ui.platform.ComposeView
|
||||||
|
android:id="@+id/compose_view"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
Loading…
Reference in New Issue