Files
simplex-chat/apps/ios/Shared/Views/Chat/ScanCodeView.swift
T
Narasimha-sc 435a42c00b ui: recognise every QR code in every scanner, with a clear wrong-type alert
Every QR scanner now recognises every kind of code the app uses —
connection links, server addresses, migration links, desktop addresses
and a contact's security code — instead of only the one kind its screen
expects. When a code of the wrong kind is scanned, one shared alert says
what it is (a complete sentence) and where to scan it, and offers to
connect directly when it is a connection link. A single sealed type, one
classifier and one alert serve every screen on Android, desktop and iOS.
2026-06-22 16:23:06 +00:00

64 lines
2.0 KiB
Swift

//
// ScanCodeView.swift
// SimpleX (iOS)
//
// Created by Evgeny on 10/12/2022.
// Copyright © 2022 SimpleX Chat. All rights reserved.
//
import SwiftUI
import CodeScanner
struct ScanCodeView: View {
@Environment(\.dismiss) var dismiss: DismissAction
@EnvironmentObject var theme: AppTheme
@Binding var connectionVerified: Bool
var verify: (String?) async -> (Bool, String)?
@State private var showCodeError = false
var body: some View {
VStack(alignment: .leading) {
CodeScannerView(codeTypes: [.qr], scanMode: .oncePerCode, completion: processQRCode)
.aspectRatio(1, contentMode: .fit)
.cornerRadius(12)
Text("Scan security code from your contact's app.")
.padding(.top)
}
.padding()
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
.alert(isPresented: $showCodeError) {
Alert(title: Text("Incorrect security code!"))
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
}
func processQRCode(_ resp: Result<ScanResult, ScanError>) {
switch resp {
case let .success(r):
handleScan(r.string, expected: .securityCode, theme: theme) { qr in
Task {
if let (ok, _) = await verify(qr.text) {
await MainActor.run {
connectionVerified = ok
if ok {
dismiss()
} else {
showCodeError = true
}
}
}
}
}
case let .failure(e):
logger.error("ScanCodeView.processQRCode QR code error: \(e.localizedDescription)")
dismiss()
}
}
}
struct ScanCodeView_Previews: PreviewProvider {
static var previews: some View {
ScanCodeView(connectionVerified: Binding.constant(true), verify: {_ in nil})
}
}