SPB Git

Repository Public

ReceiveView.swift 5.7 KB 85 lines swift

1// SP Wallet iOS2// File: Features/Receive/ReceiveView.swift3// Description: Receiving: the wallet's own address on a chosen network — given by the isolated signer through the server, never4//              derived or typed on the device — as text and QR code, with the warnings that avoid a loss (wrong network, memo,5//              activation minimum). Works offline from the last synchronized addresses, which never change.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import SwiftUI10import SPWalletCore1112struct ReceiveView: View {13    @Environment(AppModel.self) private var model14    @Environment(\.dismiss) private var dismiss15    let initialNetwork: String?16    @State private var network: String?17    @State private var copied = false1819    var body: some View {20        NavigationStack {21            ScrollView {22                VStack(spacing: Theme.Space.section) {23                    if let id = network, let n = model.network(id), let account = model.account(id) {24                        addressCard(n, account)25                    } else {26                        VStack(alignment: .leading, spacing: 4) {27                            SectionHeader(title: String(localized: "Sur quel réseau veux-tu recevoir ?"))28                            if model.accounts.value == nil { Text(String(localized: "Adresses non disponibles : connecte-toi une première fois.")).font(.spCallout).foregroundStyle(Theme.mute) }29                            NetworkPicker(selection: $network, filter: { model.account($0.id) != nil })30                        }.spCard()31                    }32                }33                .padding(.horizontal, Theme.Space.gutter).padding(.vertical, 12)34                .frame(maxWidth: 560).frame(maxWidth: .infinity)35            }36            .spPage()37            .navigationTitle(String(localized: "Recevoir")).navigationBarTitleDisplayMode(.inline)38            .toolbar {39                ToolbarItem(placement: .cancellationAction) { Button(String(localized: "Fermer")) { dismiss() } }40                if network != nil { ToolbarItem(placement: .topBarTrailing) { Button(String(localized: "Réseau")) { network = nil } } }41            }42            .onAppear { if network == nil { network = initialNetwork } }43        }44    }4546    private func addressCard(_ n: Network, _ account: Account) -> some View {47        VStack(spacing: 16) {48            HStack(spacing: 10) { NetworkIcon(network: n.id, size: 34); Text(n.name).font(.spHeadline).foregroundStyle(Theme.ink); Spacer() }49            QRCodeView(content: ReceiveURI.content(address: account.address, network: n)).accessibilityIdentifier("receive.qr")50            Text(account.address).font(.spMono).foregroundStyle(Theme.ink).multilineTextAlignment(.center).textSelection(.enabled)51                .padding(12).frame(maxWidth: .infinity).background(Theme.elev, in: RoundedRectangle(cornerRadius: Theme.Radius.small))52                .accessibilityIdentifier("receive.address")53            HStack(spacing: 10) {54                Button {55                    // the pasteboard copy expires: an address left in the clipboard is a classic way to send to the wrong place later56                    UIPasteboard.general.setItems([[UIPasteboard.typeAutomatic: account.address]], options: [.expirationDate: Date().addingTimeInterval(180)])57                    copied = true58                    UINotificationFeedbackGenerator().notificationOccurred(.success)59                } label: { Label(copied ? String(localized: "Copiée") : String(localized: "Copier"), systemImage: copied ? "checkmark" : "doc.on.doc") }60                .buttonStyle(.spPrimary).accessibilityIdentifier("receive.copy")61                ShareLink(item: account.address) { Label(String(localized: "Partager"), systemImage: "square.and.arrow.up") }.buttonStyle(.spSecondary)62            }63            Notice(tone: .warn, title: String(localized: "N’envoie que des actifs du réseau \(n.name) à cette adresse"),64                   message: String(localized: "Actifs reconnus ici : \(([n.native.symbol] + n.tokens.map(\.symbol)).joined(separator: ", ")). Un actif envoyé depuis un autre réseau, ou un jeton absent du registre, peut être perdu ou rester invisible."))65            if n.memo != nil {66                Notice(tone: .info, title: String(localized: "Aucun mémo ni tag requis"), message: String(localized: "Cette adresse appartient à ce portefeuille seul. Si l’expéditeur exige un mémo, il peut le laisser vide."))67            }68            if let a = n.activation {69                Notice(tone: .info, title: String(localized: "Minimum d’activation : \(a) \(n.native.symbol)"), message: String(localized: "Sur ce réseau, un compte doit détenir ce minimum pour exister. Un premier dépôt inférieur peut être refusé par le réseau."))70            }71            if model.accounts.value?.signerReachable == false || !model.accounts.live {72                Notice(tone: .info, title: String(localized: "Adresse enregistrée"), message: model.accounts.value?.addressesAsOf.map { String(localized: "Donnée par le signataire le \($0.formatted(date: .abbreviated, time: .shortened)). Les adresses du portefeuille ne changent pas.") })73            }74            if let u = URL(string: account.explorer), u.scheme == "https" {75                Link(destination: u) { Label(String(localized: "Voir l’adresse dans l’explorateur"), systemImage: "arrow.up.right.square").font(.spCallout.weight(.semibold)) }76            }77        }.spCard()78    }79}8081/** What the Receive QR encodes: the plain address (universally readable). A payment URI is used only where SPWalletCore defines one. */82enum ReceiveURI {83    static func content(address: String, network: Network) -> String { (try? PaymentURI.make(address: address, network: network)) ?? address }84}85
Files main
Menu

Theme