SPB Git

Repository Public License

SettingsView.swift 8.4 KB 152 lines swift

1// ORIX Arcade2// File: App/SettingsView.swift3// Description: Settings tab — audio, haptics, motion and accessibility options, save export/import/reset with confirmation, privacy and about.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import SwiftUI8import UniformTypeIdentifiers910nonisolated struct SaveDocument: FileDocument {11    static let readableContentTypes: [UTType] = [.json]12    var data: Data1314    init(data: Data) { self.data = data }1516    init(configuration: ReadConfiguration) throws {17        guard let contents = configuration.file.regularFileContents else { throw SaveError.unreadable }18        data = contents19    }2021    func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper { FileWrapper(regularFileWithContents: data) }22}2324struct SettingsView: View {25    @Environment(ArcadeServices.self) private var services26    @State private var exportDocument: SaveDocument?27    @State private var showExporter = false28    @State private var showImporter = false29    @State private var pendingImport: Data?30    @State private var confirmReset = false31    @State private var message: String?3233    var body: some View {34        ScrollView {35            VStack(alignment: .leading, spacing: OrixSpace.lg) {36                Text("Settings").font(OrixFont.display).foregroundStyle(OrixColor.textPrimary).padding(.top, OrixSpace.xs)3738                group("Sound") {39                    OrixToggleRow(title: "Music", systemImage: "music.note", isOn: bind(\.musicOn))40                    OrixSliderRow(title: "Music volume", systemImage: "speaker.wave.1.fill", value: bind(\.musicVolume))41                    OrixToggleRow(title: "Sound effects", systemImage: "speaker.wave.2.fill", isOn: bind(\.sfxOn))42                    OrixSliderRow(title: "Effects volume", systemImage: "speaker.wave.3.fill", value: bind(\.sfxVolume))43                    Text("Audio follows the silent switch and never interrupts your own music.")44                        .font(OrixFont.caption).foregroundStyle(OrixColor.textTertiary)45                }4647                group("Feel") {48                    OrixToggleRow(title: "Haptics", detail: "Taps for impacts, combos and near misses", systemImage: "iphone.radiowaves.left.and.right", isOn: bind(\.hapticsOn))49                    OrixSliderRow(title: "Screen shake", systemImage: "waveform.path", value: bind(\.shakeIntensity))50                    VStack(alignment: .leading, spacing: 6) {51                        Text("Visual effects").font(OrixFont.body).foregroundStyle(OrixColor.textPrimary)52                        OrixSegmented(options: [(EffectsQuality.auto, "Auto"), (.high, "Full"), (.low, "Light")], selection: bind(\.effects))53                        Text("Auto lowers particle counts when frames run long or Low Power Mode is on.")54                            .font(OrixFont.caption).foregroundStyle(OrixColor.textTertiary)55                    }56                }5758                group("Accessibility") {59                    OrixToggleRow(title: "Reduce motion", detail: "Also follows the system setting", systemImage: "figure.walk.motion", isOn: bind(\.reduceMotion))60                    OrixToggleRow(title: "High contrast", detail: "Brighter text and outlines", systemImage: "circle.lefthalf.filled", isOn: bind(\.highContrast))61                    OrixToggleRow(title: "Left-handed layout", detail: "Moves the pause button to the left", systemImage: "hand.raised.fill", isOn: bind(\.leftHanded))62                }6364                group("Your data") {65                    Text("Progress is stored only on this iPhone. Export a copy to keep it safe or move it to another device.")66                        .font(OrixFont.caption).foregroundStyle(OrixColor.textSecondary)67                    OrixButton(title: "Export save", systemImage: "square.and.arrow.up", kind: .secondary) {68                        guard let data = try? services.store.exportData() else { message = "The save could not be exported."; return }69                        exportDocument = SaveDocument(data: data)70                        showExporter = true71                    }72                    .accessibilityIdentifier("exportSave")73                    OrixButton(title: "Import save", systemImage: "square.and.arrow.down", kind: .secondary) { showImporter = true }74                    OrixButton(title: "Reset progress", systemImage: "trash", kind: .destructive) { confirmReset = true }75                        .accessibilityIdentifier("resetProgress")76                    if let message { Text(message).font(OrixFont.caption).foregroundStyle(OrixColor.gold) }77                }7879                group("Privacy") {80                    Label("No account, no sign-in", systemImage: "person.crop.circle.badge.xmark")81                    Label("No ads, analytics or tracking", systemImage: "eye.slash.fill")82                    Label("No network access — works in airplane mode", systemImage: "airplane")83                }84                .font(OrixFont.callout)85                .foregroundStyle(OrixColor.textSecondary)8687                VStack(spacing: 6) {88                    OrixLogoMark().frame(width: 44, height: 44)89                    OrixWordmark(size: 15)90                    Text("Version \(Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "–") (\(Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String ?? "–"))")91                        .font(OrixFont.caption).foregroundStyle(OrixColor.textTertiary)92                    Text("All art, sound and music are generated in code.").font(OrixFont.caption).foregroundStyle(OrixColor.textTertiary)93                }94                .frame(maxWidth: .infinity)95                .padding(.top, OrixSpace.xs)96            }97            .padding(.horizontal, OrixSpace.md)98            .padding(.bottom, 110)99        }100        .scrollIndicators(.hidden)101        .fileExporter(isPresented: $showExporter, document: exportDocument, contentType: .json, defaultFilename: "ORIX-Arcade-save") { result in102            if case .success = result { message = "Save exported." }103        }104        .fileImporter(isPresented: $showImporter, allowedContentTypes: [.json]) { result in105            guard case .success(let url) = result else { return }106            let scoped = url.startAccessingSecurityScopedResource()107            defer { if scoped { url.stopAccessingSecurityScopedResource() } }108            guard let raw = try? Data(contentsOf: url) else { message = "That file could not be read."; return }109            do {110                _ = try SaveCodec.importEnvelope(raw)111                pendingImport = raw112            } catch {113                message = error.localizedDescription114            }115        }116        .alert("Replace your progress?", isPresented: Binding(get: { pendingImport != nil }, set: { if !$0 { pendingImport = nil } })) {117            Button("Replace", role: .destructive) {118                if let raw = pendingImport {119                    do { try services.store.importData(raw); message = "Save imported." } catch { message = error.localizedDescription }120                    services.updateSettings { _ in }121                }122                pendingImport = nil123            }124            Button("Cancel", role: .cancel) { pendingImport = nil }125        } message: {126            Text("The imported save will replace everything on this iPhone. A copy of your current progress is kept on the device.")127        }128        .alert("Reset all progress?", isPresented: $confirmReset) {129            Button("Reset", role: .destructive) {130                services.store.resetProgress()131                message = "Progress reset. Your settings were kept."132            }133            Button("Cancel", role: .cancel) {}134        } message: {135            Text("Scores, levels, upgrades and awards will be cleared. Your settings are kept.")136        }137    }138139    private func bind<Value>(_ keyPath: WritableKeyPath<ArcadeSettings, Value>) -> Binding<Value> {140        Binding(get: { services.settings[keyPath: keyPath] }, set: { value in services.updateSettings { $0[keyPath: keyPath] = value } })141    }142143    private func group<Content: View>(_ title: String, @ViewBuilder content: () -> Content) -> some View {144        VStack(alignment: .leading, spacing: OrixSpace.sm) {145            SectionHeader(title: title)146            VStack(alignment: .leading, spacing: OrixSpace.sm) { content() }147                .frame(maxWidth: .infinity, alignment: .leading)148                .glassPanel()149        }150    }151}152
Files main
Menu

Theme