mirror of
https://github.com/sudosylabs/vnidrop.git
synced 2026-08-05 18:39:55 +02:00
Enable complete strict concurrency and switch the app target to Swift 6. - Isolate model dependency protocols to @MainActor - Make the CoreRepository blocking-FFI bridge race-free: nonisolated(unsafe) core handle, nonisolated runCore/readSnapshot, @Sendable work block, Sendable domain models - Fix Binding method-reference captures; @preconcurrency imports for CoreNFC/AVFoundation/VnidropCore; isolate the NFC/QR delegate helpers
147 lines
5.8 KiB
Swift
147 lines
5.8 KiB
Swift
import SwiftUI
|
|
|
|
enum ReceiveMethodAvailability { case available, unavailable, hidden }
|
|
|
|
/// Invitation acquisition actions, ported from `ReceiveInvitationActions` (iosMain).
|
|
@MainActor
|
|
protocol ReceiveInvitationActions: AnyObject {
|
|
var fileAvailability: ReceiveMethodAvailability { get }
|
|
var qrAvailability: ReceiveMethodAvailability { get }
|
|
var nfcAvailability: ReceiveMethodAvailability { get }
|
|
|
|
func pickInvitation(onResult: @escaping (Result<String, Error>) -> Void)
|
|
func scanQrCode(onResult: @escaping (Result<String, Error>) -> Void)
|
|
func readNfcInvitation(onResult: @escaping (Result<String, Error>) -> Void)
|
|
func cancel()
|
|
}
|
|
|
|
/// Method chooser panel, ported from `ReceiveMethodPanel` in `ReceiveScreen.kt`.
|
|
struct ReceiveMethodPanel: View {
|
|
@Environment(\.vniColors) private var colors
|
|
@ObservedObject var model: ReceiveModel
|
|
@State private var actions: ReceiveInvitationActions = makeReceiveInvitationActions()
|
|
|
|
var body: some View {
|
|
VStack(alignment: .leading, spacing: 12) {
|
|
Text(LocalizedStringKey("receive_choose_method_title")).font(VniType.titleLarge)
|
|
Text(LocalizedStringKey("receive_choose_method_body")).foregroundStyle(colors.foregroundLighter)
|
|
|
|
MethodRow(
|
|
icon: "doc", titleKey: "receive_method_file", descKey: "receive_method_file_description",
|
|
availability: actions.fileAvailability
|
|
) { actions.pickInvitation { model.onInvitationResult(.invitationFile, $0) } }
|
|
|
|
if actions.qrAvailability != .hidden {
|
|
MethodRow(
|
|
icon: "qrcode.viewfinder", titleKey: "receive_method_scan", descKey: "receive_method_scan_description",
|
|
availability: actions.qrAvailability
|
|
) { actions.scanQrCode { model.onInvitationResult(.qrCode, $0) } }
|
|
}
|
|
if actions.nfcAvailability != .hidden {
|
|
MethodRow(
|
|
icon: "wave.3.right",
|
|
titleOverride: model.state.isWaitingForNfc ? String(localized: "receive_nfc_waiting") : nil,
|
|
titleKey: "receive_method_nfc", descKey: "receive_method_nfc_description",
|
|
availability: model.state.isWaitingForNfc ? .unavailable : actions.nfcAvailability
|
|
) {
|
|
model.setWaitingForNfc(true)
|
|
actions.readNfcInvitation { model.onInvitationResult(.nfc, $0) }
|
|
}
|
|
}
|
|
}
|
|
.padding(.horizontal, 20).padding(.vertical, 14)
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
}
|
|
}
|
|
|
|
private struct MethodRow: View {
|
|
@Environment(\.vniColors) private var colors
|
|
let icon: String
|
|
var titleOverride: String? = nil
|
|
let titleKey: String
|
|
let descKey: String
|
|
let availability: ReceiveMethodAvailability
|
|
let onTap: () -> Void
|
|
|
|
var body: some View {
|
|
let enabled = availability == .available
|
|
Button(action: onTap) {
|
|
HStack(spacing: 14) {
|
|
Image(systemName: icon).font(.system(size: 22))
|
|
.foregroundStyle(enabled ? colors.brandLink : colors.foregroundLighter)
|
|
.frame(width: 24)
|
|
VStack(alignment: .leading, spacing: 3) {
|
|
if let titleOverride {
|
|
Text(titleOverride).font(VniType.bodyLarge)
|
|
} else {
|
|
Text(LocalizedStringKey(titleKey)).font(VniType.bodyLarge)
|
|
}
|
|
Text(LocalizedStringKey(descKey)).font(VniType.bodySmall).foregroundStyle(colors.foregroundLighter)
|
|
}
|
|
Spacer()
|
|
if availability == .unavailable {
|
|
Text(LocalizedStringKey("value_unavailable")).font(VniType.labelSmall).foregroundStyle(colors.foregroundLighter)
|
|
}
|
|
}
|
|
.padding(16)
|
|
.frame(maxWidth: .infinity)
|
|
.background(colors.backgroundSurface200, in: RoundedRectangle(cornerRadius: 14))
|
|
.contentShape(Rectangle())
|
|
}
|
|
.buttonStyle(.plain)
|
|
.disabled(!enabled)
|
|
}
|
|
}
|
|
|
|
/// Invitation review panel, ported from `InvitationReviewPanel` in `ReceiveScreen.kt`.
|
|
struct InvitationReviewPanel: View {
|
|
@Environment(\.vniColors) private var colors
|
|
@ObservedObject var model: ReceiveModel
|
|
|
|
private var state: ReceiveState { model.state }
|
|
|
|
var body: some View {
|
|
VStack(alignment: .leading, spacing: 14) {
|
|
Text(LocalizedStringKey("receive_review_title")).font(VniType.titleLarge)
|
|
if state.isInspecting {
|
|
ProgressView().frame(maxWidth: .infinity).padding(40)
|
|
}
|
|
if let inspection = state.inspection {
|
|
let metadata = inspection.metadata
|
|
VStack(alignment: .leading, spacing: 8) {
|
|
Text(metadata.transferName).font(VniType.bodyLarge).lineLimit(2)
|
|
Text("\(metadata.fileCount) \(String(localized: "metadata_files").lowercased()) · \(formatBytes(metadata.totalSize))")
|
|
.foregroundStyle(colors.foregroundLighter)
|
|
}
|
|
.padding(16)
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
.background(colors.backgroundSurface200, in: RoundedRectangle(cornerRadius: 14))
|
|
|
|
Field(label: String(localized: "field_receiver_name"),
|
|
value: Binding(get: { state.receiverName }, set: { model.setReceiverName($0) }))
|
|
Text(state.receiveFolder?.displayName ?? String(localized: "value_unavailable"))
|
|
.font(VniType.bodySmall)
|
|
.foregroundStyle(state.folderAccessStatus == .writable ? colors.foregroundLight : colors.destructiveDefault)
|
|
|
|
if state.isReceiving {
|
|
let progressId = state.activeReceiveTransferId
|
|
?? model.coreState.events.first { $0.direction == "receive" && $0.transferId != nil }?.transferId
|
|
let progress = progressId.flatMap { progressForTransfer(events: model.coreState.events, transferId: $0) }
|
|
ProgressRow(labelKey: progress?.labelKey ?? "progress_receiving", progress: progress?.progress, detail: progress?.detail)
|
|
SecondaryButton(title: String(localized: "button_cancel_receive"), action: model.cancelActiveReceive)
|
|
} else {
|
|
PrimaryButton(
|
|
title: String(localized: "button_receive"), action: model.receive,
|
|
enabled: state.canReceive(coreInitialized: model.coreState.isInitialized)
|
|
)
|
|
}
|
|
if let error = state.lastReceiveError {
|
|
Text(error.resolved()).font(VniType.bodySmall).foregroundStyle(colors.destructiveDefault)
|
|
}
|
|
}
|
|
}
|
|
.padding(.horizontal, 20).padding(.vertical, 14)
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
}
|
|
}
|