mirror of
https://github.com/kalarina-bit/Matewave.git
synced 2026-11-22 08:06:09 +00:00
Add app source for F-Droid release (v1.0.43)
Adds the full Android source tree with unit tests, fastlane store metadata and the Gradle wrapper. The archive shipped without gradle-wrapper.jar, so the official Gradle 9.7.1 wrapper jar is included to make ./gradlew work from a fresh clone. .gitignore now also excludes build output and keystores. README gains build instructions and an updated structure tree. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015g6ECVSKcBG8CJPrqC3G5o
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
package cc.skysparkle.matewave.ui.components
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.BitmapFactory
|
||||
import android.util.DisplayMetrics
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.ImageBitmap
|
||||
import androidx.compose.ui.graphics.asImageBitmap
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import cc.skysparkle.matewave.R
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
private object BackgroundAssetCache {
|
||||
@Volatile private var bitmap: ImageBitmap? = null
|
||||
|
||||
fun cached(): ImageBitmap? = bitmap
|
||||
|
||||
fun load(context: Context): ImageBitmap? {
|
||||
bitmap?.let { return it }
|
||||
synchronized(this) {
|
||||
bitmap?.let { return it }
|
||||
val metrics: DisplayMetrics = context.resources.displayMetrics
|
||||
val target = maxOf(metrics.widthPixels, metrics.heightPixels).coerceAtLeast(1)
|
||||
|
||||
val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }
|
||||
BitmapFactory.decodeResource(context.resources, R.drawable.bg_forest, bounds)
|
||||
|
||||
var sample = 1
|
||||
while (maxOf(bounds.outWidth, bounds.outHeight) / sample > target * 2) sample *= 2
|
||||
|
||||
val options = BitmapFactory.Options().apply {
|
||||
inSampleSize = sample
|
||||
inPreferredConfig = android.graphics.Bitmap.Config.RGB_565
|
||||
}
|
||||
val decoded = runCatching {
|
||||
BitmapFactory.decodeResource(context.resources, R.drawable.bg_forest, options)
|
||||
?.asImageBitmap()
|
||||
}.getOrNull()
|
||||
bitmap = decoded
|
||||
return decoded
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun AppBackgroundImage(modifier: Modifier = Modifier.fillMaxSize()) {
|
||||
val context = LocalContext.current
|
||||
|
||||
var bitmap by remember { mutableStateOf(BackgroundAssetCache.cached()) }
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
if (bitmap == null) {
|
||||
bitmap = withContext(Dispatchers.IO) { BackgroundAssetCache.load(context) }
|
||||
}
|
||||
}
|
||||
|
||||
val image = bitmap
|
||||
if (image != null) {
|
||||
Image(
|
||||
bitmap = image,
|
||||
contentDescription = null,
|
||||
modifier = modifier,
|
||||
contentScale = ContentScale.Crop
|
||||
)
|
||||
} else {
|
||||
Box(modifier = modifier.background(Color(0xFF101413)))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
package cc.skysparkle.matewave.ui.components
|
||||
|
||||
import cc.skysparkle.matewave.ui.theme.ink
|
||||
|
||||
import androidx.compose.animation.animateColorAsState
|
||||
import androidx.compose.animation.core.tween
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
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.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.ColorFilter
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import cc.skysparkle.matewave.R
|
||||
import cc.skysparkle.matewave.ui.theme.UiCornerRadius
|
||||
|
||||
enum class BottomTab { HOME, FRIENDS, SETTINGS, ACCOUNT }
|
||||
|
||||
@Composable
|
||||
fun AppBottomBar(
|
||||
current: BottomTab,
|
||||
onHomeClick: () -> Unit,
|
||||
onFriendsClick: () -> Unit,
|
||||
onSettingsClick: () -> Unit,
|
||||
onAccountClick: () -> Unit,
|
||||
|
||||
accountAvatarBase64: String? = null
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 24.dp, vertical = 4.dp)
|
||||
.background(cc.skysparkle.matewave.ui.theme.FloatingBarFill, RoundedCornerShape(UiCornerRadius))
|
||||
.padding(vertical = 6.dp),
|
||||
horizontalArrangement = Arrangement.SpaceEvenly
|
||||
) {
|
||||
NavItem(iconRes = R.drawable.ic_home, label = stringResource(R.string.nav_home), active = current == BottomTab.HOME, onClick = onHomeClick)
|
||||
|
||||
NavItem(iconRes = R.drawable.ic_friends, label = stringResource(R.string.nav_friends), active = current == BottomTab.FRIENDS, onClick = onFriendsClick)
|
||||
NavItem(iconRes = R.drawable.ic_settings, label = stringResource(R.string.nav_settings), active = current == BottomTab.SETTINGS, onClick = onSettingsClick)
|
||||
|
||||
AccountNavItem(
|
||||
avatarBase64 = accountAvatarBase64,
|
||||
active = current == BottomTab.ACCOUNT,
|
||||
onClick = onAccountClick
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun RowScope.NavItem(iconRes: Int, label: String, active: Boolean, onClick: () -> Unit) {
|
||||
val shape = RoundedCornerShape(UiCornerRadius)
|
||||
|
||||
val highlight by animateColorAsState(
|
||||
if (active) ink.copy(alpha = 0.14f) else ink.copy(alpha = 0f),
|
||||
animationSpec = tween(220),
|
||||
label = "navHighlight"
|
||||
)
|
||||
val itemColor by animateColorAsState(
|
||||
if (active) ink else ink.copy(alpha = 0.7f),
|
||||
animationSpec = tween(220),
|
||||
label = "navColor"
|
||||
)
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.weight(1f)
|
||||
.clip(shape)
|
||||
.background(highlight, shape)
|
||||
.clickable(onClick = onClick)
|
||||
.padding(vertical = 8.dp),
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
Image(
|
||||
painter = painterResource(iconRes),
|
||||
contentDescription = null,
|
||||
colorFilter = ColorFilter.tint(itemColor),
|
||||
modifier = Modifier.size(24.dp)
|
||||
)
|
||||
Spacer(Modifier.height(4.dp))
|
||||
Text(
|
||||
label,
|
||||
color = if (active) itemColor else Color.Transparent,
|
||||
style = MaterialTheme.typography.labelMedium
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun RowScope.AccountNavItem(avatarBase64: String?, active: Boolean, onClick: () -> Unit) {
|
||||
val shape = RoundedCornerShape(UiCornerRadius)
|
||||
val bitmap = rememberChatImage(avatarBase64, AVATAR_MAX_DIMENSION_PX)
|
||||
val highlight by animateColorAsState(
|
||||
if (active) ink.copy(alpha = 0.14f) else ink.copy(alpha = 0f),
|
||||
animationSpec = tween(220),
|
||||
label = "navHighlight"
|
||||
)
|
||||
val itemColor by animateColorAsState(
|
||||
if (active) ink else ink.copy(alpha = 0.7f),
|
||||
animationSpec = tween(220),
|
||||
label = "navColor"
|
||||
)
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.weight(1f)
|
||||
.clip(shape)
|
||||
.background(highlight, shape)
|
||||
.clickable(onClick = onClick)
|
||||
.padding(vertical = 8.dp),
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
if (bitmap != null) {
|
||||
Image(
|
||||
bitmap = bitmap,
|
||||
contentDescription = null,
|
||||
contentScale = ContentScale.Crop,
|
||||
modifier = Modifier.size(24.dp).clip(CircleShape)
|
||||
)
|
||||
} else {
|
||||
Image(
|
||||
painter = painterResource(R.drawable.ic_default_avatar),
|
||||
contentDescription = null,
|
||||
colorFilter = ColorFilter.tint(itemColor),
|
||||
modifier = Modifier.size(24.dp)
|
||||
)
|
||||
}
|
||||
Spacer(Modifier.height(4.dp))
|
||||
Text(
|
||||
stringResource(R.string.account_title),
|
||||
color = if (active) itemColor else Color.Transparent,
|
||||
style = MaterialTheme.typography.labelMedium
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
package cc.skysparkle.matewave.ui.components
|
||||
|
||||
import android.os.Build
|
||||
import android.view.WindowManager
|
||||
import androidx.compose.animation.core.FastOutSlowInEasing
|
||||
import androidx.compose.animation.core.animateFloatAsState
|
||||
import androidx.compose.animation.core.tween
|
||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.SideEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.draw.drawBehind
|
||||
import androidx.compose.ui.graphics.graphicsLayer
|
||||
import androidx.compose.ui.platform.LocalView
|
||||
import androidx.compose.ui.window.DialogProperties
|
||||
import androidx.compose.ui.window.DialogWindowProvider
|
||||
import cc.skysparkle.matewave.ui.theme.DialogScrimColor
|
||||
import cc.skysparkle.matewave.ui.theme.ink
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.ColumnScope
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.RowScope
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.widthIn
|
||||
import androidx.compose.foundation.layout.WindowInsets
|
||||
import androidx.compose.foundation.layout.safeDrawing
|
||||
import androidx.compose.foundation.layout.windowInsetsPadding
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.window.Dialog
|
||||
|
||||
val DialogSurface = Color(0xFF262B29)
|
||||
val DialogAccent = Color(0xFF93DBA3)
|
||||
val DialogShape = RoundedCornerShape(28.dp)
|
||||
|
||||
@Composable
|
||||
fun AppDialog(
|
||||
onDismissRequest: () -> Unit,
|
||||
title: String? = null,
|
||||
actions: (@Composable RowScope.() -> Unit)? = null,
|
||||
content: @Composable ColumnScope.() -> Unit = {}
|
||||
) {
|
||||
Dialog(
|
||||
onDismissRequest = onDismissRequest,
|
||||
|
||||
properties = DialogProperties(usePlatformDefaultWidth = false, decorFitsSystemWindows = false)
|
||||
) {
|
||||
val window = (LocalView.current.parent as? DialogWindowProvider)?.window
|
||||
SideEffect {
|
||||
window?.let { w ->
|
||||
w.setDimAmount(0f)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||
runCatching {
|
||||
if (w.windowManager.isCrossWindowBlurEnabled) {
|
||||
w.addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND)
|
||||
w.attributes = w.attributes.apply { blurBehindRadius = 24 }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var shown by remember { mutableStateOf(false) }
|
||||
LaunchedEffect(Unit) { shown = true }
|
||||
val progress by animateFloatAsState(
|
||||
targetValue = if (shown) 1f else 0f,
|
||||
animationSpec = tween(durationMillis = 240, easing = FastOutSlowInEasing),
|
||||
label = "dialogAppear"
|
||||
)
|
||||
val scrim = DialogScrimColor
|
||||
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.drawBehind { drawRect(scrim.copy(alpha = scrim.alpha * progress)) }
|
||||
.clickable(
|
||||
interactionSource = remember { MutableInteractionSource() },
|
||||
indication = null,
|
||||
onClick = onDismissRequest
|
||||
),
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.windowInsetsPadding(WindowInsets.safeDrawing)
|
||||
.padding(horizontal = 24.dp, vertical = 16.dp)
|
||||
.fillMaxWidth()
|
||||
.widthIn(max = 560.dp)
|
||||
.graphicsLayer {
|
||||
alpha = progress
|
||||
val s = 0.92f + 0.08f * progress
|
||||
scaleX = s
|
||||
scaleY = s
|
||||
translationY = (1f - progress) * 24.dp.toPx()
|
||||
}
|
||||
.clip(DialogShape)
|
||||
.background(DialogSurface)
|
||||
|
||||
.clickable(
|
||||
interactionSource = remember { MutableInteractionSource() },
|
||||
indication = null
|
||||
) { }
|
||||
.padding(start = 24.dp, end = 16.dp, top = 24.dp, bottom = 16.dp)
|
||||
) {
|
||||
if (title != null) {
|
||||
Text(
|
||||
title,
|
||||
color = ink,
|
||||
style = MaterialTheme.typography.headlineSmall,
|
||||
modifier = Modifier.padding(end = 8.dp)
|
||||
)
|
||||
Spacer(Modifier.height(16.dp))
|
||||
}
|
||||
// Long content scrolls between the fixed title and buttons.
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.weight(1f, fill = false)
|
||||
.fillMaxWidth()
|
||||
.verticalScroll(rememberScrollState())
|
||||
.padding(end = 8.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp),
|
||||
content = content
|
||||
)
|
||||
if (actions != null) {
|
||||
Spacer(Modifier.height(24.dp))
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.spacedBy(4.dp, Alignment.End),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
content = actions
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun DialogText(text: String, modifier: Modifier = Modifier) {
|
||||
Text(
|
||||
text,
|
||||
color = ink.copy(alpha = 0.78f),
|
||||
style = MaterialTheme.typography.bodyLarge,
|
||||
modifier = modifier
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun DialogAction(
|
||||
text: String,
|
||||
onClick: () -> Unit,
|
||||
enabled: Boolean = true,
|
||||
color: Color = DialogAccent
|
||||
) {
|
||||
Text(
|
||||
text,
|
||||
color = if (enabled) color else color.copy(alpha = 0.4f),
|
||||
style = MaterialTheme.typography.titleSmall,
|
||||
modifier = Modifier
|
||||
.clip(RoundedCornerShape(50))
|
||||
.then(if (enabled) Modifier.clickable(onClick = onClick) else Modifier)
|
||||
.padding(horizontal = 12.dp, vertical = 10.dp)
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package cc.skysparkle.matewave.ui.components
|
||||
|
||||
import cc.skysparkle.matewave.ui.theme.ink
|
||||
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.ColorFilter
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.unit.Dp
|
||||
import androidx.compose.ui.unit.dp
|
||||
|
||||
@Composable
|
||||
fun AppIcon(
|
||||
resId: Int,
|
||||
size: Dp = 20.dp,
|
||||
tint: Color = ink,
|
||||
modifier: Modifier = Modifier,
|
||||
contentDescription: String? = null
|
||||
) {
|
||||
Image(
|
||||
painter = painterResource(resId),
|
||||
contentDescription = contentDescription,
|
||||
colorFilter = ColorFilter.tint(tint),
|
||||
modifier = modifier.size(size)
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun ButtonIcon(resId: Int, tint: Color = ink, size: Dp = 18.dp) {
|
||||
AppIcon(resId, size = size, tint = tint, modifier = Modifier.padding(end = 8.dp))
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package cc.skysparkle.matewave.ui.components
|
||||
|
||||
import cc.skysparkle.matewave.ui.theme.ink
|
||||
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import cc.skysparkle.matewave.engine.Piece
|
||||
import cc.skysparkle.matewave.engine.PieceColor
|
||||
import cc.skysparkle.matewave.engine.PieceType
|
||||
|
||||
@Composable
|
||||
fun CapturedPiecesRow(
|
||||
captured: List<PieceType>,
|
||||
capturedColor: PieceColor,
|
||||
advantage: Int,
|
||||
modifier: Modifier = Modifier
|
||||
) {
|
||||
Row(
|
||||
modifier = modifier.fillMaxWidth().height(22.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(3.dp), verticalAlignment = Alignment.CenterVertically) {
|
||||
captured.groupBy { it }.forEach { (type, group) ->
|
||||
Row(horizontalArrangement = Arrangement.spacedBy((-10).dp)) {
|
||||
repeat(group.size) {
|
||||
Image(
|
||||
painter = painterResource(Piece(type, capturedColor).drawableRes()),
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(20.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (advantage > 0) {
|
||||
Spacer(Modifier.width(6.dp))
|
||||
Text(
|
||||
"+$advantage",
|
||||
color = ink.copy(alpha = 0.85f),
|
||||
style = MaterialTheme.typography.labelLarge
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
package cc.skysparkle.matewave.ui.components
|
||||
|
||||
import cc.skysparkle.matewave.ui.theme.ink
|
||||
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.heightIn
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.widthIn
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import cc.skysparkle.matewave.R
|
||||
import cc.skysparkle.matewave.ui.theme.ChessGreen
|
||||
import cc.skysparkle.matewave.ui.theme.whiteOutlinedTextFieldColors
|
||||
import cc.skysparkle.matewave.viewmodel.ChatEntry
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
@Composable
|
||||
fun ChatDialog(
|
||||
messages: List<ChatEntry>,
|
||||
onSendText: (String) -> Unit,
|
||||
onSendImage: (String) -> Unit,
|
||||
onDismiss: () -> Unit
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
val scope = rememberCoroutineScope()
|
||||
var text by remember { mutableStateOf("") }
|
||||
var sendingImage by remember { mutableStateOf(false) }
|
||||
val scrollState = rememberScrollState()
|
||||
|
||||
val imagePicker = rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { uri ->
|
||||
if (uri == null) return@rememberLauncherForActivityResult
|
||||
sendingImage = true
|
||||
scope.launch {
|
||||
val encoded = withContext(Dispatchers.IO) { encodeImageForChat(context, uri) }
|
||||
sendingImage = false
|
||||
if (encoded != null) onSendImage(encoded)
|
||||
}
|
||||
}
|
||||
|
||||
LaunchedEffect(messages.size) {
|
||||
if (messages.isNotEmpty()) scrollState.animateScrollTo(scrollState.maxValue)
|
||||
}
|
||||
|
||||
AppDialog(
|
||||
onDismissRequest = onDismiss,
|
||||
title = stringResource(R.string.chat_title),
|
||||
actions = { DialogAction(stringResource(R.string.chat_close), onDismiss) }
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.heightIn(min = 120.dp, max = 320.dp)
|
||||
.verticalScroll(scrollState),
|
||||
verticalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
if (messages.isEmpty()) {
|
||||
DialogText(stringResource(R.string.chat_empty))
|
||||
}
|
||||
messages.forEach { entry -> ChatBubble(entry) }
|
||||
}
|
||||
|
||||
if (sendingImage) {
|
||||
GlassProgress(stringResource(R.string.chat_sending_image))
|
||||
}
|
||||
|
||||
Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
GlassOutlinedButton(onClick = { imagePicker.launch("image/*") }) {
|
||||
AppIcon(R.drawable.ic_add_photo, size = 22.dp)
|
||||
}
|
||||
OutlinedTextField(
|
||||
value = text,
|
||||
onValueChange = { text = it },
|
||||
modifier = Modifier.weight(1f),
|
||||
placeholder = { Text(stringResource(R.string.chat_input_hint), color = ink.copy(alpha = 0.5f)) },
|
||||
colors = whiteOutlinedTextFieldColors()
|
||||
)
|
||||
GlassButton(
|
||||
onClick = {
|
||||
if (text.isNotBlank()) {
|
||||
onSendText(text.trim())
|
||||
text = ""
|
||||
}
|
||||
},
|
||||
enabled = text.isNotBlank()
|
||||
) {
|
||||
AppIcon(R.drawable.ic_arrow_forward, size = 20.dp)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun ChatBubble(entry: ChatEntry) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = if (entry.fromMe) Arrangement.End else Arrangement.Start
|
||||
) {
|
||||
val shape = RoundedCornerShape(14.dp)
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.widthIn(max = 220.dp)
|
||||
.background(if (entry.fromMe) ChessGreen.copy(alpha = 0.55f) else ink.copy(alpha = 0.15f), shape)
|
||||
.padding(10.dp)
|
||||
) {
|
||||
entry.text?.let { Text(it, color = ink) }
|
||||
entry.imageBase64?.let { b64 ->
|
||||
val image = rememberChatImage(b64)
|
||||
if (image != null) {
|
||||
Image(
|
||||
bitmap = image,
|
||||
contentDescription = null,
|
||||
contentScale = ContentScale.Fit,
|
||||
modifier = Modifier.size(180.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
package cc.skysparkle.matewave.ui.components
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.graphics.Matrix
|
||||
import android.media.ExifInterface
|
||||
import android.net.Uri
|
||||
import android.util.Base64
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.produceState
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.graphics.ImageBitmap
|
||||
import androidx.compose.ui.graphics.asImageBitmap
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.ByteArrayOutputStream
|
||||
|
||||
private const val MAX_DIMENSION_PX = 640
|
||||
private const val JPEG_QUALITY = 60
|
||||
|
||||
const val AVATAR_MAX_DIMENSION_PX = 320
|
||||
const val AVATAR_JPEG_QUALITY = 75
|
||||
|
||||
fun encodeImageForChat(
|
||||
context: Context,
|
||||
uri: Uri,
|
||||
maxDimension: Int = MAX_DIMENSION_PX,
|
||||
quality: Int = JPEG_QUALITY
|
||||
): String? {
|
||||
return try {
|
||||
val resolver = context.contentResolver
|
||||
// Read the size first and decode a reduced copy: a full camera photo would not fit in memory.
|
||||
val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }
|
||||
val boundsStream = resolver.openInputStream(uri) ?: return null
|
||||
boundsStream.use { BitmapFactory.decodeStream(it, null, bounds) }
|
||||
if (bounds.outWidth <= 0 || bounds.outHeight <= 0) return null
|
||||
var sample = 1
|
||||
while (maxOf(bounds.outWidth, bounds.outHeight) / (sample * 2) >= maxDimension) sample *= 2
|
||||
val options = BitmapFactory.Options().apply { inSampleSize = sample }
|
||||
val decoded = resolver.openInputStream(uri)?.use { BitmapFactory.decodeStream(it, null, options) } ?: return null
|
||||
|
||||
val rotation = runCatching {
|
||||
resolver.openInputStream(uri)?.use { input ->
|
||||
when (ExifInterface(input).getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)) {
|
||||
ExifInterface.ORIENTATION_ROTATE_90 -> 90f
|
||||
ExifInterface.ORIENTATION_ROTATE_180 -> 180f
|
||||
ExifInterface.ORIENTATION_ROTATE_270 -> 270f
|
||||
else -> 0f
|
||||
}
|
||||
}
|
||||
}.getOrNull() ?: 0f
|
||||
|
||||
val scale = minOf(1f, maxDimension.toFloat() / maxOf(decoded.width, decoded.height))
|
||||
val matrix = Matrix().apply {
|
||||
if (scale < 1f) postScale(scale, scale)
|
||||
if (rotation != 0f) postRotate(rotation)
|
||||
}
|
||||
val prepared = if (scale < 1f || rotation != 0f) {
|
||||
Bitmap.createBitmap(decoded, 0, 0, decoded.width, decoded.height, matrix, true)
|
||||
} else {
|
||||
decoded
|
||||
}
|
||||
|
||||
val output = ByteArrayOutputStream()
|
||||
prepared.compress(Bitmap.CompressFormat.JPEG, quality, output)
|
||||
Base64.encodeToString(output.toByteArray(), Base64.NO_WRAP)
|
||||
} catch (e: Exception) {
|
||||
null
|
||||
} catch (e: OutOfMemoryError) {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
private object DecodedImageCache {
|
||||
private val cache = object : android.util.LruCache<String, Bitmap>(budgetBytes()) {
|
||||
override fun sizeOf(key: String, value: Bitmap): Int = value.byteCount
|
||||
}
|
||||
|
||||
private fun budgetBytes(): Int =
|
||||
(Runtime.getRuntime().maxMemory() / 16).toInt().coerceIn(4 shl 20, 32 shl 20)
|
||||
|
||||
fun key(base64: String, maxDimension: Int) = "$maxDimension:${base64.length}:${base64.hashCode()}"
|
||||
|
||||
fun get(key: String): Bitmap? = cache.get(key)
|
||||
fun put(key: String, bitmap: Bitmap) { cache.put(key, bitmap) }
|
||||
}
|
||||
|
||||
fun decodeChatImage(base64: String, maxDimension: Int = 0): Bitmap? {
|
||||
return try {
|
||||
val bytes = Base64.decode(base64, Base64.NO_WRAP)
|
||||
if (maxDimension <= 0) {
|
||||
BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
|
||||
} else {
|
||||
val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }
|
||||
BitmapFactory.decodeByteArray(bytes, 0, bytes.size, bounds)
|
||||
var sample = 1
|
||||
while (maxOf(bounds.outWidth, bounds.outHeight) / (sample * 2) >= maxDimension) sample *= 2
|
||||
val options = BitmapFactory.Options().apply { inSampleSize = sample }
|
||||
BitmapFactory.decodeByteArray(bytes, 0, bytes.size, options)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun rememberChatImage(base64: String?, maxDimension: Int = 0): ImageBitmap? {
|
||||
if (base64 == null) return null
|
||||
val key = remember(base64, maxDimension) { DecodedImageCache.key(base64, maxDimension) }
|
||||
val state = produceState<ImageBitmap?>(
|
||||
initialValue = DecodedImageCache.get(key)?.asImageBitmap(),
|
||||
key1 = key
|
||||
) {
|
||||
if (value == null) {
|
||||
value = withContext(Dispatchers.Default) {
|
||||
val bitmap = decodeChatImage(base64, maxDimension)
|
||||
if (bitmap != null) DecodedImageCache.put(key, bitmap)
|
||||
bitmap?.asImageBitmap()
|
||||
}
|
||||
}
|
||||
}
|
||||
return state.value
|
||||
}
|
||||
@@ -0,0 +1,341 @@
|
||||
package cc.skysparkle.matewave.ui.components
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.BitmapFactory
|
||||
import android.graphics.Paint
|
||||
import android.graphics.Typeface
|
||||
import androidx.compose.foundation.Canvas
|
||||
import androidx.compose.foundation.gestures.detectDragGesturesAfterLongPress
|
||||
import androidx.compose.foundation.gestures.detectTapGestures
|
||||
import androidx.compose.foundation.layout.aspectRatio
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberUpdatedState
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.geometry.Offset
|
||||
import androidx.compose.ui.geometry.Size
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.FilterQuality
|
||||
import androidx.compose.ui.graphics.ImageBitmap
|
||||
import androidx.compose.ui.graphics.asImageBitmap
|
||||
import androidx.compose.ui.graphics.drawscope.DrawScope
|
||||
import androidx.compose.ui.graphics.drawscope.Stroke
|
||||
import androidx.compose.ui.graphics.nativeCanvas
|
||||
import androidx.compose.ui.graphics.toArgb
|
||||
import androidx.compose.ui.input.pointer.pointerInput
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.unit.IntOffset
|
||||
import androidx.compose.ui.unit.IntSize
|
||||
import cc.skysparkle.matewave.engine.Board
|
||||
import cc.skysparkle.matewave.engine.Move
|
||||
import cc.skysparkle.matewave.engine.Piece
|
||||
import cc.skysparkle.matewave.engine.PieceColor
|
||||
import cc.skysparkle.matewave.engine.PieceType
|
||||
import cc.skysparkle.matewave.settings.BoardAppearance
|
||||
import cc.skysparkle.matewave.ui.theme.HighlightCheck
|
||||
import cc.skysparkle.matewave.ui.theme.HighlightHint
|
||||
import cc.skysparkle.matewave.ui.theme.HighlightLastMove
|
||||
import cc.skysparkle.matewave.ui.theme.HighlightMove
|
||||
import kotlin.math.floor
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
private object PieceImages {
|
||||
@Volatile private var cache: Array<ImageBitmap>? = null
|
||||
|
||||
fun get(context: Context): Array<ImageBitmap> {
|
||||
cache?.let { return it }
|
||||
synchronized(this) {
|
||||
cache?.let { return it }
|
||||
val options = BitmapFactory.Options().apply { inScaled = false }
|
||||
val types = PieceType.values()
|
||||
val loaded = Array(PieceColor.values().size * types.size) { i ->
|
||||
val piece = Piece(types[i % types.size], PieceColor.values()[i / types.size])
|
||||
BitmapFactory.decodeResource(context.resources, piece.drawableRes(), options).asImageBitmap()
|
||||
}
|
||||
cache = loaded
|
||||
return loaded
|
||||
}
|
||||
}
|
||||
|
||||
fun indexOf(piece: Piece): Int = piece.color.ordinal * PieceType.values().size + piece.type.ordinal
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun ChessBoardView(
|
||||
board: Board,
|
||||
selectedSquare: Int?,
|
||||
legalTargets: List<Move>,
|
||||
kingInCheckSquare: Int?,
|
||||
flipped: Boolean,
|
||||
onSquareClick: (Int) -> Unit,
|
||||
modifier: Modifier = Modifier,
|
||||
lastMove: Move? = null,
|
||||
hintSquare: Int? = null,
|
||||
interactionEnabled: Boolean = true,
|
||||
/** A square briefly tinted to confirm or reject a move (puzzles). */
|
||||
flashSquare: Int? = null,
|
||||
flashColor: Color = Color.Transparent
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
val appearance = remember { BoardAppearance(context) }
|
||||
val theme = appearance.theme
|
||||
val showCoords = appearance.showCoordinates
|
||||
val imagePieces = appearance.useImagePieces
|
||||
val showLastMove = appearance.showLastMove
|
||||
val showCheck = appearance.showCheckHighlight
|
||||
|
||||
val pieceImages = if (imagePieces) remember(context) { PieceImages.get(context) } else null
|
||||
|
||||
val targetMask = remember(legalTargets) {
|
||||
var mask = 0L
|
||||
for (m in legalTargets) mask = mask or (1L shl m.to)
|
||||
mask
|
||||
}
|
||||
|
||||
val coordPaint = remember {
|
||||
Paint().apply { isAntiAlias = true; typeface = Typeface.DEFAULT_BOLD }
|
||||
}
|
||||
val glyphPaint = remember {
|
||||
Paint().apply { isAntiAlias = true; textAlign = Paint.Align.CENTER }
|
||||
}
|
||||
|
||||
val currentOnSquareClick by rememberUpdatedState(onSquareClick)
|
||||
val currentBoard by rememberUpdatedState(board)
|
||||
val currentSelected by rememberUpdatedState(selectedSquare)
|
||||
val currentTargetMask by rememberUpdatedState(targetMask)
|
||||
|
||||
var dragSquare by remember { mutableStateOf<Int?>(null) }
|
||||
var dragPos by remember { mutableStateOf<Offset?>(null) }
|
||||
|
||||
Canvas(
|
||||
modifier = modifier
|
||||
.fillMaxWidth()
|
||||
.aspectRatio(1f)
|
||||
.pointerInput(flipped, interactionEnabled) {
|
||||
if (interactionEnabled) {
|
||||
detectTapGestures { tap ->
|
||||
val cellPx = size.width / 8f
|
||||
val col = floor(tap.x / cellPx).toInt().coerceIn(0, 7)
|
||||
val row = floor(tap.y / cellPx).toInt().coerceIn(0, 7)
|
||||
val displayRank = if (flipped) row else 7 - row
|
||||
val displayFile = if (flipped) 7 - col else col
|
||||
currentOnSquareClick(displayRank * 8 + displayFile)
|
||||
}
|
||||
}
|
||||
}
|
||||
.pointerInput(flipped, interactionEnabled) {
|
||||
if (interactionEnabled) {
|
||||
detectDragGesturesAfterLongPress(
|
||||
onDragStart = { start ->
|
||||
val src = squareAt(start.x, start.y, size.width.toFloat(), flipped)
|
||||
val piece = if (src >= 0) currentBoard.pieceAt(src) else null
|
||||
|
||||
if (piece != null && piece.color == currentBoard.sideToMove) {
|
||||
dragSquare = src
|
||||
dragPos = start
|
||||
|
||||
if (currentSelected == src) cc.skysparkle.matewave.settings.Haptics.select()
|
||||
|
||||
if (currentSelected != src) currentOnSquareClick(src)
|
||||
}
|
||||
},
|
||||
onDrag = { change, _ ->
|
||||
if (dragSquare != null) dragPos = change.position
|
||||
},
|
||||
onDragEnd = {
|
||||
val src = dragSquare
|
||||
val pos = dragPos
|
||||
dragSquare = null
|
||||
dragPos = null
|
||||
if (src != null && pos != null && currentSelected == src) {
|
||||
val target = squareAt(pos.x, pos.y, size.width.toFloat(), flipped)
|
||||
if (target >= 0 && target != src &&
|
||||
((currentTargetMask shr target) and 1L) != 0L
|
||||
) {
|
||||
currentOnSquareClick(target)
|
||||
}
|
||||
}
|
||||
},
|
||||
onDragCancel = {
|
||||
dragSquare = null
|
||||
dragPos = null
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
) {
|
||||
val cell = size.width / 8f
|
||||
val lastFrom = if (showLastMove) (lastMove?.from ?: -1) else -1
|
||||
val lastTo = if (showLastMove) (lastMove?.to ?: -1) else -1
|
||||
val selected = selectedSquare ?: -1
|
||||
val checkSquare = if (showCheck) (kingInCheckSquare ?: -1) else -1
|
||||
val hint = hintSquare ?: -1
|
||||
val flash = flashSquare ?: -1
|
||||
|
||||
val dSq = dragSquare
|
||||
val dPos = dragPos
|
||||
val liftedSq = if (dSq != null && dPos != null && selected == dSq) dSq else -1
|
||||
val liftedAt: Offset? = if (liftedSq >= 0) dPos else null
|
||||
|
||||
for (row in 0 until 8) {
|
||||
for (col in 0 until 8) {
|
||||
val displayRank = if (flipped) row else 7 - row
|
||||
val displayFile = if (flipped) 7 - col else col
|
||||
val sqIndex = displayRank * 8 + displayFile
|
||||
val isLight = (displayRank + displayFile) % 2 == 1
|
||||
var color = if (isLight) theme.light else theme.dark
|
||||
if (sqIndex == lastFrom || sqIndex == lastTo) color = HighlightLastMove
|
||||
if (sqIndex == hint) color = HighlightHint
|
||||
if (sqIndex == selected) color = HighlightMove
|
||||
if (sqIndex == checkSquare) color = HighlightCheck
|
||||
drawRect(
|
||||
color = color,
|
||||
topLeft = Offset(col * cell, row * cell),
|
||||
size = Size(cell, cell)
|
||||
)
|
||||
if (sqIndex == flash) {
|
||||
drawRect(color = flashColor, topLeft = Offset(col * cell, row * cell), size = Size(cell, cell))
|
||||
}
|
||||
|
||||
if (showCoords) {
|
||||
val labelColor = if (isLight) theme.dark else theme.light
|
||||
if (row == 7) {
|
||||
drawCoordinate(
|
||||
('a' + displayFile).toString(),
|
||||
Offset(col * cell + cell * 0.78f, row * cell + cell * 0.80f),
|
||||
cell, labelColor, coordPaint
|
||||
)
|
||||
}
|
||||
if (col == 0) {
|
||||
drawCoordinate(
|
||||
(displayRank + 1).toString(),
|
||||
Offset(col * cell + cell * 0.10f, row * cell + cell * 0.22f),
|
||||
cell, labelColor, coordPaint
|
||||
)
|
||||
}
|
||||
}
|
||||
if ((targetMask shr sqIndex) and 1L != 0L) {
|
||||
drawCircle(
|
||||
color = HighlightMove,
|
||||
radius = cell * 0.14f,
|
||||
center = Offset(col * cell + cell / 2, row * cell + cell / 2)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val pieceSize = cell * 0.86f
|
||||
val inset = (cell - pieceSize) / 2f
|
||||
val pieceDst = IntSize(pieceSize.roundToInt(), pieceSize.roundToInt())
|
||||
if (pieceImages == null) {
|
||||
glyphPaint.textSize = cell * 0.62f
|
||||
glyphPaint.color = GLYPH_COLOR
|
||||
}
|
||||
for (row in 0 until 8) {
|
||||
for (col in 0 until 8) {
|
||||
val displayRank = if (flipped) row else 7 - row
|
||||
val displayFile = if (flipped) 7 - col else col
|
||||
val pieceSq = displayRank * 8 + displayFile
|
||||
if (pieceSq == liftedSq) continue
|
||||
val piece = board.pieceAt(pieceSq) ?: continue
|
||||
if (pieceImages != null) {
|
||||
drawImage(
|
||||
image = pieceImages[PieceImages.indexOf(piece)],
|
||||
dstOffset = IntOffset(
|
||||
(col * cell + inset).roundToInt(),
|
||||
(row * cell + inset).roundToInt()
|
||||
),
|
||||
dstSize = pieceDst,
|
||||
filterQuality = FilterQuality.Medium
|
||||
)
|
||||
} else {
|
||||
val metrics = glyphPaint.fontMetrics
|
||||
drawContext.canvas.nativeCanvas.drawText(
|
||||
piece.unicode(),
|
||||
col * cell + cell / 2f,
|
||||
row * cell + cell / 2f - (metrics.ascent + metrics.descent) / 2f,
|
||||
glyphPaint
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (liftedAt != null) {
|
||||
val liftedPiece = board.pieceAt(liftedSq)
|
||||
if (liftedPiece != null) {
|
||||
val hover = squareAt(liftedAt.x, liftedAt.y, size.width, flipped)
|
||||
if (hover >= 0 && ((targetMask shr hover) and 1L) != 0L) {
|
||||
val hRank = hover / 8
|
||||
val hFile = hover % 8
|
||||
val hRow = if (flipped) hRank else 7 - hRank
|
||||
val hCol = if (flipped) 7 - hFile else hFile
|
||||
drawRect(
|
||||
color = Color.White.copy(alpha = 0.75f),
|
||||
topLeft = Offset(hCol * cell, hRow * cell),
|
||||
size = Size(cell, cell),
|
||||
style = Stroke(width = cell * 0.07f)
|
||||
)
|
||||
}
|
||||
val liftedSize = pieceSize * LIFT_SCALE
|
||||
val cx = liftedAt.x
|
||||
val cy = liftedAt.y - cell * 0.55f
|
||||
drawOval(
|
||||
color = Color.Black.copy(alpha = 0.30f),
|
||||
topLeft = Offset(liftedAt.x - liftedSize * 0.36f, liftedAt.y + cell * 0.05f),
|
||||
size = Size(liftedSize * 0.72f, liftedSize * 0.28f)
|
||||
)
|
||||
if (pieceImages != null) {
|
||||
val liftedDst = IntSize(liftedSize.roundToInt(), liftedSize.roundToInt())
|
||||
drawImage(
|
||||
image = pieceImages[PieceImages.indexOf(liftedPiece)],
|
||||
dstOffset = IntOffset(
|
||||
(cx - liftedSize / 2f).roundToInt(),
|
||||
(cy - liftedSize / 2f).roundToInt()
|
||||
),
|
||||
dstSize = liftedDst,
|
||||
filterQuality = FilterQuality.Medium
|
||||
)
|
||||
} else {
|
||||
glyphPaint.textSize = cell * 0.62f * LIFT_SCALE
|
||||
val metrics = glyphPaint.fontMetrics
|
||||
drawContext.canvas.nativeCanvas.drawText(
|
||||
liftedPiece.unicode(),
|
||||
cx,
|
||||
cy - (metrics.ascent + metrics.descent) / 2f,
|
||||
glyphPaint
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private const val LIFT_SCALE = 1.6f
|
||||
|
||||
private fun squareAt(x: Float, y: Float, boardPx: Float, flipped: Boolean): Int {
|
||||
if (x < 0f || y < 0f || x >= boardPx || y >= boardPx) return -1
|
||||
val cellPx = boardPx / 8f
|
||||
val col = floor(x / cellPx).toInt().coerceIn(0, 7)
|
||||
val row = floor(y / cellPx).toInt().coerceIn(0, 7)
|
||||
val displayRank = if (flipped) row else 7 - row
|
||||
val displayFile = if (flipped) 7 - col else col
|
||||
return displayRank * 8 + displayFile
|
||||
}
|
||||
|
||||
private val GLYPH_COLOR = Color(0xFF1B1B1B).toArgb()
|
||||
|
||||
private fun DrawScope.drawCoordinate(
|
||||
text: String,
|
||||
position: Offset,
|
||||
cell: Float,
|
||||
color: Color,
|
||||
paint: Paint
|
||||
) {
|
||||
paint.color = color.toArgb()
|
||||
paint.textSize = cell * 0.20f
|
||||
drawContext.canvas.nativeCanvas.drawText(text, position.x, position.y, paint)
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
package cc.skysparkle.matewave.ui.components
|
||||
|
||||
import cc.skysparkle.matewave.ui.theme.ink
|
||||
|
||||
import androidx.compose.animation.core.animateFloatAsState
|
||||
import androidx.compose.animation.core.spring
|
||||
import androidx.compose.foundation.LocalIndication
|
||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
||||
import androidx.compose.foundation.interaction.collectIsPressedAsState
|
||||
import androidx.compose.runtime.CompositionLocalProvider
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.graphics.graphicsLayer
|
||||
import cc.skysparkle.matewave.ui.theme.LocalInk
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.border
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.ColumnScope
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.RowScope
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.navigationBarsPadding
|
||||
import androidx.compose.foundation.layout.statusBarsPadding
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.layout.widthIn
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.alpha
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.Shape
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import cc.skysparkle.matewave.R
|
||||
import cc.skysparkle.matewave.ui.theme.ChessGreen
|
||||
import cc.skysparkle.matewave.ui.theme.PanelBorder
|
||||
import cc.skysparkle.matewave.ui.theme.PanelFill
|
||||
import cc.skysparkle.matewave.ui.theme.TabletMaxContentWidth
|
||||
import cc.skysparkle.matewave.ui.theme.UiCornerRadius
|
||||
import cc.skysparkle.matewave.ui.theme.glass
|
||||
|
||||
private val UiShape: Shape = RoundedCornerShape(UiCornerRadius)
|
||||
|
||||
@Composable
|
||||
fun GlassScaffold(
|
||||
title: String,
|
||||
onBack: (() -> Unit)? = null,
|
||||
actions: @Composable RowScope.() -> Unit = {},
|
||||
scrollable: Boolean = true,
|
||||
|
||||
bottomBar: (@Composable () -> Unit)? = null,
|
||||
/** Replaces the plain title, e.g. with the opening name and its status badge. */
|
||||
titleContent: (@Composable RowScope.() -> Unit)? = null,
|
||||
content: @Composable ColumnScope.() -> Unit
|
||||
) {
|
||||
Box(modifier = Modifier.fillMaxSize()) {
|
||||
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.TopCenter) {
|
||||
Column(modifier = Modifier.fillMaxSize().widthIn(max = TabletMaxContentWidth)) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth().statusBarsPadding().padding(horizontal = 4.dp, vertical = 4.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
if (onBack != null) {
|
||||
IconButton(onClick = onBack) {
|
||||
AppIcon(
|
||||
R.drawable.ic_arrow_back,
|
||||
size = 24.dp,
|
||||
contentDescription = stringResource(R.string.back)
|
||||
)
|
||||
}
|
||||
} else {
|
||||
Spacer(Modifier.width(48.dp))
|
||||
}
|
||||
if (titleContent != null) {
|
||||
Row(modifier = Modifier.weight(1f), verticalAlignment = Alignment.CenterVertically, content = titleContent)
|
||||
} else {
|
||||
Text(
|
||||
title,
|
||||
color = ink,
|
||||
style = MaterialTheme.typography.titleLarge,
|
||||
modifier = Modifier.weight(1f)
|
||||
)
|
||||
}
|
||||
actions()
|
||||
}
|
||||
|
||||
val scrollState = rememberScrollState()
|
||||
|
||||
val contentModifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.weight(1f)
|
||||
.padding(horizontal = 16.dp)
|
||||
.then(if (scrollable) Modifier.verticalScroll(scrollState) else Modifier)
|
||||
.then(if (bottomBar == null) Modifier.navigationBarsPadding() else Modifier)
|
||||
.padding(bottom = 16.dp)
|
||||
|
||||
Column(
|
||||
modifier = contentModifier,
|
||||
verticalArrangement = Arrangement.spacedBy(14.dp),
|
||||
content = content
|
||||
)
|
||||
|
||||
if (bottomBar != null) {
|
||||
Box(modifier = Modifier.navigationBarsPadding()) { bottomBar() }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun Modifier.pressScale(interactionSource: MutableInteractionSource): Modifier {
|
||||
val pressed by interactionSource.collectIsPressedAsState()
|
||||
val scale by animateFloatAsState(
|
||||
targetValue = if (pressed) 0.97f else 1f,
|
||||
animationSpec = spring(dampingRatio = 0.6f, stiffness = 900f),
|
||||
label = "pressScale"
|
||||
)
|
||||
return this.graphicsLayer { scaleX = scale; scaleY = scale }
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun GlassButton(
|
||||
onClick: () -> Unit,
|
||||
modifier: Modifier = Modifier,
|
||||
enabled: Boolean = true,
|
||||
content: @Composable RowScope.() -> Unit
|
||||
) {
|
||||
val interaction = remember { MutableInteractionSource() }
|
||||
|
||||
CompositionLocalProvider(LocalInk provides Color.White) {
|
||||
Row(
|
||||
modifier = modifier
|
||||
.pressScale(interaction)
|
||||
.alpha(if (enabled) 1f else 0.4f)
|
||||
.clip(UiShape)
|
||||
.background(ChessGreen)
|
||||
.then(
|
||||
if (enabled) Modifier.clickable(
|
||||
interactionSource = interaction,
|
||||
indication = LocalIndication.current,
|
||||
onClick = onClick
|
||||
) else Modifier
|
||||
)
|
||||
.padding(horizontal = 20.dp, vertical = 14.dp),
|
||||
horizontalArrangement = Arrangement.Center,
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
content = content
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun GlassOutlinedButton(
|
||||
onClick: () -> Unit,
|
||||
modifier: Modifier = Modifier,
|
||||
enabled: Boolean = true,
|
||||
content: @Composable RowScope.() -> Unit
|
||||
) {
|
||||
val interaction = remember { MutableInteractionSource() }
|
||||
Row(
|
||||
modifier = modifier
|
||||
.pressScale(interaction)
|
||||
.alpha(if (enabled) 1f else 0.4f)
|
||||
.clip(UiShape)
|
||||
.background(PanelFill)
|
||||
.border(1.dp, PanelBorder, UiShape)
|
||||
.then(
|
||||
if (enabled) Modifier.clickable(
|
||||
interactionSource = interaction,
|
||||
indication = LocalIndication.current,
|
||||
onClick = onClick
|
||||
) else Modifier
|
||||
)
|
||||
.padding(horizontal = 20.dp, vertical = 14.dp),
|
||||
horizontalArrangement = Arrangement.Center,
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
content = content
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun GlassCard(modifier: Modifier = Modifier, shape: Shape = UiShape, content: @Composable ColumnScope.() -> Unit) {
|
||||
Column(
|
||||
modifier = modifier
|
||||
.glass(shape)
|
||||
.border(1.dp, PanelBorder, shape)
|
||||
.padding(14.dp),
|
||||
content = content
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun GlassProgress(text: String) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(10.dp)) {
|
||||
CircularProgressIndicator(color = ink, modifier = Modifier.padding(2.dp))
|
||||
Text(text, color = ink, style = MaterialTheme.typography.bodyMedium)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package cc.skysparkle.matewave.ui.components
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.ColumnScope
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.RowScope
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import cc.skysparkle.matewave.R
|
||||
import cc.skysparkle.matewave.ui.theme.UiCornerRadius
|
||||
import cc.skysparkle.matewave.ui.theme.glass
|
||||
import cc.skysparkle.matewave.ui.theme.ink
|
||||
|
||||
/** Small uppercase caption above a group. */
|
||||
@Composable
|
||||
fun SectionCaption(text: String, modifier: Modifier = Modifier) {
|
||||
Text(
|
||||
text.uppercase(),
|
||||
color = ink.copy(alpha = 0.5f),
|
||||
style = MaterialTheme.typography.labelMedium,
|
||||
modifier = modifier.padding(start = 4.dp, top = 6.dp)
|
||||
)
|
||||
}
|
||||
|
||||
/** Rows of one kind in a single block, instead of a card per row. Separate rows with [ListDivider]. */
|
||||
@Composable
|
||||
fun ListGroup(modifier: Modifier = Modifier, content: @Composable ColumnScope.() -> Unit) {
|
||||
Column(
|
||||
modifier = modifier
|
||||
.fillMaxWidth()
|
||||
.glass(RoundedCornerShape(UiCornerRadius))
|
||||
.padding(vertical = 2.dp),
|
||||
content = content
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun ListDivider(inset: Int = 52) {
|
||||
Box(
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(start = inset.dp)
|
||||
.height(1.dp)
|
||||
.background(ink.copy(alpha = 0.07f))
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* One row: optional icon or custom leading content, title with an optional subtitle, an
|
||||
* optional grey value on the right, custom trailing content and a chevron when it navigates.
|
||||
*/
|
||||
@Composable
|
||||
fun ListRow(
|
||||
title: String,
|
||||
modifier: Modifier = Modifier,
|
||||
subtitle: String? = null,
|
||||
icon: Int? = null,
|
||||
iconTint: Color = ink,
|
||||
titleColor: Color = ink,
|
||||
subtitleColor: Color = ink.copy(alpha = 0.6f),
|
||||
value: String? = null,
|
||||
valueColor: Color = ink.copy(alpha = 0.55f),
|
||||
chevron: Boolean = false,
|
||||
leading: (@Composable () -> Unit)? = null,
|
||||
trailing: (@Composable RowScope.() -> Unit)? = null,
|
||||
onClick: (() -> Unit)? = null
|
||||
) {
|
||||
Row(
|
||||
modifier = modifier
|
||||
.fillMaxWidth()
|
||||
.then(if (onClick != null) Modifier.clickable(onClick = onClick) else Modifier)
|
||||
.padding(horizontal = 16.dp, vertical = 12.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
when {
|
||||
leading != null -> { leading(); Spacer(Modifier.width(12.dp)) }
|
||||
icon != null -> { AppIcon(icon, size = 22.dp, tint = iconTint); Spacer(Modifier.width(14.dp)) }
|
||||
}
|
||||
Column(modifier = Modifier.weight(1f)) {
|
||||
Text(title, color = titleColor, style = MaterialTheme.typography.bodyLarge, maxLines = 2, overflow = TextOverflow.Ellipsis)
|
||||
if (subtitle != null) {
|
||||
Text(subtitle, color = subtitleColor, style = MaterialTheme.typography.bodySmall, maxLines = 2, overflow = TextOverflow.Ellipsis)
|
||||
}
|
||||
}
|
||||
if (value != null) {
|
||||
Spacer(Modifier.width(8.dp))
|
||||
Text(value, color = valueColor, style = MaterialTheme.typography.bodyMedium, maxLines = 1)
|
||||
}
|
||||
if (trailing != null) {
|
||||
Spacer(Modifier.width(8.dp))
|
||||
trailing()
|
||||
}
|
||||
if (chevron) {
|
||||
Spacer(Modifier.width(8.dp))
|
||||
AppIcon(R.drawable.ic_arrow_forward, size = 16.dp, tint = ink.copy(alpha = 0.5f))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package cc.skysparkle.matewave.ui.components
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.lazy.LazyRow
|
||||
import androidx.compose.foundation.lazy.itemsIndexed
|
||||
import androidx.compose.foundation.lazy.rememberLazyListState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import cc.skysparkle.matewave.R
|
||||
import cc.skysparkle.matewave.engine.Board
|
||||
import cc.skysparkle.matewave.engine.PieceColor
|
||||
import cc.skysparkle.matewave.openings.OpeningName
|
||||
import cc.skysparkle.matewave.ui.theme.ChessGreen
|
||||
import cc.skysparkle.matewave.ui.theme.ink
|
||||
|
||||
private data class MoveToken(val text: String, val moveIndex: Int?)
|
||||
|
||||
private val BookTint = androidx.compose.ui.graphics.Color(0xFF93DBA3)
|
||||
private val OutOfBookTint = androidx.compose.ui.graphics.Color(0xFFFFB86B)
|
||||
|
||||
/**
|
||||
* Compact move list that scrolls to the current move, with the opening name underneath.
|
||||
* [currentIndex] is the number of moves played to reach the shown position.
|
||||
*/
|
||||
@Composable
|
||||
fun MovesBar(
|
||||
sans: List<String>,
|
||||
startBoard: Board,
|
||||
currentIndex: Int,
|
||||
opening: OpeningName?,
|
||||
modifier: Modifier = Modifier,
|
||||
/** Leading moves that are opening theory; they are tinted in the list. */
|
||||
bookPlies: Int = 0,
|
||||
/** "Theory" or "Out of book from move N"; null hides the status. */
|
||||
bookStatus: String? = null,
|
||||
inBook: Boolean = true,
|
||||
onMoveClick: ((Int) -> Unit)? = null
|
||||
) {
|
||||
if (sans.isEmpty() && opening == null) return
|
||||
val tokens = remember(sans, startBoard) {
|
||||
buildList {
|
||||
sans.forEachIndexed { i, san ->
|
||||
val whiteMove = (startBoard.sideToMove == PieceColor.WHITE) == (i % 2 == 0)
|
||||
val number = startBoard.fullmoveNumber + (i + if (startBoard.sideToMove == PieceColor.BLACK) 1 else 0) / 2
|
||||
if (whiteMove) add(MoveToken("$number.", null))
|
||||
else if (i == 0) add(MoveToken("$number…", null))
|
||||
add(MoveToken(san, i))
|
||||
}
|
||||
}
|
||||
}
|
||||
val listState = rememberLazyListState()
|
||||
LaunchedEffect(currentIndex, tokens.size) {
|
||||
val target = tokens.indexOfFirst { it.moveIndex == currentIndex - 1 }
|
||||
if (target >= 0) listState.animateScrollToItem(maxOf(0, target - 3))
|
||||
}
|
||||
|
||||
Column(modifier = modifier.fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(4.dp)) {
|
||||
if (sans.isNotEmpty()) {
|
||||
LazyRow(state = listState, horizontalArrangement = Arrangement.spacedBy(2.dp)) {
|
||||
itemsIndexed(tokens) { _, token ->
|
||||
val index = token.moveIndex
|
||||
if (index == null) {
|
||||
Text(
|
||||
token.text,
|
||||
color = ink.copy(alpha = 0.5f),
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
modifier = Modifier.padding(start = 6.dp, top = 4.dp, bottom = 4.dp)
|
||||
)
|
||||
} else {
|
||||
val current = index == currentIndex - 1
|
||||
Text(
|
||||
token.text,
|
||||
color = if (index < bookPlies) BookTint else ink,
|
||||
fontWeight = if (current) FontWeight.Bold else FontWeight.Medium,
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
modifier = Modifier
|
||||
.clip(RoundedCornerShape(8.dp))
|
||||
.then(if (current) Modifier.background(ChessGreen.copy(alpha = 0.45f)) else Modifier)
|
||||
.then(if (onMoveClick != null) Modifier.clickable { onMoveClick(index + 1) } else Modifier)
|
||||
.padding(horizontal = 6.dp, vertical = 4.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (opening != null || bookStatus != null) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
AppIcon(R.drawable.ic_book, size = 14.dp, tint = if (inBook) BookTint else ink.copy(alpha = 0.5f))
|
||||
Spacer(Modifier.width(6.dp))
|
||||
Text(
|
||||
opening?.let { "${it.eco} · ${it.name}" } ?: "",
|
||||
color = ink.copy(alpha = 0.85f),
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
modifier = Modifier.weight(1f, fill = false)
|
||||
)
|
||||
if (bookStatus != null) {
|
||||
Spacer(Modifier.width(8.dp))
|
||||
Text(
|
||||
bookStatus,
|
||||
color = if (inBook) BookTint else OutOfBookTint,
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
maxLines = 1,
|
||||
modifier = Modifier
|
||||
.clip(RoundedCornerShape(6.dp))
|
||||
.background((if (inBook) BookTint else OutOfBookTint).copy(alpha = 0.15f))
|
||||
.padding(horizontal = 6.dp, vertical = 2.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
package cc.skysparkle.matewave.ui.components
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.ContextWrapper
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.Uri
|
||||
import android.provider.Settings
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.core.app.ActivityCompat
|
||||
import androidx.core.content.ContextCompat
|
||||
import cc.skysparkle.matewave.R
|
||||
|
||||
/**
|
||||
* Asks for [permissions] after a short explanation. Once Android stops showing its own prompt
|
||||
* (the user declined twice), the explanation leads to the app's system settings instead, so
|
||||
* the button never silently does nothing.
|
||||
*/
|
||||
@Composable
|
||||
fun rememberPermissionRequester(
|
||||
permissions: List<String>,
|
||||
rationaleTitle: String,
|
||||
rationaleText: String,
|
||||
onResult: (granted: Boolean) -> Unit
|
||||
): () -> Unit {
|
||||
val context = LocalContext.current
|
||||
var showRationale by remember { mutableStateOf(false) }
|
||||
var openSettings by remember { mutableStateOf(false) }
|
||||
|
||||
val launcher = rememberLauncherForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { results ->
|
||||
onResult(results.values.all { it })
|
||||
}
|
||||
|
||||
if (showRationale) {
|
||||
PermissionRationaleDialog(
|
||||
title = rationaleTitle,
|
||||
text = rationaleText,
|
||||
confirmText = stringResource(if (openSettings) R.string.permission_open_settings else R.string.permission_allow),
|
||||
onConfirm = {
|
||||
showRationale = false
|
||||
if (openSettings) {
|
||||
openAppSettings(context)
|
||||
onResult(false)
|
||||
} else {
|
||||
markRequested(context, permissions)
|
||||
launcher.launch(permissions.toTypedArray())
|
||||
}
|
||||
},
|
||||
onDismiss = {
|
||||
showRationale = false
|
||||
onResult(false)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
return {
|
||||
val allGranted = permissions.all {
|
||||
ContextCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED
|
||||
}
|
||||
if (allGranted) {
|
||||
onResult(true)
|
||||
} else {
|
||||
openSettings = permanentlyDenied(context, permissions)
|
||||
showRationale = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private const val REQUESTS_PREFS = "permission_requests"
|
||||
|
||||
private fun markRequested(context: Context, permissions: List<String>) {
|
||||
val editor = context.getSharedPreferences(REQUESTS_PREFS, Context.MODE_PRIVATE).edit()
|
||||
permissions.forEach { editor.putBoolean(it, true) }
|
||||
editor.apply()
|
||||
}
|
||||
|
||||
/** Asked before, still denied, and Android would not show its prompt again. */
|
||||
private fun permanentlyDenied(context: Context, permissions: List<String>): Boolean {
|
||||
val activity = context.findActivity() ?: return false
|
||||
val prefs = context.getSharedPreferences(REQUESTS_PREFS, Context.MODE_PRIVATE)
|
||||
return permissions.any { permission ->
|
||||
prefs.getBoolean(permission, false) &&
|
||||
ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED &&
|
||||
!ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)
|
||||
}
|
||||
}
|
||||
|
||||
private fun openAppSettings(context: Context) {
|
||||
runCatching {
|
||||
context.startActivity(
|
||||
Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:${context.packageName}"))
|
||||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private tailrec fun Context.findActivity(): Activity? = when (this) {
|
||||
is Activity -> this
|
||||
is ContextWrapper -> baseContext.findActivity()
|
||||
else -> null
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun PermissionRationaleDialog(
|
||||
title: String,
|
||||
text: String,
|
||||
confirmText: String,
|
||||
onConfirm: () -> Unit,
|
||||
onDismiss: () -> Unit
|
||||
) {
|
||||
AppDialog(
|
||||
onDismissRequest = onDismiss,
|
||||
title = title,
|
||||
actions = {
|
||||
DialogAction(stringResource(R.string.permission_not_now), onDismiss)
|
||||
DialogAction(confirmText, onConfirm)
|
||||
}
|
||||
) {
|
||||
DialogText(text)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package cc.skysparkle.matewave.ui.components
|
||||
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.border
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.aspectRatio
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import cc.skysparkle.matewave.R
|
||||
import cc.skysparkle.matewave.engine.Piece
|
||||
import cc.skysparkle.matewave.engine.PieceColor
|
||||
import cc.skysparkle.matewave.engine.PieceType
|
||||
import cc.skysparkle.matewave.ui.theme.PanelBorder
|
||||
import cc.skysparkle.matewave.ui.theme.UiCornerRadius
|
||||
import cc.skysparkle.matewave.ui.theme.ink
|
||||
|
||||
/** The four promotion pieces, drawn with the same images as the board. */
|
||||
@Composable
|
||||
fun PromotionDialog(color: PieceColor, onChoose: (PieceType) -> Unit, onDismiss: () -> Unit) {
|
||||
AppDialog(
|
||||
onDismissRequest = onDismiss,
|
||||
title = stringResource(R.string.promotion_title),
|
||||
actions = {
|
||||
DialogAction(stringResource(R.string.account_cancel), onDismiss, color = ink.copy(alpha = 0.75f))
|
||||
}
|
||||
) {
|
||||
Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
listOf(PieceType.QUEEN, PieceType.ROOK, PieceType.BISHOP, PieceType.KNIGHT).forEach { type ->
|
||||
val optionShape = RoundedCornerShape(UiCornerRadius)
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.weight(1f)
|
||||
.aspectRatio(1f)
|
||||
.clip(optionShape)
|
||||
.background(ink.copy(alpha = 0.10f))
|
||||
.border(1.dp, PanelBorder, optionShape)
|
||||
.clickable { onChoose(type) }
|
||||
.padding(8.dp),
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
Image(
|
||||
painter = painterResource(Piece(type, color).drawableRes()),
|
||||
contentDescription = null,
|
||||
modifier = Modifier.fillMaxWidth().aspectRatio(1f)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package cc.skysparkle.matewave.ui.components
|
||||
|
||||
import cc.skysparkle.matewave.ui.theme.ink
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.unit.dp
|
||||
import cc.skysparkle.matewave.ui.theme.ChessGreen
|
||||
|
||||
@Composable
|
||||
fun SetupSectionLabel(text: String, modifier: Modifier = Modifier) {
|
||||
Text(
|
||||
text.uppercase(),
|
||||
color = ink.copy(alpha = 0.5f),
|
||||
style = MaterialTheme.typography.labelMedium,
|
||||
modifier = modifier
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun SetupSegmented(options: List<String>, selectedIndex: Int, onSelect: (Int) -> Unit) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.clip(RoundedCornerShape(50))
|
||||
.background(ink.copy(alpha = 0.08f))
|
||||
.padding(4.dp),
|
||||
horizontalArrangement = Arrangement.spacedBy(4.dp)
|
||||
) {
|
||||
options.forEachIndexed { index, label ->
|
||||
val selected = index == selectedIndex
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.weight(1f)
|
||||
.clip(RoundedCornerShape(50))
|
||||
.background(if (selected) ChessGreen.copy(alpha = 0.35f) else Color.Transparent)
|
||||
.clickable { onSelect(index) }
|
||||
.padding(vertical = 10.dp),
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
Text(label, color = ink, style = MaterialTheme.typography.bodyMedium)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package cc.skysparkle.matewave.ui.components
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.border
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import cc.skysparkle.matewave.R
|
||||
import cc.skysparkle.matewave.clock.TimeControl
|
||||
import cc.skysparkle.matewave.ui.theme.ChessGreen
|
||||
import cc.skysparkle.matewave.ui.theme.ink
|
||||
|
||||
/** "10+0" from "Rapid 10+0"; the infinity sign for no limit. */
|
||||
fun TimeControl.shortLabel(): String = if (this == TimeControl.NO_LIMIT) "∞" else label.substringAfterLast(' ')
|
||||
|
||||
@Composable
|
||||
fun TimeControl.displayName(): String =
|
||||
if (this == TimeControl.NO_LIMIT) stringResource(R.string.time_no_limit) else shortLabel()
|
||||
|
||||
/** Every time control visible at once, grouped by category, instead of a clipped scrolling row. */
|
||||
@Composable
|
||||
fun TimeControlGrid(selected: TimeControl, onSelect: (TimeControl) -> Unit) {
|
||||
val groups = listOf(
|
||||
R.string.time_cat_bullet to listOf(TimeControl.BULLET_1_0, TimeControl.BULLET_2_1),
|
||||
R.string.time_cat_blitz to listOf(TimeControl.BLITZ_3_0, TimeControl.BLITZ_5_0, TimeControl.BLITZ_5_3),
|
||||
R.string.time_cat_rapid to listOf(TimeControl.RAPID_10_0, TimeControl.RAPID_15_10),
|
||||
R.string.time_cat_classical to listOf(TimeControl.CLASSICAL_30_0, TimeControl.CLASSICAL_30_20),
|
||||
R.string.time_no_limit to listOf(TimeControl.NO_LIMIT)
|
||||
)
|
||||
Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
groups.forEach { (title, options) ->
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Text(
|
||||
stringResource(title),
|
||||
color = ink.copy(alpha = 0.6f),
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
modifier = Modifier.width(110.dp)
|
||||
)
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
options.forEach { tc ->
|
||||
val active = tc == selected
|
||||
val shape = RoundedCornerShape(50)
|
||||
Text(
|
||||
tc.shortLabel(),
|
||||
color = ink,
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
modifier = Modifier
|
||||
.clip(shape)
|
||||
.background(if (active) ChessGreen.copy(alpha = 0.35f) else ink.copy(alpha = 0.08f))
|
||||
.border(1.dp, if (active) ChessGreen else ink.copy(alpha = 0.12f), shape)
|
||||
.clickable { onSelect(tc) }
|
||||
.padding(horizontal = 14.dp, vertical = 7.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,510 @@
|
||||
package cc.skysparkle.matewave.ui.screens
|
||||
|
||||
import cc.skysparkle.matewave.ui.components.ListDivider
|
||||
import cc.skysparkle.matewave.ui.components.ListGroup
|
||||
import cc.skysparkle.matewave.ui.components.ListRow
|
||||
import cc.skysparkle.matewave.ui.components.SectionCaption
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.collectAsState
|
||||
|
||||
import cc.skysparkle.matewave.ui.theme.ink
|
||||
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.border
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Brush
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.ColorFilter
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.Dp
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import cc.skysparkle.matewave.R
|
||||
import cc.skysparkle.matewave.ui.components.DialogText
|
||||
import cc.skysparkle.matewave.ui.components.DialogAction
|
||||
import cc.skysparkle.matewave.ui.components.AppDialog
|
||||
import cc.skysparkle.matewave.ui.components.AppBottomBar
|
||||
import cc.skysparkle.matewave.ui.components.BottomTab
|
||||
import cc.skysparkle.matewave.data.ProfileStore
|
||||
import cc.skysparkle.matewave.ui.components.AppIcon
|
||||
import cc.skysparkle.matewave.ui.components.GlassOutlinedButton
|
||||
import cc.skysparkle.matewave.ui.components.GlassScaffold
|
||||
import cc.skysparkle.matewave.ui.components.rememberChatImage
|
||||
import cc.skysparkle.matewave.ui.components.encodeImageForChat
|
||||
import cc.skysparkle.matewave.ui.theme.ChessGreen
|
||||
import cc.skysparkle.matewave.ui.theme.PanelBorder
|
||||
import cc.skysparkle.matewave.ui.theme.whiteOutlinedTextFieldColors
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
@Composable
|
||||
fun AccountScreen(
|
||||
studyViewModel: cc.skysparkle.matewave.viewmodel.OpeningStudyViewModel,
|
||||
onHomeClick: () -> Unit,
|
||||
onFriendsClick: () -> Unit,
|
||||
onSettingsClick: () -> Unit,
|
||||
onStudyClick: () -> Unit
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
val scope = rememberCoroutineScope()
|
||||
var profile by remember { mutableStateOf(ProfileStore.get()) }
|
||||
var showLevelPicker by remember { mutableStateOf(false) }
|
||||
var showForgetConfirm by remember { mutableStateOf(false) }
|
||||
var editingName by remember { mutableStateOf(false) }
|
||||
var copied by remember { mutableStateOf(false) }
|
||||
|
||||
val backgroundPicker = rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { uri ->
|
||||
if (uri == null) return@rememberLauncherForActivityResult
|
||||
scope.launch {
|
||||
val encoded = withContext(Dispatchers.IO) { encodeImageForChat(context, uri) }
|
||||
if (encoded != null) {
|
||||
ProfileStore.setBackground(encoded)
|
||||
profile = ProfileStore.get()
|
||||
}
|
||||
}
|
||||
}
|
||||
val avatarPicker = rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { uri ->
|
||||
if (uri == null) return@rememberLauncherForActivityResult
|
||||
scope.launch {
|
||||
val encoded = withContext(Dispatchers.IO) {
|
||||
encodeImageForChat(
|
||||
context, uri,
|
||||
cc.skysparkle.matewave.ui.components.AVATAR_MAX_DIMENSION_PX,
|
||||
cc.skysparkle.matewave.ui.components.AVATAR_JPEG_QUALITY
|
||||
)
|
||||
}
|
||||
if (encoded != null) {
|
||||
ProfileStore.setAvatar(encoded)
|
||||
profile = ProfileStore.get()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val myFingerprint = remember {
|
||||
cc.skysparkle.matewave.security.DeviceKeys.publicKeyFingerprint(context)
|
||||
}
|
||||
|
||||
LaunchedEffect(Unit) { studyViewModel.init(context) }
|
||||
val study by studyViewModel.overview.collectAsState()
|
||||
var showStats by remember { mutableStateOf(false) }
|
||||
var showSecurity by remember { mutableStateOf(false) }
|
||||
|
||||
GlassScaffold(
|
||||
title = stringResource(R.string.account_title),
|
||||
onBack = null,
|
||||
bottomBar = {
|
||||
AppBottomBar(
|
||||
current = BottomTab.ACCOUNT,
|
||||
onHomeClick = onHomeClick,
|
||||
onFriendsClick = onFriendsClick,
|
||||
onSettingsClick = onSettingsClick,
|
||||
onAccountClick = { },
|
||||
accountAvatarBase64 = profile.avatarBase64
|
||||
)
|
||||
}
|
||||
) {
|
||||
androidx.compose.runtime.CompositionLocalProvider(cc.skysparkle.matewave.ui.theme.LocalInk provides Color.White) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.height(168.dp)
|
||||
.clip(RoundedCornerShape(20.dp))
|
||||
.clickable { backgroundPicker.launch("image/*") }
|
||||
) {
|
||||
val bgBitmap = rememberChatImage(profile.backgroundBase64)
|
||||
if (bgBitmap != null) {
|
||||
Image(
|
||||
bitmap = bgBitmap,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.fillMaxWidth().height(168.dp),
|
||||
contentScale = ContentScale.Crop
|
||||
)
|
||||
} else {
|
||||
Image(
|
||||
painter = painterResource(R.drawable.bg_profile_default),
|
||||
contentDescription = null,
|
||||
modifier = Modifier.fillMaxWidth().height(168.dp),
|
||||
contentScale = ContentScale.Crop
|
||||
)
|
||||
}
|
||||
|
||||
Box(
|
||||
modifier = Modifier.fillMaxWidth().height(168.dp).background(
|
||||
Brush.verticalGradient(
|
||||
0.0f to Color.Black.copy(alpha = 0.30f),
|
||||
0.55f to Color.Black.copy(alpha = 0.45f),
|
||||
1.0f to Color.Black.copy(alpha = 0.72f)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.align(Alignment.TopEnd)
|
||||
.padding(10.dp)
|
||||
.size(36.dp)
|
||||
.clip(CircleShape)
|
||||
.background(Color.Black.copy(alpha = 0.45f))
|
||||
.clickable { backgroundPicker.launch("image/*") },
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
AppIcon(R.drawable.ic_add_photo, size = 20.dp)
|
||||
}
|
||||
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.align(Alignment.BottomStart)
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 18.dp, vertical = 16.dp)
|
||||
) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(16.dp)) {
|
||||
Box {
|
||||
AvatarCircle(
|
||||
base64 = profile.avatarBase64,
|
||||
size = 76.dp,
|
||||
modifier = Modifier.clickable { avatarPicker.launch("image/*") }
|
||||
)
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.align(Alignment.BottomEnd)
|
||||
.size(28.dp)
|
||||
.clip(CircleShape)
|
||||
.background(ChessGreen)
|
||||
.clickable { avatarPicker.launch("image/*") },
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
AppIcon(R.drawable.ic_edit, size = 14.dp)
|
||||
}
|
||||
}
|
||||
Column {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
Text(
|
||||
profile.name,
|
||||
color = ink,
|
||||
fontSize = 23.sp,
|
||||
fontWeight = FontWeight.SemiBold,
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis
|
||||
)
|
||||
|
||||
AppIcon(
|
||||
R.drawable.ic_edit,
|
||||
size = 18.dp,
|
||||
tint = ink.copy(alpha = 0.75f),
|
||||
modifier = Modifier.clickable { editingName = true }
|
||||
)
|
||||
}
|
||||
Text(
|
||||
stringResource(R.string.account_header_summary, profile.elo, cc.skysparkle.matewave.stats.Stats.gamesPlayed()),
|
||||
color = ink.copy(alpha = 0.72f),
|
||||
style = MaterialTheme.typography.bodyMedium
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (profile.avatarBase64 != null || profile.backgroundBase64 != null) {
|
||||
Text(
|
||||
stringResource(R.string.account_reset_avatar),
|
||||
color = ink.copy(alpha = 0.55f),
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
modifier = Modifier.clickable {
|
||||
ProfileStore.resetAvatarToDefault()
|
||||
ProfileStore.setBackground(null)
|
||||
profile = ProfileStore.get()
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
if (!profile.levelChosen) {
|
||||
GlassOutlinedButton(onClick = { showLevelPicker = true }, modifier = Modifier.fillMaxWidth()) {
|
||||
Text(stringResource(R.string.account_choose_level), color = ink)
|
||||
}
|
||||
}
|
||||
|
||||
// Progress: the reason to open a profile. The rating is shown here once, with its trend.
|
||||
SectionCaption(stringResource(R.string.account_progress))
|
||||
ListGroup {
|
||||
val curve = remember(profile.elo) { cc.skysparkle.matewave.stats.Stats.ratingCurve(profile.elo) }
|
||||
ListRow(
|
||||
title = stringResource(R.string.account_rating_row),
|
||||
icon = R.drawable.ic_license,
|
||||
iconTint = ChessGreen,
|
||||
value = "${profile.elo}",
|
||||
valueColor = ink,
|
||||
trailing = { MiniRatingGraph(points = curve, modifier = Modifier.width(64.dp).height(24.dp)) },
|
||||
chevron = true,
|
||||
onClick = { showStats = true }
|
||||
)
|
||||
ListDivider()
|
||||
ListRow(
|
||||
title = stringResource(R.string.study_title),
|
||||
icon = R.drawable.ic_crown,
|
||||
value = "${study.learnedCount}/${study.courses.size} · ★${study.masteredCount}",
|
||||
chevron = true,
|
||||
onClick = onStudyClick
|
||||
)
|
||||
ListDivider()
|
||||
ListRow(
|
||||
title = stringResource(R.string.puzzles_title),
|
||||
icon = R.drawable.ic_puzzle,
|
||||
value = stringResource(R.string.puzzle_solved_count, cc.skysparkle.matewave.stats.Stats.solvedPuzzles().size)
|
||||
)
|
||||
}
|
||||
|
||||
// Security folded into one row; the details live in a dialog.
|
||||
if (myFingerprint != null) {
|
||||
SectionCaption(stringResource(R.string.account_section_security))
|
||||
ListGroup {
|
||||
ListRow(
|
||||
title = stringResource(R.string.account_device_key),
|
||||
icon = R.drawable.ic_key,
|
||||
value = myFingerprint.take(4).uppercase() + "…" + myFingerprint.takeLast(4).uppercase(),
|
||||
chevron = true,
|
||||
onClick = { showSecurity = true }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (showStats) StatsDialog(onDismiss = { showStats = false })
|
||||
|
||||
if (showSecurity && myFingerprint != null) {
|
||||
AppDialog(
|
||||
onDismissRequest = { showSecurity = false; copied = false },
|
||||
title = stringResource(R.string.account_section_security),
|
||||
actions = { DialogAction(stringResource(R.string.chat_close), { showSecurity = false; copied = false }) }
|
||||
) {
|
||||
Text(stringResource(R.string.account_fingerprint), color = ink.copy(alpha = 0.6f), style = MaterialTheme.typography.labelMedium)
|
||||
Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(10.dp)) {
|
||||
Text(groupFingerprint(myFingerprint), color = ink, style = MaterialTheme.typography.bodyMedium, modifier = Modifier.weight(1f))
|
||||
AppIcon(
|
||||
R.drawable.ic_copy,
|
||||
size = 20.dp,
|
||||
modifier = Modifier.clickable {
|
||||
val clipboard = context.getSystemService(android.content.Context.CLIPBOARD_SERVICE) as android.content.ClipboardManager
|
||||
clipboard.setPrimaryClip(android.content.ClipData.newPlainText("fingerprint", myFingerprint))
|
||||
copied = true
|
||||
}
|
||||
)
|
||||
AppIcon(
|
||||
R.drawable.ic_share,
|
||||
size = 20.dp,
|
||||
modifier = Modifier.clickable {
|
||||
val send = android.content.Intent(android.content.Intent.ACTION_SEND).apply {
|
||||
type = "text/plain"
|
||||
putExtra(android.content.Intent.EXTRA_TEXT, groupFingerprint(myFingerprint))
|
||||
}
|
||||
context.startActivity(android.content.Intent.createChooser(send, null))
|
||||
}
|
||||
)
|
||||
}
|
||||
if (copied) {
|
||||
Text(stringResource(R.string.account_fingerprint_copied), color = ChessGreen, style = MaterialTheme.typography.bodySmall)
|
||||
}
|
||||
DialogText(stringResource(R.string.account_fingerprint_explain))
|
||||
DialogText(stringResource(R.string.account_trusted_keys_hint))
|
||||
Text(
|
||||
stringResource(R.string.account_forget_keys),
|
||||
color = Color(0xFFFF9B9B),
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
modifier = Modifier
|
||||
.clickable { showSecurity = false; showForgetConfirm = true }
|
||||
.padding(vertical = 6.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (editingName) {
|
||||
EditNameDialog(
|
||||
initial = profile.name,
|
||||
onSave = { newName ->
|
||||
ProfileStore.setName(newName)
|
||||
profile = ProfileStore.get()
|
||||
// Nearby players should see the new name right away.
|
||||
cc.skysparkle.matewave.network.Transports.lan(context).republishProfile()
|
||||
cc.skysparkle.matewave.network.Transports.ble(context).republishProfile()
|
||||
editingName = false
|
||||
},
|
||||
onDismiss = { editingName = false }
|
||||
)
|
||||
}
|
||||
|
||||
if (showForgetConfirm) {
|
||||
ConfirmDialog(
|
||||
title = stringResource(R.string.account_forget_keys),
|
||||
text = stringResource(R.string.account_forget_keys_confirm),
|
||||
onConfirm = {
|
||||
cc.skysparkle.matewave.security.KnownPeers(context).forgetAll()
|
||||
showForgetConfirm = false
|
||||
},
|
||||
onDismiss = { showForgetConfirm = false }
|
||||
)
|
||||
}
|
||||
|
||||
if (showLevelPicker) {
|
||||
LevelPickerDialog(
|
||||
onChoose = { elo ->
|
||||
ProfileStore.setStartingElo(elo)
|
||||
profile = ProfileStore.get()
|
||||
cc.skysparkle.matewave.network.Transports.lan(context).republishProfile()
|
||||
showLevelPicker = false
|
||||
},
|
||||
onDismiss = { showLevelPicker = false }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private const val MAX_NAME_LENGTH = 20
|
||||
|
||||
@Composable
|
||||
private fun EditNameDialog(initial: String, onSave: (String) -> Unit, onDismiss: () -> Unit) {
|
||||
var value by remember { mutableStateOf(initial) }
|
||||
AppDialog(
|
||||
onDismissRequest = onDismiss,
|
||||
title = stringResource(R.string.account_name),
|
||||
actions = {
|
||||
DialogAction(stringResource(R.string.account_cancel), onDismiss, color = ink.copy(alpha = 0.75f))
|
||||
DialogAction(
|
||||
stringResource(R.string.account_save_name),
|
||||
onClick = { if (value.isNotBlank()) onSave(value.trim()) },
|
||||
enabled = value.isNotBlank()
|
||||
)
|
||||
}
|
||||
) {
|
||||
OutlinedTextField(
|
||||
value = value,
|
||||
onValueChange = { if (it.length <= MAX_NAME_LENGTH) value = it },
|
||||
singleLine = true,
|
||||
colors = whiteOutlinedTextFieldColors(),
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
)
|
||||
Text(
|
||||
"${value.length} / $MAX_NAME_LENGTH",
|
||||
color = ink.copy(alpha = 0.45f),
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
textAlign = TextAlign.End
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun groupFingerprint(fingerprint: String): String =
|
||||
fingerprint.uppercase().chunked(4).joinToString(" ")
|
||||
|
||||
@Composable
|
||||
private fun ConfirmDialog(title: String, text: String, onConfirm: () -> Unit, onDismiss: () -> Unit) {
|
||||
AppDialog(
|
||||
onDismissRequest = onDismiss,
|
||||
title = title,
|
||||
actions = {
|
||||
DialogAction(stringResource(R.string.account_cancel), onDismiss, color = ink.copy(alpha = 0.75f))
|
||||
DialogAction(stringResource(R.string.account_confirm), onConfirm)
|
||||
}
|
||||
) {
|
||||
DialogText(text)
|
||||
}
|
||||
}
|
||||
|
||||
private data class SkillLevel(val labelRes: Int, val elo: Int)
|
||||
|
||||
private val SKILL_LEVELS = listOf(
|
||||
SkillLevel(R.string.skill_novice, 800),
|
||||
SkillLevel(R.string.skill_amateur, 1200),
|
||||
SkillLevel(R.string.skill_pro, 1800),
|
||||
SkillLevel(R.string.skill_master, 2200)
|
||||
)
|
||||
|
||||
@Composable
|
||||
private fun LevelPickerDialog(onChoose: (Int) -> Unit, onDismiss: () -> Unit) {
|
||||
AppDialog(
|
||||
onDismissRequest = onDismiss,
|
||||
title = stringResource(R.string.account_level_dialog_title),
|
||||
actions = {
|
||||
DialogAction(stringResource(R.string.account_cancel), onDismiss, color = ink.copy(alpha = 0.75f))
|
||||
}
|
||||
) {
|
||||
SKILL_LEVELS.forEach { level ->
|
||||
val rowShape = RoundedCornerShape(12.dp)
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.clip(rowShape)
|
||||
.background(ink.copy(alpha = 0.07f))
|
||||
.clickable { onChoose(level.elo) }
|
||||
.padding(horizontal = 16.dp, vertical = 14.dp),
|
||||
horizontalArrangement = Arrangement.SpaceBetween
|
||||
) {
|
||||
Text(stringResource(level.labelRes), color = ink, style = MaterialTheme.typography.titleMedium)
|
||||
Text("~${level.elo}", color = ink.copy(alpha = 0.7f), style = MaterialTheme.typography.titleMedium)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun AvatarCircle(base64: String?, size: Dp, modifier: Modifier = Modifier) {
|
||||
val bitmap = rememberChatImage(base64, cc.skysparkle.matewave.ui.components.AVATAR_MAX_DIMENSION_PX)
|
||||
Box(
|
||||
modifier = modifier
|
||||
.size(size)
|
||||
.clip(CircleShape)
|
||||
.background(ChessGreen.copy(alpha = 0.6f))
|
||||
.border(2.dp, PanelBorder, CircleShape),
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
if (bitmap != null) {
|
||||
Image(
|
||||
bitmap = bitmap,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(size).clip(CircleShape),
|
||||
contentScale = ContentScale.Crop
|
||||
)
|
||||
} else {
|
||||
Image(
|
||||
painter = painterResource(R.drawable.ic_default_avatar),
|
||||
contentDescription = null,
|
||||
colorFilter = ColorFilter.tint(ink),
|
||||
modifier = Modifier.size(size * 0.8f)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
package cc.skysparkle.matewave.ui.screens
|
||||
|
||||
import cc.skysparkle.matewave.ui.theme.ink
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.border
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Switch
|
||||
import androidx.compose.material3.SwitchDefaults
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.foundation.Image
|
||||
import cc.skysparkle.matewave.R
|
||||
import cc.skysparkle.matewave.settings.BoardAppearance
|
||||
import cc.skysparkle.matewave.settings.BoardTheme
|
||||
import cc.skysparkle.matewave.ui.components.GlassCard
|
||||
import cc.skysparkle.matewave.ui.components.GlassScaffold
|
||||
import cc.skysparkle.matewave.ui.theme.GlassBorder
|
||||
|
||||
@Composable
|
||||
fun AppearanceScreen(onBack: () -> Unit) {
|
||||
val context = LocalContext.current
|
||||
val appearance = remember { BoardAppearance(context) }
|
||||
|
||||
var theme by remember { mutableStateOf(appearance.theme) }
|
||||
var coords by remember { mutableStateOf(appearance.showCoordinates) }
|
||||
var images by remember { mutableStateOf(appearance.useImagePieces) }
|
||||
var lastMove by remember { mutableStateOf(appearance.showLastMove) }
|
||||
var checkGlow by remember { mutableStateOf(appearance.showCheckHighlight) }
|
||||
var openingBar by remember { mutableStateOf(appearance.showOpening) }
|
||||
|
||||
GlassScaffold(title = stringResource(R.string.appearance_title), onBack = onBack) {
|
||||
Text(
|
||||
stringResource(R.string.appearance_board_theme),
|
||||
color = ink,
|
||||
style = MaterialTheme.typography.titleMedium
|
||||
)
|
||||
|
||||
BoardTheme.entries.forEach { candidate ->
|
||||
GlassCard(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(vertical = 4.dp)
|
||||
.clickable {
|
||||
theme = candidate
|
||||
appearance.theme = candidate
|
||||
}
|
||||
) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(12.dp)) {
|
||||
ThemePreview(candidate, showPieces = images)
|
||||
Text(
|
||||
stringResource(candidate.titleRes),
|
||||
color = ink,
|
||||
style = MaterialTheme.typography.bodyLarge,
|
||||
modifier = Modifier.weight(1f)
|
||||
)
|
||||
if (candidate == theme) {
|
||||
cc.skysparkle.matewave.ui.components.AppIcon(
|
||||
R.drawable.ic_check_box, size = 20.dp, tint = Color(0xFF7BE495)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
AppearanceToggle(
|
||||
title = stringResource(R.string.appearance_coordinates),
|
||||
subtitle = stringResource(R.string.appearance_coordinates_hint),
|
||||
checked = coords
|
||||
) { coords = it; appearance.showCoordinates = it }
|
||||
|
||||
AppearanceToggle(
|
||||
title = stringResource(R.string.appearance_image_pieces),
|
||||
subtitle = stringResource(R.string.appearance_image_pieces_hint),
|
||||
checked = images
|
||||
) { images = it; appearance.useImagePieces = it }
|
||||
|
||||
AppearanceToggle(
|
||||
title = stringResource(R.string.appearance_last_move),
|
||||
subtitle = stringResource(R.string.appearance_last_move_hint),
|
||||
checked = lastMove
|
||||
) { lastMove = it; appearance.showLastMove = it }
|
||||
|
||||
AppearanceToggle(
|
||||
title = stringResource(R.string.appearance_check),
|
||||
subtitle = stringResource(R.string.appearance_check_hint),
|
||||
checked = checkGlow
|
||||
) { checkGlow = it; appearance.showCheckHighlight = it }
|
||||
|
||||
AppearanceToggle(
|
||||
title = stringResource(R.string.appearance_opening_in_game),
|
||||
subtitle = stringResource(R.string.appearance_opening_in_game_hint),
|
||||
checked = openingBar
|
||||
) { openingBar = it; appearance.showOpening = it }
|
||||
|
||||
Text(
|
||||
stringResource(R.string.appearance_restart_hint),
|
||||
color = ink.copy(alpha = 0.6f),
|
||||
style = MaterialTheme.typography.bodySmall
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun ThemePreview(theme: BoardTheme, showPieces: Boolean) {
|
||||
val shape = RoundedCornerShape(6.dp)
|
||||
Column(modifier = Modifier.size(56.dp).clip(shape).border(1.dp, GlassBorder, shape)) {
|
||||
repeat(2) { row ->
|
||||
Row(modifier = Modifier.weight(1f)) {
|
||||
repeat(2) { col ->
|
||||
val light = (row + col) % 2 == 0
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.weight(1f)
|
||||
.fillMaxWidth()
|
||||
.background(if (light) theme.light else theme.dark),
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
if (showPieces && row == 0 && col == 0) {
|
||||
Image(
|
||||
painter = painterResource(R.drawable.piece_white_king),
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(22.dp)
|
||||
)
|
||||
}
|
||||
if (showPieces && row == 1 && col == 1) {
|
||||
Image(
|
||||
painter = painterResource(R.drawable.piece_black_knight),
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(22.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun AppearanceToggle(
|
||||
title: String,
|
||||
subtitle: String,
|
||||
checked: Boolean,
|
||||
onCheckedChange: (Boolean) -> Unit
|
||||
) {
|
||||
GlassCard(modifier = Modifier.fillMaxWidth()) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Column(modifier = Modifier.weight(1f)) {
|
||||
Text(title, color = ink, style = MaterialTheme.typography.titleMedium)
|
||||
Text(subtitle, color = ink.copy(alpha = 0.7f), style = MaterialTheme.typography.bodySmall)
|
||||
}
|
||||
Switch(
|
||||
checked = checked,
|
||||
onCheckedChange = onCheckedChange,
|
||||
colors = SwitchDefaults.colors(
|
||||
checkedThumbColor = Color.White,
|
||||
checkedTrackColor = Color(0xFF4F8A5B)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,315 @@
|
||||
package cc.skysparkle.matewave.ui.screens
|
||||
|
||||
import androidx.compose.animation.core.animateFloat
|
||||
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.width
|
||||
import cc.skysparkle.matewave.ui.components.ListDivider
|
||||
import cc.skysparkle.matewave.ui.components.ListGroup
|
||||
import cc.skysparkle.matewave.ui.components.ListRow
|
||||
import cc.skysparkle.matewave.ui.components.SectionCaption
|
||||
import cc.skysparkle.matewave.ui.components.TimeControlGrid
|
||||
import cc.skysparkle.matewave.ui.components.shortLabel
|
||||
import cc.skysparkle.matewave.ui.components.AppDialog
|
||||
import cc.skysparkle.matewave.ui.components.DialogAction
|
||||
import cc.skysparkle.matewave.ui.components.GlassOutlinedButton
|
||||
|
||||
import cc.skysparkle.matewave.ui.theme.ink
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.DisposableEffect
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import cc.skysparkle.matewave.R
|
||||
import cc.skysparkle.matewave.ui.components.ButtonIcon
|
||||
import cc.skysparkle.matewave.clock.TimeControl
|
||||
import cc.skysparkle.matewave.data.FriendsStore
|
||||
import android.bluetooth.BluetoothAdapter
|
||||
import android.content.Intent
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import cc.skysparkle.matewave.network.AppPermissions
|
||||
import cc.skysparkle.matewave.network.NetworkRequirements
|
||||
import kotlinx.coroutines.delay
|
||||
import cc.skysparkle.matewave.network.NearbyPlayer
|
||||
import cc.skysparkle.matewave.network.Transports
|
||||
import cc.skysparkle.matewave.network.TransportType
|
||||
import cc.skysparkle.matewave.presence.LocalPresence
|
||||
import cc.skysparkle.matewave.ui.components.AppIcon
|
||||
import cc.skysparkle.matewave.ui.components.GlassButton
|
||||
import cc.skysparkle.matewave.ui.components.GlassScaffold
|
||||
import cc.skysparkle.matewave.ui.components.rememberPermissionRequester
|
||||
import cc.skysparkle.matewave.ui.theme.ChessGreen
|
||||
|
||||
private enum class Hint { BLUETOOTH_PERMISSION, BLUETOOTH_OFF, WIFI_OFF }
|
||||
|
||||
private val WarningAmber = Color(0xFFFFC46B)
|
||||
|
||||
/** A player found over either transport. */
|
||||
private data class NearbyEntry(
|
||||
val name: String,
|
||||
val elo: Int?,
|
||||
val type: TransportType,
|
||||
val address: String,
|
||||
val avatar: String?
|
||||
)
|
||||
|
||||
@Composable
|
||||
fun ConnectionScreen(
|
||||
presence: LocalPresence,
|
||||
onStartHost: (TimeControl) -> Unit,
|
||||
onJoin: (TransportType, TimeControl, targetId: String) -> Unit,
|
||||
onBack: () -> Unit
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
val last = remember { cc.skysparkle.matewave.settings.LastGameSetup(context) }
|
||||
var timeControl by remember { mutableStateOf(last.nearbyTime) }
|
||||
var pickTime by remember { mutableStateOf(false) }
|
||||
var btPlayers by remember { mutableStateOf<List<NearbyPlayer>>(emptyList()) }
|
||||
var btAllowed by remember { mutableStateOf(AppPermissions.allGranted(context, AppPermissions.nearby)) }
|
||||
val profile = remember { cc.skysparkle.matewave.data.ProfileStore.get() }
|
||||
val visible = remember { cc.skysparkle.matewave.presence.PresenceSettings(context).announceEnabled }
|
||||
var network by remember { mutableStateOf(NetworkRequirements.check(context)) }
|
||||
|
||||
fun startBluetooth() {
|
||||
val ble = Transports.ble(context)
|
||||
runCatching {
|
||||
ble.startPresence()
|
||||
ble.discoverNearby { found -> btPlayers = found }
|
||||
}
|
||||
}
|
||||
|
||||
// Wi-Fi and Bluetooth can be switched on while the screen is open: notice it and search again.
|
||||
LaunchedEffect(Unit) {
|
||||
while (true) {
|
||||
delay(3_000)
|
||||
val now = NetworkRequirements.check(context)
|
||||
if (btAllowed && now.bluetoothOn && !network.bluetoothOn) startBluetooth()
|
||||
if (!now.bluetoothOn) btPlayers = emptyList()
|
||||
if (now.wifiConnected && !network.wifiConnected) presence.refresh()
|
||||
network = now
|
||||
}
|
||||
}
|
||||
val enableBluetooth = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { }
|
||||
|
||||
val requestNearby = rememberPermissionRequester(
|
||||
permissions = AppPermissions.nearby,
|
||||
rationaleTitle = stringResource(R.string.mesh_permission_title),
|
||||
rationaleText = stringResource(R.string.mesh_permission_text),
|
||||
onResult = { granted -> btAllowed = granted }
|
||||
)
|
||||
|
||||
// One search over both transports: Wi-Fi presence runs app-wide, Bluetooth while this screen is open.
|
||||
DisposableEffect(btAllowed) {
|
||||
if (btAllowed) startBluetooth()
|
||||
onDispose {
|
||||
if (btAllowed) runCatching {
|
||||
val ble = Transports.ble(context)
|
||||
ble.stopDiscovery()
|
||||
ble.stopPresenceIfIdle()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val peers by presence.peers.collectAsState()
|
||||
val friends = remember(peers) { FriendsStore.list() }
|
||||
val entries = remember(peers, btPlayers, friends) {
|
||||
val lan = peers.values.map { peer ->
|
||||
NearbyEntry(
|
||||
name = peer.name,
|
||||
elo = peer.elo,
|
||||
type = TransportType.LAN,
|
||||
address = peer.address,
|
||||
avatar = friends.firstOrNull { it.userId == peer.playerId }?.avatarBase64
|
||||
)
|
||||
}
|
||||
val lanFingerprints = peers.values.map { it.fingerprint }.filter { it.isNotEmpty() }
|
||||
// The same phone seen over both channels is listed once, over Wi-Fi. Bluetooth carries
|
||||
// a shorter fingerprint: the first bytes of the same key hash.
|
||||
val bt = btPlayers.filter { player ->
|
||||
val short = player.fingerprint
|
||||
short.isNullOrEmpty() || lanFingerprints.none { it.startsWith(short) }
|
||||
}.map {
|
||||
NearbyEntry(name = it.name, elo = null, type = TransportType.BLUETOOTH, address = it.address, avatar = null)
|
||||
}
|
||||
(lan + bt).sortedBy { it.name.lowercase() }
|
||||
}
|
||||
|
||||
GlassScaffold(
|
||||
title = stringResource(R.string.vsplayer_nearby_title),
|
||||
onBack = onBack,
|
||||
bottomBar = {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth().padding(horizontal = 20.dp, vertical = 12.dp),
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
GlassButton(
|
||||
onClick = { last.nearbyTime = timeControl; onStartHost(timeControl) },
|
||||
modifier = Modifier.weight(1f)
|
||||
) {
|
||||
ButtonIcon(R.drawable.ic_add, size = 20.dp)
|
||||
Text(stringResource(R.string.nearby_create), color = ink, style = MaterialTheme.typography.titleMedium, maxLines = 1)
|
||||
}
|
||||
GlassOutlinedButton(onClick = { pickTime = true }) {
|
||||
Text(timeControl.shortLabel() + " ▾", color = ink, maxLines = 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
) {
|
||||
SearchPulse()
|
||||
Text(
|
||||
if (visible) stringResource(R.string.nearby_visible_as, profile.name) else stringResource(R.string.nearby_hidden),
|
||||
color = ink.copy(alpha = 0.65f),
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
textAlign = androidx.compose.ui.text.style.TextAlign.Center,
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
)
|
||||
|
||||
// What keeps others from being found, each with the fix one tap away.
|
||||
val hints = listOfNotNull(
|
||||
Hint.BLUETOOTH_PERMISSION.takeIf { !btAllowed && network.bluetoothSupported },
|
||||
Hint.BLUETOOTH_OFF.takeIf { btAllowed && network.bluetoothSupported && !network.bluetoothOn },
|
||||
Hint.WIFI_OFF.takeIf { !network.wifiConnected }
|
||||
)
|
||||
if (hints.isNotEmpty()) {
|
||||
ListGroup {
|
||||
hints.forEachIndexed { index, hint ->
|
||||
if (index > 0) ListDivider()
|
||||
when (hint) {
|
||||
Hint.BLUETOOTH_PERMISSION -> ListRow(
|
||||
title = stringResource(R.string.nearby_enable_bt),
|
||||
icon = R.drawable.ic_bluetooth,
|
||||
chevron = true,
|
||||
onClick = { requestNearby() }
|
||||
)
|
||||
Hint.BLUETOOTH_OFF -> ListRow(
|
||||
title = stringResource(R.string.reason_bluetooth_off),
|
||||
subtitle = stringResource(R.string.nearby_bt_off_hint),
|
||||
icon = R.drawable.ic_bluetooth,
|
||||
iconTint = WarningAmber,
|
||||
chevron = true,
|
||||
onClick = {
|
||||
runCatching { enableBluetooth.launch(Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)) }
|
||||
}
|
||||
)
|
||||
Hint.WIFI_OFF -> ListRow(
|
||||
title = stringResource(R.string.reason_wifi_unavailable),
|
||||
subtitle = stringResource(R.string.nearby_wifi_hint),
|
||||
icon = R.drawable.ic_wifi,
|
||||
iconTint = WarningAmber
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (entries.isEmpty()) {
|
||||
Text(
|
||||
stringResource(R.string.nearby_empty),
|
||||
color = ink.copy(alpha = 0.6f),
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
textAlign = androidx.compose.ui.text.style.TextAlign.Center,
|
||||
modifier = Modifier.fillMaxWidth().padding(top = 8.dp)
|
||||
)
|
||||
} else {
|
||||
SectionCaption(stringResource(R.string.nearby_players))
|
||||
ListGroup {
|
||||
entries.forEachIndexed { index, entry ->
|
||||
if (index > 0) ListDivider(inset = 68)
|
||||
val transport = stringResource(if (entry.type == TransportType.LAN) R.string.transport_wifi else R.string.transport_bluetooth)
|
||||
ListRow(
|
||||
title = entry.name.ifBlank { stringResource(R.string.players_unknown_name) },
|
||||
subtitle = listOfNotNull(entry.elo?.toString(), transport).joinToString(" · "),
|
||||
leading = { AvatarCircle(base64 = entry.avatar, size = 40.dp) },
|
||||
trailing = {
|
||||
AppIcon(
|
||||
if (entry.type == TransportType.LAN) R.drawable.ic_wifi_local else R.drawable.ic_bluetooth,
|
||||
size = 16.dp,
|
||||
tint = ink.copy(alpha = 0.5f)
|
||||
)
|
||||
Spacer(Modifier.width(8.dp))
|
||||
Text(
|
||||
stringResource(R.string.players_play),
|
||||
color = Color.White,
|
||||
style = MaterialTheme.typography.labelLarge,
|
||||
modifier = Modifier
|
||||
.clip(RoundedCornerShape(50))
|
||||
.background(ChessGreen)
|
||||
.clickable { onJoin(entry.type, timeControl, entry.address) }
|
||||
.padding(horizontal = 14.dp, vertical = 6.dp)
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (pickTime) {
|
||||
AppDialog(
|
||||
onDismissRequest = { pickTime = false },
|
||||
title = stringResource(R.string.time_control_label),
|
||||
actions = { DialogAction(stringResource(R.string.chat_close), { pickTime = false }) }
|
||||
) {
|
||||
TimeControlGrid(selected = timeControl, onSelect = { timeControl = it; last.nearbyTime = it; pickTime = false })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Concentric rings pulsing around the knight: the search is always running. */
|
||||
@Composable
|
||||
private fun SearchPulse() {
|
||||
val transition = androidx.compose.animation.core.rememberInfiniteTransition(label = "pulse")
|
||||
val phase by transition.animateFloat(
|
||||
initialValue = 0f,
|
||||
targetValue = 1f,
|
||||
animationSpec = androidx.compose.animation.core.infiniteRepeatable(
|
||||
androidx.compose.animation.core.tween(1800, easing = androidx.compose.animation.core.LinearEasing)
|
||||
),
|
||||
label = "phase"
|
||||
)
|
||||
Column(modifier = Modifier.fillMaxWidth().padding(top = 8.dp), horizontalAlignment = Alignment.CenterHorizontally) {
|
||||
Box(modifier = Modifier.size(120.dp), contentAlignment = Alignment.Center) {
|
||||
androidx.compose.foundation.Canvas(modifier = Modifier.size(120.dp)) {
|
||||
for (k in 0 until 3) {
|
||||
val p = (phase + k / 3f) % 1f
|
||||
drawCircle(
|
||||
color = ChessGreen.copy(alpha = (1f - p) * 0.45f),
|
||||
radius = size.minDimension / 2f * (0.3f + 0.7f * p),
|
||||
style = androidx.compose.ui.graphics.drawscope.Stroke(width = 2.dp.toPx())
|
||||
)
|
||||
}
|
||||
}
|
||||
androidx.compose.foundation.Image(
|
||||
painter = androidx.compose.ui.res.painterResource(R.drawable.piece_white_knight),
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(40.dp)
|
||||
)
|
||||
}
|
||||
Text(stringResource(R.string.nearby_searching), color = ink, style = MaterialTheme.typography.titleMedium)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,330 @@
|
||||
package cc.skysparkle.matewave.ui.screens
|
||||
|
||||
import cc.skysparkle.matewave.ui.components.ListDivider
|
||||
import cc.skysparkle.matewave.ui.components.ListGroup
|
||||
import cc.skysparkle.matewave.ui.components.ListRow
|
||||
import cc.skysparkle.matewave.ui.components.SectionCaption
|
||||
import cc.skysparkle.matewave.ui.components.SetupSegmented
|
||||
import cc.skysparkle.matewave.ui.components.GlassButton
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
|
||||
import cc.skysparkle.matewave.ui.theme.ink
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.ui.window.Dialog
|
||||
import cc.skysparkle.matewave.R
|
||||
import cc.skysparkle.matewave.data.FriendEntry
|
||||
import cc.skysparkle.matewave.data.FriendsStore
|
||||
import cc.skysparkle.matewave.presence.LocalPresence
|
||||
import cc.skysparkle.matewave.ui.components.AppBottomBar
|
||||
import cc.skysparkle.matewave.ui.components.AppIcon
|
||||
import cc.skysparkle.matewave.ui.components.BottomTab
|
||||
import cc.skysparkle.matewave.ui.components.GlassScaffold
|
||||
import cc.skysparkle.matewave.ui.theme.ChessGreen
|
||||
|
||||
@Composable
|
||||
fun FriendsScreen(
|
||||
presence: LocalPresence,
|
||||
onHomeClick: () -> Unit,
|
||||
onSettingsClick: () -> Unit,
|
||||
onAccountClick: () -> Unit,
|
||||
onPlay: () -> Unit = {}
|
||||
) {
|
||||
var onlineOnly by remember { mutableStateOf(false) }
|
||||
var version by remember { mutableStateOf(0) }
|
||||
var opened by remember { mutableStateOf<FriendEntry?>(null) }
|
||||
|
||||
val myProfile = remember { cc.skysparkle.matewave.data.ProfileStore.get() }
|
||||
|
||||
val peers by presence.peers.collectAsState()
|
||||
val friends = remember(peers, version) { FriendsStore.list() }
|
||||
|
||||
// Fresh search on opening, then refresh "5 min ago" labels while the screen is visible.
|
||||
androidx.compose.runtime.LaunchedEffect(Unit) {
|
||||
presence.refresh()
|
||||
while (true) {
|
||||
kotlinx.coroutines.delay(30_000)
|
||||
version++
|
||||
}
|
||||
}
|
||||
|
||||
GlassScaffold(
|
||||
title = stringResource(R.string.friends_title),
|
||||
onBack = null,
|
||||
bottomBar = {
|
||||
AppBottomBar(
|
||||
current = BottomTab.FRIENDS,
|
||||
onHomeClick = onHomeClick,
|
||||
onFriendsClick = { },
|
||||
onSettingsClick = onSettingsClick,
|
||||
onAccountClick = onAccountClick,
|
||||
accountAvatarBase64 = myProfile.avatarBase64
|
||||
)
|
||||
}
|
||||
) {
|
||||
val savedIds = friends.map { it.userId }.toSet()
|
||||
val strangers = peers.values.filter { it.playerId !in savedIds }
|
||||
|
||||
if (friends.isEmpty() && strangers.isEmpty()) {
|
||||
// Empty state with the action that fills the list.
|
||||
Spacer(Modifier.height(24.dp))
|
||||
Text(stringResource(R.string.friends_nobody_title), color = ink, style = MaterialTheme.typography.titleMedium)
|
||||
Text(stringResource(R.string.friends_nobody_hint), color = ink.copy(alpha = 0.65f), style = MaterialTheme.typography.bodyMedium)
|
||||
GlassButton(onClick = onPlay, modifier = Modifier.fillMaxWidth()) {
|
||||
Text(stringResource(R.string.friends_play_nearby), color = ink)
|
||||
}
|
||||
return@GlassScaffold
|
||||
}
|
||||
|
||||
SetupSegmented(
|
||||
options = listOf(stringResource(R.string.friends_filter_all), stringResource(R.string.friends_filter_online)),
|
||||
selectedIndex = if (onlineOnly) 1 else 0
|
||||
) { onlineOnly = it == 1 }
|
||||
|
||||
val visible = friends
|
||||
.filter { !onlineOnly || presence.isOnline(it.userId) }
|
||||
.sortedWith(compareByDescending<FriendEntry> { presence.isOnline(it.userId) }.thenByDescending { it.lastPlayedMs })
|
||||
val favorites = visible.filter { it.favorite }
|
||||
val others = visible.filter { !it.favorite }
|
||||
|
||||
FriendGroup(
|
||||
title = stringResource(R.string.friends_group_favorites),
|
||||
list = favorites,
|
||||
isOnline = presence::isOnline,
|
||||
onOpen = { opened = it },
|
||||
onToggleFavorite = { FriendsStore.toggleFavorite(it.userId); version++ },
|
||||
onPlay = onPlay
|
||||
)
|
||||
FriendGroup(
|
||||
title = if (favorites.isNotEmpty()) stringResource(R.string.friends_group_others) else null,
|
||||
list = others,
|
||||
isOnline = presence::isOnline,
|
||||
onOpen = { opened = it },
|
||||
onToggleFavorite = { FriendsStore.toggleFavorite(it.userId); version++ },
|
||||
onPlay = onPlay
|
||||
)
|
||||
|
||||
if (strangers.isNotEmpty()) {
|
||||
SectionCaption(stringResource(R.string.friends_group_nearby))
|
||||
ListGroup {
|
||||
strangers.sortedBy { it.name }.forEachIndexed { index, peer ->
|
||||
if (index > 0) ListDivider(inset = 68)
|
||||
ListRow(
|
||||
title = displayName(peer.name, peer.playerId),
|
||||
subtitle = stringResource(R.string.friends_rating, peer.elo),
|
||||
leading = { AvatarCircle(base64 = null, size = 40.dp) },
|
||||
trailing = { PlayChip(onPlay) }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (visible.isEmpty() && strangers.isEmpty()) {
|
||||
Text(stringResource(R.string.friends_filter_empty), color = ink.copy(alpha = 0.6f), style = MaterialTheme.typography.bodyMedium)
|
||||
}
|
||||
|
||||
opened?.let { friend ->
|
||||
FriendCard(friend = friend, online = presence.isOnline(friend.userId), onDismiss = { opened = null })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun FriendGroup(
|
||||
title: String?,
|
||||
list: List<FriendEntry>,
|
||||
isOnline: (String) -> Boolean,
|
||||
onOpen: (FriendEntry) -> Unit,
|
||||
onToggleFavorite: (FriendEntry) -> Unit,
|
||||
onPlay: () -> Unit
|
||||
) {
|
||||
if (list.isEmpty()) return
|
||||
if (title != null) SectionCaption(title)
|
||||
ListGroup {
|
||||
list.forEachIndexed { index, friend ->
|
||||
if (index > 0) ListDivider(inset = 68)
|
||||
FriendRow(
|
||||
friend = friend,
|
||||
online = isOnline(friend.userId),
|
||||
onOpen = { onOpen(friend) },
|
||||
onToggleFavorite = { onToggleFavorite(friend) },
|
||||
onPlay = onPlay
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Players who never set a name are all called "Player"; a short piece of their id tells them
|
||||
* apart in the list.
|
||||
*/
|
||||
@Composable
|
||||
private fun displayName(name: String, id: String): String {
|
||||
val default = stringResource(R.string.player_default_name)
|
||||
val generic = name.isBlank() || name == default || name == "Player"
|
||||
return if (generic) "${name.ifBlank { default }} · ${id.filter { it.isLetterOrDigit() }.take(4).uppercase()}" else name
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun PlayChip(onClick: () -> Unit) {
|
||||
Text(
|
||||
stringResource(R.string.friends_play),
|
||||
color = Color.White,
|
||||
style = MaterialTheme.typography.labelLarge,
|
||||
modifier = Modifier
|
||||
.clip(RoundedCornerShape(50))
|
||||
.background(ChessGreen)
|
||||
.clickable(onClick = onClick)
|
||||
.padding(horizontal = 14.dp, vertical = 6.dp)
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun FriendCard(friend: FriendEntry, online: Boolean, onDismiss: () -> Unit) {
|
||||
Dialog(onDismissRequest = onDismiss) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.clip(cc.skysparkle.matewave.ui.components.DialogShape)
|
||||
.background(cc.skysparkle.matewave.ui.components.DialogSurface)
|
||||
) {
|
||||
androidx.compose.runtime.CompositionLocalProvider(cc.skysparkle.matewave.ui.theme.LocalInk provides Color.White) {
|
||||
Box(modifier = Modifier.fillMaxWidth().height(150.dp)) {
|
||||
val bg = cc.skysparkle.matewave.ui.components.rememberChatImage(friend.backgroundBase64)
|
||||
if (bg != null) {
|
||||
androidx.compose.foundation.Image(
|
||||
bitmap = bg,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.fillMaxWidth().height(150.dp),
|
||||
contentScale = androidx.compose.ui.layout.ContentScale.Crop
|
||||
)
|
||||
} else {
|
||||
androidx.compose.foundation.Image(
|
||||
painter = androidx.compose.ui.res.painterResource(R.drawable.bg_profile_default),
|
||||
contentDescription = null,
|
||||
modifier = Modifier.fillMaxWidth().height(150.dp),
|
||||
contentScale = androidx.compose.ui.layout.ContentScale.Crop
|
||||
)
|
||||
}
|
||||
Box(
|
||||
modifier = Modifier.fillMaxWidth().height(150.dp).background(
|
||||
androidx.compose.ui.graphics.Brush.verticalGradient(
|
||||
0f to Color.Black.copy(alpha = 0.15f),
|
||||
1f to Color.Black.copy(alpha = 0.75f)
|
||||
)
|
||||
)
|
||||
)
|
||||
Row(
|
||||
modifier = Modifier.align(Alignment.BottomStart).padding(16.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(12.dp)
|
||||
) {
|
||||
AvatarCircle(base64 = friend.avatarBase64, size = 64.dp)
|
||||
Column {
|
||||
Text(friend.name, color = ink, style = MaterialTheme.typography.titleLarge)
|
||||
Text(
|
||||
stringResource(R.string.friends_rating, friend.elo),
|
||||
color = ink.copy(alpha = 0.75f),
|
||||
style = MaterialTheme.typography.bodyMedium
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Column(modifier = Modifier.padding(16.dp), verticalArrangement = Arrangement.spacedBy(10.dp)) {
|
||||
Text(
|
||||
if (online) stringResource(R.string.friends_online)
|
||||
else lastSeenText(maxOf(friend.lastPlayedMs, friend.lastSeenMs)),
|
||||
color = if (online) Color(0xFF7BE495) else ink.copy(alpha = 0.6f),
|
||||
style = MaterialTheme.typography.bodyMedium
|
||||
)
|
||||
Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.End) {
|
||||
cc.skysparkle.matewave.ui.components.DialogAction(
|
||||
stringResource(R.string.chat_close),
|
||||
onClick = onDismiss
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun FriendRow(
|
||||
friend: FriendEntry,
|
||||
online: Boolean,
|
||||
onOpen: () -> Unit,
|
||||
onToggleFavorite: () -> Unit,
|
||||
onPlay: () -> Unit
|
||||
) {
|
||||
ListRow(
|
||||
title = displayName(friend.name, friend.userId),
|
||||
subtitle = stringResource(R.string.friends_rating, friend.elo) + " · " +
|
||||
(if (online) stringResource(R.string.friends_online) else lastSeenText(maxOf(friend.lastPlayedMs, friend.lastSeenMs))),
|
||||
subtitleColor = if (online) ChessGreen else ink.copy(alpha = 0.55f),
|
||||
leading = {
|
||||
Box {
|
||||
AvatarCircle(base64 = friend.avatarBase64, size = 40.dp)
|
||||
if (online) {
|
||||
Box(
|
||||
Modifier
|
||||
.align(Alignment.BottomEnd)
|
||||
.size(11.dp)
|
||||
.clip(CircleShape)
|
||||
.background(ChessGreen)
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
trailing = {
|
||||
if (online) PlayChip(onPlay)
|
||||
AppIcon(
|
||||
R.drawable.ic_star,
|
||||
size = 20.dp,
|
||||
tint = if (friend.favorite) StarGold else ink.copy(alpha = 0.25f),
|
||||
modifier = Modifier
|
||||
.padding(start = 6.dp)
|
||||
.clip(CircleShape)
|
||||
.clickable(onClick = onToggleFavorite)
|
||||
.padding(6.dp)
|
||||
)
|
||||
},
|
||||
onClick = onOpen
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun lastSeenText(lastSeenMs: Long): String {
|
||||
if (lastSeenMs <= 0) return stringResource(R.string.friends_offline)
|
||||
val minutes = (System.currentTimeMillis() - lastSeenMs) / 60_000
|
||||
return when {
|
||||
minutes < 60 -> stringResource(R.string.friends_minutes_ago, minutes.coerceAtLeast(1).toInt())
|
||||
minutes < 60 * 24 -> stringResource(R.string.friends_hours_ago, (minutes / 60).toInt())
|
||||
minutes < 60 * 48 -> stringResource(R.string.friends_yesterday)
|
||||
else -> stringResource(R.string.friends_days_ago, (minutes / (60 * 24)).toInt())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,570 @@
|
||||
package cc.skysparkle.matewave.ui.screens
|
||||
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
|
||||
import androidx.compose.foundation.layout.padding
|
||||
|
||||
import cc.skysparkle.matewave.ui.theme.ink
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import cc.skysparkle.matewave.R
|
||||
import cc.skysparkle.matewave.ui.components.DialogText
|
||||
import cc.skysparkle.matewave.ui.components.DialogAction
|
||||
import cc.skysparkle.matewave.ui.components.AppDialog
|
||||
import cc.skysparkle.matewave.ui.components.AppIcon
|
||||
import cc.skysparkle.matewave.ui.components.ButtonIcon
|
||||
import cc.skysparkle.matewave.engine.ChessEngine
|
||||
import cc.skysparkle.matewave.engine.GameStatus
|
||||
import cc.skysparkle.matewave.engine.PieceColor
|
||||
import cc.skysparkle.matewave.network.ConnectionState
|
||||
import cc.skysparkle.matewave.ui.components.ChatDialog
|
||||
import cc.skysparkle.matewave.ui.components.ChessBoardView
|
||||
import cc.skysparkle.matewave.ui.components.GlassOutlinedButton
|
||||
import cc.skysparkle.matewave.ui.components.GlassScaffold
|
||||
import cc.skysparkle.matewave.ui.components.PromotionDialog
|
||||
import cc.skysparkle.matewave.ui.theme.ChessGreen
|
||||
import cc.skysparkle.matewave.viewmodel.GameMode
|
||||
import cc.skysparkle.matewave.viewmodel.GameUiState
|
||||
import cc.skysparkle.matewave.viewmodel.GameViewModel
|
||||
|
||||
@Composable
|
||||
fun GameScreen(viewModel: GameViewModel, onExit: () -> Unit) {
|
||||
val exitGame = {
|
||||
viewModel.leaveNetworkGame()
|
||||
onExit()
|
||||
}
|
||||
val state by viewModel.uiState.collectAsState()
|
||||
var showChat by remember { mutableStateOf(false) }
|
||||
val context = androidx.compose.ui.platform.LocalContext.current
|
||||
val showOpeningBar = remember { cc.skysparkle.matewave.settings.BoardAppearance(context).showOpening }
|
||||
|
||||
// Leaving an ongoing network game counts as a loss, so ask first; a game against the AI is saved.
|
||||
var confirmLeave by remember { mutableStateOf(false) }
|
||||
var confirmResign by remember { mutableStateOf(false) }
|
||||
val requestExit: () -> Unit = {
|
||||
val ongoing = state.status == GameStatus.ONGOING && state.gameStarted
|
||||
if (ongoing && state.mode == GameMode.VS_PLAYER_NETWORK) {
|
||||
confirmLeave = true
|
||||
} else {
|
||||
exitGame()
|
||||
}
|
||||
}
|
||||
androidx.activity.compose.BackHandler(onBack = requestExit)
|
||||
|
||||
var gameOverHidden by remember(state.status, state.opponentLeft) { mutableStateOf(false) }
|
||||
|
||||
// Opening shown in the top bar: "A40 · Englund Gambit" with a THEORY / out-of-book badge.
|
||||
val start = state.startBoard
|
||||
val sans = state.moveSans
|
||||
val showOpening = showOpeningBar && state.mode != GameMode.PUZZLE
|
||||
val fromStart = remember(start) { cc.skysparkle.matewave.engine.Notation.isStandardStart(start) }
|
||||
val opening = remember(sans, fromStart) {
|
||||
if (fromStart) cc.skysparkle.matewave.openings.OpeningBook.openingName(sans) else null
|
||||
}
|
||||
val bookPlies = remember(sans, fromStart) {
|
||||
if (fromStart) cc.skysparkle.matewave.openings.OpeningBook.bookDepth(sans) else 0
|
||||
}
|
||||
val inBook = sans.isNotEmpty() && bookPlies >= sans.size
|
||||
val bookStatus = when {
|
||||
!fromStart || sans.isEmpty() -> null
|
||||
inBook -> stringResource(R.string.opening_in_theory)
|
||||
bookPlies < cc.skysparkle.matewave.openings.OpeningBook.MAX_TREE_PLY ->
|
||||
stringResource(R.string.opening_out_of_book, bookPlies / 2 + 1)
|
||||
else -> null
|
||||
}
|
||||
|
||||
GlassScaffold(
|
||||
title = "",
|
||||
onBack = requestExit,
|
||||
titleContent = if (showOpening && opening != null) {
|
||||
{ OpeningTitle("${opening.eco} · ${opening.name}", bookStatus, inBook) }
|
||||
} else null
|
||||
) {
|
||||
if (state.mode == GameMode.VS_PLAYER_NETWORK) ConnectionBanner(state)
|
||||
TrustBanners(state)
|
||||
|
||||
val material = remember(state.board) { cc.skysparkle.matewave.engine.Material.balance(state.board) }
|
||||
val bottomColor = state.myColor
|
||||
val topColor = if (bottomColor == PieceColor.WHITE) PieceColor.BLACK else PieceColor.WHITE
|
||||
val kingInCheckSquare = if (ChessEngine.isInCheck(state.board, state.board.sideToMove)) {
|
||||
state.board.kingSquare(state.board.sideToMove)
|
||||
} else null
|
||||
|
||||
PlayerRow(state, topColor, isMe = false, material = material)
|
||||
|
||||
ChessBoardView(
|
||||
board = state.board,
|
||||
selectedSquare = state.selectedSquare,
|
||||
legalTargets = state.legalTargetsFromSelected,
|
||||
kingInCheckSquare = kingInCheckSquare,
|
||||
flipped = state.myColor == PieceColor.BLACK,
|
||||
onSquareClick = viewModel::onSquareClicked,
|
||||
lastMove = state.lastMove,
|
||||
interactionEnabled = state.status == GameStatus.ONGOING && state.pendingPromotion == null &&
|
||||
(state.mode != GameMode.VS_PLAYER_NETWORK ||
|
||||
(state.connectionState == ConnectionState.CONNECTED &&
|
||||
state.handshake == cc.skysparkle.matewave.viewmodel.HandshakeState.PLAYING))
|
||||
)
|
||||
|
||||
PlayerRow(state, bottomColor, isMe = true, material = material)
|
||||
|
||||
// The move ribbon: only moves here, the opening name lives in the top bar.
|
||||
if (showOpening) {
|
||||
cc.skysparkle.matewave.ui.components.MovesBar(
|
||||
sans = sans,
|
||||
startBoard = start,
|
||||
currentIndex = sans.size,
|
||||
opening = null,
|
||||
bookPlies = bookPlies
|
||||
)
|
||||
}
|
||||
|
||||
androidx.compose.animation.AnimatedVisibility(
|
||||
visible = state.opponentNotice == cc.skysparkle.matewave.viewmodel.OpponentNotice.DRAW_DECLINED,
|
||||
enter = androidx.compose.animation.fadeIn() + androidx.compose.animation.expandVertically(),
|
||||
exit = androidx.compose.animation.fadeOut() + androidx.compose.animation.shrinkVertically()
|
||||
) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
AppIcon(R.drawable.ic_draw, size = 18.dp, tint = Color(0xFFFFB86B))
|
||||
Text(stringResource(R.string.draw_declined_notice), color = ink, style = MaterialTheme.typography.bodyMedium)
|
||||
}
|
||||
}
|
||||
androidx.compose.runtime.LaunchedEffect(state.opponentNotice) {
|
||||
if (state.opponentNotice == cc.skysparkle.matewave.viewmodel.OpponentNotice.DRAW_DECLINED) {
|
||||
kotlinx.coroutines.delay(NOTICE_VISIBLE_MS)
|
||||
viewModel.dismissOpponentNotice()
|
||||
}
|
||||
}
|
||||
|
||||
if (state.mode == GameMode.VS_PLAYER_NETWORK && state.status == GameStatus.ONGOING) {
|
||||
// Both actions neutral: resigning must not look like the main action.
|
||||
Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
GlassOutlinedButton(
|
||||
onClick = { viewModel.offerDraw() },
|
||||
modifier = Modifier.weight(1f),
|
||||
enabled = !state.drawOfferSentByMe
|
||||
) {
|
||||
Text(
|
||||
if (state.drawOfferSentByMe) stringResource(R.string.draw_offer_waiting) else stringResource(R.string.offer_draw),
|
||||
color = ink,
|
||||
maxLines = 1
|
||||
)
|
||||
}
|
||||
GlassOutlinedButton(onClick = { confirmResign = true }, modifier = Modifier.weight(1f)) {
|
||||
Text(stringResource(R.string.resign), color = ink, maxLines = 1)
|
||||
}
|
||||
}
|
||||
ChatButton(
|
||||
unreadCount = state.unreadChatCount,
|
||||
onClick = { viewModel.markChatRead(); showChat = true }
|
||||
)
|
||||
}
|
||||
|
||||
// The result dialog was closed to look at the board: one tap brings it back.
|
||||
if (state.status != GameStatus.ONGOING && gameOverHidden) {
|
||||
GlassOutlinedButton(onClick = { gameOverHidden = false }, modifier = Modifier.fillMaxWidth()) {
|
||||
Text(stringResource(R.string.game_over_title), color = ink, maxLines = 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (confirmLeave) {
|
||||
AppDialog(
|
||||
onDismissRequest = { confirmLeave = false },
|
||||
title = stringResource(R.string.game_leave_title),
|
||||
actions = {
|
||||
DialogAction(stringResource(R.string.account_cancel), { confirmLeave = false }, color = ink.copy(alpha = 0.75f))
|
||||
DialogAction(stringResource(R.string.game_leave_confirm), { confirmLeave = false; exitGame() })
|
||||
}
|
||||
) {
|
||||
DialogText(stringResource(R.string.game_leave_network))
|
||||
}
|
||||
}
|
||||
|
||||
if (confirmResign) {
|
||||
AppDialog(
|
||||
onDismissRequest = { confirmResign = false },
|
||||
title = stringResource(R.string.game_resign_title),
|
||||
actions = {
|
||||
DialogAction(stringResource(R.string.account_cancel), { confirmResign = false }, color = ink.copy(alpha = 0.75f))
|
||||
DialogAction(stringResource(R.string.resign), { confirmResign = false; viewModel.resign() })
|
||||
}
|
||||
) {
|
||||
DialogText(stringResource(R.string.game_resign_text))
|
||||
}
|
||||
}
|
||||
|
||||
state.pendingPromotion?.let {
|
||||
PromotionDialog(
|
||||
color = state.board.sideToMove,
|
||||
onChoose = viewModel::choosePromotion,
|
||||
onDismiss = viewModel::cancelPromotion
|
||||
)
|
||||
}
|
||||
|
||||
if (state.drawOfferFromOpponent) {
|
||||
IncomingDrawOfferDialog(
|
||||
onAccept = { viewModel.respondToDrawOffer(true) },
|
||||
onDecline = { viewModel.respondToDrawOffer(false) }
|
||||
)
|
||||
}
|
||||
|
||||
if (showChat) {
|
||||
ChatDialog(
|
||||
messages = state.chatMessages,
|
||||
onSendText = viewModel::sendChatMessage,
|
||||
onSendImage = viewModel::sendChatImage,
|
||||
onDismiss = { showChat = false }
|
||||
)
|
||||
}
|
||||
|
||||
if (state.status != GameStatus.ONGOING && !gameOverHidden) {
|
||||
GameOverDialog(
|
||||
state.status, state,
|
||||
onRematch = {
|
||||
viewModel.rematchVsAi()
|
||||
},
|
||||
onExit = exitGame,
|
||||
onDismiss = { gameOverHidden = true },
|
||||
onOfferRematch = { viewModel.offerRematch() }
|
||||
)
|
||||
}
|
||||
|
||||
if (state.rematchOfferFromOpponent) {
|
||||
RematchOfferDialog(
|
||||
onAccept = { viewModel.acceptRematch() },
|
||||
onDecline = { viewModel.declineRematch() }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun RematchOfferDialog(onAccept: () -> Unit, onDecline: () -> Unit) {
|
||||
AppDialog(
|
||||
onDismissRequest = onDecline,
|
||||
title = stringResource(R.string.rematch_offer_title),
|
||||
actions = {
|
||||
DialogAction(stringResource(R.string.rematch_decline), onDecline, color = ink.copy(alpha = 0.75f))
|
||||
DialogAction(stringResource(R.string.rematch_accept), onAccept)
|
||||
}
|
||||
) {
|
||||
DialogText(stringResource(R.string.rematch_offer_text))
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun ChatButton(unreadCount: Int, onClick: () -> Unit) {
|
||||
Box {
|
||||
GlassOutlinedButton(onClick = onClick, modifier = Modifier.fillMaxWidth()) {
|
||||
ButtonIcon(R.drawable.ic_chat)
|
||||
Text(stringResource(R.string.chat_button), color = ink)
|
||||
}
|
||||
if (unreadCount > 0) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.align(Alignment.TopEnd)
|
||||
.size(20.dp)
|
||||
.clip(CircleShape)
|
||||
.background(ChessGreen),
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
Text(unreadCount.toString(), color = Color.White, fontSize = 11.sp)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun IncomingDrawOfferDialog(onAccept: () -> Unit, onDecline: () -> Unit) {
|
||||
AppDialog(
|
||||
onDismissRequest = onDecline,
|
||||
title = stringResource(R.string.draw_offer_received_title),
|
||||
actions = {
|
||||
DialogAction(stringResource(R.string.decline), onDecline, color = ink.copy(alpha = 0.75f))
|
||||
DialogAction(stringResource(R.string.accept), onAccept)
|
||||
}
|
||||
) {
|
||||
DialogText(stringResource(R.string.draw_offer_received_text))
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun GameOverDialog(
|
||||
status: GameStatus,
|
||||
state: GameUiState,
|
||||
onRematch: () -> Unit,
|
||||
onExit: () -> Unit,
|
||||
onDismiss: () -> Unit,
|
||||
onOfferRematch: () -> Unit = {}
|
||||
) {
|
||||
val colorWhite = stringResource(R.string.color_white)
|
||||
val colorBlack = stringResource(R.string.color_black)
|
||||
val message = when (status) {
|
||||
GameStatus.CHECKMATE -> {
|
||||
val winner = if (state.board.sideToMove == PieceColor.WHITE) colorBlack else colorWhite
|
||||
stringResource(R.string.checkmate_winner, winner)
|
||||
}
|
||||
GameStatus.STALEMATE -> stringResource(R.string.stalemate)
|
||||
GameStatus.DRAW_50_MOVES -> stringResource(R.string.draw_50_moves)
|
||||
GameStatus.DRAW_REPETITION -> stringResource(R.string.draw_repetition)
|
||||
GameStatus.DRAW_MATERIAL -> stringResource(R.string.draw_material)
|
||||
GameStatus.TIMEOUT -> {
|
||||
val winner = if (state.timedOutSide == PieceColor.WHITE) colorBlack else colorWhite
|
||||
stringResource(R.string.timeout_winner, winner)
|
||||
}
|
||||
GameStatus.TIMEOUT_DRAW -> stringResource(R.string.timeout_draw)
|
||||
GameStatus.RESIGNATION -> {
|
||||
val winner = if (state.resignedSide == PieceColor.WHITE) colorBlack else colorWhite
|
||||
stringResource(R.string.resignation_winner, winner)
|
||||
}
|
||||
GameStatus.DRAW_AGREEMENT -> stringResource(R.string.draw_agreement)
|
||||
GameStatus.ONGOING -> return
|
||||
}
|
||||
|
||||
val waitingForRematch = state.mode == GameMode.VS_PLAYER_NETWORK && !state.opponentLeft && state.rematchOfferedByMe
|
||||
|
||||
val shownMessage = if (state.opponentLeftMidGame) stringResource(R.string.opponent_left_win) else message
|
||||
AppDialog(
|
||||
onDismissRequest = onDismiss,
|
||||
title = stringResource(R.string.game_over_title),
|
||||
actions = {
|
||||
DialogAction(stringResource(R.string.back_to_menu_short), onExit, color = ink.copy(alpha = 0.75f))
|
||||
if (state.mode == GameMode.VS_AI) {
|
||||
DialogAction(stringResource(R.string.play_again), onRematch)
|
||||
} else if (state.mode == GameMode.VS_PLAYER_NETWORK && !state.opponentLeft && !waitingForRematch) {
|
||||
DialogAction(stringResource(R.string.rematch_offer), onOfferRematch)
|
||||
}
|
||||
}
|
||||
) {
|
||||
DialogText(shownMessage)
|
||||
if (waitingForRematch) {
|
||||
DialogText(stringResource(R.string.rematch_waiting))
|
||||
}
|
||||
|
||||
if (state.opponentNotice == cc.skysparkle.matewave.viewmodel.OpponentNotice.REMATCH_DECLINED) {
|
||||
Text(
|
||||
stringResource(R.string.rematch_declined_notice),
|
||||
color = Color(0xFFFFB86B),
|
||||
style = MaterialTheme.typography.bodyMedium
|
||||
)
|
||||
}
|
||||
|
||||
if (state.opponentLeft && !state.opponentLeftMidGame) {
|
||||
Text(
|
||||
stringResource(R.string.opponent_left_after_game),
|
||||
color = Color(0xFFFFB86B),
|
||||
style = MaterialTheme.typography.bodyMedium
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private const val NO_LIMIT_THRESHOLD_SECONDS = 365L * 24 * 3600
|
||||
|
||||
private const val NOTICE_VISIBLE_MS = 4_000L
|
||||
|
||||
@Composable
|
||||
private fun SideCaptured(material: cc.skysparkle.matewave.engine.MaterialBalance, side: PieceColor) {
|
||||
cc.skysparkle.matewave.ui.components.CapturedPiecesRow(
|
||||
captured = if (side == PieceColor.WHITE) material.capturedByWhite else material.capturedByBlack,
|
||||
capturedColor = if (side == PieceColor.WHITE) PieceColor.BLACK else PieceColor.WHITE,
|
||||
advantage = if (side == PieceColor.WHITE) material.advantage else -material.advantage
|
||||
)
|
||||
}
|
||||
|
||||
/** Opening name in the top bar with a small status badge underneath. */
|
||||
@Composable
|
||||
private fun OpeningTitle(name: String, status: String?, inBook: Boolean) {
|
||||
Column(modifier = Modifier.padding(start = 4.dp)) {
|
||||
Text(
|
||||
name,
|
||||
color = ink,
|
||||
style = MaterialTheme.typography.titleSmall,
|
||||
maxLines = 1,
|
||||
overflow = androidx.compose.ui.text.style.TextOverflow.Ellipsis
|
||||
)
|
||||
if (status != null) {
|
||||
val tint = if (inBook) ChessGreen else Color(0xFFFFB86B)
|
||||
Text(
|
||||
status.uppercase(),
|
||||
color = tint,
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
maxLines = 1,
|
||||
modifier = Modifier
|
||||
.padding(top = 2.dp)
|
||||
.clip(RoundedCornerShape(4.dp))
|
||||
.background(tint.copy(alpha = 0.15f))
|
||||
.padding(horizontal = 6.dp, vertical = 1.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A player strip: activity dot, name and rating, captured pieces, and a large clock. The side to
|
||||
* move gets a bright clock on a soft plate; the other side is dimmed. No cards around it.
|
||||
*/
|
||||
@Composable
|
||||
private fun PlayerRow(
|
||||
state: GameUiState,
|
||||
side: PieceColor,
|
||||
isMe: Boolean,
|
||||
material: cc.skysparkle.matewave.engine.MaterialBalance
|
||||
) {
|
||||
val active = state.status == GameStatus.ONGOING && state.board.sideToMove == side
|
||||
// Re-read after the game: the rating changes when it ends.
|
||||
val profile = remember(state.status) { cc.skysparkle.matewave.data.ProfileStore.get() }
|
||||
val (name, elo, avatar) = when {
|
||||
isMe -> Triple(profile.name, profile.elo, profile.avatarBase64)
|
||||
state.mode == GameMode.VS_AI -> Triple(
|
||||
stringResource(R.string.game_ai_name, stringResource(difficultyName(state.aiDifficulty))),
|
||||
cc.skysparkle.matewave.data.ProfileStore.aiNominalElo(state.aiDifficulty),
|
||||
null
|
||||
)
|
||||
else -> Triple(
|
||||
state.opponentProfile?.name ?: stringResource(R.string.game_opponent),
|
||||
state.opponentProfile?.elo,
|
||||
state.opponentProfile?.avatarBase64
|
||||
)
|
||||
}
|
||||
Row(modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.size(8.dp)
|
||||
.clip(CircleShape)
|
||||
.background(if (active) ChessGreen else ink.copy(alpha = 0.15f))
|
||||
)
|
||||
Spacer(Modifier.width(10.dp))
|
||||
AvatarCircle(base64 = avatar, size = 34.dp)
|
||||
Spacer(Modifier.width(10.dp))
|
||||
Column(modifier = Modifier.weight(1f)) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(6.dp)) {
|
||||
Text(
|
||||
name,
|
||||
color = if (active) ink else ink.copy(alpha = 0.75f),
|
||||
style = MaterialTheme.typography.titleSmall,
|
||||
maxLines = 1,
|
||||
overflow = androidx.compose.ui.text.style.TextOverflow.Ellipsis
|
||||
)
|
||||
if (!isMe && state.mode == GameMode.VS_PLAYER_NETWORK &&
|
||||
state.handshake == cc.skysparkle.matewave.viewmodel.HandshakeState.PLAYING &&
|
||||
!state.opponentUntrusted && !state.opponentUnverified
|
||||
) {
|
||||
AppIcon(R.drawable.ic_encrypted, size = 14.dp, tint = ChessGreen)
|
||||
}
|
||||
}
|
||||
if (elo != null) {
|
||||
Text(stringResource(R.string.friends_elo_value, elo), color = ink.copy(alpha = 0.55f), style = MaterialTheme.typography.labelSmall)
|
||||
}
|
||||
SideCaptured(material, side)
|
||||
}
|
||||
state.clockState?.let { cs ->
|
||||
BigClock(if (side == PieceColor.WHITE) cs.whiteMillis else cs.blackMillis, active)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun BigClock(millis: Long, active: Boolean) {
|
||||
val totalSeconds = millis / 1000
|
||||
val low = active && totalSeconds < 20
|
||||
val color = when {
|
||||
low -> Color(0xFFFF6B6B)
|
||||
active -> ink
|
||||
else -> ink.copy(alpha = 0.45f)
|
||||
}
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.clip(RoundedCornerShape(10.dp))
|
||||
.background(if (active) ink.copy(alpha = 0.12f) else Color.Transparent)
|
||||
.padding(horizontal = 12.dp, vertical = 6.dp),
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
if (totalSeconds >= NO_LIMIT_THRESHOLD_SECONDS) {
|
||||
AppIcon(R.drawable.ic_infinity, size = 28.dp, tint = color)
|
||||
} else {
|
||||
Text(
|
||||
"%d:%02d".format(totalSeconds / 60, totalSeconds % 60),
|
||||
color = color,
|
||||
style = MaterialTheme.typography.headlineSmall,
|
||||
fontWeight = androidx.compose.ui.text.font.FontWeight.SemiBold
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun ConnectionBanner(state: GameUiState) {
|
||||
when (state.connectionState) {
|
||||
ConnectionState.CONNECTING -> Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
CircularProgressIndicator(color = ink, strokeWidth = 2.dp, modifier = Modifier.size(16.dp))
|
||||
Text(stringResource(R.string.connecting_to_opponent), color = ink, style = MaterialTheme.typography.bodyMedium)
|
||||
}
|
||||
ConnectionState.FAILED -> Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(6.dp)) {
|
||||
AppIcon(R.drawable.ic_error, size = 18.dp, tint = Color(0xFFFF6B6B))
|
||||
Text(stringResource(R.string.connection_failed), color = Color(0xFFFF6B6B), style = MaterialTheme.typography.bodyMedium)
|
||||
}
|
||||
ConnectionState.DISCONNECTED -> if (state.gameStarted) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(6.dp)) {
|
||||
AppIcon(R.drawable.ic_signal_disconnected, size = 18.dp, tint = Color(0xFFFF6B6B))
|
||||
Text(stringResource(R.string.connection_lost), color = Color(0xFFFF6B6B), style = MaterialTheme.typography.bodyMedium)
|
||||
}
|
||||
}
|
||||
ConnectionState.CONNECTED -> Unit
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun TrustBanners(state: GameUiState) {
|
||||
val (icon, text, color) = when {
|
||||
state.opponentUntrusted -> Triple(R.drawable.ic_no_encryption, stringResource(R.string.trust_impersonation), Color(0xFFFF6B6B))
|
||||
state.opponentOutdated -> Triple(R.drawable.ic_error, stringResource(R.string.trust_outdated), Color(0xFFFF6B6B))
|
||||
state.opponentUnverified -> Triple(R.drawable.ic_no_encryption, stringResource(R.string.trust_unverified), Color(0xFFFFC46B))
|
||||
else -> Triple(0, null, Color.Unspecified)
|
||||
}
|
||||
if (text != null) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(6.dp)) {
|
||||
AppIcon(icon, size = 16.dp, tint = color)
|
||||
Text(text, color = color, style = MaterialTheme.typography.bodySmall)
|
||||
}
|
||||
}
|
||||
state.newPeerFingerprint?.let { fingerprint ->
|
||||
Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(6.dp)) {
|
||||
AppIcon(R.drawable.ic_key, size = 16.dp, tint = Color(0xFFFFC46B))
|
||||
Text(stringResource(R.string.trust_first_meeting, fingerprint), color = Color(0xFFFFC46B), style = MaterialTheme.typography.bodySmall)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Name of an AI level, shared by the setup, game and home screens. */
|
||||
internal fun difficultyName(d: cc.skysparkle.matewave.ai.AiDifficulty): Int = when (d) {
|
||||
cc.skysparkle.matewave.ai.AiDifficulty.EASY -> R.string.difficulty_easy
|
||||
cc.skysparkle.matewave.ai.AiDifficulty.MEDIUM -> R.string.difficulty_medium
|
||||
cc.skysparkle.matewave.ai.AiDifficulty.HARD -> R.string.difficulty_hard
|
||||
cc.skysparkle.matewave.ai.AiDifficulty.EXPERT -> R.string.difficulty_expert
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
package cc.skysparkle.matewave.ui.screens
|
||||
|
||||
import cc.skysparkle.matewave.ui.theme.ink
|
||||
|
||||
import android.content.Context
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.heightIn
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import cc.skysparkle.matewave.R
|
||||
import cc.skysparkle.matewave.ui.components.AppDialog
|
||||
import cc.skysparkle.matewave.ui.components.AppIcon
|
||||
import cc.skysparkle.matewave.ui.components.ButtonIcon
|
||||
import cc.skysparkle.matewave.ui.components.DialogAccent
|
||||
import cc.skysparkle.matewave.ui.components.DialogAction
|
||||
import cc.skysparkle.matewave.ui.components.GlassCard
|
||||
import cc.skysparkle.matewave.ui.components.GlassOutlinedButton
|
||||
import cc.skysparkle.matewave.ui.components.GlassScaffold
|
||||
import cc.skysparkle.matewave.ui.components.SetupSectionLabel
|
||||
|
||||
private data class LicenseEntry(
|
||||
val name: String,
|
||||
val author: String,
|
||||
val version: String?,
|
||||
val license: String,
|
||||
val url: String,
|
||||
val noteRes: Int? = null
|
||||
)
|
||||
|
||||
private const val APACHE = "Apache License 2.0"
|
||||
private const val AOSP = "The Android Open Source Project"
|
||||
|
||||
private val LIBRARIES = listOf(
|
||||
LicenseEntry(
|
||||
"Jetpack Compose (UI, Foundation, Material 3)", AOSP, "BOM 2026.09.00", APACHE,
|
||||
"https://developer.android.com/jetpack/androidx/releases/compose"
|
||||
),
|
||||
LicenseEntry(
|
||||
"AndroidX Activity Compose", AOSP, "1.13.0", APACHE,
|
||||
"https://developer.android.com/jetpack/androidx/releases/activity"
|
||||
),
|
||||
LicenseEntry(
|
||||
"AndroidX Core KTX", AOSP, "1.18.0", APACHE,
|
||||
"https://developer.android.com/jetpack/androidx/releases/core"
|
||||
),
|
||||
LicenseEntry(
|
||||
"AndroidX Navigation Compose", AOSP, "2.9.7", APACHE,
|
||||
"https://developer.android.com/jetpack/androidx/releases/navigation"
|
||||
),
|
||||
LicenseEntry(
|
||||
"AndroidX Lifecycle ViewModel Compose", AOSP, "2.11.0", APACHE,
|
||||
"https://developer.android.com/jetpack/androidx/releases/lifecycle"
|
||||
),
|
||||
LicenseEntry(
|
||||
"AndroidX \u2014 other libraries (pulled in by the ones above)", AOSP, null, APACHE,
|
||||
"https://developer.android.com/jetpack/androidx"
|
||||
),
|
||||
LicenseEntry(
|
||||
"ZXing Core (QR codes)", "ZXing authors", "3.5.4", APACHE,
|
||||
"https://github.com/zxing/zxing"
|
||||
),
|
||||
LicenseEntry(
|
||||
"kotlinx.coroutines", "JetBrains", "1.11.0", APACHE,
|
||||
"https://github.com/Kotlin/kotlinx.coroutines"
|
||||
),
|
||||
LicenseEntry(
|
||||
"Kotlin Standard Library", "JetBrains", null, APACHE,
|
||||
"https://github.com/JetBrains/kotlin"
|
||||
)
|
||||
)
|
||||
|
||||
private val DATA = listOf(
|
||||
LicenseEntry(
|
||||
"Lichess Puzzle Database", "Lichess", null, "CC0 1.0",
|
||||
"https://database.lichess.org/#puzzles", R.string.licenses_note_puzzles
|
||||
),
|
||||
LicenseEntry(
|
||||
"Lichess Standard Games Database (2013-05)", "Lichess", null, "CC0 1.0",
|
||||
"https://database.lichess.org/#standard_games", R.string.licenses_note_games
|
||||
),
|
||||
LicenseEntry(
|
||||
"lichess-org/chess-openings", "Lichess contributors", null, "CC0 1.0",
|
||||
"https://github.com/lichess-org/chess-openings", R.string.licenses_note_openings
|
||||
)
|
||||
)
|
||||
|
||||
private val ASSETS = listOf(
|
||||
LicenseEntry(
|
||||
"Misty forest scene with pine trees in haze", "Jean-Daniel Francoeur", null, "Pexels License",
|
||||
"https://www.pexels.com/photo/misty-forest-scene-with-pine-trees-in-haze-31737036/",
|
||||
R.string.licenses_note_photo
|
||||
),
|
||||
LicenseEntry(
|
||||
"Material Symbols", "Google", null, APACHE,
|
||||
"https://iconbuddy.com/material-symbols", R.string.licenses_note_icons
|
||||
),
|
||||
LicenseEntry(
|
||||
"Vector Chess Pieces", "RhosGFX", null, "CC0 1.0",
|
||||
"https://rhosgfx.itch.io/vector-chess-pieces", R.string.licenses_note_pieces
|
||||
)
|
||||
)
|
||||
|
||||
private val RADIO = listOf(
|
||||
LicenseEntry(
|
||||
"Radio Swiss Jazz", "SRG SSR", null, "\u00A9 SRG SSR",
|
||||
"https://www.radioswissjazz.ch/en/reception/internet", R.string.licenses_note_swissjazz
|
||||
)
|
||||
)
|
||||
|
||||
@Composable
|
||||
fun LicensesScreen(onBack: () -> Unit) {
|
||||
val context = LocalContext.current
|
||||
var showApache by remember { mutableStateOf(false) }
|
||||
|
||||
GlassScaffold(title = stringResource(R.string.licenses_title), onBack = onBack) {
|
||||
Text(
|
||||
stringResource(R.string.licenses_intro),
|
||||
color = ink.copy(alpha = 0.75f),
|
||||
style = MaterialTheme.typography.bodyMedium
|
||||
)
|
||||
|
||||
SetupSectionLabel(stringResource(R.string.licenses_group_libraries))
|
||||
LIBRARIES.forEach { entry -> LicenseCard(entry) { openUrl(context, entry.url) } }
|
||||
GlassOutlinedButton(onClick = { showApache = true }, modifier = Modifier.fillMaxWidth()) {
|
||||
ButtonIcon(R.drawable.ic_copyright)
|
||||
Text(stringResource(R.string.licenses_apache_full), color = ink)
|
||||
}
|
||||
|
||||
SetupSectionLabel(stringResource(R.string.licenses_group_data))
|
||||
DATA.forEach { entry -> LicenseCard(entry) { openUrl(context, entry.url) } }
|
||||
|
||||
SetupSectionLabel(stringResource(R.string.licenses_group_assets))
|
||||
ASSETS.forEach { entry -> LicenseCard(entry) { openUrl(context, entry.url) } }
|
||||
|
||||
SetupSectionLabel(stringResource(R.string.licenses_group_radio))
|
||||
Text(
|
||||
stringResource(R.string.licenses_radio_hint),
|
||||
color = ink.copy(alpha = 0.6f),
|
||||
style = MaterialTheme.typography.bodySmall
|
||||
)
|
||||
RADIO.forEach { entry -> LicenseCard(entry) { openUrl(context, entry.url) } }
|
||||
}
|
||||
|
||||
if (showApache) {
|
||||
ApacheLicenseDialog(onDismiss = { showApache = false })
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun LicenseCard(entry: LicenseEntry, onClick: () -> Unit) {
|
||||
GlassCard(modifier = Modifier.fillMaxWidth().clickable(onClick = onClick)) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Column(modifier = Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(2.dp)) {
|
||||
Text(entry.name, color = ink, style = MaterialTheme.typography.titleMedium)
|
||||
Text(
|
||||
listOfNotNull(entry.author, entry.version).joinToString(" \u00B7 "),
|
||||
color = ink.copy(alpha = 0.65f),
|
||||
style = MaterialTheme.typography.bodySmall
|
||||
)
|
||||
Text(entry.license, color = DialogAccent, style = MaterialTheme.typography.bodySmall)
|
||||
entry.noteRes?.let { note ->
|
||||
Text(
|
||||
stringResource(note),
|
||||
color = ink.copy(alpha = 0.7f),
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
modifier = Modifier.padding(top = 6.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
AppIcon(R.drawable.ic_arrow_forward, size = 16.dp, tint = ink.copy(alpha = 0.6f))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun ApacheLicenseDialog(onDismiss: () -> Unit) {
|
||||
val context = LocalContext.current
|
||||
val text = remember {
|
||||
runCatching {
|
||||
context.resources.openRawResource(R.raw.license_apache_2_0)
|
||||
.bufferedReader().use { it.readText() }
|
||||
}.getOrDefault("https://www.apache.org/licenses/LICENSE-2.0")
|
||||
.trim()
|
||||
.split(Regex("\n\\s*\n"))
|
||||
.joinToString("\n\n") { paragraph -> paragraph.lines().joinToString(" ") { it.trim() } }
|
||||
}
|
||||
AppDialog(
|
||||
onDismissRequest = onDismiss,
|
||||
title = APACHE,
|
||||
actions = { DialogAction(stringResource(R.string.chat_close), onDismiss) }
|
||||
) {
|
||||
Column(modifier = Modifier.fillMaxWidth().heightIn(max = 380.dp).verticalScroll(rememberScrollState())) {
|
||||
Text(text, color = ink.copy(alpha = 0.78f), style = MaterialTheme.typography.bodySmall)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun openUrl(context: Context, url: String) = cc.skysparkle.matewave.settings.AppLinks.open(context, url)
|
||||
@@ -0,0 +1,371 @@
|
||||
package cc.skysparkle.matewave.ui.screens
|
||||
|
||||
import androidx.compose.foundation.layout.statusBarsPadding
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import cc.skysparkle.matewave.ui.components.ListDivider
|
||||
import cc.skysparkle.matewave.ui.components.ListGroup
|
||||
import cc.skysparkle.matewave.ui.components.ListRow
|
||||
import cc.skysparkle.matewave.ui.components.SectionCaption
|
||||
|
||||
import cc.skysparkle.matewave.ui.theme.ink
|
||||
|
||||
import androidx.compose.foundation.Canvas
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.border
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.navigationBarsPadding
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.layout.widthIn
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.geometry.Offset
|
||||
import androidx.compose.ui.graphics.Brush
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.ColorFilter
|
||||
import androidx.compose.ui.graphics.Path
|
||||
import androidx.compose.ui.graphics.StrokeCap
|
||||
import androidx.compose.ui.graphics.drawscope.Stroke
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.compose.foundation.Image
|
||||
import cc.skysparkle.matewave.R
|
||||
import cc.skysparkle.matewave.ui.components.DialogText
|
||||
import cc.skysparkle.matewave.ui.components.DialogAction
|
||||
import cc.skysparkle.matewave.ui.components.AppDialog
|
||||
import cc.skysparkle.matewave.stats.Stats
|
||||
import cc.skysparkle.matewave.ui.components.AppBottomBar
|
||||
import cc.skysparkle.matewave.ui.components.BottomTab
|
||||
import cc.skysparkle.matewave.ui.theme.ChessGreen
|
||||
import cc.skysparkle.matewave.ui.theme.TabletMaxContentWidth
|
||||
import cc.skysparkle.matewave.ui.theme.UiCornerRadius
|
||||
import cc.skysparkle.matewave.ui.theme.glass
|
||||
import cc.skysparkle.matewave.viewmodel.GameViewModel
|
||||
import cc.skysparkle.matewave.viewmodel.ResumeInfo
|
||||
import cc.skysparkle.matewave.ui.components.displayName
|
||||
|
||||
@Composable
|
||||
fun MainMenuScreen(
|
||||
viewModel: GameViewModel,
|
||||
studyViewModel: cc.skysparkle.matewave.viewmodel.OpeningStudyViewModel,
|
||||
onContinueGame: () -> Unit,
|
||||
onQuickGame: () -> Unit,
|
||||
onVsAiClick: () -> Unit,
|
||||
onPuzzlesClick: () -> Unit,
|
||||
onStudyClick: () -> Unit,
|
||||
onVsPlayerClick: () -> Unit,
|
||||
onSettingsClick: () -> Unit,
|
||||
onAccountClick: () -> Unit,
|
||||
onFriendsClick: () -> Unit
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
val resumable by viewModel.resumable.collectAsState()
|
||||
val ongoing = resumable
|
||||
val hasOngoingGame = ongoing != null
|
||||
val profile = remember { cc.skysparkle.matewave.data.ProfileStore.get() }
|
||||
val lastSetup = remember { cc.skysparkle.matewave.settings.LastGameSetup(context) }
|
||||
LaunchedEffect(Unit) { studyViewModel.init(context) }
|
||||
val study by studyViewModel.overview.collectAsState()
|
||||
|
||||
var showRatingGate by remember { mutableStateOf(false) }
|
||||
|
||||
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.TopCenter) {
|
||||
Column(modifier = Modifier.fillMaxSize().widthIn(max = TabletMaxContentWidth)) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.statusBarsPadding()
|
||||
.padding(top = 16.dp, start = 20.dp, end = 20.dp, bottom = 10.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Image(
|
||||
painter = painterResource(R.drawable.logo_knight),
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(width = 30.dp, height = 44.dp)
|
||||
)
|
||||
Spacer(Modifier.width(12.dp))
|
||||
Greeting(modifier = Modifier.weight(1f))
|
||||
// The rating is always in sight.
|
||||
Text(
|
||||
"${profile.elo}",
|
||||
color = ink,
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
modifier = Modifier
|
||||
.clip(RoundedCornerShape(50))
|
||||
.background(ink.copy(alpha = 0.10f))
|
||||
.padding(horizontal = 12.dp, vertical = 6.dp)
|
||||
)
|
||||
}
|
||||
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.weight(1f)
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 20.dp)
|
||||
.verticalScroll(rememberScrollState()),
|
||||
verticalArrangement = Arrangement.spacedBy(14.dp)
|
||||
) {
|
||||
// The most urgent thing first: an unfinished game.
|
||||
if (ongoing != null) ContinueCard(ongoing, onContinueGame)
|
||||
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(12.dp), modifier = Modifier.fillMaxWidth()) {
|
||||
ModeTile(
|
||||
iconRes = R.drawable.ic_ai_app,
|
||||
title = stringResource(R.string.mode_ai_title),
|
||||
subtitle = stringResource(R.string.mode_ai_subtitle),
|
||||
modifier = Modifier.weight(1f),
|
||||
onClick = { if (profile.levelChosen) onVsAiClick() else showRatingGate = true }
|
||||
)
|
||||
ModeTile(
|
||||
iconRes = R.drawable.ic_profile_photo,
|
||||
title = stringResource(R.string.mode_online_title),
|
||||
subtitle = stringResource(R.string.mode_online_subtitle),
|
||||
modifier = Modifier.weight(1f),
|
||||
onClick = { if (profile.levelChosen) onVsPlayerClick() else showRatingGate = true }
|
||||
)
|
||||
}
|
||||
|
||||
SectionCaption(stringResource(R.string.home_today))
|
||||
ListGroup {
|
||||
if (!hasOngoingGame) {
|
||||
ListRow(
|
||||
title = stringResource(R.string.quick_game_title),
|
||||
subtitle = stringResource(
|
||||
R.string.quick_game_subtitle,
|
||||
stringResource(difficultyName(lastSetup.aiDifficulty)),
|
||||
lastSetup.aiTime.displayName()
|
||||
),
|
||||
icon = R.drawable.ic_quick_game,
|
||||
iconTint = ChessGreen,
|
||||
chevron = true,
|
||||
onClick = { if (profile.levelChosen) onQuickGame() else showRatingGate = true }
|
||||
)
|
||||
ListDivider()
|
||||
}
|
||||
if (study.dueCards > 0) {
|
||||
ListRow(
|
||||
title = stringResource(R.string.study_review_title),
|
||||
subtitle = stringResource(R.string.study_review_summary, study.dueCards, study.dueCourses),
|
||||
icon = R.drawable.ic_review,
|
||||
iconTint = cc.skysparkle.matewave.ui.screens.StarGold,
|
||||
chevron = true,
|
||||
onClick = onStudyClick
|
||||
)
|
||||
} else {
|
||||
ListRow(
|
||||
title = stringResource(R.string.study_title),
|
||||
subtitle = stringResource(R.string.study_menu_subtitle),
|
||||
icon = R.drawable.ic_crown,
|
||||
chevron = true,
|
||||
onClick = onStudyClick
|
||||
)
|
||||
}
|
||||
ListDivider()
|
||||
ListRow(
|
||||
title = stringResource(R.string.puzzles_title),
|
||||
subtitle = stringResource(R.string.puzzles_subtitle),
|
||||
icon = R.drawable.ic_puzzle,
|
||||
chevron = true,
|
||||
onClick = onPuzzlesClick
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Box(modifier = Modifier.navigationBarsPadding()) {
|
||||
AppBottomBar(
|
||||
current = BottomTab.HOME,
|
||||
onHomeClick = { },
|
||||
onFriendsClick = onFriendsClick,
|
||||
onSettingsClick = onSettingsClick,
|
||||
onAccountClick = onAccountClick,
|
||||
accountAvatarBase64 = profile.avatarBase64
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (showRatingGate) {
|
||||
RatingGateDialog(
|
||||
onOpenAccount = { showRatingGate = false; onAccountClick() },
|
||||
onDismiss = { showRatingGate = false }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun Greeting(modifier: Modifier = Modifier) {
|
||||
val hour = remember { java.util.Calendar.getInstance().get(java.util.Calendar.HOUR_OF_DAY) }
|
||||
val greeting = when (hour) {
|
||||
in 5..11 -> stringResource(R.string.greeting_morning)
|
||||
in 12..17 -> stringResource(R.string.greeting_afternoon)
|
||||
in 18..22 -> stringResource(R.string.greeting_evening)
|
||||
else -> stringResource(R.string.greeting_night)
|
||||
}
|
||||
Column(modifier = modifier) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Text(greeting, color = ink, style = MaterialTheme.typography.headlineSmall)
|
||||
Spacer(Modifier.width(6.dp))
|
||||
Text("👋", fontSize = 20.sp)
|
||||
}
|
||||
Text(stringResource(R.string.greeting_ready), color = ink.copy(alpha = 0.7f), style = MaterialTheme.typography.bodyMedium)
|
||||
}
|
||||
}
|
||||
|
||||
/** An unfinished game against the AI: move number, whose turn and the clock, one tap to resume. */
|
||||
@Composable
|
||||
private fun ContinueCard(info: ResumeInfo, onClick: () -> Unit) {
|
||||
val turn = stringResource(if (info.myTurn) R.string.home_your_move else R.string.home_ai_move)
|
||||
val clock = info.myMillis?.let { ms ->
|
||||
val s = ms.coerceAtLeast(0) / 1000
|
||||
"%d:%02d".format(s / 60, s % 60)
|
||||
}
|
||||
val details = stringResource(R.string.home_continue_details, info.moveNumber, turn) + (clock?.let { " · $it" } ?: "")
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.glass(RoundedCornerShape(UiCornerRadius))
|
||||
.border(1.dp, ChessGreen.copy(alpha = 0.6f), RoundedCornerShape(UiCornerRadius))
|
||||
.clickable(onClick = onClick)
|
||||
.padding(16.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Column(modifier = Modifier.weight(1f)) {
|
||||
Text(stringResource(R.string.continue_game_title), color = ink, style = MaterialTheme.typography.titleMedium)
|
||||
Text(details, color = ink.copy(alpha = 0.7f), style = MaterialTheme.typography.bodySmall)
|
||||
}
|
||||
Box(
|
||||
modifier = Modifier.size(44.dp).clip(CircleShape).background(ChessGreen),
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
cc.skysparkle.matewave.ui.components.AppIcon(R.drawable.ic_arrow_forward, size = 20.dp, tint = Color.White)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** One of the two equal ways to start a game. */
|
||||
@Composable
|
||||
private fun ModeTile(iconRes: Int, title: String, subtitle: String, modifier: Modifier = Modifier, onClick: () -> Unit) {
|
||||
Column(
|
||||
modifier = modifier
|
||||
.glass(RoundedCornerShape(UiCornerRadius))
|
||||
.clickable(onClick = onClick)
|
||||
.padding(14.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(4.dp)
|
||||
) {
|
||||
Image(
|
||||
painter = painterResource(iconRes),
|
||||
contentDescription = null,
|
||||
colorFilter = ColorFilter.tint(ink),
|
||||
modifier = Modifier.size(24.dp)
|
||||
)
|
||||
Text(title, color = ink, style = MaterialTheme.typography.titleMedium)
|
||||
Text(subtitle, color = ink.copy(alpha = 0.65f), style = MaterialTheme.typography.bodySmall)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun RatingGateDialog(onOpenAccount: () -> Unit, onDismiss: () -> Unit) {
|
||||
AppDialog(
|
||||
onDismissRequest = onDismiss,
|
||||
title = stringResource(R.string.rating_gate_title),
|
||||
actions = {
|
||||
DialogAction(stringResource(R.string.account_cancel), onDismiss, color = ink.copy(alpha = 0.75f))
|
||||
DialogAction(stringResource(R.string.rating_gate_open), onOpenAccount)
|
||||
}
|
||||
) {
|
||||
DialogText(stringResource(R.string.rating_gate_text))
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
internal fun StatsDialog(onDismiss: () -> Unit) {
|
||||
val curve = remember { Stats.ratingCurve(cc.skysparkle.matewave.data.ProfileStore.get().elo) }
|
||||
AppDialog(
|
||||
onDismissRequest = onDismiss,
|
||||
title = stringResource(R.string.stats_dialog_title),
|
||||
actions = { DialogAction(stringResource(R.string.chat_close), onDismiss) }
|
||||
) {
|
||||
DialogText(stringResource(R.string.stats_dialog_summary, Stats.wins(), Stats.winPercent(), Stats.streak()))
|
||||
MiniRatingGraph(points = curve, modifier = Modifier.fillMaxWidth().height(140.dp))
|
||||
Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
|
||||
Text(stringResource(R.string.stats_min, curve.min()), color = ink.copy(alpha = 0.6f), style = MaterialTheme.typography.labelMedium)
|
||||
Text(stringResource(R.string.stats_now, curve.last()), color = ink, style = MaterialTheme.typography.labelMedium)
|
||||
Text(stringResource(R.string.stats_max, curve.max()), color = ink.copy(alpha = 0.6f), style = MaterialTheme.typography.labelMedium)
|
||||
}
|
||||
Text(
|
||||
stringResource(R.string.stats_graph_caption),
|
||||
color = ink.copy(alpha = 0.6f),
|
||||
style = MaterialTheme.typography.bodySmall
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
internal fun MiniRatingGraph(points: List<Int>, modifier: Modifier = Modifier) {
|
||||
val lineInk = ink
|
||||
Canvas(modifier = modifier) {
|
||||
if (points.size < 2) return@Canvas
|
||||
val minV = points.min().toFloat()
|
||||
val maxV = points.max().toFloat()
|
||||
val range = (maxV - minV).coerceAtLeast(1f)
|
||||
val stepX = size.width / (points.size - 1)
|
||||
|
||||
fun yOf(v: Int) = size.height - ((v - minV) / range) * size.height
|
||||
|
||||
val gridLines = 4
|
||||
for (i in 0..gridLines) {
|
||||
val y = size.height / gridLines * i
|
||||
drawLine(lineInk.copy(alpha = 0.08f), Offset(0f, y), Offset(size.width, y), strokeWidth = 1f)
|
||||
}
|
||||
// Reference line: the rating at the start of the shown history.
|
||||
val zeroY = yOf(points.first())
|
||||
drawLine(lineInk.copy(alpha = 0.3f), Offset(0f, zeroY), Offset(size.width, zeroY), strokeWidth = 1.5f)
|
||||
|
||||
val line = Path().apply {
|
||||
moveTo(0f, yOf(points[0]))
|
||||
points.forEachIndexed { i, v -> if (i > 0) lineTo(i * stepX, yOf(v)) }
|
||||
}
|
||||
val fill = Path().apply {
|
||||
addPath(line)
|
||||
lineTo(size.width, size.height)
|
||||
lineTo(0f, size.height)
|
||||
close()
|
||||
}
|
||||
drawPath(
|
||||
fill,
|
||||
brush = Brush.verticalGradient(listOf(ChessGreen.copy(alpha = 0.35f), ChessGreen.copy(alpha = 0f)))
|
||||
)
|
||||
drawPath(line, color = ChessGreen, style = Stroke(width = 4f, cap = StrokeCap.Round))
|
||||
|
||||
points.forEachIndexed { i, v ->
|
||||
val center = Offset(i * stepX, yOf(v))
|
||||
drawCircle(color = lineInk, radius = 5f, center = center)
|
||||
drawCircle(color = ChessGreen, radius = 3f, center = center)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,301 @@
|
||||
package cc.skysparkle.matewave.ui.screens
|
||||
|
||||
import android.widget.Toast
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.BoxWithConstraints
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.aspectRatio
|
||||
import androidx.compose.foundation.layout.fillMaxHeight
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableIntStateOf
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.alpha
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Brush
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import cc.skysparkle.matewave.R
|
||||
import cc.skysparkle.matewave.engine.PieceColor
|
||||
import cc.skysparkle.matewave.openings.CourseInfo
|
||||
import cc.skysparkle.matewave.openings.CourseStatus
|
||||
import cc.skysparkle.matewave.openings.artRes
|
||||
import cc.skysparkle.matewave.ui.components.AppIcon
|
||||
import cc.skysparkle.matewave.ui.components.GlassButton
|
||||
import cc.skysparkle.matewave.ui.components.GlassScaffold
|
||||
import cc.skysparkle.matewave.ui.components.SetupSegmented
|
||||
import cc.skysparkle.matewave.ui.theme.ChessGreen
|
||||
import cc.skysparkle.matewave.ui.theme.UiCornerRadius
|
||||
import cc.skysparkle.matewave.ui.theme.glass
|
||||
import cc.skysparkle.matewave.ui.theme.ink
|
||||
import cc.skysparkle.matewave.viewmodel.OpeningStudyViewModel
|
||||
import cc.skysparkle.matewave.viewmodel.StudyOverview
|
||||
|
||||
val StarGold = Color(0xFFFFC857)
|
||||
|
||||
@Composable
|
||||
fun OpeningStudyScreen(viewModel: OpeningStudyViewModel, onBack: () -> Unit, onOpenTrainer: () -> Unit) {
|
||||
val context = LocalContext.current
|
||||
LaunchedEffect(Unit) { viewModel.init(context) }
|
||||
val overview by viewModel.overview.collectAsState()
|
||||
var side by rememberSaveable { mutableIntStateOf(0) }
|
||||
|
||||
GlassScaffold(
|
||||
title = stringResource(R.string.study_title),
|
||||
onBack = onBack,
|
||||
actions = { HeaderProgress(overview) }
|
||||
) {
|
||||
val total = overview.courses.size.coerceAtLeast(1)
|
||||
ProgressBar(overview.courses.sumOf { it.stars }.toFloat() / (total * 3), height = 4)
|
||||
|
||||
NextAction(overview, viewModel, onOpenTrainer)
|
||||
|
||||
SetupSegmented(
|
||||
options = listOf(stringResource(R.string.study_for_white), stringResource(R.string.study_for_black)),
|
||||
selectedIndex = side
|
||||
) { side = it }
|
||||
|
||||
val lockedMessage = overview.learning?.let { stringResource(R.string.study_locked_toast, it.course.name) }
|
||||
val sideColor = if (side == 0) PieceColor.WHITE else PieceColor.BLACK
|
||||
BoxWithConstraints(modifier = Modifier.fillMaxWidth()) {
|
||||
val columns = when {
|
||||
maxWidth < 360.dp -> 2
|
||||
maxWidth < 600.dp -> 3
|
||||
else -> 4
|
||||
}
|
||||
Column(verticalArrangement = Arrangement.spacedBy(14.dp)) {
|
||||
overview.courses.filter { it.course.side == sideColor }.chunked(columns).forEach { row ->
|
||||
Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(10.dp)) {
|
||||
row.forEach { info ->
|
||||
CourseCard(info, Modifier.weight(1f)) {
|
||||
if (viewModel.open(info.course)) onOpenTrainer()
|
||||
else if (lockedMessage != null) Toast.makeText(context, lockedMessage, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
repeat(columns - row.size) { Spacer(Modifier.weight(1f)) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** "3/24 ★ 1" in the top bar. */
|
||||
@Composable
|
||||
private fun HeaderProgress(overview: StudyOverview) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.padding(end = 12.dp)) {
|
||||
Text("${overview.learnedCount}/${overview.courses.size}", color = ink.copy(alpha = 0.8f), style = MaterialTheme.typography.titleSmall)
|
||||
Spacer(Modifier.width(10.dp))
|
||||
AppIcon(R.drawable.ic_star_badge, size = 16.dp, tint = StarGold)
|
||||
Spacer(Modifier.width(4.dp))
|
||||
Text("${overview.masteredCount}", color = ink.copy(alpha = 0.8f), style = MaterialTheme.typography.titleSmall)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The one thing to do now, shown only when there is something to do: review, continue the
|
||||
* course in progress, or start the first course. Otherwise just the time of the next review.
|
||||
*/
|
||||
@Composable
|
||||
private fun NextAction(overview: StudyOverview, viewModel: OpeningStudyViewModel, onOpenTrainer: () -> Unit) {
|
||||
val firstNew = overview.courses.firstOrNull { it.status == CourseStatus.NEW }
|
||||
val learning = overview.learning
|
||||
val nextDueAt = overview.nextDueAt
|
||||
when {
|
||||
overview.dueCards > 0 -> ActionCard(
|
||||
icon = R.drawable.ic_review,
|
||||
title = stringResource(R.string.study_review_title),
|
||||
subtitle = stringResource(R.string.study_review_summary, overview.dueCards, overview.dueCourses),
|
||||
button = stringResource(R.string.study_review_button)
|
||||
) { if (viewModel.reviewNext()) onOpenTrainer() }
|
||||
learning != null -> ActionCard(
|
||||
icon = R.drawable.ic_book,
|
||||
title = stringResource(R.string.study_continue_title),
|
||||
subtitle = learning.course.name,
|
||||
button = stringResource(R.string.study_continue_button),
|
||||
progress = learning.learned.toFloat() / learning.total.coerceAtLeast(1)
|
||||
) { if (viewModel.open(learning.course)) onOpenTrainer() }
|
||||
overview.learnedCount == 0 && firstNew != null -> ActionCard(
|
||||
icon = R.drawable.ic_book,
|
||||
title = stringResource(R.string.study_start_with, firstNew.course.name),
|
||||
subtitle = null,
|
||||
button = stringResource(R.string.study_start_button)
|
||||
) { if (viewModel.open(firstNew.course)) onOpenTrainer() }
|
||||
nextDueAt != null -> Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
AppIcon(R.drawable.ic_review, size = 16.dp, tint = ink.copy(alpha = 0.55f))
|
||||
Spacer(Modifier.width(8.dp))
|
||||
Text(stringResource(R.string.study_next_review, formatDueIn(nextDueAt)), color = ink.copy(alpha = 0.65f), style = MaterialTheme.typography.bodyMedium)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun ActionCard(icon: Int, title: String, subtitle: String?, button: String, progress: Float? = null, onClick: () -> Unit) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.glass(RoundedCornerShape(UiCornerRadius))
|
||||
.padding(horizontal = 14.dp, vertical = 12.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
AppIcon(icon, size = 22.dp, tint = StarGold)
|
||||
Spacer(Modifier.width(12.dp))
|
||||
Column(modifier = Modifier.weight(1f)) {
|
||||
Text(title, color = ink, style = MaterialTheme.typography.titleSmall, maxLines = 2, overflow = TextOverflow.Ellipsis)
|
||||
if (subtitle != null) {
|
||||
Text(subtitle, color = ink.copy(alpha = 0.65f), style = MaterialTheme.typography.bodySmall, maxLines = 2, overflow = TextOverflow.Ellipsis)
|
||||
}
|
||||
if (progress != null) {
|
||||
Spacer(Modifier.height(6.dp))
|
||||
ProgressBar(progress, height = 4)
|
||||
}
|
||||
}
|
||||
Spacer(Modifier.width(12.dp))
|
||||
GlassButton(onClick = onClick) { Text(button, color = ink, maxLines = 1) }
|
||||
}
|
||||
}
|
||||
|
||||
/** A course card without a frame: the knight on a soft glow, name, size and one status line. */
|
||||
@Composable
|
||||
private fun CourseCard(info: CourseInfo, modifier: Modifier, onClick: () -> Unit) {
|
||||
val locked = info.status == CourseStatus.LOCKED
|
||||
val glow = when {
|
||||
info.mastered -> StarGold.copy(alpha = 0.20f)
|
||||
info.status == CourseStatus.DUE -> ChessGreen.copy(alpha = 0.22f)
|
||||
else -> ink.copy(alpha = 0.08f)
|
||||
}
|
||||
val family = info.course.name.substringBefore(':').trim()
|
||||
val variation = info.course.name.substringAfter(':', "").trim()
|
||||
|
||||
Column(
|
||||
modifier = modifier
|
||||
.alpha(if (locked) 0.4f else 1f)
|
||||
.clip(RoundedCornerShape(UiCornerRadius))
|
||||
.clickable(onClick = onClick)
|
||||
.padding(4.dp)
|
||||
) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.aspectRatio(0.85f)
|
||||
.background(Brush.radialGradient(listOf(glow, Color.Transparent))),
|
||||
contentAlignment = Alignment.BottomCenter
|
||||
) {
|
||||
Image(
|
||||
painter = painterResource(info.course.artRes()),
|
||||
contentDescription = null,
|
||||
contentScale = ContentScale.Fit,
|
||||
modifier = Modifier.fillMaxSize().padding(6.dp)
|
||||
)
|
||||
when {
|
||||
info.mastered -> AppIcon(R.drawable.ic_star_badge, size = 22.dp, tint = StarGold, modifier = Modifier.align(Alignment.TopEnd))
|
||||
info.status == CourseStatus.DUE -> AppIcon(R.drawable.ic_review, size = 20.dp, tint = ChessGreen, modifier = Modifier.align(Alignment.TopEnd))
|
||||
}
|
||||
}
|
||||
Spacer(Modifier.height(6.dp))
|
||||
Text(family, color = ink, style = MaterialTheme.typography.labelLarge, maxLines = 1, overflow = TextOverflow.Ellipsis)
|
||||
Text(
|
||||
variation.ifEmpty { " " },
|
||||
color = ink.copy(alpha = 0.65f),
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
minLines = 2,
|
||||
maxLines = 2,
|
||||
overflow = TextOverflow.Ellipsis
|
||||
)
|
||||
Spacer(Modifier.height(4.dp))
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
AppIcon(R.drawable.ic_timer, size = 12.dp, tint = ink.copy(alpha = 0.55f))
|
||||
Spacer(Modifier.width(4.dp))
|
||||
Text(
|
||||
LocalContext.current.resources.getQuantityString(R.plurals.study_moves_count, info.total, info.total),
|
||||
color = ink.copy(alpha = 0.55f),
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
maxLines = 1
|
||||
)
|
||||
}
|
||||
// One status line of fixed height, so rows stay even; empty for a new course.
|
||||
Box(modifier = Modifier.fillMaxWidth().height(20.dp), contentAlignment = Alignment.CenterStart) {
|
||||
when (info.status) {
|
||||
CourseStatus.NEW -> Unit
|
||||
CourseStatus.LOCKED -> Text(stringResource(R.string.study_locked_short), color = ink.copy(alpha = 0.7f), style = MaterialTheme.typography.labelSmall)
|
||||
CourseStatus.LEARNING -> ProgressBar(info.learned.toFloat() / info.total.coerceAtLeast(1), height = 4)
|
||||
CourseStatus.DUE -> Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Box(Modifier.size(7.dp).clip(CircleShape).background(ChessGreen))
|
||||
Spacer(Modifier.width(6.dp))
|
||||
Text(stringResource(R.string.study_status_due, info.dueCards), color = ChessGreen, style = MaterialTheme.typography.labelSmall, maxLines = 1)
|
||||
}
|
||||
CourseStatus.WAITING -> Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
if (info.stars > 0) Stars(info.stars, size = 12.dp)
|
||||
Spacer(Modifier.width(6.dp))
|
||||
info.nextDueAt?.let {
|
||||
Text(formatDueIn(it), color = ink.copy(alpha = 0.55f), style = MaterialTheme.typography.labelSmall, maxLines = 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun Stars(count: Int, size: androidx.compose.ui.unit.Dp = 16.dp) {
|
||||
Row {
|
||||
repeat(3) { i ->
|
||||
AppIcon(R.drawable.ic_star, size = size, tint = if (i < count) StarGold else ink.copy(alpha = 0.2f))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun ProgressBar(fraction: Float, height: Int = 8) {
|
||||
val f = fraction.coerceIn(0f, 1f)
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.height(height.dp)
|
||||
.clip(RoundedCornerShape((height / 2).dp))
|
||||
.background(ink.copy(alpha = 0.12f))
|
||||
) {
|
||||
if (f > 0f) Box(Modifier.fillMaxWidth(f).fillMaxHeight().clip(RoundedCornerShape((height / 2).dp)).background(ChessGreen))
|
||||
}
|
||||
}
|
||||
|
||||
/** "5 min", "4 h", "3 d" until [at]; "now" if already due. */
|
||||
@Composable
|
||||
fun formatDueIn(at: Long): String {
|
||||
val diff = at - System.currentTimeMillis()
|
||||
val minute = 60_000L
|
||||
val hour = 60 * minute
|
||||
val day = 24 * hour
|
||||
return when {
|
||||
diff <= 0 -> stringResource(R.string.study_due_now)
|
||||
diff < hour -> stringResource(R.string.study_minutes, ((diff + minute - 1) / minute).toInt())
|
||||
diff < day -> stringResource(R.string.study_hours, ((diff + hour - 1) / hour).toInt())
|
||||
else -> stringResource(R.string.study_days, ((diff + day - 1) / day).toInt())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
package cc.skysparkle.matewave.ui.screens
|
||||
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.DisposableEffect
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import cc.skysparkle.matewave.R
|
||||
import cc.skysparkle.matewave.engine.Board
|
||||
import cc.skysparkle.matewave.engine.PieceColor
|
||||
import cc.skysparkle.matewave.openings.OpeningBook
|
||||
import cc.skysparkle.matewave.ui.components.ButtonIcon
|
||||
import cc.skysparkle.matewave.ui.components.ChessBoardView
|
||||
import cc.skysparkle.matewave.ui.components.GlassButton
|
||||
import cc.skysparkle.matewave.ui.components.GlassCard
|
||||
import cc.skysparkle.matewave.ui.components.GlassOutlinedButton
|
||||
import cc.skysparkle.matewave.ui.components.GlassScaffold
|
||||
import cc.skysparkle.matewave.ui.components.MovesBar
|
||||
import cc.skysparkle.matewave.ui.theme.ChessGreen
|
||||
import cc.skysparkle.matewave.ui.theme.ink
|
||||
import cc.skysparkle.matewave.viewmodel.OpeningStudyViewModel
|
||||
import cc.skysparkle.matewave.viewmodel.SessionMode
|
||||
import cc.skysparkle.matewave.viewmodel.TrainerPhase
|
||||
|
||||
@Composable
|
||||
fun OpeningTrainerScreen(viewModel: OpeningStudyViewModel, onBack: () -> Unit) {
|
||||
val state by viewModel.state.collectAsState()
|
||||
DisposableEffect(viewModel) { onDispose { viewModel.leave() } }
|
||||
val course = state.course ?: return
|
||||
val startBoard = remember { Board.startPosition() }
|
||||
val sans = state.sans
|
||||
val opening = remember(sans) { OpeningBook.openingName(sans) }
|
||||
|
||||
GlassScaffold(title = course.name, onBack = onBack) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Text(
|
||||
stringResource(
|
||||
when (state.mode) {
|
||||
SessionMode.LEARN -> R.string.study_mode_learn
|
||||
SessionMode.REVIEW -> R.string.study_mode_review
|
||||
SessionMode.PRACTICE -> R.string.study_mode_practice
|
||||
}
|
||||
),
|
||||
color = ChessGreen,
|
||||
style = MaterialTheme.typography.labelLarge
|
||||
)
|
||||
Spacer(Modifier.weight(1f))
|
||||
val asked = state.asked.size
|
||||
if (asked > 0 && state.phase != TrainerPhase.COMPLETE) {
|
||||
Text(
|
||||
stringResource(R.string.study_progress, minOf(state.answered + 1, asked), asked),
|
||||
color = ink.copy(alpha = 0.7f),
|
||||
style = MaterialTheme.typography.bodySmall
|
||||
)
|
||||
}
|
||||
}
|
||||
ProgressBar(state.answered.toFloat() / state.asked.size.coerceAtLeast(1))
|
||||
|
||||
MovesBar(sans = sans, startBoard = startBoard, currentIndex = sans.size, opening = opening)
|
||||
|
||||
ChessBoardView(
|
||||
board = state.board,
|
||||
selectedSquare = state.selectedSquare,
|
||||
legalTargets = state.legalTargets,
|
||||
kingInCheckSquare = null,
|
||||
flipped = course.side == PieceColor.BLACK,
|
||||
onSquareClick = { viewModel.onSquareClick(it) },
|
||||
lastMove = state.lastMove,
|
||||
hintSquare = state.hintSquare,
|
||||
interactionEnabled = state.phase == TrainerPhase.YOUR_MOVE || state.phase == TrainerPhase.WRONG
|
||||
)
|
||||
|
||||
when (state.phase) {
|
||||
TrainerPhase.YOUR_MOVE -> Text(
|
||||
stringResource(if (state.mode == SessionMode.LEARN) R.string.study_learn_prompt else R.string.study_recall_prompt),
|
||||
color = ink,
|
||||
style = MaterialTheme.typography.titleMedium
|
||||
)
|
||||
TrainerPhase.AUTO_MOVE -> Text(
|
||||
stringResource(R.string.study_opponent),
|
||||
color = ink.copy(alpha = 0.6f),
|
||||
style = MaterialTheme.typography.titleMedium
|
||||
)
|
||||
TrainerPhase.WRONG -> Text(
|
||||
stringResource(R.string.study_wrong),
|
||||
color = Color(0xFFFF8A80),
|
||||
style = MaterialTheme.typography.titleMedium
|
||||
)
|
||||
TrainerPhase.COMPLETE -> {
|
||||
val result = state.result
|
||||
GlassCard(modifier = Modifier.fillMaxWidth()) {
|
||||
Text(
|
||||
stringResource(if (state.mode == SessionMode.LEARN) R.string.study_result_learned else R.string.study_result_review),
|
||||
color = ink,
|
||||
style = MaterialTheme.typography.titleLarge
|
||||
)
|
||||
Spacer(Modifier.height(6.dp))
|
||||
Stars(result?.stars ?: 0, size = 22.dp)
|
||||
Spacer(Modifier.height(6.dp))
|
||||
val change = state.starChange
|
||||
val stars = result?.stars ?: 0
|
||||
val (starText, starColor) = when {
|
||||
change != null && change.after > change.before ->
|
||||
stringResource(R.string.study_star_gained) to StarGold
|
||||
change != null && change.after < change.before ->
|
||||
stringResource(R.string.study_star_lost) to Color(0xFFFF8A80)
|
||||
stars >= 3 -> stringResource(R.string.study_all_stars) to StarGold
|
||||
state.mode == SessionMode.PRACTICE -> stringResource(R.string.study_practice_no_stars) to ink.copy(alpha = 0.65f)
|
||||
stars > 0 -> stringResource(R.string.study_reviews_to_next, result?.reviewsToNext ?: 0) to ink.copy(alpha = 0.8f)
|
||||
else -> "" to ink
|
||||
}
|
||||
if (starText.isNotEmpty()) {
|
||||
Text(starText, color = starColor, style = MaterialTheme.typography.bodyMedium)
|
||||
Spacer(Modifier.height(4.dp))
|
||||
}
|
||||
if (state.asked.isNotEmpty()) {
|
||||
Text(
|
||||
stringResource(R.string.study_result_first_try, state.firstTry, state.asked.size),
|
||||
color = ink.copy(alpha = 0.8f),
|
||||
style = MaterialTheme.typography.bodyMedium
|
||||
)
|
||||
}
|
||||
result?.nextDueAt?.let {
|
||||
Text(
|
||||
stringResource(R.string.study_next_review, formatDueIn(it)),
|
||||
color = ink.copy(alpha = 0.65f),
|
||||
style = MaterialTheme.typography.bodySmall
|
||||
)
|
||||
}
|
||||
}
|
||||
Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
GlassOutlinedButton(onClick = { viewModel.practiceAgain() }, modifier = Modifier.weight(1f)) {
|
||||
Text(stringResource(R.string.study_again), color = ink, maxLines = 1)
|
||||
}
|
||||
GlassButton(onClick = { if (!viewModel.continueStudy()) onBack() }, modifier = Modifier.weight(1f)) {
|
||||
Text(stringResource(R.string.study_next), color = ink, maxLines = 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (state.phase != TrainerPhase.COMPLETE) {
|
||||
Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
if (state.mode != SessionMode.LEARN) {
|
||||
GlassOutlinedButton(
|
||||
onClick = { viewModel.showHint() },
|
||||
enabled = state.phase == TrainerPhase.YOUR_MOVE,
|
||||
modifier = Modifier.weight(1f)
|
||||
) {
|
||||
ButtonIcon(R.drawable.ic_hint)
|
||||
Text(stringResource(R.string.puzzle_hint), color = ink, maxLines = 1)
|
||||
}
|
||||
}
|
||||
// A review is not restarted: it would only replay the moves still due.
|
||||
if (state.mode != SessionMode.REVIEW) {
|
||||
GlassOutlinedButton(onClick = { viewModel.restart() }, modifier = Modifier.weight(1f)) {
|
||||
Text(stringResource(R.string.study_restart), color = ink, maxLines = 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,294 @@
|
||||
package cc.skysparkle.matewave.ui.screens
|
||||
|
||||
import cc.skysparkle.matewave.ui.theme.ink
|
||||
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Switch
|
||||
import androidx.compose.material3.SwitchDefaults
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.height
|
||||
import cc.skysparkle.matewave.R
|
||||
import cc.skysparkle.matewave.settings.FeedbackSettings
|
||||
import cc.skysparkle.matewave.network.AppPermissions
|
||||
import cc.skysparkle.matewave.network.DisconnectReason
|
||||
import cc.skysparkle.matewave.network.NetworkLog
|
||||
import cc.skysparkle.matewave.network.NetworkRequirements
|
||||
import cc.skysparkle.matewave.ui.components.GlassCard
|
||||
import cc.skysparkle.matewave.ui.components.GlassOutlinedButton
|
||||
import cc.skysparkle.matewave.ui.components.GlassScaffold
|
||||
import cc.skysparkle.matewave.ui.components.rememberPermissionRequester
|
||||
|
||||
@Composable
|
||||
fun PermissionsScreen(onBack: () -> Unit) {
|
||||
val context = LocalContext.current
|
||||
|
||||
var tick by remember { mutableStateOf(0) }
|
||||
val status = remember(tick) { NetworkRequirements.check(context) }
|
||||
LaunchedEffect(Unit) {
|
||||
while (true) { kotlinx.coroutines.delay(2_000); tick++ }
|
||||
}
|
||||
|
||||
val requestNearby = rememberPermissionRequester(
|
||||
permissions = AppPermissions.nearby,
|
||||
rationaleTitle = stringResource(R.string.mesh_permission_title),
|
||||
rationaleText = stringResource(R.string.mesh_permission_text),
|
||||
onResult = { tick++ }
|
||||
)
|
||||
|
||||
GlassScaffold(title = stringResource(R.string.permissions_card_title), onBack = onBack) {
|
||||
PermissionItem(
|
||||
iconRes = R.drawable.ic_communication,
|
||||
granted = status.permissionsGranted,
|
||||
title = stringResource(R.string.perm_nearby_title),
|
||||
subtitle = stringResource(R.string.perm_nearby_text),
|
||||
onGrant = { requestNearby() }
|
||||
)
|
||||
|
||||
val presenceSettings = remember { cc.skysparkle.matewave.presence.PresenceSettings(context) }
|
||||
var announce by remember { mutableStateOf(presenceSettings.announceEnabled) }
|
||||
GlassCard(modifier = Modifier.fillMaxWidth()) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
cc.skysparkle.matewave.ui.components.ButtonIcon(R.drawable.ic_online, size = 24.dp)
|
||||
Column(modifier = Modifier.weight(1f)) {
|
||||
Text(
|
||||
stringResource(R.string.perm_announce_title),
|
||||
color = ink,
|
||||
style = MaterialTheme.typography.titleMedium
|
||||
)
|
||||
Text(
|
||||
stringResource(R.string.perm_announce_text),
|
||||
color = ink.copy(alpha = 0.7f),
|
||||
style = MaterialTheme.typography.bodySmall
|
||||
)
|
||||
}
|
||||
Switch(
|
||||
checked = announce,
|
||||
onCheckedChange = {
|
||||
announce = it
|
||||
presenceSettings.announceEnabled = it
|
||||
|
||||
cc.skysparkle.matewave.network.Transports.applyAnnounceSetting()
|
||||
},
|
||||
colors = SwitchDefaults.colors(
|
||||
checkedThumbColor = Color.White,
|
||||
checkedTrackColor = Color(0xFF4F8A5B)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
val feedbackSettings = remember { FeedbackSettings(context) }
|
||||
var vibration by remember { mutableStateOf(feedbackSettings.vibrationEnabled) }
|
||||
GlassCard(modifier = Modifier.fillMaxWidth()) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
cc.skysparkle.matewave.ui.components.ButtonIcon(R.drawable.ic_vibration, size = 24.dp)
|
||||
Column(modifier = Modifier.weight(1f)) {
|
||||
Text(
|
||||
stringResource(R.string.perm_vibration_title),
|
||||
color = ink,
|
||||
style = MaterialTheme.typography.titleMedium
|
||||
)
|
||||
Text(
|
||||
stringResource(R.string.perm_vibration_text),
|
||||
color = ink.copy(alpha = 0.7f),
|
||||
style = MaterialTheme.typography.bodySmall
|
||||
)
|
||||
}
|
||||
Switch(
|
||||
checked = vibration,
|
||||
onCheckedChange = {
|
||||
vibration = it
|
||||
feedbackSettings.vibrationEnabled = it
|
||||
},
|
||||
colors = SwitchDefaults.colors(
|
||||
checkedThumbColor = Color.White,
|
||||
checkedTrackColor = Color(0xFF4F8A5B)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
var background by remember { mutableStateOf(presenceSettings.backgroundEnabled) }
|
||||
val requestNotifications = rememberPermissionRequester(
|
||||
permissions = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {
|
||||
listOf(android.Manifest.permission.POST_NOTIFICATIONS)
|
||||
} else emptyList(),
|
||||
rationaleTitle = stringResource(R.string.perm_background_title),
|
||||
rationaleText = stringResource(R.string.perm_background_notification),
|
||||
onResult = { granted ->
|
||||
if (granted) {
|
||||
cc.skysparkle.matewave.presence.PresenceService.start(context)
|
||||
} else {
|
||||
background = false
|
||||
presenceSettings.backgroundEnabled = false
|
||||
}
|
||||
}
|
||||
)
|
||||
GlassCard(modifier = Modifier.fillMaxWidth()) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
cc.skysparkle.matewave.ui.components.ButtonIcon(R.drawable.ic_background_activity, size = 24.dp)
|
||||
Column(modifier = Modifier.weight(1f)) {
|
||||
Text(
|
||||
stringResource(R.string.perm_background_title),
|
||||
color = ink,
|
||||
style = MaterialTheme.typography.titleMedium
|
||||
)
|
||||
Text(
|
||||
stringResource(R.string.perm_background_text),
|
||||
color = ink.copy(alpha = 0.7f),
|
||||
style = MaterialTheme.typography.bodySmall
|
||||
)
|
||||
}
|
||||
Switch(
|
||||
checked = background,
|
||||
onCheckedChange = {
|
||||
background = it
|
||||
presenceSettings.backgroundEnabled = it
|
||||
if (it) {
|
||||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU &&
|
||||
!cc.skysparkle.matewave.network.AppPermissions.allGranted(
|
||||
context, listOf(android.Manifest.permission.POST_NOTIFICATIONS)
|
||||
)
|
||||
) {
|
||||
requestNotifications()
|
||||
} else {
|
||||
cc.skysparkle.matewave.presence.PresenceService.start(context)
|
||||
}
|
||||
} else {
|
||||
cc.skysparkle.matewave.presence.PresenceService.stop(context)
|
||||
}
|
||||
},
|
||||
colors = SwitchDefaults.colors(
|
||||
checkedThumbColor = Color.White,
|
||||
checkedTrackColor = Color(0xFF4F8A5B)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
if (background) {
|
||||
Spacer(Modifier.height(8.dp))
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
cc.skysparkle.matewave.ui.components.ButtonIcon(
|
||||
R.drawable.ic_notification, tint = Color(0xFFFFC46B), size = 16.dp
|
||||
)
|
||||
Text(
|
||||
stringResource(R.string.perm_background_notification),
|
||||
color = ink.copy(alpha = 0.7f),
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
modifier = Modifier.weight(1f)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (background && !cc.skysparkle.matewave.network.BatteryOptimization.isIgnoring(context)) {
|
||||
Spacer(Modifier.height(8.dp))
|
||||
Text(
|
||||
stringResource(R.string.perm_battery_needed),
|
||||
color = Color(0xFFFFC46B),
|
||||
style = MaterialTheme.typography.bodySmall
|
||||
)
|
||||
GlassOutlinedButton(
|
||||
onClick = { cc.skysparkle.matewave.network.BatteryOptimization.openSettings(context) },
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
cc.skysparkle.matewave.ui.components.ButtonIcon(R.drawable.ic_go_settings)
|
||||
Text(stringResource(R.string.perm_battery_open), color = ink)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val lastReason by NetworkLog.lastReason.collectAsState()
|
||||
if (lastReason != DisconnectReason.NONE) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
cc.skysparkle.matewave.ui.components.ButtonIcon(
|
||||
R.drawable.ic_error, tint = Color(0xFFFFC46B), size = 16.dp
|
||||
)
|
||||
Text(
|
||||
stringResource(R.string.diag_last_reason, stringResource(lastReason.textRes)),
|
||||
color = Color(0xFFFFC46B),
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
modifier = Modifier.weight(1f)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
val metricsTick by cc.skysparkle.matewave.network.NetworkMetrics.updated.collectAsState()
|
||||
val metricsText = remember(metricsTick) {
|
||||
cc.skysparkle.matewave.network.NetworkMetrics.asText(context.resources)
|
||||
}
|
||||
Text(
|
||||
metricsText.trim(),
|
||||
color = ink.copy(alpha = 0.7f),
|
||||
style = MaterialTheme.typography.bodySmall
|
||||
)
|
||||
|
||||
GlassOutlinedButton(
|
||||
onClick = {
|
||||
val clipboard = context.getSystemService(android.content.Context.CLIPBOARD_SERVICE)
|
||||
as android.content.ClipboardManager
|
||||
|
||||
val report = buildString {
|
||||
appendLine(context.getString(R.string.diag_report_counters))
|
||||
append(cc.skysparkle.matewave.network.NetworkMetrics.asText(context.resources))
|
||||
appendLine()
|
||||
appendLine(context.getString(R.string.diag_report_events))
|
||||
append(NetworkLog.asText().ifBlank { context.getString(R.string.diag_log_empty) })
|
||||
}
|
||||
clipboard.setPrimaryClip(
|
||||
android.content.ClipData.newPlainText("network report", report)
|
||||
)
|
||||
},
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
cc.skysparkle.matewave.ui.components.ButtonIcon(R.drawable.ic_copy)
|
||||
Text(stringResource(R.string.diag_copy_log), color = ink)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun PermissionItem(
|
||||
granted: Boolean,
|
||||
title: String,
|
||||
subtitle: String,
|
||||
onGrant: () -> Unit,
|
||||
iconRes: Int? = null
|
||||
) {
|
||||
GlassCard(modifier = Modifier.fillMaxWidth()) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
cc.skysparkle.matewave.ui.components.AppIcon(
|
||||
resId = if (granted) R.drawable.ic_approve else R.drawable.ic_cancel,
|
||||
size = 20.dp,
|
||||
tint = if (granted) Color(0xFF7BE495) else Color(0xFFFF6B6B)
|
||||
)
|
||||
if (iconRes != null) {
|
||||
cc.skysparkle.matewave.ui.components.AppIcon(iconRes, size = 22.dp)
|
||||
}
|
||||
Text(title, color = ink, style = MaterialTheme.typography.titleMedium)
|
||||
}
|
||||
Text(subtitle, color = ink.copy(alpha = 0.75f), style = MaterialTheme.typography.bodySmall)
|
||||
if (!granted) {
|
||||
GlassOutlinedButton(onClick = onGrant, modifier = Modifier.fillMaxWidth()) {
|
||||
Text(stringResource(R.string.perm_grant), color = ink)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,218 @@
|
||||
package cc.skysparkle.matewave.ui.screens
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.border
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.layout.widthIn
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.dp
|
||||
import cc.skysparkle.matewave.R
|
||||
import cc.skysparkle.matewave.engine.ChessEngine
|
||||
import cc.skysparkle.matewave.engine.Notation
|
||||
import cc.skysparkle.matewave.engine.PieceColor
|
||||
import cc.skysparkle.matewave.puzzles.Puzzle
|
||||
import cc.skysparkle.matewave.puzzles.PuzzleRepository
|
||||
import cc.skysparkle.matewave.puzzles.themeTitleRes
|
||||
import cc.skysparkle.matewave.stats.Stats
|
||||
import cc.skysparkle.matewave.ui.components.ButtonIcon
|
||||
import cc.skysparkle.matewave.ui.components.ChessBoardView
|
||||
import cc.skysparkle.matewave.ui.components.GlassButton
|
||||
import cc.skysparkle.matewave.ui.components.GlassOutlinedButton
|
||||
import cc.skysparkle.matewave.ui.components.GlassScaffold
|
||||
import cc.skysparkle.matewave.ui.components.PromotionDialog
|
||||
import cc.skysparkle.matewave.ui.theme.ChessGreen
|
||||
import cc.skysparkle.matewave.ui.theme.ink
|
||||
import cc.skysparkle.matewave.viewmodel.GameViewModel
|
||||
import kotlinx.coroutines.delay
|
||||
|
||||
private val WrongRed = Color(0xFFFF6B6B)
|
||||
|
||||
@Composable
|
||||
fun PuzzleSolveScreen(viewModel: GameViewModel, onBack: () -> Unit, onNextPuzzle: (Puzzle) -> Unit) {
|
||||
val context = LocalContext.current
|
||||
val state by viewModel.uiState.collectAsState()
|
||||
val puzzle = state.puzzle
|
||||
|
||||
// Confirmation on the board: the destination of a correct move flashes green; a wrong
|
||||
// move stays red until it is taken back.
|
||||
var greenFlash by remember { mutableStateOf<Int?>(null) }
|
||||
LaunchedEffect(state.puzzleMoveIndex, state.puzzleSolved) {
|
||||
if ((state.puzzleMoveIndex > 0 || state.puzzleSolved) && !state.puzzleFailedAttempt) {
|
||||
greenFlash = state.lastMove?.to
|
||||
delay(450)
|
||||
greenFlash = null
|
||||
}
|
||||
}
|
||||
val redSquare = if (state.puzzleFailedAttempt && state.puzzleAutoMoving) state.lastMove?.to else null
|
||||
|
||||
GlassScaffold(
|
||||
title = puzzle?.let { stringResource(R.string.puzzle_title_rating, it.rating) } ?: stringResource(R.string.puzzle_title_fallback),
|
||||
onBack = onBack,
|
||||
actions = {
|
||||
if (puzzle != null) {
|
||||
Text(
|
||||
stringResource(puzzle.themeTitleRes()).uppercase(),
|
||||
color = ChessGreen,
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
maxLines = 1,
|
||||
modifier = Modifier
|
||||
.padding(end = 12.dp)
|
||||
.clip(RoundedCornerShape(4.dp))
|
||||
.background(ChessGreen.copy(alpha = 0.15f))
|
||||
.padding(horizontal = 6.dp, vertical = 2.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
) {
|
||||
// On tablets the board is capped and centred, so the task and buttons fit without scrolling.
|
||||
ChessBoardView(
|
||||
board = state.board,
|
||||
selectedSquare = state.selectedSquare,
|
||||
legalTargets = state.legalTargetsFromSelected,
|
||||
kingInCheckSquare = if (ChessEngine.isInCheck(state.board, state.board.sideToMove)) {
|
||||
state.board.kingSquare(state.board.sideToMove)
|
||||
} else null,
|
||||
flipped = state.myColor == PieceColor.BLACK,
|
||||
onSquareClick = viewModel::onSquareClicked,
|
||||
modifier = Modifier.align(Alignment.CenterHorizontally).widthIn(max = 560.dp),
|
||||
lastMove = state.lastMove,
|
||||
hintSquare = state.puzzleHintFrom,
|
||||
interactionEnabled = state.puzzleStarted && !state.puzzleAutoMoving && !state.puzzleSolved && state.pendingPromotion == null,
|
||||
flashSquare = redSquare ?: greenFlash,
|
||||
flashColor = if (redSquare != null) WrongRed.copy(alpha = 0.55f) else ChessGreen.copy(alpha = 0.55f)
|
||||
)
|
||||
|
||||
// The task, in the same format as the status in a game: a dot in the side's colour.
|
||||
val (text, color) = when {
|
||||
state.puzzleSolved -> "✓ " + stringResource(R.string.puzzle_solved) to ChessGreen
|
||||
state.puzzleFailedAttempt -> "✗ " + stringResource(R.string.puzzle_wrong_move) to WrongRed
|
||||
state.puzzleAutoMoving -> stringResource(R.string.opponent_turn) to ink.copy(alpha = 0.7f)
|
||||
state.myColor == PieceColor.WHITE -> stringResource(R.string.puzzle_find_best_white) to ink
|
||||
else -> stringResource(R.string.puzzle_find_best_black) to ink
|
||||
}
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Box(
|
||||
Modifier
|
||||
.size(10.dp)
|
||||
.clip(CircleShape)
|
||||
.background(if (state.myColor == PieceColor.WHITE) Color(0xFFF0F0F0) else Color(0xFF222222))
|
||||
.border(1.dp, ink.copy(alpha = 0.5f), CircleShape)
|
||||
)
|
||||
Spacer(Modifier.width(10.dp))
|
||||
Text(text, color = color, style = MaterialTheme.typography.titleMedium)
|
||||
}
|
||||
|
||||
// Progress through multi-move puzzles: one dot per move of the solver.
|
||||
if (puzzle != null) {
|
||||
val total = (puzzle.solutionUci.size + 1) / 2
|
||||
if (total > 1) {
|
||||
val done = if (state.puzzleSolved) total else state.puzzleMoveIndex / 2
|
||||
Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(6.dp)) {
|
||||
repeat(total) { i ->
|
||||
Box(
|
||||
Modifier
|
||||
.size(8.dp)
|
||||
.clip(CircleShape)
|
||||
.background(if (i < done) ChessGreen else ink.copy(alpha = 0.2f))
|
||||
)
|
||||
}
|
||||
Spacer(Modifier.width(4.dp))
|
||||
Text(
|
||||
stringResource(R.string.puzzle_move_of, minOf(done + 1, total), total),
|
||||
color = ink.copy(alpha = 0.55f),
|
||||
style = MaterialTheme.typography.labelSmall
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (state.puzzleSolutionRevealed && !state.puzzleSolved) {
|
||||
val expectedUci = puzzle?.solutionUci?.getOrNull(state.puzzleMoveIndex)
|
||||
val san = expectedUci?.let { uci ->
|
||||
ChessEngine.legalMoves(state.board).firstOrNull { it.toUci() == uci }?.let { Notation.toSan(state.board, it, withCheck = true) }
|
||||
}
|
||||
Text(
|
||||
text = if (san != null) stringResource(R.string.puzzle_solution_prefix, san) else stringResource(R.string.puzzle_solution_unavailable),
|
||||
color = ink.copy(alpha = 0.85f),
|
||||
style = MaterialTheme.typography.bodyMedium
|
||||
)
|
||||
}
|
||||
|
||||
if (state.puzzleSolved) {
|
||||
val next = remember(puzzle?.id) {
|
||||
PuzzleRepository.pickForRating(
|
||||
context,
|
||||
cc.skysparkle.matewave.data.ProfileStore.get().elo,
|
||||
Stats.solvedPuzzles(),
|
||||
exclude = puzzle?.id
|
||||
)
|
||||
}
|
||||
if (next != null) {
|
||||
GlassButton(onClick = { onNextPuzzle(next) }, modifier = Modifier.fillMaxWidth()) {
|
||||
Text(stringResource(R.string.puzzle_next), color = ink, style = MaterialTheme.typography.titleMedium)
|
||||
}
|
||||
}
|
||||
Text(
|
||||
stringResource(R.string.puzzle_solved_count, Stats.solvedPuzzles().size),
|
||||
color = ink.copy(alpha = 0.55f),
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
textAlign = TextAlign.Center,
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
)
|
||||
} else if (state.puzzleStarted) {
|
||||
// The hint is the action; showing the solution is giving up, so it is a quiet link.
|
||||
GlassOutlinedButton(
|
||||
onClick = { viewModel.showPuzzleHint() },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
enabled = !state.puzzleAutoMoving
|
||||
) {
|
||||
ButtonIcon(R.drawable.ic_hint)
|
||||
Text(stringResource(R.string.puzzle_hint), color = ink)
|
||||
}
|
||||
Text(
|
||||
stringResource(R.string.puzzle_show_solution),
|
||||
color = ink.copy(alpha = if (state.puzzleAutoMoving) 0.3f else 0.6f),
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
textAlign = TextAlign.Center,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.clip(RoundedCornerShape(8.dp))
|
||||
.clickable(enabled = !state.puzzleAutoMoving) { viewModel.revealPuzzleSolution() }
|
||||
.padding(vertical = 8.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
state.pendingPromotion?.let {
|
||||
PromotionDialog(
|
||||
color = state.board.sideToMove,
|
||||
onChoose = viewModel::choosePromotion,
|
||||
onDismiss = viewModel::cancelPromotion
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,289 @@
|
||||
package cc.skysparkle.matewave.ui.screens
|
||||
|
||||
import cc.skysparkle.matewave.ui.components.ListDivider
|
||||
import cc.skysparkle.matewave.ui.components.ListGroup
|
||||
import cc.skysparkle.matewave.ui.components.ListRow
|
||||
import cc.skysparkle.matewave.ui.components.SectionCaption
|
||||
|
||||
import cc.skysparkle.matewave.ui.theme.ink
|
||||
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Switch
|
||||
import androidx.compose.material3.SwitchDefaults
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import cc.skysparkle.matewave.R
|
||||
import cc.skysparkle.matewave.ui.components.DialogAccent
|
||||
import cc.skysparkle.matewave.ui.components.DialogAction
|
||||
import cc.skysparkle.matewave.ui.components.AppDialog
|
||||
import cc.skysparkle.matewave.audio.RadioState
|
||||
import cc.skysparkle.matewave.audio.SoundManager
|
||||
import cc.skysparkle.matewave.ui.components.AppBottomBar
|
||||
import cc.skysparkle.matewave.ui.components.BottomTab
|
||||
import cc.skysparkle.matewave.ui.components.GlassScaffold
|
||||
import cc.skysparkle.matewave.ui.theme.ChessGreen
|
||||
|
||||
@Composable
|
||||
fun SettingsScreen(
|
||||
onHomeClick: () -> Unit,
|
||||
onFriendsClick: () -> Unit,
|
||||
onAccountClick: () -> Unit,
|
||||
onOpenPermissions: () -> Unit,
|
||||
onOpenAppearance: () -> Unit,
|
||||
onOpenLicenses: () -> Unit,
|
||||
onOpenShare: () -> Unit
|
||||
) {
|
||||
var musicOn by remember { mutableStateOf(SoundManager.musicEnabled) }
|
||||
var sfxOn by remember { mutableStateOf(SoundManager.sfxEnabled) }
|
||||
val context = LocalContext.current
|
||||
|
||||
val myProfile = remember { cc.skysparkle.matewave.data.ProfileStore.get() }
|
||||
val versionName = remember {
|
||||
runCatching {
|
||||
val pm = context.packageManager
|
||||
val info = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {
|
||||
pm.getPackageInfo(context.packageName, android.content.pm.PackageManager.PackageInfoFlags.of(0))
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
pm.getPackageInfo(context.packageName, 0)
|
||||
}
|
||||
info.versionName
|
||||
}.getOrNull() ?: "—"
|
||||
}
|
||||
|
||||
var showAbout by remember { mutableStateOf(false) }
|
||||
var statusTick by remember { mutableStateOf(0) }
|
||||
val status = remember(statusTick) {
|
||||
cc.skysparkle.matewave.network.NetworkRequirements.check(context)
|
||||
}
|
||||
LaunchedEffect(Unit) {
|
||||
while (true) { kotlinx.coroutines.delay(3_000); statusTick++ }
|
||||
}
|
||||
|
||||
val feedback = remember { cc.skysparkle.matewave.settings.FeedbackSettings(context) }
|
||||
val appearance = remember { cc.skysparkle.matewave.settings.BoardAppearance(context) }
|
||||
var vibrationOn by remember { mutableStateOf(feedback.vibrationEnabled) }
|
||||
var openingBarOn by remember { mutableStateOf(appearance.showOpening) }
|
||||
val radioState by SoundManager.radioState.collectAsState()
|
||||
|
||||
GlassScaffold(
|
||||
title = stringResource(R.string.nav_settings),
|
||||
onBack = null,
|
||||
bottomBar = {
|
||||
AppBottomBar(
|
||||
current = BottomTab.SETTINGS,
|
||||
onHomeClick = onHomeClick,
|
||||
onFriendsClick = onFriendsClick,
|
||||
onSettingsClick = { },
|
||||
onAccountClick = onAccountClick,
|
||||
accountAvatarBase64 = myProfile.avatarBase64
|
||||
)
|
||||
}
|
||||
) {
|
||||
SectionCaption(stringResource(R.string.settings_section_sound))
|
||||
ListGroup {
|
||||
ListRow(
|
||||
title = stringResource(R.string.settings_radio),
|
||||
subtitle = stringResource(R.string.settings_radio_subtitle),
|
||||
icon = R.drawable.ic_music,
|
||||
// Radio status sits in the row instead of a stray line under it.
|
||||
value = if (!musicOn) null else when (radioState) {
|
||||
RadioState.CONNECTING -> stringResource(R.string.settings_radio_connecting)
|
||||
RadioState.ERROR -> stringResource(R.string.settings_radio_error)
|
||||
else -> null
|
||||
},
|
||||
valueColor = if (radioState == RadioState.ERROR) Color(0xFFFF6B6B) else ink.copy(alpha = 0.55f),
|
||||
trailing = { SettingSwitch(musicOn) { musicOn = it; SoundManager.setMusicEnabled(it) } }
|
||||
)
|
||||
ListDivider()
|
||||
ListRow(
|
||||
title = stringResource(R.string.settings_sfx),
|
||||
subtitle = stringResource(R.string.settings_sfx_subtitle),
|
||||
icon = R.drawable.ic_sound_effect,
|
||||
trailing = { SettingSwitch(sfxOn) { sfxOn = it; SoundManager.setSfxEnabled(it) } }
|
||||
)
|
||||
ListDivider()
|
||||
ListRow(
|
||||
title = stringResource(R.string.perm_vibration_title),
|
||||
icon = R.drawable.ic_vibration,
|
||||
trailing = { SettingSwitch(vibrationOn) { vibrationOn = it; feedback.vibrationEnabled = it } }
|
||||
)
|
||||
}
|
||||
|
||||
SectionCaption(stringResource(R.string.settings_section_game))
|
||||
ListGroup {
|
||||
ListRow(
|
||||
title = stringResource(R.string.appearance_title),
|
||||
subtitle = stringResource(R.string.appearance_subtitle),
|
||||
icon = R.drawable.ic_board_view,
|
||||
chevron = true,
|
||||
onClick = onOpenAppearance
|
||||
)
|
||||
ListDivider()
|
||||
ListRow(
|
||||
title = stringResource(R.string.appearance_opening_in_game),
|
||||
icon = R.drawable.ic_book,
|
||||
trailing = { SettingSwitch(openingBarOn) { openingBarOn = it; appearance.showOpening = it } }
|
||||
)
|
||||
}
|
||||
|
||||
SectionCaption(stringResource(R.string.settings_section_system))
|
||||
ListGroup {
|
||||
ListRow(
|
||||
title = stringResource(R.string.permissions_card_title),
|
||||
icon = if (status.onlineBlocked) R.drawable.ic_permissions else R.drawable.ic_check_permission,
|
||||
iconTint = if (status.onlineBlocked) Color(0xFFFF6B6B) else ChessGreen,
|
||||
value = stringResource(if (status.onlineBlocked) R.string.permissions_card_blocked_short else R.string.permissions_card_ok_short),
|
||||
valueColor = if (status.onlineBlocked) Color(0xFFFF6B6B) else ink.copy(alpha = 0.55f),
|
||||
chevron = true,
|
||||
onClick = onOpenPermissions
|
||||
)
|
||||
ListDivider()
|
||||
ListRow(
|
||||
title = stringResource(R.string.privacy_policy),
|
||||
icon = R.drawable.ic_policy,
|
||||
chevron = true,
|
||||
onClick = { cc.skysparkle.matewave.settings.AppLinks.open(context, cc.skysparkle.matewave.settings.AppLinks.PRIVACY_POLICY) }
|
||||
)
|
||||
ListDivider()
|
||||
ListRow(
|
||||
title = stringResource(R.string.about_section),
|
||||
icon = R.drawable.ic_info,
|
||||
value = versionName,
|
||||
chevron = true,
|
||||
onClick = { showAbout = true }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (showAbout) {
|
||||
AppDialog(
|
||||
onDismissRequest = { showAbout = false },
|
||||
title = stringResource(R.string.about_section),
|
||||
actions = { DialogAction(stringResource(R.string.chat_close), onClick = { showAbout = false }) }
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.spacedBy(6.dp)
|
||||
) {
|
||||
Image(
|
||||
painter = painterResource(R.drawable.ic_launcher_card),
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(72.dp).clip(RoundedCornerShape(16.dp))
|
||||
)
|
||||
Text(
|
||||
stringResource(R.string.app_title),
|
||||
color = ink,
|
||||
style = MaterialTheme.typography.headlineSmall
|
||||
)
|
||||
Text(
|
||||
stringResource(R.string.settings_version, versionName),
|
||||
color = DialogAccent,
|
||||
style = MaterialTheme.typography.bodySmall
|
||||
)
|
||||
Text(
|
||||
stringResource(R.string.settings_description),
|
||||
color = ink.copy(alpha = 0.78f),
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
textAlign = androidx.compose.ui.text.style.TextAlign.Center
|
||||
)
|
||||
}
|
||||
|
||||
AboutRow(
|
||||
iconRes = R.drawable.ic_share,
|
||||
title = stringResource(R.string.share_row_title),
|
||||
subtitle = stringResource(R.string.share_row_subtitle),
|
||||
onClick = {
|
||||
showAbout = false
|
||||
onOpenShare()
|
||||
}
|
||||
)
|
||||
AboutRow(
|
||||
iconRes = R.drawable.ic_copyright,
|
||||
title = stringResource(R.string.licenses_title),
|
||||
subtitle = stringResource(R.string.licenses_subtitle),
|
||||
onClick = {
|
||||
showAbout = false
|
||||
onOpenLicenses()
|
||||
}
|
||||
)
|
||||
AboutRow(
|
||||
iconRes = R.drawable.ic_repo,
|
||||
title = stringResource(R.string.settings_repo_title),
|
||||
subtitle = cc.skysparkle.matewave.settings.AppLinks.REPOSITORY_LABEL,
|
||||
onClick = { cc.skysparkle.matewave.settings.AppLinks.open(context, cc.skysparkle.matewave.settings.AppLinks.REPOSITORY) }
|
||||
)
|
||||
AboutRow(
|
||||
iconRes = R.drawable.ic_policy,
|
||||
title = stringResource(R.string.privacy_policy),
|
||||
subtitle = "skysparkle.cc/privacy",
|
||||
onClick = { cc.skysparkle.matewave.settings.AppLinks.open(context, cc.skysparkle.matewave.settings.AppLinks.PRIVACY_POLICY) }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun AboutRow(iconRes: Int, title: String, subtitle: String, onClick: () -> Unit) {
|
||||
val rowShape = RoundedCornerShape(12.dp)
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.clip(rowShape)
|
||||
.background(ink.copy(alpha = 0.07f))
|
||||
.clickable(onClick = onClick)
|
||||
.padding(horizontal = 14.dp, vertical = 12.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
cc.skysparkle.matewave.ui.components.AppIcon(iconRes, size = 28.dp)
|
||||
Spacer(Modifier.width(14.dp))
|
||||
Column(modifier = Modifier.weight(1f)) {
|
||||
Text(title, color = ink, style = MaterialTheme.typography.titleMedium)
|
||||
Text(subtitle, color = ink.copy(alpha = 0.65f), style = MaterialTheme.typography.bodySmall)
|
||||
}
|
||||
cc.skysparkle.matewave.ui.components.AppIcon(
|
||||
R.drawable.ic_arrow_forward, size = 16.dp, tint = ink.copy(alpha = 0.6f)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun SettingSwitch(checked: Boolean, onCheckedChange: (Boolean) -> Unit) {
|
||||
Switch(
|
||||
checked = checked,
|
||||
onCheckedChange = onCheckedChange,
|
||||
colors = SwitchDefaults.colors(
|
||||
checkedTrackColor = ChessGreen,
|
||||
checkedThumbColor = Color.White,
|
||||
uncheckedTrackColor = ink.copy(alpha = 0.15f),
|
||||
uncheckedThumbColor = Color.White.copy(alpha = 0.7f)
|
||||
)
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,238 @@
|
||||
package cc.skysparkle.matewave.ui.screens
|
||||
|
||||
import cc.skysparkle.matewave.ui.theme.ink
|
||||
|
||||
import android.content.Intent
|
||||
import android.text.format.Formatter
|
||||
import androidx.compose.foundation.Canvas
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.HorizontalDivider
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.DisposableEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.geometry.Offset
|
||||
import androidx.compose.ui.geometry.Size
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.platform.LocalView
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.dp
|
||||
import cc.skysparkle.matewave.R
|
||||
import cc.skysparkle.matewave.share.ApkShareServer
|
||||
import cc.skysparkle.matewave.share.ApkSharing
|
||||
import cc.skysparkle.matewave.share.LocalNetwork
|
||||
import cc.skysparkle.matewave.ui.components.AppIcon
|
||||
import cc.skysparkle.matewave.ui.components.ButtonIcon
|
||||
import cc.skysparkle.matewave.ui.components.DialogAccent
|
||||
import cc.skysparkle.matewave.ui.components.GlassCard
|
||||
import cc.skysparkle.matewave.ui.components.GlassOutlinedButton
|
||||
import cc.skysparkle.matewave.ui.components.GlassScaffold
|
||||
import com.google.zxing.BarcodeFormat
|
||||
import com.google.zxing.EncodeHintType
|
||||
import com.google.zxing.qrcode.QRCodeWriter
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@Composable
|
||||
fun ShareApkScreen(onBack: () -> Unit) {
|
||||
val context = LocalContext.current
|
||||
val scope = rememberCoroutineScope()
|
||||
val view = LocalView.current
|
||||
val appName = stringResource(R.string.app_title)
|
||||
val apk = remember { ApkSharing.apkFile(context) }
|
||||
var url by remember { mutableStateOf<String?>(null) }
|
||||
var preparing by remember { mutableStateOf(false) }
|
||||
var failed by remember { mutableStateOf(false) }
|
||||
|
||||
DisposableEffect(Unit) {
|
||||
val server = ApkShareServer(apk, appName)
|
||||
val ip = LocalNetwork.wifiIpv4()
|
||||
if (ip != null && apk.exists()) {
|
||||
val port = runCatching { server.start() }.getOrNull()
|
||||
if (port != null) url = "http://$ip:$port/"
|
||||
}
|
||||
view.keepScreenOn = true
|
||||
onDispose {
|
||||
server.stop()
|
||||
view.keepScreenOn = false
|
||||
}
|
||||
}
|
||||
|
||||
GlassScaffold(title = stringResource(R.string.share_row_title), onBack = onBack) {
|
||||
Column(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.spacedBy(14.dp)
|
||||
) {
|
||||
AppIcon(R.drawable.ic_wifi, size = 44.dp, tint = DialogAccent)
|
||||
Text(
|
||||
stringResource(R.string.share_wifi_title),
|
||||
color = ink,
|
||||
style = MaterialTheme.typography.headlineSmall
|
||||
)
|
||||
Text(
|
||||
stringResource(R.string.share_wifi_subtitle),
|
||||
color = ink.copy(alpha = 0.75f),
|
||||
style = MaterialTheme.typography.bodyLarge,
|
||||
textAlign = TextAlign.Center
|
||||
)
|
||||
|
||||
val address = url
|
||||
if (address != null) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.size(240.dp)
|
||||
.clip(RoundedCornerShape(20.dp))
|
||||
|
||||
.background(Color.White)
|
||||
.padding(18.dp)
|
||||
) {
|
||||
QrCode(address, Modifier.size(204.dp))
|
||||
}
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.clip(RoundedCornerShape(12.dp))
|
||||
.background(ink.copy(alpha = 0.10f))
|
||||
.padding(vertical = 14.dp, horizontal = 12.dp),
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
Text(address, color = ink.copy(alpha = 0.85f), style = MaterialTheme.typography.bodyMedium)
|
||||
}
|
||||
} else {
|
||||
Text(
|
||||
stringResource(R.string.share_no_wifi),
|
||||
color = Color(0xFFFFC46B),
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
textAlign = TextAlign.Center
|
||||
)
|
||||
}
|
||||
|
||||
Text(
|
||||
stringResource(R.string.share_apk_size, Formatter.formatShortFileSize(context, apk.length())),
|
||||
color = ink.copy(alpha = 0.7f),
|
||||
style = MaterialTheme.typography.bodyMedium
|
||||
)
|
||||
if (ApkSharing.isSplitInstall(context)) {
|
||||
Text(
|
||||
stringResource(R.string.share_split_warning),
|
||||
color = Color(0xFFFFC46B),
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
textAlign = TextAlign.Center
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
GlassCard(modifier = Modifier.fillMaxWidth()) {
|
||||
Text(
|
||||
stringResource(R.string.share_instructions_title),
|
||||
color = ink,
|
||||
style = MaterialTheme.typography.titleMedium
|
||||
)
|
||||
Column(modifier = Modifier.padding(top = 8.dp), verticalArrangement = Arrangement.spacedBy(6.dp)) {
|
||||
listOf(
|
||||
R.string.share_step_1, R.string.share_step_2, R.string.share_step_3, R.string.share_step_4
|
||||
).forEachIndexed { index, step ->
|
||||
Text(
|
||||
"${index + 1}. ${stringResource(step)}",
|
||||
color = ink.copy(alpha = 0.85f),
|
||||
style = MaterialTheme.typography.bodyMedium
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
HorizontalDivider(color = ink.copy(alpha = 0.15f))
|
||||
|
||||
Column(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.spacedBy(10.dp)
|
||||
) {
|
||||
Text(
|
||||
stringResource(R.string.share_other_title),
|
||||
color = ink,
|
||||
style = MaterialTheme.typography.titleMedium
|
||||
)
|
||||
Text(
|
||||
stringResource(R.string.share_other_hint),
|
||||
color = ink.copy(alpha = 0.7f),
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
textAlign = TextAlign.Center
|
||||
)
|
||||
}
|
||||
|
||||
GlassOutlinedButton(
|
||||
onClick = {
|
||||
if (!preparing) {
|
||||
preparing = true
|
||||
failed = false
|
||||
scope.launch {
|
||||
val uri = runCatching { ApkSharing.copyForSharing(context, appName) }.getOrNull()
|
||||
preparing = false
|
||||
if (uri == null) {
|
||||
failed = true
|
||||
} else {
|
||||
val send = Intent(Intent.ACTION_SEND).apply {
|
||||
type = "application/vnd.android.package-archive"
|
||||
putExtra(Intent.EXTRA_STREAM, uri)
|
||||
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||
}
|
||||
context.startActivity(Intent.createChooser(send, null))
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
ButtonIcon(R.drawable.ic_share)
|
||||
Text(
|
||||
stringResource(if (preparing) R.string.share_preparing else R.string.share_button),
|
||||
color = ink
|
||||
)
|
||||
}
|
||||
if (failed) {
|
||||
Text(
|
||||
stringResource(R.string.share_failed),
|
||||
color = Color(0xFFFF6B6B),
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
textAlign = TextAlign.Center,
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun QrCode(text: String, modifier: Modifier = Modifier) {
|
||||
val matrix = remember(text) {
|
||||
QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, 0, 0, mapOf(EncodeHintType.MARGIN to 0))
|
||||
}
|
||||
Canvas(modifier = modifier) {
|
||||
val modules = matrix.width
|
||||
val cell = size.width / modules
|
||||
for (y in 0 until modules) {
|
||||
for (x in 0 until modules) {
|
||||
if (matrix.get(x, y)) {
|
||||
drawRect(Color.Black, Offset(x * cell, y * cell), Size(cell + 0.6f, cell + 0.6f))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
package cc.skysparkle.matewave.ui.screens
|
||||
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import cc.skysparkle.matewave.ui.components.AppIcon
|
||||
import cc.skysparkle.matewave.ui.components.SectionCaption
|
||||
import cc.skysparkle.matewave.ui.components.TimeControlGrid
|
||||
import cc.skysparkle.matewave.ui.components.displayName
|
||||
|
||||
import cc.skysparkle.matewave.ui.theme.ink
|
||||
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.border
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.draw.clip
|
||||
import cc.skysparkle.matewave.ui.components.SetupSegmented
|
||||
import cc.skysparkle.matewave.ui.theme.ChessGreen
|
||||
import cc.skysparkle.matewave.ui.theme.PanelBorder
|
||||
import cc.skysparkle.matewave.R
|
||||
import cc.skysparkle.matewave.ai.AiDifficulty
|
||||
import cc.skysparkle.matewave.clock.TimeControl
|
||||
import cc.skysparkle.matewave.engine.PieceColor
|
||||
import cc.skysparkle.matewave.ui.components.GlassButton
|
||||
import cc.skysparkle.matewave.ui.components.GlassScaffold
|
||||
|
||||
@Composable
|
||||
private fun difficultyLabel(difficulty: AiDifficulty): String = stringResource(difficultyName(difficulty))
|
||||
|
||||
@Composable
|
||||
fun VsAiSetupScreen(
|
||||
onBack: () -> Unit,
|
||||
onStart: (AiDifficulty, PieceColor?, TimeControl) -> Unit
|
||||
) {
|
||||
val context = androidx.compose.ui.platform.LocalContext.current
|
||||
val last = remember { cc.skysparkle.matewave.settings.LastGameSetup(context) }
|
||||
var difficulty by remember { mutableStateOf(last.aiDifficulty) }
|
||||
var color by remember { mutableStateOf(last.aiColor) }
|
||||
var timeControl by remember { mutableStateOf(last.aiTime) }
|
||||
val levels = AiDifficulty.entries
|
||||
|
||||
GlassScaffold(
|
||||
title = stringResource(R.string.vsai_title),
|
||||
onBack = onBack,
|
||||
// The start button stays at the bottom, in reach of the thumb, with a summary of the choice.
|
||||
bottomBar = {
|
||||
Box(modifier = Modifier.padding(horizontal = 20.dp, vertical = 12.dp)) {
|
||||
GlassButton(
|
||||
onClick = {
|
||||
last.aiDifficulty = difficulty
|
||||
last.aiColor = color
|
||||
last.aiTime = timeControl
|
||||
onStart(difficulty, color, timeControl)
|
||||
},
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
Text(
|
||||
stringResource(R.string.vsai_play_summary, difficultyLabel(difficulty), timeControl.displayName()),
|
||||
color = ink,
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
maxLines = 1
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
) {
|
||||
// Who you are playing: changes with the level.
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Box(
|
||||
modifier = Modifier.size(52.dp).clip(CircleShape).background(ink.copy(alpha = 0.10f)),
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
AppIcon(R.drawable.ic_ai_app, size = 28.dp)
|
||||
}
|
||||
Spacer(Modifier.width(14.dp))
|
||||
Column(modifier = Modifier.weight(1f)) {
|
||||
Text(
|
||||
stringResource(R.string.game_ai_name, difficultyLabel(difficulty)) + " ≈" +
|
||||
cc.skysparkle.matewave.data.ProfileStore.aiNominalElo(difficulty),
|
||||
color = ink,
|
||||
style = MaterialTheme.typography.titleMedium
|
||||
)
|
||||
Text(stringResource(aiDescription(difficulty)), color = ink.copy(alpha = 0.65f), style = MaterialTheme.typography.bodySmall)
|
||||
}
|
||||
}
|
||||
|
||||
SectionCaption(stringResource(R.string.difficulty_label))
|
||||
SetupSegmented(
|
||||
options = levels.map { difficultyLabel(it) },
|
||||
selectedIndex = levels.indexOf(difficulty)
|
||||
) { difficulty = levels[it] }
|
||||
Row(modifier = Modifier.fillMaxWidth()) {
|
||||
levels.forEach {
|
||||
Text(
|
||||
"≈" + cc.skysparkle.matewave.data.ProfileStore.aiNominalElo(it),
|
||||
color = ink.copy(alpha = if (it == difficulty) 0.8f else 0.45f),
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
textAlign = androidx.compose.ui.text.style.TextAlign.Center,
|
||||
modifier = Modifier.weight(1f)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
SectionCaption(stringResource(R.string.your_color))
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
ColorChip(R.drawable.ic_white_colour, stringResource(R.string.color_white), color == PieceColor.WHITE) { color = PieceColor.WHITE }
|
||||
ColorChip(R.drawable.ic_black_colour, stringResource(R.string.color_black), color == PieceColor.BLACK) { color = PieceColor.BLACK }
|
||||
ColorChip(R.drawable.ic_random_colour, stringResource(R.string.color_random), color == null) { color = null }
|
||||
}
|
||||
|
||||
SectionCaption(stringResource(R.string.time_control_label))
|
||||
TimeControlGrid(selected = timeControl, onSelect = { timeControl = it })
|
||||
}
|
||||
}
|
||||
|
||||
private fun aiDescription(d: AiDifficulty): Int = when (d) {
|
||||
AiDifficulty.EASY -> R.string.ai_desc_easy
|
||||
AiDifficulty.MEDIUM -> R.string.ai_desc_medium
|
||||
AiDifficulty.HARD -> R.string.ai_desc_hard
|
||||
AiDifficulty.EXPERT -> R.string.ai_desc_expert
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun ColorChip(iconRes: Int, text: String, selected: Boolean, onClick: () -> Unit) {
|
||||
val shape = RoundedCornerShape(50)
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.clip(shape)
|
||||
.background(if (selected) ChessGreen.copy(alpha = 0.35f) else ink.copy(alpha = 0.10f))
|
||||
.border(1.dp, if (selected) ChessGreen else PanelBorder, shape)
|
||||
.clickable(onClick = onClick)
|
||||
.padding(horizontal = 14.dp, vertical = 8.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
Image(
|
||||
painter = painterResource(iconRes),
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(16.dp)
|
||||
)
|
||||
Text(text, color = ink, style = MaterialTheme.typography.bodySmall)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package cc.skysparkle.matewave.ui.theme
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Brush
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.Shape
|
||||
import androidx.compose.ui.unit.dp
|
||||
|
||||
val PanelFill = Color(0xFF14171C).copy(alpha = 0.86f)
|
||||
val PanelBorder = Color.White.copy(alpha = 0.10f)
|
||||
|
||||
val UiCornerRadius = 8.dp
|
||||
val GlassShape: Shape = RoundedCornerShape(UiCornerRadius)
|
||||
|
||||
val TabletMaxContentWidth = 600.dp
|
||||
|
||||
fun Modifier.glass(shape: Shape = GlassShape): Modifier = this
|
||||
.clip(shape)
|
||||
.background(PanelFill)
|
||||
|
||||
val GlassBorder = PanelBorder
|
||||
|
||||
val PhotoScrim = Brush.verticalGradient(
|
||||
0.00f to Color.Black.copy(alpha = 0.84f),
|
||||
0.10f to Color.Black.copy(alpha = 0.72f),
|
||||
0.22f to Color.Black.copy(alpha = 0.58f),
|
||||
0.38f to Color.Black.copy(alpha = 0.48f),
|
||||
0.55f to Color.Black.copy(alpha = 0.47f),
|
||||
0.72f to Color.Black.copy(alpha = 0.55f),
|
||||
0.88f to Color.Black.copy(alpha = 0.68f),
|
||||
1.00f to Color.Black.copy(alpha = 0.80f)
|
||||
)
|
||||
|
||||
val DialogScrimColor = Color.Black.copy(alpha = 0.62f)
|
||||
|
||||
val FloatingBarFill = Color.Black.copy(alpha = 0.25f)
|
||||
@@ -0,0 +1,13 @@
|
||||
package cc.skysparkle.matewave.ui.theme
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.ReadOnlyComposable
|
||||
import androidx.compose.runtime.compositionLocalOf
|
||||
import androidx.compose.ui.graphics.Color
|
||||
|
||||
val LocalInk = compositionLocalOf { Color.White }
|
||||
|
||||
val ink: Color
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
get() = LocalInk.current
|
||||
@@ -0,0 +1,41 @@
|
||||
package cc.skysparkle.matewave.ui.theme
|
||||
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedTextFieldDefaults
|
||||
import androidx.compose.material3.darkColorScheme
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.CompositionLocalProvider
|
||||
import androidx.compose.ui.graphics.Color
|
||||
|
||||
val ChessGreen = Color(0xFF3E7C4A)
|
||||
val ChessGreenDark = Color(0xFF1B4D2E)
|
||||
val HighlightMove = Color(0x8858A6FF)
|
||||
val HighlightCheck = Color(0x88E53935)
|
||||
val HighlightLastMove = Color(0x664FC3F7)
|
||||
val HighlightHint = Color(0x88FFC107)
|
||||
|
||||
private val DarkScheme = darkColorScheme(
|
||||
primary = ChessGreen,
|
||||
secondary = ChessGreenDark,
|
||||
background = Color(0xFF121212),
|
||||
surface = Color(0xFF1E1E1E)
|
||||
)
|
||||
|
||||
/** The app has a single dark theme; system bars are set up in MainActivity. */
|
||||
@Composable
|
||||
fun MatewaveTheme(content: @Composable () -> Unit) {
|
||||
MaterialTheme(colorScheme = DarkScheme) {
|
||||
CompositionLocalProvider(LocalInk provides Color.White, content = content)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun whiteOutlinedTextFieldColors() = OutlinedTextFieldDefaults.colors(
|
||||
focusedTextColor = ink,
|
||||
unfocusedTextColor = ink,
|
||||
focusedBorderColor = ink.copy(alpha = 0.6f),
|
||||
unfocusedBorderColor = ink.copy(alpha = 0.3f),
|
||||
focusedLabelColor = ink.copy(alpha = 0.8f),
|
||||
unfocusedLabelColor = ink.copy(alpha = 0.6f),
|
||||
cursorColor = ink
|
||||
)
|
||||
Reference in New Issue
Block a user