1
Fork 0
mirror of https://github.com/Steffo99/twom.git synced 2024-11-21 23:54:26 +00:00

Clean up code

This commit is contained in:
Steffo 2024-01-08 18:08:19 +01:00
parent 6aeea6d170
commit 7565d511c3
Signed by: steffo
GPG key ID: 2A24051445686895
18 changed files with 191 additions and 256 deletions

View file

@ -16,16 +16,6 @@ import androidx.compose.ui.unit.dp
import eu.steffo.twom.R
import eu.steffo.twom.matrix.LocalSession
import eu.steffo.twom.matrix.avatar.AvatarFromURL
import eu.steffo.twom.theme.colorRoleLater
import eu.steffo.twom.theme.colorRoleMaybe
import eu.steffo.twom.theme.colorRoleNoway
import eu.steffo.twom.theme.colorRoleSure
import eu.steffo.twom.theme.colorRoleUnknown
import eu.steffo.twom.theme.iconRoleLater
import eu.steffo.twom.theme.iconRoleMaybe
import eu.steffo.twom.theme.iconRoleNoway
import eu.steffo.twom.theme.iconRoleSure
import eu.steffo.twom.theme.iconRoleUnknown
import org.matrix.android.sdk.api.session.getUser
// TODO: Check this with brain on
@ -42,27 +32,9 @@ fun MemberListItem(
val user = session?.getUser(memberId)
val colorRole = when (rsvpAnswer) {
RSVPAnswer.SURE -> colorRoleSure()
RSVPAnswer.LATER -> colorRoleLater()
RSVPAnswer.MAYBE -> colorRoleMaybe()
RSVPAnswer.NOWAY -> colorRoleNoway()
null -> colorRoleUnknown()
}
val iconRole = when (rsvpAnswer) {
RSVPAnswer.SURE -> iconRoleSure
RSVPAnswer.LATER -> iconRoleLater
RSVPAnswer.MAYBE -> iconRoleMaybe
RSVPAnswer.NOWAY -> iconRoleNoway
null -> iconRoleUnknown
}
val iconDescription = when (rsvpAnswer) {
RSVPAnswer.SURE -> stringResource(R.string.room_rsvp_sure_response)
RSVPAnswer.LATER -> stringResource(R.string.room_rsvp_later_response)
RSVPAnswer.MAYBE -> stringResource(R.string.room_rsvp_maybe_response)
RSVPAnswer.NOWAY -> stringResource(R.string.room_rsvp_noway_response)
null -> stringResource(R.string.room_rsvp_unknown_response)
}
val icon = rsvpAnswer.toIcon()
val responseResourceId = rsvpAnswer.toResponseResourceId()
val colorRole = rsvpAnswer.toStaticColorRole()
ListItem(
modifier = modifier.clickable {
@ -87,18 +59,16 @@ fun MemberListItem(
},
trailingContent = {
Icon(
imageVector = iconRole,
contentDescription = iconDescription,
imageVector = icon,
contentDescription = stringResource(responseResourceId),
tint = colorRole.value,
)
},
supportingContent = {
if (rsvpComment != "") {
Text(
text = rsvpComment,
color = colorRole.value,
)
}
},
)
}

View file

@ -1,8 +1,73 @@
package eu.steffo.twom.room
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.vector.ImageVector
import eu.steffo.twom.R
import eu.steffo.twom.theme.StaticColorRole
import eu.steffo.twom.theme.colorRoleLater
import eu.steffo.twom.theme.colorRoleMaybe
import eu.steffo.twom.theme.colorRoleNoway
import eu.steffo.twom.theme.colorRoleSure
import eu.steffo.twom.theme.colorRoleUnknown
import eu.steffo.twom.theme.iconLater
import eu.steffo.twom.theme.iconMaybe
import eu.steffo.twom.theme.iconNoway
import eu.steffo.twom.theme.iconSure
import eu.steffo.twom.theme.iconUnknown
enum class RSVPAnswer {
SURE,
LATER,
MAYBE,
NOWAY,
}
@Composable
fun RSVPAnswer?.toStaticColorRole(): StaticColorRole {
return when (this) {
RSVPAnswer.SURE -> colorRoleSure()
RSVPAnswer.LATER -> colorRoleLater()
RSVPAnswer.MAYBE -> colorRoleMaybe()
RSVPAnswer.NOWAY -> colorRoleNoway()
null -> colorRoleUnknown()
}
}
fun RSVPAnswer?.toIcon(): ImageVector {
return when (this) {
RSVPAnswer.SURE -> iconSure
RSVPAnswer.LATER -> iconLater
RSVPAnswer.MAYBE -> iconMaybe
RSVPAnswer.NOWAY -> iconNoway
null -> iconUnknown
}
}
fun RSVPAnswer.toLabelResourceId(): Int {
return when (this) {
RSVPAnswer.SURE -> R.string.room_rsvp_sure_label
RSVPAnswer.LATER -> R.string.room_rsvp_later_label
RSVPAnswer.MAYBE -> R.string.room_rsvp_maybe_label
RSVPAnswer.NOWAY -> R.string.room_rsvp_noway_label
}
}
fun RSVPAnswer?.toResponseResourceId(): Int {
return when (this) {
RSVPAnswer.SURE -> R.string.room_rsvp_sure_response
RSVPAnswer.LATER -> R.string.room_rsvp_later_response
RSVPAnswer.MAYBE -> R.string.room_rsvp_maybe_response
RSVPAnswer.NOWAY -> R.string.room_rsvp_noway_response
null -> R.string.room_rsvp_unknown_response
}
}
fun RSVPAnswer?.toPlaceholderResourceId(): Int {
return when (this) {
RSVPAnswer.SURE -> R.string.room_rsvp_sure_placeholder
RSVPAnswer.LATER -> R.string.room_rsvp_later_placeholder
RSVPAnswer.MAYBE -> R.string.room_rsvp_maybe_placeholder
RSVPAnswer.NOWAY -> R.string.room_rsvp_noway_placeholder
null -> R.string.room_rsvp_unknown_placeholder
}
}

View file

@ -7,33 +7,35 @@ import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import eu.steffo.twom.theme.StaticColorRole
import eu.steffo.twom.theme.TwoMPadding
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun RoomActivityChip(
fun RSVPAnswerFilterChip(
modifier: Modifier = Modifier,
representing: RSVPAnswer,
selected: Boolean = false,
onClick: () -> Unit = {},
text: String,
imageVector: ImageVector,
colorRole: StaticColorRole,
) {
val icon = representing.toIcon()
val colorRole = representing.toStaticColorRole()
val labelResourceId = representing.toLabelResourceId()
FilterChip(
modifier = TwoMPadding.chips,
modifier = modifier,
selected = selected,
onClick = onClick,
leadingIcon = {
Icon(
imageVector = imageVector,
imageVector = icon,
contentDescription = null,
)
},
label = {
Text(
text = text,
text = stringResource(labelResourceId),
style = MaterialTheme.typography.bodyLarge,
)
},

View file

@ -13,66 +13,53 @@ import eu.steffo.twom.theme.TwoMPadding
@Composable
@Preview
fun RoomActivityChipSelector(
fun RSVPAnswerSelectRow(
modifier: Modifier = Modifier,
value: RSVPAnswer? = null,
onChange: (answer: RSVPAnswer?) -> Unit = {},
) {
fun toggleSwitch(representing: RSVPAnswer): () -> Unit {
return {
onChange(
when (value) {
representing -> null
else -> representing
}
)
}
}
Box(
modifier = modifier
.horizontalScroll(rememberScrollState())
) {
Row(
modifier = Modifier
.padding(start = 8.dp, end = 8.dp)
.padding(start = 10.dp, end = 10.dp)
) {
RoomActivityChipSure(
RSVPAnswerFilterChip(
modifier = TwoMPadding.chips,
representing = RSVPAnswer.SURE,
selected = (value == RSVPAnswer.SURE),
onClick = {
onChange(
when (value) {
RSVPAnswer.SURE -> null
else -> RSVPAnswer.SURE
}
onClick = toggleSwitch(RSVPAnswer.SURE)
)
}
)
RoomActivityChipLater(
RSVPAnswerFilterChip(
modifier = TwoMPadding.chips,
representing = RSVPAnswer.LATER,
selected = (value == RSVPAnswer.LATER),
onClick = {
onChange(
when (value) {
RSVPAnswer.LATER -> null
else -> RSVPAnswer.LATER
}
onClick = toggleSwitch(RSVPAnswer.LATER)
)
}
)
RoomActivityChipMaybe(
RSVPAnswerFilterChip(
modifier = TwoMPadding.chips,
representing = RSVPAnswer.MAYBE,
selected = (value == RSVPAnswer.MAYBE),
onClick = {
onChange(
when (value) {
RSVPAnswer.MAYBE -> null
else -> RSVPAnswer.MAYBE
}
onClick = toggleSwitch(RSVPAnswer.MAYBE)
)
}
)
RoomActivityChipNoway(
RSVPAnswerFilterChip(
modifier = TwoMPadding.chips,
representing = RSVPAnswer.NOWAY,
selected = (value == RSVPAnswer.NOWAY),
onClick = {
onChange(
when (value) {
RSVPAnswer.NOWAY -> null
else -> RSVPAnswer.NOWAY
}
)
}
onClick = toggleSwitch(RSVPAnswer.NOWAY)
)
}
}

View file

@ -1,6 +1,5 @@
package eu.steffo.twom.room
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
@ -10,47 +9,29 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import eu.steffo.twom.R
import eu.steffo.twom.theme.colorRoleLater
import eu.steffo.twom.theme.colorRoleMaybe
import eu.steffo.twom.theme.colorRoleNoway
import eu.steffo.twom.theme.colorRoleSure
import eu.steffo.twom.theme.colorRoleUnknown
@Composable
@Preview
fun RoomActivityCommentField(
fun RSVPCommentField(
modifier: Modifier = Modifier,
value: String = "",
onValueChange: (value: String) -> Unit = {},
rsvpAnswer: RSVPAnswer? = null,
onChange: (value: String) -> Unit = {},
currentRsvpAnswer: RSVPAnswer? = null,
) {
val colorRole = when (rsvpAnswer) {
RSVPAnswer.SURE -> colorRoleSure()
RSVPAnswer.LATER -> colorRoleLater()
RSVPAnswer.MAYBE -> colorRoleMaybe()
RSVPAnswer.NOWAY -> colorRoleNoway()
null -> colorRoleUnknown()
}
val colorRole = currentRsvpAnswer.toStaticColorRole()
OutlinedTextField(
modifier = Modifier.fillMaxWidth(),
modifier = modifier,
value = value,
onValueChange = onValueChange,
onValueChange = onChange,
singleLine = true,
shape = MaterialTheme.shapes.small,
placeholder = {
Text(
text = when (rsvpAnswer) {
RSVPAnswer.SURE -> stringResource(R.string.room_rsvp_sure_placeholder)
RSVPAnswer.LATER -> stringResource(R.string.room_rsvp_later_placeholder)
RSVPAnswer.MAYBE -> stringResource(R.string.room_rsvp_maybe_placeholder)
RSVPAnswer.NOWAY -> stringResource(R.string.room_rsvp_noway_placeholder)
null -> stringResource(R.string.room_rsvp_unknown_placeholder)
}
text = stringResource(currentRsvpAnswer.toPlaceholderResourceId())
)
},
colors = if (rsvpAnswer != null) {
colors = if (currentRsvpAnswer != null) {
OutlinedTextFieldDefaults.colors(
focusedContainerColor = colorRole.valueContainer,
unfocusedContainerColor = colorRole.valueContainer,

View file

@ -1,5 +1,7 @@
import androidx.compose.foundation.layout.fillMaxWidth
package eu.steffo.twom.room
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
@ -7,18 +9,26 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import eu.steffo.twom.R
import eu.steffo.twom.room.RSVPAnswer
@Composable
@Preview
fun RoomActivityUpdateButton(
fun RSVPUpdateButton(
modifier: Modifier = Modifier,
enabled: Boolean = true,
onClick: () -> Unit = {},
rsvpAnswer: RSVPAnswer? = null,
currentRsvpAnswer: RSVPAnswer? = null,
) {
val colorRole = currentRsvpAnswer.toStaticColorRole()
Button(
modifier = Modifier.fillMaxWidth(),
modifier = modifier,
enabled = enabled,
onClick = onClick,
shape = MaterialTheme.shapes.small,
colors = ButtonDefaults.buttonColors(
containerColor = colorRole.value,
contentColor = colorRole.onValue,
)
) {
Text(
text = stringResource(R.string.room_update_label)

View file

@ -1,10 +1,46 @@
package eu.steffo.twom.room
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
@Composable
@Preview
fun RoomActivityAnswerForm() {
fun RoomActivityAnswerForm(
currentRsvpAnswer: RSVPAnswer? = null,
currentRsvpComment: String = "",
onUpdate: (rsvpAnswer: RSVPAnswer?, rsvpComment: String) -> Unit = { _, _ -> },
) {
var rsvpAnswer by rememberSaveable { mutableStateOf(currentRsvpAnswer) }
var rsvpComment by rememberSaveable { mutableStateOf(currentRsvpComment) }
val hasChanged = (rsvpAnswer != currentRsvpAnswer || rsvpComment != currentRsvpComment)
RSVPAnswerSelectRow(
value = rsvpAnswer,
onChange = { rsvpAnswer = it },
)
RSVPCommentField(
modifier = Modifier
.padding(start = 10.dp, end = 10.dp)
.fillMaxWidth(),
value = rsvpComment,
onChange = { rsvpComment = it },
currentRsvpAnswer = rsvpAnswer,
)
RSVPUpdateButton(
modifier = Modifier
.padding(start = 10.dp, end = 10.dp, top = 4.dp, bottom = 4.dp)
.fillMaxWidth(),
onClick = { onUpdate(rsvpAnswer, rsvpComment) },
enabled = hasChanged,
currentRsvpAnswer = rsvpAnswer,
)
}

View file

@ -1,25 +0,0 @@
package eu.steffo.twom.room
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import eu.steffo.twom.R
import eu.steffo.twom.theme.colorRoleLater
import eu.steffo.twom.theme.iconRoleLater
@Composable
@Preview
fun RoomActivityChipLater(
modifier: Modifier = Modifier,
selected: Boolean = false,
onClick: () -> Unit = {},
) {
RoomActivityChip(
selected = selected,
onClick = onClick,
imageVector = iconRoleLater,
text = stringResource(R.string.room_rsvp_later_label),
colorRole = colorRoleLater(),
)
}

View file

@ -1,26 +0,0 @@
package eu.steffo.twom.room
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import eu.steffo.twom.R
import eu.steffo.twom.theme.colorRoleMaybe
import eu.steffo.twom.theme.iconRoleMaybe
@Composable
@Preview
fun RoomActivityChipMaybe(
modifier: Modifier = Modifier,
selected: Boolean = false,
onClick: () -> Unit = {},
) {
// TODO: Pick a better color
RoomActivityChip(
selected = selected,
onClick = onClick,
imageVector = iconRoleMaybe,
text = stringResource(R.string.room_rsvp_maybe_label),
colorRole = colorRoleMaybe(),
)
}

View file

@ -1,25 +0,0 @@
package eu.steffo.twom.room
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import eu.steffo.twom.R
import eu.steffo.twom.theme.colorRoleNoway
import eu.steffo.twom.theme.iconRoleNoway
@Composable
@Preview
fun RoomActivityChipNoway(
modifier: Modifier = Modifier,
selected: Boolean = false,
onClick: () -> Unit = {},
) {
RoomActivityChip(
selected = selected,
onClick = onClick,
imageVector = iconRoleNoway,
text = stringResource(R.string.room_rsvp_noway_label),
colorRole = colorRoleNoway(),
)
}

View file

@ -1,26 +0,0 @@
package eu.steffo.twom.room
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import eu.steffo.twom.R
import eu.steffo.twom.theme.colorRoleSure
import eu.steffo.twom.theme.iconRoleSure
@Composable
@Preview
fun RoomActivityChipSure(
modifier: Modifier = Modifier,
selected: Boolean = false,
onClick: () -> Unit = {},
) {
RoomActivityChip(
selected = selected,
onClick = onClick,
imageVector = iconRoleSure,
text = stringResource(R.string.room_rsvp_sure_label),
colorRole = colorRoleSure(),
)
}

View file

@ -1,9 +1,7 @@
package eu.steffo.twom.room
import RoomActivityUpdateButton
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
@ -13,7 +11,6 @@ import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import eu.steffo.twom.R
import eu.steffo.twom.matrix.LocalSession
import eu.steffo.twom.theme.TwoMPadding
@ -49,25 +46,14 @@ fun RoomActivityContent(
)
}
RoomActivityChipSelector(
value = rsvpAnswer,
onChange = { rsvpAnswer = it }
)
Row(Modifier.padding(start = 10.dp, end = 10.dp)) {
RoomActivityCommentField(
value = rsvpComment,
onValueChange = { rsvpComment = it },
rsvpAnswer = rsvpAnswer,
)
RoomActivityAnswerForm(
currentRsvpAnswer = rsvpAnswer,
currentRsvpComment = rsvpComment,
onUpdate = { answer, comment ->
rsvpAnswer = answer
rsvpComment = comment
}
Row(Modifier.padding(all = 10.dp)) {
RoomActivityUpdateButton(
onClick = {},
rsvpAnswer = rsvpAnswer,
)
}
Row(TwoMPadding.base) {
Text(

View file

@ -1,14 +1,14 @@
package eu.steffo.twom.theme
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
@Composable
fun colorRoleUnknown(): StaticColorRole {
return StaticColorRole(
value = LocalContentColor.current,
onValue = LocalContentColor.current,
valueContainer = LocalContentColor.current,
onValueContainer = LocalContentColor.current,
value = MaterialTheme.colorScheme.inverseSurface,
onValue = MaterialTheme.colorScheme.inverseOnSurface,
valueContainer = MaterialTheme.colorScheme.surfaceVariant,
onValueContainer = MaterialTheme.colorScheme.onSurfaceVariant,
)
}

View file

@ -3,4 +3,4 @@ package eu.steffo.twom.theme
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Schedule
val iconRoleLater = Icons.Outlined.Schedule
val iconLater = Icons.Outlined.Schedule

View file

@ -3,4 +3,4 @@ package eu.steffo.twom.theme
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.PauseCircle
val iconRoleMaybe = Icons.Outlined.PauseCircle
val iconMaybe = Icons.Outlined.PauseCircle

View file

@ -3,4 +3,4 @@ package eu.steffo.twom.theme
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.RemoveCircleOutline
val iconRoleNoway = Icons.Outlined.RemoveCircleOutline
val iconNoway = Icons.Outlined.RemoveCircleOutline

View file

@ -3,4 +3,4 @@ package eu.steffo.twom.theme
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.CheckCircle
val iconRoleSure = Icons.Outlined.CheckCircle
val iconSure = Icons.Outlined.CheckCircle

View file

@ -3,4 +3,4 @@ package eu.steffo.twom.theme
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Circle
val iconRoleUnknown = Icons.Outlined.Circle
val iconUnknown = Icons.Outlined.Circle