| 1 |
package io.oxygenhub.android.ui.account |
| 2 |
|
| 3 |
import androidx.compose.foundation.layout.Arrangement |
| 4 |
import androidx.compose.foundation.layout.Box |
| 5 |
import androidx.compose.foundation.layout.Column |
| 6 |
import androidx.compose.foundation.layout.Spacer |
| 7 |
import androidx.compose.foundation.layout.fillMaxWidth |
| 8 |
import androidx.compose.foundation.layout.height |
| 9 |
import androidx.compose.foundation.layout.padding |
| 10 |
import androidx.compose.foundation.layout.size |
| 11 |
import androidx.compose.foundation.layout.widthIn |
| 12 |
import androidx.compose.foundation.rememberScrollState |
| 13 |
import androidx.compose.foundation.verticalScroll |
| 14 |
import androidx.compose.material.icons.Icons |
| 15 |
import androidx.compose.material.icons.automirrored.outlined.Logout |
| 16 |
import androidx.compose.material.icons.outlined.Dns |
| 17 |
import androidx.compose.material.icons.outlined.Key |
| 18 |
import androidx.compose.material.icons.outlined.Memory |
| 19 |
import androidx.compose.material3.AlertDialog |
| 20 |
import androidx.compose.material3.Card |
| 21 |
import androidx.compose.material3.CircularProgressIndicator |
| 22 |
import androidx.compose.material3.Icon |
| 23 |
import androidx.compose.material3.ListItem |
| 24 |
import androidx.compose.material3.MaterialTheme |
| 25 |
import androidx.compose.material3.OutlinedButton |
| 26 |
import androidx.compose.material3.Text |
| 27 |
import androidx.compose.material3.TextButton |
| 28 |
import androidx.compose.runtime.Composable |
| 29 |
import androidx.compose.runtime.getValue |
| 30 |
import androidx.compose.runtime.mutableStateOf |
| 31 |
import androidx.compose.runtime.remember |
| 32 |
import androidx.compose.runtime.rememberCoroutineScope |
| 33 |
import androidx.compose.runtime.setValue |
| 34 |
import androidx.compose.ui.Alignment |
| 35 |
import androidx.compose.ui.Modifier |
| 36 |
import androidx.compose.ui.text.font.FontWeight |
| 37 |
import androidx.compose.ui.unit.dp |
| 38 |
import io.oxygenhub.android.auth.Session |
| 39 |
import io.oxygenhub.android.api.Avatar |
| 40 |
import io.oxygenhub.android.api.HubApi |
| 41 |
import io.oxygenhub.android.runner.RunnerCard |
| 42 |
import io.oxygenhub.android.ui.common.AppScreen |
| 43 |
import io.oxygenhub.android.ui.common.AvatarImage |
| 44 |
import kotlinx.coroutines.launch |
| 45 |
|
| 46 |
/** Who is signed in, to which hub, with what; and signing out. */ |
| 47 |
@Composable |
| 48 |
fun AccountScreen(session: Session, avatar: Avatar?, api: HubApi, nativeCore: () -> String, onSignOut: suspend () -> Unit) { |
| 49 |
var confirming by remember { mutableStateOf(false) } |
| 50 |
var signingOut by remember { mutableStateOf(false) } |
| 51 |
val scope = rememberCoroutineScope() |
| 52 |
val core = remember { runCatching(nativeCore).getOrElse { "unavailable: ${it.message}" } } |
| 53 |
|
| 54 |
AppScreen(title = "Account") { |
| 55 |
Box(Modifier.fillMaxWidth(), contentAlignment = Alignment.TopCenter) { |
| 56 |
Column( |
| 57 |
Modifier.widthIn(max = 600.dp).verticalScroll(rememberScrollState()).padding(16.dp), |
| 58 |
verticalArrangement = Arrangement.spacedBy(16.dp), |
| 59 |
) { |
| 60 |
Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) { |
| 61 |
AvatarImage(avatar, session.username, size = 72.dp) |
| 62 |
Spacer(Modifier.height(8.dp)) |
| 63 |
Text(session.username, style = MaterialTheme.typography.titleLarge, fontWeight = FontWeight.Bold) |
| 64 |
} |
| 65 |
RunnerCard(api, session.hub, session.canWrite) |
| 66 |
Card(Modifier.fillMaxWidth()) { |
| 67 |
ListItem( |
| 68 |
leadingContent = { Icon(Icons.Outlined.Dns, contentDescription = null) }, |
| 69 |
headlineContent = { Text("Hub") }, |
| 70 |
supportingContent = { Text(session.hub) }, |
| 71 |
) |
| 72 |
ListItem( |
| 73 |
leadingContent = { Icon(Icons.Outlined.Key, contentDescription = null) }, |
| 74 |
headlineContent = { Text("Allowed") }, |
| 75 |
supportingContent = { Text(session.scopes.ifEmpty { listOf("(the hub did not say)") }.joinToString(", ")) }, |
| 76 |
) |
| 77 |
ListItem( |
| 78 |
leadingContent = { Icon(Icons.Outlined.Memory, contentDescription = null) }, |
| 79 |
headlineContent = { Text("Native core") }, |
| 80 |
supportingContent = { Text(core) }, |
| 81 |
) |
| 82 |
} |
| 83 |
Text( |
| 84 |
"This phone's sign-in is a device token, kept encrypted with a key in the Android Keystore. " + |
| 85 |
"It lasts while it is used, and ends after 90 days without use. Signing out revokes it on the hub.", |
| 86 |
style = MaterialTheme.typography.bodySmall, |
| 87 |
color = MaterialTheme.colorScheme.onSurfaceVariant, |
| 88 |
) |
| 89 |
OutlinedButton(onClick = { confirming = true }, enabled = !signingOut, modifier = Modifier.fillMaxWidth()) { |
| 90 |
if (signingOut) { |
| 91 |
CircularProgressIndicator(Modifier.size(18.dp), strokeWidth = 2.dp) |
| 92 |
} else { |
| 93 |
Icon(Icons.AutoMirrored.Outlined.Logout, contentDescription = null) |
| 94 |
} |
| 95 |
Spacer(Modifier.size(8.dp)) |
| 96 |
Text("Sign out") |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
if (confirming) { |
| 103 |
AlertDialog( |
| 104 |
onDismissRequest = { confirming = false }, |
| 105 |
title = { Text("Sign out?") }, |
| 106 |
text = { Text("The token this phone uses is revoked on ${session.hub}. You can sign in again at any time.") }, |
| 107 |
confirmButton = { |
| 108 |
TextButton(onClick = { |
| 109 |
confirming = false |
| 110 |
signingOut = true |
| 111 |
scope.launch { onSignOut() } |
| 112 |
}) { Text("Sign out") } |
| 113 |
}, |
| 114 |
dismissButton = { TextButton(onClick = { confirming = false }) { Text("Cancel") } }, |
| 115 |
) |
| 116 |
} |
| 117 |
} |