Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : Immo-Ka Admin iOS9// File : ImmoKaAdmin/App/ImmoKaAdminApp.swift10// Description : point d'entrée — session d'administration, écran de11// connexion ou outil, voile de confidentialité (sélecteur12// d'apps) et verrouillage au retour de l'arrière-plan.13// Module : ImmoKaAdmin14// Repository : https://git.spboucher.ai/immo-ka-admin-ios15// Project Website : https://www.immo-ka.com16// Created : 2026-11-3017// Last Modified : 2026-11-3018//19// =============================================================================20// AUTHOR & PROFESSIONAL AFFILIATION21// =============================================================================22//23// Author : Simon-Pierre Boucher24// Position : Professor of Finance25// Institution : Université du Québec en Outaouais (UQO)26// Department : Département des sciences administratives27// Organization : SP Boucher28//29// Website : https://spboucher.ai30// Git : https://git.spboucher.ai31//32// =============================================================================33// OFFICIAL CONTACT DIRECTORY34// =============================================================================35//36// General : info@spboucher.ai37// Professional : contact@spboucher.ai38// Direct : simon@spboucher.ai39// Research : research@spboucher.ai40// Development : dev@spboucher.ai41// Support : support@spboucher.ai42// Security/Admin : admin@spboucher.ai43// Notifications : notifications@spboucher.ai44//45// Preferred Contact: support@spboucher.ai46//47// =============================================================================48// COPYRIGHT & INTELLECTUAL PROPERTY49// =============================================================================50//51// Copyright (c) 202652// Simon-Pierre Boucher53//54// All rights reserved where applicable.55//56// This file is governed by the applicable project license,57// copyright notices, institutional agreements and relevant law.58//59// Unless expressly authorized by the applicable license or60// rights holder, copying, redistribution, sublicensing,61// publication and commercial exploitation are not permitted.62//63// Existing third-party rights and notices remain unaffected.64//65// =============================================================================66// CONFIDENTIALITY & SECURITY67// =============================================================================68//69// Classification : Public70// Distribution : Public repository; use is governed by the project license71// Security Contact : admin@spboucher.ai72//73// Access to this source does not independently grant permission74// to redistribute or disclose confidential materials.75//76// Do not embed credentials, private keys, access tokens,77// certificates or confidential personal information.78//79// =============================================================================80// LICENSE & MAINTENANCE81// =============================================================================82//83// License : None specified (no license file in this repository)84// Maintainer : Simon-Pierre Boucher85// Technical Contact: dev@spboucher.ai86//87// =============================================================================88// END OF SP BOUCHER SOURCE HEADER89// =============================================================================9091import SwiftUI92import UIKit9394@main95struct ImmoKaAdminApp: App {96 @State private var session: AdminSession97 @Environment(\.scenePhase) private var scenePhase9899 init() {100 #if DEBUG101 // essais : coffre en mémoire quand un jeton d'essai est fourni (rien ne reste dans le trousseau du simulateur)102 let debugToken = UserDefaults.standard.string(forKey: "IKAdminToken")103 _session = State(initialValue: AdminSession(store: debugToken == nil ? KeychainStore() : MemoryStore()))104 #else105 _session = State(initialValue: AdminSession())106 #endif107 }108109 @State private var veil = VeilController()110111 var body: some Scene {112 WindowGroup {113 Group {114 if session.isSignedIn { RootView() } else if session.restoring { RestoringView() } else { LoginView() }115 }116 // verrouillée : le contenu reste monté (un message en cours d'écriture n'est pas perdu) mais il n'est117 // ni visible, ni touchable, ni lisible par VoiceOver ; le voile est dans une fenêtre au-dessus de tout118 .opacity(session.locked ? 0 : 1)119 .allowsHitTesting(!session.locked)120 .accessibilityHidden(session.locked)121 .environment(session)122 .tint(Theme.brand)123 .preferredColorScheme(Appearance.forced)124 .onChange(of: scenePhase) { _, phase in125 switch phase {126 case .background: session.didEnterBackground()127 case .active: session.willEnterForeground()128 default: break129 }130 veil.update(session: session, active: phase == .active)131 }132 .onChange(of: session.locked) { _, _ in veil.update(session: session, active: scenePhase == .active) }133 .task {134 #if DEBUG135 if let t = UserDefaults.standard.string(forKey: "IKAdminToken"), !t.isEmpty {136 await session.adoptDebugToken(t)137 if UserDefaults.standard.bool(forKey: "IKLock") { session.debugLock() }138 return139 }140 #endif141 await session.restore()142 }143 }144 }145}146147/// Voile de confidentialité et écran de verrouillage, dans une fenêtre posée au-dessus de toutes les autres :148/// feuilles, alertes et claviers de l'app sont couverts aussi. iOS photographie l'app pour le sélecteur d'apps149/// quand elle quitte le premier plan : le voile est posé dès que la scène n'est plus active.150@MainActor151final class VeilController {152 private var window: UIWindow?153154 func update(session: AdminSession, active: Bool) {155 let needed = !active || session.locked156 if needed {157 if window == nil, let scene = UIApplication.shared.connectedScenes.compactMap({ $0 as? UIWindowScene }).first {158 let w = UIWindow(windowScene: scene)159 w.windowLevel = .alert + 1160 let host = UIHostingController(rootView: VeilRoot().environment(session).tint(Theme.brand).preferredColorScheme(Appearance.forced))161 host.view.backgroundColor = UIColor(Theme.bg)162 w.rootViewController = host163 window = w164 }165 window?.isHidden = false166 if session.locked { window?.makeKey() }167 } else if let w = window {168 w.isHidden = true169 window = nil170 // la fenêtre de l'app redevient la fenêtre active (clavier, VoiceOver)171 w.windowScene?.windows.first { $0 !== w && !$0.isHidden }?.makeKey()172 }173 }174}175176/// Contenu de la fenêtre du voile : l'écran de verrouillage si l'app est verrouillée, sinon la marque seule.177struct VeilRoot: View {178 @Environment(AdminSession.self) private var session179 var body: some View {180 if session.locked { LockView() } else { PrivacyVeil() }181 }182}183184/// Thème imposé par argument de lancement (captures) : `-appearance light|dark`. Sinon, celui du système.185enum Appearance {186 static var forced: ColorScheme? {187 switch UserDefaults.standard.string(forKey: "appearance") {188 case "light": .light189 case "dark": .dark190 default: nil191 }192 }193}194195/// La marque : la maison en filet dont la porte s'ouvre, comme sur l'icône.196struct HouseGlyph: View {197 var size: CGFloat = 44198 var body: some View {199 Canvas { ctx, s in200 let k = s.width / 64201 func p(_ x: CGFloat, _ y: CGFloat) -> CGPoint { CGPoint(x: x * k, y: y * k) }202 var house = Path()203 house.move(to: p(32, 6.5)); house.addLine(to: p(41.5, 15.3))204 house.addLine(to: p(41.5, 9.5)); house.addLine(to: p(47, 9.5)); house.addLine(to: p(47, 20.4)); house.addLine(to: p(49.7, 22.9))205 house.addCurve(to: p(59.8, 27.9), control1: p(52.7, 25.7), control2: p(55.7, 27.2))206 house.addCurve(to: p(59.5, 31.5), control1: p(62.1, 28.3), control2: p(61.9, 31.5))207 house.addLine(to: p(53, 31.5)); house.addLine(to: p(53, 58)); house.addLine(to: p(11, 58)); house.addLine(to: p(11, 31.5)); house.addLine(to: p(4.5, 31.5))208 house.addCurve(to: p(4.2, 27.9), control1: p(2.1, 31.5), control2: p(1.9, 28.3))209 house.addCurve(to: p(14.3, 22.9), control1: p(8.3, 27.2), control2: p(11.3, 25.7))210 house.closeSubpath()211 ctx.stroke(house, with: .color(Theme.ink), style: StrokeStyle(lineWidth: 2.4 * k, lineCap: .round, lineJoin: .round))212 var panel = Path()213 panel.move(to: p(24.5, 31.5)); panel.addLine(to: p(36.5, 34.6)); panel.addLine(to: p(36.5, 55.2)); panel.addLine(to: p(24.5, 58)); panel.closeSubpath()214 ctx.stroke(panel, with: .color(Theme.ink), style: StrokeStyle(lineWidth: 1.9 * k, lineJoin: .round))215 var sill = Path(); sill.move(to: p(5, 62)); sill.addLine(to: p(59, 62))216 ctx.stroke(sill, with: .color(Theme.ink), style: StrokeStyle(lineWidth: 2.4 * k, lineCap: .round))217 let r = 1.7 * k218 ctx.fill(Path(ellipseIn: CGRect(x: 33.2 * k - r, y: 44.6 * k - r, width: 2 * r, height: 2 * r)), with: .color(Theme.brand))219 }220 .frame(width: size, height: size)221 .accessibilityHidden(true)222 }223}224225struct PrivacyVeil: View {226 var body: some View {227 ZStack {228 Theme.bg.ignoresSafeArea()229 VStack(spacing: 12) {230 HouseGlyph(size: 56)231 Text("Immo-Ka Admin").font(.editorial(22)).foregroundStyle(Theme.ink)232 }233 }234 .accessibilityHidden(true)235 }236}237238struct RestoringView: View {239 var body: some View {240 ZStack {241 Theme.bg.ignoresSafeArea()242 VStack(spacing: 14) { HouseGlyph(size: 56); ProgressView() }243 }244 }245}246247struct LockView: View {248 @Environment(AdminSession.self) private var session249 var body: some View {250 ZStack {251 Theme.bg.ignoresSafeArea()252 VStack(spacing: 18) {253 HouseGlyph(size: 64)254 Text("Administration verrouillée").font(.editorial(24)).foregroundStyle(Theme.ink).multilineTextAlignment(.center)255 Text("L'app est restée en arrière-plan. Identifiez-vous pour continuer.").font(.subheadline).foregroundStyle(Theme.ink2).multilineTextAlignment(.center)256 Button { Task { await session.unlock() } } label: { Label("Déverrouiller", systemImage: "lock.open").frame(maxWidth: 280) }257 .buttonStyle(PrimaryButtonStyle())258 .disabled(session.unlocking)259 .accessibilityIdentifier("lock.unlock")260 Button("Se déconnecter") { Task { await session.signOut() } }261 .font(.subheadline.weight(.medium)).frame(minHeight: 44)262 .accessibilityIdentifier("lock.signOut")263 }264 .padding(28)265 }266 .task {267 #if DEBUG268 // captures et tests : le voile reste posé, sans demande d'identification au lancement269 if UserDefaults.standard.bool(forKey: "IKLock") { return }270 #endif271 await session.unlock()272 }273 }274}275