|
| 1 | +// |
| 2 | +// ExtendedPublicKeySheet.swift |
| 3 | +// DashWallet |
| 4 | +// |
| 5 | +// Copyright © 2026 Dash Core Group. All rights reserved. |
| 6 | +// |
| 7 | +// Licensed under the MIT License (the "License"); |
| 8 | +// you may not use this file except in compliance with the License. |
| 9 | +// You may obtain a copy of the License at |
| 10 | +// |
| 11 | +// https://opensource.org/licenses/MIT |
| 12 | +// |
| 13 | +// Unless required by applicable law or agreed to in writing, software |
| 14 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +// See the License for the specific language governing permissions and |
| 17 | +// limitations under the License. |
| 18 | +// |
| 19 | + |
| 20 | +import SwiftUI |
| 21 | +import UIKit |
| 22 | + |
| 23 | +// MARK: - ExtendedPublicKeySheetViewModel |
| 24 | + |
| 25 | +class ExtendedPublicKeySheetViewModel: ObservableObject { |
| 26 | + @Published var keyValue: String = "" |
| 27 | + @Published var qrImage: UIImage? = nil |
| 28 | + |
| 29 | + init() { |
| 30 | + loadKey() |
| 31 | + } |
| 32 | + |
| 33 | + private func loadKey() { |
| 34 | + let model = ExtendedPublicKeysModel() |
| 35 | + guard let firstPath = model.derivationPaths.first else { return } |
| 36 | + keyValue = firstPath.item.value |
| 37 | + qrImage = generateQRCode(from: keyValue) |
| 38 | + } |
| 39 | + |
| 40 | + private func generateQRCode(from string: String) -> UIImage? { |
| 41 | + guard !string.isEmpty, |
| 42 | + let filter = CIFilter(name: "CIQRCodeGenerator"), |
| 43 | + let data = string.data(using: .utf8) else { return nil } |
| 44 | + |
| 45 | + filter.setValue(data, forKey: "inputMessage") |
| 46 | + filter.setValue("M", forKey: "inputCorrectionLevel") |
| 47 | + |
| 48 | + guard let outputImage = filter.outputImage else { return nil } |
| 49 | + |
| 50 | + let scale = 180.0 / outputImage.extent.width |
| 51 | + let scaledImage = outputImage.transformed(by: CGAffineTransform(scaleX: scale, y: scale)) |
| 52 | + let context = CIContext() |
| 53 | + guard let cgImage = context.createCGImage(scaledImage, from: scaledImage.extent) else { return nil } |
| 54 | + return UIImage(cgImage: cgImage) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// MARK: - ShareSheet |
| 59 | + |
| 60 | +private struct ShareSheet: UIViewControllerRepresentable { |
| 61 | + let items: [Any] |
| 62 | + |
| 63 | + func makeUIViewController(context: Context) -> UIActivityViewController { |
| 64 | + UIActivityViewController(activityItems: items, applicationActivities: nil) |
| 65 | + } |
| 66 | + |
| 67 | + func updateUIViewController(_ uiViewController: UIActivityViewController, context: Context) {} |
| 68 | +} |
| 69 | + |
| 70 | +// MARK: - ExtendedPublicKeySheet |
| 71 | + |
| 72 | +struct ExtendedPublicKeySheet: View { |
| 73 | + @StateObject private var viewModel = ExtendedPublicKeySheetViewModel() |
| 74 | + @Environment(\.dismiss) private var dismiss |
| 75 | + @Environment(\.colorScheme) private var colorScheme |
| 76 | + |
| 77 | + @State private var isCopied = false |
| 78 | + @State private var showShareSheet = false |
| 79 | + |
| 80 | + var body: some View { |
| 81 | + VStack(spacing: 0) { |
| 82 | + // Grabber |
| 83 | + VStack { |
| 84 | + RoundedRectangle(cornerRadius: 5, style: .continuous) |
| 85 | + .fill(Color.gray300.opacity(0.5)) |
| 86 | + .frame(width: 36, height: 5) |
| 87 | + } |
| 88 | + .frame(maxWidth: .infinity, minHeight: 18) |
| 89 | + |
| 90 | + // Close button |
| 91 | + NavBarClose { |
| 92 | + dismiss() |
| 93 | + } |
| 94 | + |
| 95 | + // QR code |
| 96 | + Group { |
| 97 | + if let qrImage = viewModel.qrImage { |
| 98 | + Image(uiImage: qrImage) |
| 99 | + .interpolation(.none) |
| 100 | + .resizable() |
| 101 | + .frame(width: 180, height: 180) |
| 102 | + } else { |
| 103 | + RoundedRectangle(cornerRadius: 8, style: .continuous) |
| 104 | + .fill(Color.gray300.opacity(0.2)) |
| 105 | + .frame(width: 180, height: 180) |
| 106 | + } |
| 107 | + } |
| 108 | + .padding(.vertical, 30) |
| 109 | + |
| 110 | + // Title + tappable key text |
| 111 | + VStack(alignment: .leading, spacing: 6) { |
| 112 | + Text(NSLocalizedString("Extended public key (BIP 44)", comment: "")) |
| 113 | + .font(.system(size: 28, weight: .bold)) |
| 114 | + .foregroundColor(.primaryText) |
| 115 | + .frame(maxWidth: .infinity, alignment: .leading) |
| 116 | + |
| 117 | + Button(action: { |
| 118 | + guard !viewModel.keyValue.isEmpty else { return } |
| 119 | + UIPasteboard.general.string = viewModel.keyValue |
| 120 | + isCopied = true |
| 121 | + DispatchQueue.main.asyncAfter(deadline: .now() + 1) { |
| 122 | + isCopied = false |
| 123 | + } |
| 124 | + }) { |
| 125 | + Text(viewModel.keyValue.isEmpty |
| 126 | + ? NSLocalizedString("Not available", comment: "") |
| 127 | + : viewModel.keyValue) |
| 128 | + .font(.system(size: 15)) |
| 129 | + .foregroundColor(.secondaryText) |
| 130 | + .multilineTextAlignment(.leading) |
| 131 | + .frame(maxWidth: .infinity, alignment: .leading) |
| 132 | + } |
| 133 | + .buttonStyle(PlainButtonStyle()) |
| 134 | + .overlay( |
| 135 | + Group { |
| 136 | + if isCopied { |
| 137 | + Text(NSLocalizedString("Copied", comment: "")) |
| 138 | + .font(.caption) |
| 139 | + .padding(.horizontal, 12) |
| 140 | + .padding(.vertical, 6) |
| 141 | + .background(copiedBackground) |
| 142 | + .foregroundColor(.whiteText) |
| 143 | + .clipShape(RoundedRectangle(cornerRadius: 8, style: .continuous)) |
| 144 | + } |
| 145 | + } |
| 146 | + ) |
| 147 | + } |
| 148 | + .padding(.horizontal, 60) |
| 149 | + .padding(.top, 20) |
| 150 | + .padding(.bottom, 32) |
| 151 | + |
| 152 | + Spacer() |
| 153 | + |
| 154 | + // Share key button |
| 155 | + DashButton( |
| 156 | + text: NSLocalizedString("Share key", comment: ""), |
| 157 | + style: .tintedBlue, |
| 158 | + action: { showShareSheet = true } |
| 159 | + ) |
| 160 | + .padding(.horizontal, 60) |
| 161 | + .padding(.bottom, 20) |
| 162 | + } |
| 163 | + .background(Color.secondaryBackground) |
| 164 | + .sheet(isPresented: $showShareSheet) { |
| 165 | + if !viewModel.keyValue.isEmpty { |
| 166 | + ShareSheet(items: [viewModel.keyValue]) |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + @ViewBuilder |
| 172 | + private var copiedBackground: some View { |
| 173 | + if colorScheme == .dark { |
| 174 | + ZStack { |
| 175 | + BackgroundBlurView() |
| 176 | + Color.whiteAlpha15 |
| 177 | + } |
| 178 | + } else { |
| 179 | + Color.black.opacity(0.8) |
| 180 | + } |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +// MARK: - Preview |
| 185 | + |
| 186 | +#Preview { |
| 187 | + ExtendedPublicKeySheet() |
| 188 | +} |
0 commit comments