Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : MacLustr Storage Node9// File : MacLustrStorageNode/Features/Onboarding/OnboardingView.swift10// Description : Pairing (QR code or typed code) then choice of the11// capacity to lend. States plainly what iOS allows.12// Module : MacLustrStorageNode13// Repository : https://git.spboucher.ai/maclustr-storage-ios14// Project Website : https://storage.spboucher.ai15// Created : 2026-12-0116// Last Modified : 2026-12-0117//18// =============================================================================19// AUTHOR & PROFESSIONAL AFFILIATION20// =============================================================================21//22// Author : Simon-Pierre Boucher23// Position : Professor of Finance24// Institution : Université du Québec en Outaouais (UQO)25// Department : Département des sciences administratives26// Organization : SP Boucher27//28// Website : https://spboucher.ai29// Git : https://git.spboucher.ai30//31// =============================================================================32// OFFICIAL CONTACT DIRECTORY33// =============================================================================34//35// General : info@spboucher.ai36// Professional : contact@spboucher.ai37// Direct : simon@spboucher.ai38// Research : research@spboucher.ai39// Development : dev@spboucher.ai40// Support : support@spboucher.ai41// Security/Admin : admin@spboucher.ai42// Notifications : notifications@spboucher.ai43//44// Preferred Contact: admin@spboucher.ai45//46// =============================================================================47// COPYRIGHT & INTELLECTUAL PROPERTY48// =============================================================================49//50// Copyright (c) 202651// Simon-Pierre Boucher52//53// All rights reserved where applicable.54//55// This file is governed by the applicable project license,56// copyright notices, institutional agreements and relevant law.57//58// Unless expressly authorized by the applicable license or59// rights holder, copying, redistribution, sublicensing,60// publication and commercial exploitation are not permitted.61//62// Existing third-party rights and notices remain unaffected.63//64// =============================================================================65// CONFIDENTIALITY & SECURITY66// =============================================================================67//68// Classification : Public69// Distribution : Public repository; use is governed by the project license70// Security Contact : admin@spboucher.ai71//72// Access to this source does not independently grant permission73// to redistribute or disclose confidential materials.74//75// Do not embed credentials, private keys, access tokens,76// certificates or confidential personal information.77//78// =============================================================================79// LICENSE & MAINTENANCE80// =============================================================================81//82// License : None specified (no license file in this repository)83// Maintainer : Simon-Pierre Boucher84// Technical Contact: dev@spboucher.ai85//86// =============================================================================87// END OF SP BOUCHER SOURCE HEADER88// =============================================================================8990import SwiftUI9192struct OnboardingView: View {93 @EnvironmentObject private var node: NodeController94 @State private var scanning = false95 @State private var code = ""96 @State private var busy = false97 @State private var error: String?9899 var body: some View {100 ScrollView {101 VStack(alignment: .leading, spacing: 18) {102 header103 if node.paired { CapacityChoice() } else { pairingStep }104 }105 .frame(maxWidth: 560)106 .padding(.horizontal, 20).padding(.vertical, 28)107 .frame(maxWidth: .infinity)108 }109 .background(Theme.canvas)110 .sheet(isPresented: $scanning) {111 NavigationStack {112 QRScannerView { value in scanning = false; submit(value) }113 .ignoresSafeArea(edges: .bottom)114 .navigationTitle("Scan the pairing code").navigationBarTitleDisplayMode(.inline)115 .toolbar { ToolbarItem(placement: .cancellationAction) { Button("Cancel") { scanning = false } } }116 }117 }118 }119120 private var header: some View {121 VStack(alignment: .leading, spacing: 10) {122 Image("Mark").resizable().frame(width: 52, height: 52).clipShape(RoundedRectangle(cornerRadius: 12)).accessibilityHidden(true)123 Text("MacLustr Storage Node").font(.title.weight(.semibold)).foregroundStyle(Theme.text)124 Text(node.paired ? "This device is paired. Choose how much storage it lends to the cluster." : "Lend part of this device's free storage to the MacLustr cluster.")125 .font(.body).foregroundStyle(Theme.textSecondary)126 if node.snapshot.connection == .revoked, let why = node.snapshot.lastError {127 Text(why).font(.footnote).foregroundStyle(Theme.warning)128 }129 }130 }131132 private var pairingStep: some View {133 VStack(alignment: .leading, spacing: 14) {134 Panel(title: "What this app does") {135 fact("externaldrive.badge.checkmark", "Keeps encrypted fragments of cluster data in its private storage and returns them on request. It cannot read them: the keys stay on the cluster.")136 fact("lock.shield", "Never touches your photos, files or other apps. Fragments are excluded from your backups.")137 fact("moon.zzz", "iOS suspends apps that are not on screen. The node works fully while the app is open; in the background it only works when iOS grants time. The cluster always keeps its own copy of everything.")138 }139 Panel(title: "Pair with the cluster") {140 Text("In the MacLustr dashboard, open Mobile Storage Nodes and choose Add a device. The code is valid for a few minutes and works once.")141 .font(.footnote).foregroundStyle(Theme.textSecondary)142 Button { scanning = true } label: { Label("Scan QR code", systemImage: "qrcode.viewfinder").frame(maxWidth: .infinity) }143 .buttonStyle(.borderedProminent).controlSize(.large).disabled(busy)144 HStack(spacing: 10) {145 TextField("ABCD-EFGH-JKMN", text: $code)146 .textInputAutocapitalization(.characters).autocorrectionDisabled().font(.system(.body, design: .monospaced))147 .padding(.horizontal, 12).padding(.vertical, 10).background(Theme.inset)148 .overlay(RoundedRectangle(cornerRadius: 8).stroke(Theme.border, lineWidth: 1)).clipShape(RoundedRectangle(cornerRadius: 8))149 .submitLabel(.go).onSubmit { submit(code) }150 .accessibilityLabel("Pairing code")151 Button("Pair") { submit(code) }.buttonStyle(.bordered).controlSize(.large).disabled(busy || NodeEngine.parsePairing(code) == nil)152 }153 if busy { HStack(spacing: 8) { ProgressView(); Text("Creating the device key and registering…").font(.footnote).foregroundStyle(Theme.textSecondary) } }154 if let error { Text(error).font(.footnote).foregroundStyle(Theme.critical) }155 }156 }157 }158159 private func fact(_ icon: String, _ text: String) -> some View {160 HStack(alignment: .top, spacing: 12) {161 Image(systemName: icon).foregroundStyle(Theme.accent).frame(width: 24)162 Text(text).font(.footnote).foregroundStyle(Theme.textSecondary).fixedSize(horizontal: false, vertical: true)163 }164 }165166 private func submit(_ value: String) {167 guard !busy else { return }168 busy = true; error = nil169 Task { error = await node.pair(value); busy = false; if error == nil { code = "" } }170 }171}172173/// Second step of onboarding: the capacity to share. Reuses the allocation controls.174private struct CapacityChoice: View {175 @EnvironmentObject private var node: NodeController176 @State private var quota: Int64 = 0177178 var body: some View {179 let cap = node.snapshot.capacity180 VStack(alignment: .leading, spacing: 14) {181 Panel(title: "Identity") {182 Label(node.keyHardwareBacked ? "Device key created in the Secure Enclave" : "Device key created in the Keychain", systemImage: "key.horizontal").font(.footnote).foregroundStyle(Theme.textSecondary)183 if let p = node.pairing { Text("Node \(p.deviceID.prefix(12))… · \(p.host)").font(.system(.footnote, design: .monospaced)).foregroundStyle(Theme.textTertiary) }184 }185 Panel(title: "Capacity to lend") {186 QuotaPicker(quota: $quota, capacity: cap)187 Text("Space is used progressively, only when the cluster sends fragments. \(Format.bytes(cap.reserve)) always stay free for iOS and for you.")188 .font(.footnote).foregroundStyle(Theme.textSecondary)189 }190 Button { Task { await node.activate(quota: quota) } } label: { Text("Activate node").frame(maxWidth: .infinity) }191 .buttonStyle(.borderedProminent).controlSize(.large).disabled(quota <= 0)192 if cap.maxAllocatable < Format.gb { Text("This device has too little free space above the safety reserve to lend any.").font(.footnote).foregroundStyle(Theme.warning) }193 }194 .onAppear { if quota == 0 { quota = CapacityMonitor.options(maxAllocatable: cap.maxAllocatable).dropLast().last ?? CapacityMonitor.options(maxAllocatable: cap.maxAllocatable).last ?? 0 } }195 }196}197