SPB Git

Repository Public License

InvestigationsView.swift 10.5 KB 198 lines swift

1// ORIX Secure2// File: App/Sources/Features/Investigations/InvestigationsView.swift3// Description: Guided investigations: pick a symptom, answer what only you can see, and get observations, hypotheses and4//              proportionate actions kept apart — with a report. Nothing here claims to detect spyware.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import ORIXDesign9import ORIXSecureEngines10import ORIXSecureModel11import SwiftUI1213struct InvestigationsView: View {14    @Environment(Router.self) private var router1516    var body: some View {17        ScreenScroll {18            ORIXBanner(.info, title: Text("Start from what you noticed"),19                       message: Text("Each investigation gathers what the app can measure, asks what only you can see in Settings, and lists explanations from the most ordinary to the least likely."))20            VStack(spacing: 0) {21                ForEach(Array(Symptom.allCases.enumerated()), id: \.element.id) { index, symptom in22                    Button { router.push(.investigation(symptom)) } label: {23                        HStack(spacing: ORIX.Space.sm) {24                            ORIXGlyph(symptom.systemImage)25                            Text(verbatim: symptom.title).font(.orixHeadline).foregroundStyle(ORIX.Palette.textPrimary).multilineTextAlignment(.leading)26                            Spacer(minLength: 0)27                            Image(systemName: "chevron.right").font(.footnote.weight(.semibold)).foregroundStyle(ORIX.Palette.textTertiary).accessibilityHidden(true)28                        }29                        .padding(.vertical, 8)30                        .contentShape(Rectangle())31                    }32                    .buttonStyle(.plain)33                    .accessibilityIdentifier("symptom." + symptom.rawValue)34                    if index < Symptom.allCases.count - 1 { Divider().padding(.leading, 48) }35                }36            }37            .orixCard(padding: ORIX.Space.sm)38            LimitsCard(title: "What an investigation cannot establish", items: [39                String(localized: "Whether the device is compromised: no App Store app can determine that."),40                String(localized: "Anything about other apps, system logs or the kernel."),41                String(localized: "If you received an Apple threat notification, follow Apple's instructions and contact Apple Support."),42            ])43        }44        .navigationTitle("Investigations")45        .accessibilityIdentifier("investigations")46    }47}4849struct InvestigationFlowView: View {50    let symptom: Symptom51    @Environment(AppModel.self) private var model52    @Environment(Router.self) private var router53    @State private var answers: [String: ChecklistAnswer] = [:]54    @State private var result: InvestigationResult?55    @State private var saved = false56    private let engine = InvestigationEngine()5758    var body: some View {59        let plan = engine.plan(for: symptom)60        ScreenScroll {61            Text(verbatim: symptom.introduction).font(.orixBody).foregroundStyle(ORIX.Palette.textSecondary)62            ORIXSectionHeader("What only you can see")63            ForEach(plan.questions) { question in64                VStack(alignment: .leading, spacing: ORIX.Space.xs) {65                    Text(verbatim: question.text).font(.orixHeadline).foregroundStyle(ORIX.Palette.textPrimary)66                    if let hint = question.hint {67                        Label { Text(verbatim: hint) } icon: { Image(systemName: "gear") }.font(.orixCaption).foregroundStyle(ORIX.Palette.textTertiary)68                    }69                    Picker(question.text, selection: Binding(get: { answers[question.id] }, set: { answers[question.id] = $0; result = nil; saved = false })) {70                        Text("—").tag(ChecklistAnswer?.none)71                        Text(verbatim: ChecklistAnswer.yes.title).tag(ChecklistAnswer?.some(.yes))72                        Text(verbatim: ChecklistAnswer.no.title).tag(ChecklistAnswer?.some(.no))73                        Text(verbatim: ChecklistAnswer.notSure.title).tag(ChecklistAnswer?.some(.notSure))74                    }75                    .pickerStyle(.segmented)76                    .accessibilityIdentifier("question." + question.id)77                }78                .frame(maxWidth: .infinity, alignment: .leading)79                .orixCard()80            }81            ORIXSectionHeader("Where to look in Settings")82            VStack(alignment: .leading, spacing: ORIX.Space.sm) {83                ForEach(plan.settingsChecks) { check in84                    VStack(alignment: .leading, spacing: 2) {85                        Text(verbatim: check.title).font(.orixHeadline).foregroundStyle(ORIX.Palette.textPrimary)86                        Text(verbatim: check.path).font(.orixMono).foregroundStyle(ORIX.Palette.textSecondary)87                        Text(verbatim: check.lookFor).font(.orixCaption).foregroundStyle(ORIX.Palette.textSecondary)88                    }89                    .accessibilityElement(children: .combine)90                }91            }92            .frame(maxWidth: .infinity, alignment: .leading)93            .orixCard()94            if !plan.tools.isEmpty {95                ORIXSectionHeader("Tools that add evidence")96                FlowChips(spacing: 8) {97                    ForEach(plan.tools, id: \.self) { tool in98                        Button { router.open(tool.destination) } label: { Label(tool.destination.title, systemImage: tool.destination.systemImage) }99                            .buttonStyle(.bordered)100                    }101                }102            }103            Button { evaluate() } label: { Label("Review Findings", systemImage: "list.bullet.clipboard") }104                .buttonStyle(.orixPrimary)105                .accessibilityIdentifier("investigation.evaluate")106            if let result { resultView(result) }107        }108        .navigationTitle(Text(verbatim: symptom.title))109        .navigationBarTitleDisplayMode(.inline)110        .accessibilityIdentifier("investigation")111    }112113    private func evaluate() {114        model.refresh()115        // Findings of the network tools run in this session: the connection assessment and the last certificate check.116        let relevant: [Finding] = symptom == .networkBehavior || symptom == .dataUsage || symptom == .unknownProfile117            ? model.networkFindings + (model.network.tls?.findings ?? []) : []118        result = engine.evaluate(InvestigationInput(symptom: symptom, answers: answers, device: model.device, signals: model.signals, toolFindings: relevant))119        saved = false120        ORIXHaptics.tick()121    }122123    @ViewBuilder private func resultView(_ result: InvestigationResult) -> some View {124        ORIXSectionHeader("What was measured")125        facts(result.observations, empty: "No device reading was relevant or available for this symptom.")126        ORIXSectionHeader("What you reported")127        facts(result.reported, empty: "No question was answered yet.")128        ORIXSectionHeader("Possible explanations")129        Text("Hypotheses, best supported first. None is a confirmed diagnosis.").font(.orixCaption).foregroundStyle(ORIX.Palette.textSecondary)130        ForEach(result.hypotheses) { hypothesis in131            VStack(alignment: .leading, spacing: ORIX.Space.xs) {132                Text(verbatim: hypothesis.title).font(.orixHeadline).foregroundStyle(ORIX.Palette.textPrimary)133                ORIXChip(Text(verbatim: hypothesis.support.title),134                         systemName: hypothesis.support == .supported ? "checkmark.circle" : (hypothesis.support == .possible ? "circle.dotted" : "arrow.down.circle"),135                         tint: hypothesis.support == .supported ? ORIXAccent.secure.primary : ORIX.Palette.textSecondary)136                Text(verbatim: hypothesis.explanation).font(.orixCallout).foregroundStyle(ORIX.Palette.textSecondary)137                ForEach(hypothesis.basis, id: \.self) { basis in138                    Label { Text(verbatim: basis) } icon: { Image(systemName: "arrow.turn.down.right") }.font(.orixCaption).foregroundStyle(ORIX.Palette.textPrimary)139                }140            }141            .frame(maxWidth: .infinity, alignment: .leading)142            .orixCard()143            .accessibilityElement(children: .combine)144            .accessibilityIdentifier("hypothesis." + hypothesis.id)145        }146        ORIXSectionHeader("Recommended actions")147        VStack(alignment: .leading, spacing: ORIX.Space.sm) {148            ForEach(Array(result.actions.enumerated()), id: \.offset) { index, action in149                HStack(alignment: .top, spacing: ORIX.Space.sm) {150                    Text(verbatim: "\(index + 1).").font(.orixHeadline).foregroundStyle(ORIXAccent.secure.primary)151                    Text(verbatim: action).font(.orixCallout).foregroundStyle(ORIX.Palette.textPrimary)152                }153            }154        }155        .frame(maxWidth: .infinity, alignment: .leading)156        .orixCard()157        ORIXBanner(.info, title: Text("If the problem continues"), message: Text(verbatim: result.escalation))158        LimitsCard(title: "What was not inspected", items: result.notInspected)159        Button {160            var report = result.report()161            report.generator = ReportBuilder.generator162            saved = model.store.save(report)163            ORIXHaptics.success()164        } label: { Label(saved ? "Report Saved" : "Save Investigation Report", systemImage: saved ? "checkmark" : "square.and.arrow.down") }165            .buttonStyle(.orixSecondary)166            .disabled(saved)167            .accessibilityIdentifier("investigation.save")168    }169170    @ViewBuilder private func facts(_ evidence: [Evidence], empty: LocalizedStringKey) -> some View {171        if evidence.isEmpty {172            Text(empty).font(.orixCallout).foregroundStyle(ORIX.Palette.textSecondary).frame(maxWidth: .infinity, alignment: .leading).orixCard()173        } else {174            VStack(spacing: 0) {175                ForEach(Array(evidence.enumerated()), id: \.offset) { index, item in176                    ReadingRow(label: item.label, value: .measured(item.value))177                    if index < evidence.count - 1 { Divider() }178                }179            }180            .orixCard(padding: ORIX.Space.sm)181        }182    }183}184185extension InvestigationTool {186    var destination: Destination {187        switch self {188        case .urlAnalyzer: .linkAnalyzer189        case .networkLab: .network190        case .storage: .storage191        case .photoCleanup: .photoCleanup192        case .deviceDiagnostics: .device193        case .securityChecklist: .checklist194        case .fileScanner: .fileScanner195        }196    }197}198
Files main
Menu

Theme