mirror of https://github.com/tasks/tasks
Move more composable logic
parent
3e3de3c1d6
commit
85b8092982
@ -0,0 +1,130 @@
|
|||||||
|
package org.tasks.compose
|
||||||
|
|
||||||
|
import android.content.res.Configuration
|
||||||
|
import androidx.annotation.DrawableRes
|
||||||
|
import androidx.compose.foundation.BorderStroke
|
||||||
|
import androidx.compose.foundation.layout.defaultMinSize
|
||||||
|
import androidx.compose.foundation.layout.size
|
||||||
|
import androidx.compose.material.*
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.CompositionLocalProvider
|
||||||
|
import androidx.compose.ui.ExperimentalComposeUiApi
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.graphics.Color
|
||||||
|
import androidx.compose.ui.res.painterResource
|
||||||
|
import androidx.compose.ui.text.style.TextOverflow
|
||||||
|
import androidx.compose.ui.tooling.preview.Preview
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import com.google.android.material.composethemeadapter.MdcTheme
|
||||||
|
import org.tasks.themes.CustomIcons
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun Chip(
|
||||||
|
@DrawableRes icon: Int?,
|
||||||
|
name: String?,
|
||||||
|
theme: Int,
|
||||||
|
showText: Boolean,
|
||||||
|
showIcon: Boolean,
|
||||||
|
onClick: () -> Unit,
|
||||||
|
colorProvider: (Int) -> Int,
|
||||||
|
) {
|
||||||
|
Chip(
|
||||||
|
color = Color(colorProvider(theme)),
|
||||||
|
text = if (showText) name else null,
|
||||||
|
icon = if (showIcon && icon != null) icon else null,
|
||||||
|
onClick = onClick,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalMaterialApi::class)
|
||||||
|
@Composable
|
||||||
|
fun Chip(
|
||||||
|
text: String? = null,
|
||||||
|
icon: Int? = null,
|
||||||
|
color: Color,
|
||||||
|
onClick: () -> Unit = {},
|
||||||
|
) {
|
||||||
|
CompositionLocalProvider(
|
||||||
|
LocalMinimumTouchTargetEnforcement provides false
|
||||||
|
) {
|
||||||
|
Chip(
|
||||||
|
onClick = onClick,
|
||||||
|
border = BorderStroke(1.dp, color = color),
|
||||||
|
leadingIcon = {
|
||||||
|
if (text != null) {
|
||||||
|
ChipIcon(iconRes = icon)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
modifier = Modifier.defaultMinSize(minHeight = 26.dp),
|
||||||
|
colors = ChipDefaults.chipColors(
|
||||||
|
backgroundColor = color.copy(alpha = .1f),
|
||||||
|
contentColor = MaterialTheme.colors.onSurface
|
||||||
|
),
|
||||||
|
) {
|
||||||
|
if (text == null) {
|
||||||
|
ChipIcon(iconRes = icon)
|
||||||
|
}
|
||||||
|
text?.let {
|
||||||
|
Text(
|
||||||
|
text = it,
|
||||||
|
style = MaterialTheme.typography.caption,
|
||||||
|
maxLines = 1,
|
||||||
|
overflow = TextOverflow.Ellipsis,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
private fun ChipIcon(iconRes: Int?) {
|
||||||
|
iconRes?.let {
|
||||||
|
Icon(
|
||||||
|
painter = painterResource(id = iconRes),
|
||||||
|
contentDescription = null,
|
||||||
|
modifier = Modifier.size(18.dp)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExperimentalComposeUiApi
|
||||||
|
@Preview(showBackground = true)
|
||||||
|
@Preview(showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES)
|
||||||
|
@Composable
|
||||||
|
fun TasksChipIconAndTextPreview() {
|
||||||
|
MdcTheme {
|
||||||
|
Chip(
|
||||||
|
text = "Home",
|
||||||
|
icon = CustomIcons.getIconResId(CustomIcons.LABEL),
|
||||||
|
color = Color.Red,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExperimentalComposeUiApi
|
||||||
|
@Preview(showBackground = true)
|
||||||
|
@Preview(showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES)
|
||||||
|
@Composable
|
||||||
|
fun TasksChipIconPreview() {
|
||||||
|
MdcTheme {
|
||||||
|
Chip(
|
||||||
|
text = null,
|
||||||
|
icon = CustomIcons.getIconResId(CustomIcons.LABEL),
|
||||||
|
color = Color.Red,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExperimentalComposeUiApi
|
||||||
|
@Preview(showBackground = true)
|
||||||
|
@Preview(showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES)
|
||||||
|
@Composable
|
||||||
|
fun TasksChipTextPreview() {
|
||||||
|
MdcTheme {
|
||||||
|
Chip(
|
||||||
|
text = "Home",
|
||||||
|
icon = null,
|
||||||
|
color = Color.Red,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,17 @@
|
|||||||
|
package org.tasks.compose
|
||||||
|
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import com.google.accompanist.flowlayout.FlowRow
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun ChipGroup(modifier: Modifier = Modifier, content: @Composable () -> Unit) {
|
||||||
|
FlowRow(
|
||||||
|
mainAxisSpacing = 4.dp,
|
||||||
|
crossAxisSpacing = 4.dp,
|
||||||
|
modifier = modifier,
|
||||||
|
) {
|
||||||
|
content()
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,25 @@
|
|||||||
|
package org.tasks.compose
|
||||||
|
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import com.todoroo.astrid.api.Filter
|
||||||
|
import org.tasks.themes.CustomIcons
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun FilterChip(
|
||||||
|
filter: Filter,
|
||||||
|
defaultIcon: Int,
|
||||||
|
showText: Boolean,
|
||||||
|
showIcon: Boolean,
|
||||||
|
onClick: (Filter) -> Unit,
|
||||||
|
colorProvider: (Int) -> Int,
|
||||||
|
) {
|
||||||
|
Chip(
|
||||||
|
CustomIcons.getIcon(filter.icon, defaultIcon),
|
||||||
|
filter.listingTitle,
|
||||||
|
filter.tint,
|
||||||
|
showText,
|
||||||
|
showIcon,
|
||||||
|
onClick = { onClick(filter) },
|
||||||
|
colorProvider = colorProvider,
|
||||||
|
)
|
||||||
|
}
|
||||||
@ -0,0 +1,37 @@
|
|||||||
|
package org.tasks.compose.edit
|
||||||
|
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import com.todoroo.astrid.api.Filter
|
||||||
|
import org.tasks.R
|
||||||
|
import org.tasks.compose.ChipGroup
|
||||||
|
import org.tasks.compose.FilterChip
|
||||||
|
import org.tasks.compose.TaskEditRow
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun ListRow(
|
||||||
|
list: Filter?,
|
||||||
|
colorProvider: (Int) -> Int,
|
||||||
|
onClick: () -> Unit,
|
||||||
|
) {
|
||||||
|
TaskEditRow(
|
||||||
|
iconRes = R.drawable.ic_list_24px,
|
||||||
|
content = {
|
||||||
|
ChipGroup(modifier = Modifier.padding(vertical = 20.dp)) {
|
||||||
|
if (list != null) {
|
||||||
|
FilterChip(
|
||||||
|
filter = list,
|
||||||
|
defaultIcon = R.drawable.ic_list_24px,
|
||||||
|
showText = true,
|
||||||
|
showIcon = true,
|
||||||
|
onClick = { onClick() },
|
||||||
|
colorProvider = colorProvider
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onClick = onClick
|
||||||
|
)
|
||||||
|
}
|
||||||
@ -0,0 +1,48 @@
|
|||||||
|
package org.tasks.compose.edit
|
||||||
|
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import org.tasks.R
|
||||||
|
import org.tasks.compose.Chip
|
||||||
|
import org.tasks.compose.ChipGroup
|
||||||
|
import org.tasks.compose.DisabledText
|
||||||
|
import org.tasks.compose.TaskEditRow
|
||||||
|
import org.tasks.data.TagData
|
||||||
|
import org.tasks.themes.CustomIcons
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun TagsRow(
|
||||||
|
tags: List<TagData>,
|
||||||
|
colorProvider: (Int) -> Int,
|
||||||
|
onClick: () -> Unit,
|
||||||
|
) {
|
||||||
|
TaskEditRow(
|
||||||
|
iconRes = R.drawable.ic_outline_label_24px,
|
||||||
|
content = {
|
||||||
|
ChipGroup(modifier = Modifier.padding(top = 20.dp, bottom = 20.dp, end = 16.dp)) {
|
||||||
|
if (tags.isEmpty()) {
|
||||||
|
DisabledText(text = stringResource(id = R.string.add_tags))
|
||||||
|
} else {
|
||||||
|
tags.sortedBy(TagData::name).forEach { tag ->
|
||||||
|
Chip(
|
||||||
|
icon = CustomIcons.getIcon(
|
||||||
|
tag.getIcon()!!,
|
||||||
|
R.drawable.ic_outline_label_24px
|
||||||
|
),
|
||||||
|
name = tag.name,
|
||||||
|
theme = tag.getColor()!!,
|
||||||
|
showText = true,
|
||||||
|
showIcon = true,
|
||||||
|
onClick = onClick,
|
||||||
|
colorProvider = colorProvider,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onClick = onClick
|
||||||
|
)
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue