OXYGENHUB
# dfbc3e972872 oxygenhub-android / app / src / main / java / io / oxygenhub / android / ui / account / AccountScreen.kt
5.7 KiB · last changed Notifications when a proposal can be landed; the runner comes back after an update or restart (closes #20, refs #8) 10 days ago source blame history raw
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.alerts.LandAlertsCard
42
import io.oxygenhub.android.runner.RunnerCard
43
import io.oxygenhub.android.ui.common.AppScreen
44
import io.oxygenhub.android.ui.common.AvatarImage
45
import kotlinx.coroutines.launch
46
47
/** Who is signed in, to which hub, with what; and signing out. */
48
@Composable
49
fun AccountScreen(session: Session, avatar: Avatar?, api: HubApi, nativeCore: () -> String, onSignOut: suspend () -> Unit) {
50
    var confirming by remember { mutableStateOf(false) }
51
    var signingOut by remember { mutableStateOf(false) }
52
    val scope = rememberCoroutineScope()
53
    val core = remember { runCatching(nativeCore).getOrElse { "unavailable: ${it.message}" } }
54
55
    AppScreen(title = "Account") {
56
        Box(Modifier.fillMaxWidth(), contentAlignment = Alignment.TopCenter) {
57
            Column(
58
                Modifier.widthIn(max = 600.dp).verticalScroll(rememberScrollState()).padding(16.dp),
59
                verticalArrangement = Arrangement.spacedBy(16.dp),
60
            ) {
61
                Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
62
                    AvatarImage(avatar, session.username, size = 72.dp)
63
                    Spacer(Modifier.height(8.dp))
64
                    Text(session.username, style = MaterialTheme.typography.titleLarge, fontWeight = FontWeight.Bold)
65
                }
66
                LandAlertsCard()
67
                RunnerCard(api, session.hub, session.canWrite)
68
                Card(Modifier.fillMaxWidth()) {
69
                    ListItem(
70
                        leadingContent = { Icon(Icons.Outlined.Dns, contentDescription = null) },
71
                        headlineContent = { Text("Hub") },
72
                        supportingContent = { Text(session.hub) },
73
                    )
74
                    ListItem(
75
                        leadingContent = { Icon(Icons.Outlined.Key, contentDescription = null) },
76
                        headlineContent = { Text("Allowed") },
77
                        supportingContent = { Text(session.scopes.ifEmpty { listOf("(the hub did not say)") }.joinToString(", ")) },
78
                    )
79
                    ListItem(
80
                        leadingContent = { Icon(Icons.Outlined.Memory, contentDescription = null) },
81
                        headlineContent = { Text("Native core") },
82
                        supportingContent = { Text(core) },
83
                    )
84
                }
85
                Text(
86
                    "This phone's sign-in is a device token, kept encrypted with a key in the Android Keystore. " +
87
                        "It lasts while it is used, and ends after 90 days without use. Signing out revokes it on the hub.",
88
                    style = MaterialTheme.typography.bodySmall,
89
                    color = MaterialTheme.colorScheme.onSurfaceVariant,
90
                )
91
                OutlinedButton(onClick = { confirming = true }, enabled = !signingOut, modifier = Modifier.fillMaxWidth()) {
92
                    if (signingOut) {
93
                        CircularProgressIndicator(Modifier.size(18.dp), strokeWidth = 2.dp)
94
                    } else {
95
                        Icon(Icons.AutoMirrored.Outlined.Logout, contentDescription = null)
96
                    }
97
                    Spacer(Modifier.size(8.dp))
98
                    Text("Sign out")
99
                }
100
            }
101
        }
102
    }
103
104
    if (confirming) {
105
        AlertDialog(
106
            onDismissRequest = { confirming = false },
107
            title = { Text("Sign out?") },
108
            text = { Text("The token this phone uses is revoked on ${session.hub}. You can sign in again at any time.") },
109
            confirmButton = {
110
                TextButton(onClick = {
111
                    confirming = false
112
                    signingOut = true
113
                    scope.launch { onSignOut() }
114
                }) { Text("Sign out") }
115
            },
116
            dismissButton = { TextButton(onClick = { confirming = false }) { Text("Cancel") } },
117
        )
118
    }
119
}