SPB Git

Repository Public

SettingsCommon.swift 5.4 KB 117 lines swift

1import MCCCore2import SwiftUI3import UniformTypeIdentifiers45extension View {6    /// Confirmation dialog for a pending operation; the operation then runs through the screen's `MutationRunner`.7    func confirmation(_ pending: Binding<PendingConfirmation?>, runner: MutationRunner) -> some View {8        confirmationDialog(pending.wrappedValue?.title ?? "", isPresented: Binding(get: { pending.wrappedValue != nil }, set: { if !$0 { pending.wrappedValue = nil } }),9                           titleVisibility: .visible, presenting: pending.wrappedValue) { p in10            Button(p.confirmTitle, role: p.destructive ? .destructive : nil) {11                Task { await runner.run { try await p.action() } }12            }13            .accessibilityIdentifier(p.identifier)14            Button(tr("common.cancel"), role: .cancel) {}15        } message: { p in16            Text(p.message)17        }18    }19}2021/// Chrome shared by the settings panes: a scrolling stack of cards with the pane's title.22struct PaneScaffold<Content: View>: View {23    let pane: SettingsPane24    var load: () async -> Void = {}25    var reload: (() async -> Void)?26    @ViewBuilder var content: Content27    var body: some View {28        ScreenStack { content }29            .screen("settings-" + pane.rawValue, title: pane.title, load: load, reload: reload)30            .navigationBarTitleDisplayMode(.inline)31    }32}3334/// Small text button used inside rows (rename, revoke…): bordered, 44 pt, disabled offline or without permission.35struct RowAction: View {36    @Session private var session37    let title: String38    var systemImage: String39    var role: ButtonRole?40    var permission: String?41    var identifier: String42    let action: () -> Void43    var body: some View {44        MutationButton(title: title, systemImage: systemImage, role: role, permission: permission, identifier: identifier, action: action)45            .font(.callout)46    }47}4849/// A one-time secret (invitation link, enrollment token) shown once: QR code when asked, copy and share. It lives50/// in this view's memory only and is gone when the sheet closes.51struct OneTimeSecretSheet: View {52    @Environment(\.dismiss) private var dismiss53    @Environment(\.mccTheme) private var theme54    let title: String55    let explanation: String56    let secret: String57    var showsQR = true58    var details: [(String, String)] = []59    var extra: String?60    @State private var copied = false6162    var body: some View {63        ScrollView {64            VStack(alignment: .leading, spacing: Tokens.Space.s5) {65                InfoBanner(tone: .warning, title: tr("settings.secret.once_title"), message: explanation)66                if showsQR, let image = SettingsLogic.qrCode(for: secret) {67                    Image(uiImage: image)68                        .interpolation(.none)69                        .resizable()70                        .scaledToFit()71                        .frame(maxWidth: 280)72                        .padding(Tokens.Space.s4)73                        .background(Color.white, in: RoundedRectangle(cornerRadius: Tokens.Radius.lg, style: .continuous))74                        .frame(maxWidth: .infinity)75                        .accessibilityLabel(tr("settings.secret.qr_label"))76                        .accessibilityAddTraits(.isImage)77                        .accessibilityIdentifier("secret-qr")78                }79                Card(padding: Tokens.Space.s4) {80                    Text(secret).font(.callout.monospaced()).foregroundStyle(theme.textPrimary).textSelection(.enabled)81                        .frame(maxWidth: .infinity, alignment: .leading)82                        .accessibilityIdentifier("secret-value")83                }84                ForEach(details, id: \.0) { InfoRow($0.0, $0.1) }85                if let extra {86                    Card(padding: Tokens.Space.s4) {87                        Text(extra).font(.footnote.monospaced()).foregroundStyle(theme.textSecondary).textSelection(.enabled)88                            .frame(maxWidth: .infinity, alignment: .leading)89                    }90                }91                ShareLink(item: secret) {92                    Label(tr("settings.secret.share"), systemImage: "square.and.arrow.up").frame(maxWidth: .infinity, minHeight: Tokens.Size.touchMin)93                }94                .buttonStyle(.mccProminent)95                .accessibilityIdentifier("secret-share")96                Button {97                    UIPasteboard.general.setItems([[UTType.utf8PlainText.identifier: secret]], options: [.localOnly: true, .expirationDate: Date().addingTimeInterval(120)])98                    copied = true99                } label: {100                    Label(tr(copied ? "settings.secret.copied" : "settings.secret.copy"), systemImage: copied ? "checkmark" : "doc.on.doc")101                        .frame(maxWidth: .infinity, minHeight: Tokens.Size.touchMin)102                }103                .buttonStyle(.bordered)104                .accessibilityIdentifier("secret-copy")105            }106            .padding(Tokens.Space.s6)107            .frame(maxWidth: 560)108            .frame(maxWidth: .infinity)109        }110        .background(theme.canvas.ignoresSafeArea())111        .navigationTitle(title)112        .navigationBarTitleDisplayMode(.inline)113        .toolbar { ToolbarItem(placement: .confirmationAction) { Button(tr("common.done")) { dismiss() }.accessibilityIdentifier("secret-done") } }114        .accessibilityIdentifier("screen-secret")115    }116}117
Files main
Menu

Theme