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