OXYGENHUB
# 840c75813f86 oxygenhub-android / app / src / main / java / io / oxygenhub / android / ui / account / AccountScreen.kt
6.8 KiB · last changed Panels you can see: the hub's palette by default, clear surface steps and a 1 dp edge on every panel (closes #24) 7 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.material.icons.outlined.Palette
20
import androidx.compose.material3.AlertDialog
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.Switch
27
import androidx.compose.material3.Text
28
import androidx.compose.material3.TextButton
29
import androidx.compose.runtime.Composable
30
import androidx.compose.runtime.getValue
31
import androidx.compose.runtime.mutableStateOf
32
import androidx.compose.runtime.remember
33
import androidx.compose.runtime.rememberCoroutineScope
34
import androidx.compose.runtime.setValue
35
import androidx.compose.ui.Alignment
36
import androidx.compose.ui.Modifier
37
import androidx.compose.ui.platform.LocalContext
38
import androidx.compose.ui.text.font.FontWeight
39
import androidx.compose.ui.unit.dp
40
import io.oxygenhub.android.alerts.LandAlertsCard
41
import io.oxygenhub.android.api.Avatar
42
import io.oxygenhub.android.api.HubApi
43
import io.oxygenhub.android.auth.Session
44
import io.oxygenhub.android.runner.RunnerCard
45
import io.oxygenhub.android.ui.common.AppScreen
46
import io.oxygenhub.android.ui.common.AvatarImage
47
import io.oxygenhub.android.ui.common.Panel
48
import io.oxygenhub.android.ui.theme.Appearance
49
import io.oxygenhub.android.ui.theme.rememberWallpaperColours
50
import io.oxygenhub.android.update.UpdateCard
51
import kotlinx.coroutines.launch
52
53
/** Who is signed in, to which hub, with what; and signing out. */
54
@Composable
55
fun AccountScreen(session: Session, avatar: Avatar?, api: HubApi, nativeCore: () -> String, onSignOut: suspend () -> Unit) {
56
    var confirming by remember { mutableStateOf(false) }
57
    var signingOut by remember { mutableStateOf(false) }
58
    val scope = rememberCoroutineScope()
59
    val core = remember { runCatching(nativeCore).getOrElse { "unavailable: ${it.message}" } }
60
61
    AppScreen(title = "Account") {
62
        Box(Modifier.fillMaxWidth(), contentAlignment = Alignment.TopCenter) {
63
            Column(
64
                Modifier.widthIn(max = 600.dp).verticalScroll(rememberScrollState()).padding(16.dp),
65
                verticalArrangement = Arrangement.spacedBy(16.dp),
66
            ) {
67
                Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
68
                    AvatarImage(avatar, session.username, size = 72.dp)
69
                    Spacer(Modifier.height(8.dp))
70
                    Text(session.username, style = MaterialTheme.typography.titleLarge, fontWeight = FontWeight.Bold)
71
                }
72
                LandAlertsCard()
73
                RunnerCard(api, session.hub, session.canWrite)
74
                UpdateCard(api, session.hub)
75
                AppearanceCard()
76
                Panel(Modifier.fillMaxWidth()) {
77
                    ListItem(
78
                        leadingContent = { Icon(Icons.Outlined.Dns, contentDescription = null) },
79
                        headlineContent = { Text("Hub") },
80
                        supportingContent = { Text(session.hub) },
81
                    )
82
                    ListItem(
83
                        leadingContent = { Icon(Icons.Outlined.Key, contentDescription = null) },
84
                        headlineContent = { Text("Allowed") },
85
                        supportingContent = { Text(session.scopes.ifEmpty { listOf("(the hub did not say)") }.joinToString(", ")) },
86
                    )
87
                    ListItem(
88
                        leadingContent = { Icon(Icons.Outlined.Memory, contentDescription = null) },
89
                        headlineContent = { Text("Native core") },
90
                        supportingContent = { Text(core) },
91
                    )
92
                }
93
                Text(
94
                    "This phone's sign-in is a device token, kept encrypted with a key in the Android Keystore. " +
95
                        "It lasts while it is used, and ends after 90 days without use. Signing out revokes it on the hub.",
96
                    style = MaterialTheme.typography.bodySmall,
97
                    color = MaterialTheme.colorScheme.onSurfaceVariant,
98
                )
99
                OutlinedButton(onClick = { confirming = true }, enabled = !signingOut, modifier = Modifier.fillMaxWidth()) {
100
                    if (signingOut) {
101
                        CircularProgressIndicator(Modifier.size(18.dp), strokeWidth = 2.dp)
102
                    } else {
103
                        Icon(Icons.AutoMirrored.Outlined.Logout, contentDescription = null)
104
                    }
105
                    Spacer(Modifier.size(8.dp))
106
                    Text("Sign out")
107
                }
108
            }
109
        }
110
    }
111
112
    if (confirming) {
113
        AlertDialog(
114
            onDismissRequest = { confirming = false },
115
            title = { Text("Sign out?") },
116
            text = { Text("The token this phone uses is revoked on ${session.hub}. You can sign in again at any time.") },
117
            confirmButton = {
118
                TextButton(onClick = {
119
                    confirming = false
120
                    signingOut = true
121
                    scope.launch { onSignOut() }
122
                }) { Text("Sign out") }
123
            },
124
            dismissButton = { TextButton(onClick = { confirming = false }) { Text("Cancel") } },
125
        )
126
    }
127
}
128
129
/** The app's colours (#24): the hub's palette, or the wallpaper's. */
130
@Composable
131
private fun AppearanceCard() {
132
    val context = LocalContext.current
133
    val wallpaper = rememberWallpaperColours()
134
    Panel(Modifier.fillMaxWidth()) {
135
        ListItem(
136
            leadingContent = { Icon(Icons.Outlined.Palette, contentDescription = null) },
137
            headlineContent = { Text("Wallpaper colours") },
138
            supportingContent = { Text(if (wallpaper) "The app takes its colours from your wallpaper." else "The app uses OxygenHub's own colours, as the website does.") },
139
            trailingContent = { Switch(checked = wallpaper, onCheckedChange = { Appearance.setWallpaperColours(context, it) }) },
140
        )
141
    }
142
}