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