294 lines
9.6 KiB
Kotlin
294 lines
9.6 KiB
Kotlin
package org.thoughtcrime.securesms.ui
|
|
|
|
import androidx.annotation.DrawableRes
|
|
import androidx.compose.foundation.BorderStroke
|
|
import androidx.compose.foundation.ExperimentalFoundationApi
|
|
import androidx.compose.foundation.ScrollState
|
|
import androidx.compose.foundation.clickable
|
|
import androidx.compose.foundation.layout.Arrangement
|
|
import androidx.compose.foundation.layout.Box
|
|
import androidx.compose.foundation.layout.BoxScope
|
|
import androidx.compose.foundation.layout.Column
|
|
import androidx.compose.foundation.layout.Row
|
|
import androidx.compose.foundation.layout.RowScope
|
|
import androidx.compose.foundation.layout.Spacer
|
|
import androidx.compose.foundation.layout.fillMaxHeight
|
|
import androidx.compose.foundation.layout.fillMaxWidth
|
|
import androidx.compose.foundation.layout.height
|
|
import androidx.compose.foundation.layout.heightIn
|
|
import androidx.compose.foundation.layout.padding
|
|
import androidx.compose.foundation.layout.size
|
|
import androidx.compose.foundation.layout.width
|
|
import androidx.compose.foundation.layout.wrapContentHeight
|
|
import androidx.compose.foundation.pager.PagerState
|
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
|
import androidx.compose.material.ButtonColors
|
|
import androidx.compose.material.ButtonDefaults
|
|
import androidx.compose.material.Card
|
|
import androidx.compose.material.Colors
|
|
import androidx.compose.material.Icon
|
|
import androidx.compose.material.IconButton
|
|
import androidx.compose.material.MaterialTheme
|
|
import androidx.compose.material.OutlinedButton
|
|
import androidx.compose.material.RadioButton
|
|
import androidx.compose.material.Text
|
|
import androidx.compose.material.TextButton
|
|
import androidx.compose.runtime.Composable
|
|
import androidx.compose.runtime.rememberCoroutineScope
|
|
import androidx.compose.ui.Alignment
|
|
import androidx.compose.ui.Modifier
|
|
import androidx.compose.ui.draw.alpha
|
|
import androidx.compose.ui.draw.drawWithContent
|
|
import androidx.compose.ui.graphics.BlendMode
|
|
import androidx.compose.ui.graphics.Brush
|
|
import androidx.compose.ui.graphics.Color
|
|
import androidx.compose.ui.graphics.RectangleShape
|
|
import androidx.compose.ui.graphics.graphicsLayer
|
|
import androidx.compose.ui.res.painterResource
|
|
import androidx.compose.ui.unit.Dp
|
|
import androidx.compose.ui.unit.dp
|
|
import androidx.compose.ui.unit.sp
|
|
import androidx.compose.ui.viewinterop.AndroidView
|
|
import com.google.accompanist.pager.HorizontalPagerIndicator
|
|
import kotlinx.coroutines.launch
|
|
import network.loki.messenger.R
|
|
import org.session.libsession.utilities.recipients.Recipient
|
|
import org.thoughtcrime.securesms.components.ProfilePictureView
|
|
import org.thoughtcrime.securesms.conversation.disappearingmessages.OptionModel
|
|
import kotlin.math.min
|
|
|
|
@Composable
|
|
fun ItemButton(
|
|
text: String,
|
|
@DrawableRes icon: Int,
|
|
colors: ButtonColors = transparentButtonColors(),
|
|
contentDescription: String = text,
|
|
onClick: () -> Unit
|
|
) {
|
|
TextButton(
|
|
modifier = Modifier
|
|
.fillMaxWidth()
|
|
.height(60.dp),
|
|
colors = colors,
|
|
onClick = onClick,
|
|
shape = RectangleShape,
|
|
) {
|
|
Box(modifier = Modifier
|
|
.width(80.dp)
|
|
.fillMaxHeight()) {
|
|
Icon(
|
|
painter = painterResource(id = icon),
|
|
contentDescription = contentDescription,
|
|
modifier = Modifier.align(Alignment.Center)
|
|
)
|
|
}
|
|
Text(text, modifier = Modifier.fillMaxWidth())
|
|
}
|
|
}
|
|
|
|
@Composable
|
|
fun Cell(content: @Composable () -> Unit) {
|
|
CellWithPaddingAndMargin(padding = 0.dp) { content() }
|
|
}
|
|
@Composable
|
|
fun CellNoMargin(content: @Composable () -> Unit) {
|
|
CellWithPaddingAndMargin(padding = 0.dp, margin = 0.dp) { content() }
|
|
}
|
|
|
|
@Composable
|
|
fun CellWithPaddingAndMargin(
|
|
padding: Dp = 24.dp,
|
|
margin: Dp = 32.dp,
|
|
content: @Composable () -> Unit
|
|
) {
|
|
Card(
|
|
backgroundColor = MaterialTheme.colors.cellColor,
|
|
shape = RoundedCornerShape(16.dp),
|
|
elevation = 0.dp,
|
|
modifier = Modifier
|
|
.wrapContentHeight()
|
|
.fillMaxWidth()
|
|
.padding(horizontal = margin),
|
|
) {
|
|
Box(Modifier.padding(padding)) { content() }
|
|
}
|
|
}
|
|
|
|
@Composable
|
|
fun TitledRadioButton(option: OptionModel, onClick: () -> Unit) {
|
|
Row(
|
|
horizontalArrangement = Arrangement.spacedBy(16.dp),
|
|
modifier = Modifier
|
|
.clickable { if (!option.selected) onClick() }
|
|
.heightIn(min = 60.dp)
|
|
.padding(horizontal = 32.dp)
|
|
) {
|
|
Column(modifier = Modifier
|
|
.weight(1f)
|
|
.align(Alignment.CenterVertically)) {
|
|
Column {
|
|
Text(
|
|
text = option.title(),
|
|
fontSize = 16.sp,
|
|
modifier = Modifier.alpha(if (option.enabled) 1f else 0.5f)
|
|
)
|
|
option.subtitle?.let {
|
|
Text(
|
|
text = it(),
|
|
fontSize = 11.sp,
|
|
modifier = Modifier.alpha(if (option.enabled) 1f else 0.5f)
|
|
)
|
|
}
|
|
}
|
|
}
|
|
RadioButton(
|
|
selected = option.selected,
|
|
onClick = null,
|
|
enabled = option.enabled,
|
|
modifier = Modifier
|
|
.height(26.dp)
|
|
.align(Alignment.CenterVertically)
|
|
)
|
|
}
|
|
}
|
|
|
|
@Composable
|
|
fun OutlineButton(text: String, modifier: Modifier = Modifier, onClick: () -> Unit) {
|
|
OutlinedButton(
|
|
modifier = modifier.size(108.dp, 34.dp),
|
|
onClick = onClick,
|
|
border = BorderStroke(1.dp, LocalExtraColors.current.prominentButtonColor),
|
|
shape = RoundedCornerShape(50), // = 50% percent
|
|
colors = ButtonDefaults.outlinedButtonColors(
|
|
contentColor = LocalExtraColors.current.prominentButtonColor,
|
|
backgroundColor = MaterialTheme.colors.background
|
|
)
|
|
){
|
|
Text(text = text)
|
|
}
|
|
}
|
|
|
|
private val Colors.cellColor: Color
|
|
@Composable
|
|
get() = LocalExtraColors.current.settingsBackground
|
|
|
|
fun Modifier.fadingEdges(
|
|
scrollState: ScrollState,
|
|
topEdgeHeight: Dp = 0.dp,
|
|
bottomEdgeHeight: Dp = 20.dp
|
|
): Modifier = this.then(
|
|
Modifier
|
|
// adding layer fixes issue with blending gradient and content
|
|
.graphicsLayer { alpha = 0.99F }
|
|
.drawWithContent {
|
|
drawContent()
|
|
|
|
val topColors = listOf(Color.Transparent, Color.Black)
|
|
val topStartY = scrollState.value.toFloat()
|
|
val topGradientHeight = min(topEdgeHeight.toPx(), topStartY)
|
|
if (topGradientHeight > 0f) drawRect(
|
|
brush = Brush.verticalGradient(
|
|
colors = topColors,
|
|
startY = topStartY,
|
|
endY = topStartY + topGradientHeight
|
|
),
|
|
blendMode = BlendMode.DstIn
|
|
)
|
|
|
|
val bottomColors = listOf(Color.Black, Color.Transparent)
|
|
val bottomEndY = size.height - scrollState.maxValue + scrollState.value
|
|
val bottomGradientHeight =
|
|
min(bottomEdgeHeight.toPx(), scrollState.maxValue.toFloat() - scrollState.value)
|
|
if (bottomGradientHeight > 0f) drawRect(
|
|
brush = Brush.verticalGradient(
|
|
colors = bottomColors,
|
|
startY = bottomEndY - bottomGradientHeight,
|
|
endY = bottomEndY
|
|
),
|
|
blendMode = BlendMode.DstIn
|
|
)
|
|
}
|
|
)
|
|
|
|
@OptIn(ExperimentalFoundationApi::class)
|
|
@Composable
|
|
fun BoxScope.HorizontalPagerIndicator(pagerState: PagerState) {
|
|
if (pagerState.pageCount >= 2) Card(
|
|
shape = RoundedCornerShape(50.dp),
|
|
backgroundColor = Color.Black.copy(alpha = 0.4f),
|
|
modifier = Modifier
|
|
.align(Alignment.BottomCenter)
|
|
.padding(8.dp)
|
|
) {
|
|
Box(modifier = Modifier.padding(8.dp)) {
|
|
HorizontalPagerIndicator(
|
|
pagerState = pagerState,
|
|
pageCount = pagerState.pageCount,
|
|
activeColor = Color.White,
|
|
inactiveColor = classicDarkColors[5])
|
|
}
|
|
}
|
|
}
|
|
|
|
@OptIn(ExperimentalFoundationApi::class)
|
|
@Composable
|
|
fun RowScope.CarouselPrevButton(pagerState: PagerState) {
|
|
CarouselButton(pagerState, pagerState.canScrollBackward, R.drawable.ic_prev, -1)
|
|
}
|
|
|
|
@OptIn(ExperimentalFoundationApi::class)
|
|
@Composable
|
|
fun RowScope.CarouselNextButton(pagerState: PagerState) {
|
|
CarouselButton(pagerState, pagerState.canScrollForward, R.drawable.ic_next, 1)
|
|
}
|
|
|
|
@OptIn(ExperimentalFoundationApi::class)
|
|
@Composable
|
|
fun RowScope.CarouselButton(
|
|
pagerState: PagerState,
|
|
enabled: Boolean,
|
|
@DrawableRes id: Int,
|
|
delta: Int
|
|
) {
|
|
if (pagerState.pageCount <= 1) Spacer(modifier = Modifier.width(32.dp))
|
|
else {
|
|
val animationScope = rememberCoroutineScope()
|
|
IconButton(
|
|
modifier = Modifier
|
|
.width(40.dp)
|
|
.align(Alignment.CenterVertically),
|
|
enabled = enabled,
|
|
onClick = { animationScope.launch { pagerState.animateScrollToPage(pagerState.currentPage + delta) } }) {
|
|
Icon(
|
|
painter = painterResource(id = id),
|
|
contentDescription = null,
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
@Composable
|
|
fun Divider() {
|
|
androidx.compose.material.Divider(
|
|
modifier = Modifier.padding(horizontal = 16.dp),
|
|
)
|
|
}
|
|
|
|
@Composable
|
|
fun RowScope.Avatar(recipient: Recipient) {
|
|
Box(
|
|
modifier = Modifier
|
|
.width(60.dp)
|
|
.align(Alignment.CenterVertically)
|
|
) {
|
|
AndroidView(
|
|
factory = {
|
|
ProfilePictureView(it).apply { update(recipient) }
|
|
},
|
|
modifier = Modifier
|
|
.width(46.dp)
|
|
.height(46.dp)
|
|
)
|
|
}
|
|
}
|