SPB Git

Repository Public License

ShareViewController.swift 4.9 KB 100 lines swift

1// ORIX Secure2// File: ShareExtension/Sources/ShareViewController.swift3// Description: "Analyse Link" share extension: analyses the shared link or text inside the share sheet, with the same offline4//              engine as the app. It never opens the link, makes no network request and stores nothing.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import ORIXDesign9import ORIXSecureModel10import ORIXURLRisk11import SwiftUI12import UIKit13import UniformTypeIdentifiers1415final class ShareViewController: UIViewController {16    override func viewDidLoad() {17        super.viewDidLoad()18        ORIXLocalization.bundle = .main19        let host = UIHostingController(rootView: ShareRootView(loader: { [weak self] in await self?.sharedText() }, close: { [weak self] in20            self?.extensionContext?.completeRequest(returningItems: nil)21        }))22        addChild(host)23        host.view.frame = view.bounds24        host.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]25        view.addSubview(host.view)26        host.didMove(toParent: self)27    }2829    /// The first URL or text among the shared items.30    private func sharedText() async -> String? {31        let providers = (extensionContext?.inputItems as? [NSExtensionItem] ?? []).flatMap { $0.attachments ?? [] }32        for provider in providers where provider.hasItemConformingToTypeIdentifier(UTType.url.identifier) {33            if let url = try? await provider.loadItem(forTypeIdentifier: UTType.url.identifier) as? URL { return url.absoluteString }34        }35        for provider in providers where provider.hasItemConformingToTypeIdentifier(UTType.plainText.identifier) {36            if let text = try? await provider.loadItem(forTypeIdentifier: UTType.plainText.identifier) as? String { return text }37        }38        return nil39    }40}4142private struct ShareRootView: View {43    let loader: () async -> String?44    let close: () -> Void45    @State private var analysis: URLAnalysis?46    @State private var loaded = false4748    var body: some View {49        NavigationStack {50            ScrollView {51                VStack(alignment: .leading, spacing: ORIX.Space.md) {52                    if let analysis {53                        HStack(spacing: ORIX.Space.sm) {54                            ORIXMark().frame(width: 40, height: 40)55                            VStack(alignment: .leading, spacing: 2) {56                                Text(verbatim: analysis.level.title).font(.orixTitle).foregroundStyle(ORIX.Palette.textPrimary)57                                Text(verbatim: analysis.unicodeRegistrableDomain ?? analysis.host ?? analysis.input).font(.orixMono).foregroundStyle(ORIX.Palette.textSecondary).lineLimit(2)58                            }59                        }60                        Text(verbatim: analysis.summary).font(.orixCallout).foregroundStyle(ORIX.Palette.textSecondary)61                        ForEach(analysis.findings) { finding in62                            VStack(alignment: .leading, spacing: 4) {63                                Text(verbatim: finding.title).font(.orixHeadline).foregroundStyle(ORIX.Palette.textPrimary)64                                Text(verbatim: finding.explanation).font(.orixCallout).foregroundStyle(ORIX.Palette.textSecondary)65                                ForEach(finding.evidence) { evidence in66                                    Text(verbatim: "\(evidence.label): \(evidence.value)").font(.orixMono).foregroundStyle(ORIX.Palette.textTertiary)67                                }68                            }69                            .frame(maxWidth: .infinity, alignment: .leading)70                            .orixCard()71                        }72                        ForEach(analysis.notInspected, id: \.self) { item in73                            Label { Text(verbatim: item) } icon: { Image(systemName: "eye.slash") }.font(.orixCaption).foregroundStyle(ORIX.Palette.textTertiary)74                        }75                    } else if loaded {76                        Text("No link was found in what you shared.").font(.orixBody).foregroundStyle(ORIX.Palette.textSecondary)77                    } else {78                        ProgressView().frame(maxWidth: .infinity)79                    }80                }81                .padding(ORIX.Space.md)82            }83            .background(ORIX.Palette.canvas)84            .navigationTitle("ORIX Secure")85            .navigationBarTitleDisplayMode(.inline)86            .toolbar { ToolbarItem(placement: .confirmationAction) { Button("Done", action: close) } }87        }88        .environment(\.orixAccent, .secure)89        .tint(ORIXAccent.secure.primary)90        .task {91            if let text = await loader() {92                let candidates = extractURLs(from: text)93                let target = text.contains(where: \.isWhitespace) ? candidates.first : text94                analysis = target.map { URLRiskAnalyzer().analyze($0) }95            }96            loaded = true97        }98    }99}100
Files main
Menu

Theme