BusinessCard/BusinessCard/Views/ShareCardView.swift

237 lines
8.4 KiB
Swift

import SwiftUI
import Bedrock
import SwiftData
struct ShareCardView: View {
@Environment(AppState.self) private var appState
@State private var showingWalletAlert = false
@State private var showingNfcAlert = false
@State private var showingContactSheet = false
@State private var recipientName = ""
@State private var recipientRole = ""
@State private var recipientCompany = ""
var body: some View {
NavigationStack {
ScrollView {
VStack(spacing: Design.Spacing.large) {
Text("Share with anyone")
.font(.title2)
.bold()
.foregroundStyle(Color.Text.primary)
if let card = appState.cardStore.selectedCard {
QRCodeCardView(card: card)
ShareOptionsView(
card: card,
shareLinkService: appState.shareLinkService,
showWallet: { showingWalletAlert = true },
showNfc: { showingNfcAlert = true }
)
TrackShareButton { showingContactSheet = true }
} else {
EmptyStateView(
title: String.localized("No card selected"),
message: String.localized("Choose a card in the My Cards tab to start sharing.")
)
}
}
.padding(.horizontal, Design.Spacing.large)
.padding(.vertical, Design.Spacing.xLarge)
}
.background(Color.AppBackground.base)
.navigationTitle(String.localized("Send Work Card"))
.alert(String.localized("Apple Wallet"), isPresented: $showingWalletAlert) {
Button(String.localized("OK")) { }
} message: {
Text("Wallet export is coming soon. We'll let you know as soon as it's ready.")
}
.alert(String.localized("NFC Sharing"), isPresented: $showingNfcAlert) {
Button(String.localized("OK")) { }
} message: {
Text("Hold your phone near another device to share instantly. NFC setup is on the way.")
}
.sheet(isPresented: $showingContactSheet) {
RecordContactSheet(
recipientName: $recipientName,
recipientRole: $recipientRole,
recipientCompany: $recipientCompany
) {
saveContact()
}
}
}
}
private func saveContact() {
guard !recipientName.isEmpty, let card = appState.cardStore.selectedCard else { return }
appState.contactsStore.recordShare(
for: recipientName,
role: recipientRole,
company: recipientCompany,
cardLabel: card.label
)
recipientName = ""
recipientRole = ""
recipientCompany = ""
}
}
// MARK: - QR Code Display
private struct QRCodeCardView: View {
let card: BusinessCard
private var textColor: Color { card.theme.textColor }
var body: some View {
VStack(spacing: Design.Spacing.medium) {
QRCodeView(payload: card.vCardPayload)
.frame(width: Design.CardSize.qrSize, height: Design.CardSize.qrSize)
.padding(Design.Spacing.medium)
.background(Color.AppBackground.elevated)
.clipShape(.rect(cornerRadius: Design.CornerRadius.large))
Text("Point your camera at the QR code to receive the card")
.font(.subheadline)
.foregroundStyle(textColor.opacity(Design.Opacity.strong))
.multilineTextAlignment(.center)
}
.padding(Design.Spacing.large)
.background(card.theme.primaryColor)
.clipShape(.rect(cornerRadius: Design.CornerRadius.xLarge))
.shadow(
color: Color.Text.secondary.opacity(Design.Opacity.hint),
radius: Design.Shadow.radiusLarge,
x: Design.Shadow.offsetNone,
y: Design.Shadow.offsetSmall
)
}
}
// MARK: - Share Options
private struct ShareOptionsView: View {
let card: BusinessCard
let shareLinkService: ShareLinkProviding
let showWallet: () -> Void
let showNfc: () -> Void
var body: some View {
VStack(spacing: Design.Spacing.small) {
ShareOptionRow.share(
title: String.localized("Copy link"),
systemImage: "link",
item: shareLinkService.shareURL(for: card)
)
ShareOptionRow.link(
title: String.localized("Text your card"),
systemImage: "message",
url: shareLinkService.smsURL(for: card)
)
ShareOptionRow.link(
title: String.localized("Email your card"),
systemImage: "envelope",
url: shareLinkService.emailURL(for: card)
)
ShareOptionRow.link(
title: String.localized("Send via WhatsApp"),
systemImage: "message.fill",
url: shareLinkService.whatsappURL(for: card)
)
ShareOptionRow.link(
title: String.localized("Send via LinkedIn"),
systemImage: "link.circle",
url: shareLinkService.linkedInURL(for: card)
)
ShareOptionRow.action(
title: String.localized("Add to Apple Wallet"),
systemImage: "wallet.pass",
action: showWallet
)
ShareOptionRow.action(
title: String.localized("Share via NFC"),
systemImage: "dot.radiowaves.left.and.right",
action: showNfc
)
}
.padding(Design.Spacing.large)
.background(Color.AppBackground.elevated)
.clipShape(.rect(cornerRadius: Design.CornerRadius.large))
}
}
// MARK: - Track Button
private struct TrackShareButton: View {
let action: () -> Void
var body: some View {
Button(action: action) {
ActionRowContent(
title: String.localized("Track this share"),
subtitle: String.localized("Record who received your card"),
systemImage: "person.badge.plus"
)
}
.buttonStyle(.plain)
.accessibilityHint(String.localized("Opens a form to record who you shared your card with"))
}
}
// MARK: - Share Option Row
private enum ShareOptionRow {
static func link(title: String, systemImage: String, url: URL) -> some View {
Link(destination: url) {
RowContent(title: title, systemImage: systemImage)
}
.buttonStyle(.plain)
}
static func share(title: String, systemImage: String, item: URL) -> some View {
ShareLink(item: item) {
RowContent(title: title, systemImage: systemImage)
}
.buttonStyle(.plain)
}
static func action(title: String, systemImage: String, action: @escaping () -> Void) -> some View {
Button(action: action) {
RowContent(title: title, systemImage: systemImage)
}
.buttonStyle(.plain)
}
}
private struct RowContent: View {
let title: String
let systemImage: String
var body: some View {
HStack(spacing: Design.Spacing.medium) {
Image(systemName: systemImage)
.foregroundStyle(Color.Accent.red)
.frame(width: Design.CardSize.avatarSize, height: Design.CardSize.avatarSize)
.background(Color.AppBackground.accent)
.clipShape(.rect(cornerRadius: Design.CornerRadius.medium))
Text(title)
.foregroundStyle(Color.Text.primary)
Spacer()
Image(systemName: "chevron.right")
.foregroundStyle(Color.Text.secondary)
}
.padding(.horizontal, Design.Spacing.medium)
.padding(.vertical, Design.Spacing.small)
.background(Color.AppBackground.base)
.clipShape(.rect(cornerRadius: Design.CornerRadius.medium))
}
}
// MARK: - Preview
#Preview {
ShareCardView()
.environment(AppState(modelContext: try! ModelContainer(for: BusinessCard.self, Contact.self).mainContext))
}