WalletView.swift (6382B)
1 // 2 // WalletView.swift 3 // damus 4 // 5 // Created by William Casarin on 2023-05-05. 6 // 7 8 import SwiftUI 9 10 let WALLET_WARNING_THRESHOLD: UInt64 = 100000 11 12 struct WalletView: View { 13 let damus_state: DamusState 14 @State var show_settings: Bool = false 15 @State var show_send_sheet: Bool = false 16 @ObservedObject var model: WalletModel 17 @ObservedObject var settings: UserSettingsStore 18 @State private var showBalance: Bool = false 19 20 init(damus_state: DamusState, model: WalletModel? = nil) { 21 self.damus_state = damus_state 22 self._model = ObservedObject(wrappedValue: model ?? damus_state.wallet) 23 self._settings = ObservedObject(wrappedValue: damus_state.settings) 24 } 25 26 func MainWalletView(nwc: WalletConnectURL) -> some View { 27 ScrollView { 28 VStack(spacing: 35) { 29 if let balance = model.balance, balance > WALLET_WARNING_THRESHOLD && !settings.dismiss_wallet_high_balance_warning { 30 VStack(spacing: 10) { 31 HStack { 32 Image(systemName: "exclamationmark.circle") 33 Text("Safety Reminder", comment: "Heading for a safety reminder that appears when the user has too many funds, recommending them to learn about safeguarding their funds.") 34 .font(.title3) 35 .bold() 36 } 37 .foregroundStyle(.damusWarningTertiary) 38 39 Text("If your wallet balance is getting high, it's important to understand how to keep your funds secure. Please consider learning the best practices to ensure your assets remain safe. [Click here](https://damus.io/docs/wallet/high-balance-safety-reminder/) to learn more.", comment: "Text reminding the user has a high balance, recommending them to learn about self-custody") 40 .foregroundStyle(.damusWarningSecondary) 41 .accentColor(.damusWarningTertiary) 42 .opacity(0.8) 43 44 Button(action: { 45 settings.dismiss_wallet_high_balance_warning = true 46 }, label: { 47 Text("Dismiss", comment: "Button label to dismiss the safety reminder that the user's wallet has a high balance") 48 }) 49 .bold() 50 .foregroundStyle(.damusWarningTertiary) 51 } 52 .privacySensitive() 53 .padding() 54 .overlay( 55 RoundedRectangle(cornerRadius: 20) 56 .stroke(.damusWarningBorder, lineWidth: 1) 57 ) 58 } 59 60 VStack(spacing: 5) { 61 62 BalanceView(balance: model.balance, hide_balance: $settings.hide_wallet_balance) 63 64 Button(action: { 65 show_send_sheet = true 66 }) { 67 HStack { 68 Image(systemName: "paperplane.fill") 69 Text("Send", comment: "Button label to send bitcoin payment from wallet") 70 .font(.headline) 71 } 72 .padding(.horizontal, 10) 73 } 74 .buttonStyle(GradientButtonStyle()) 75 .padding(.bottom, 20) 76 77 TransactionsView(damus_state: damus_state, transactions: model.transactions, hide_balance: $settings.hide_wallet_balance) 78 } 79 } 80 .navigationTitle(NSLocalizedString("Wallet", comment: "Navigation title for Wallet view")) 81 .navigationBarTitleDisplayMode(.inline) 82 .padding() 83 .padding(.bottom, 50) 84 } 85 } 86 87 var body: some View { 88 switch model.connect_state { 89 case .new: 90 ConnectWalletView(model: model, nav: damus_state.nav, userKeypair: self.damus_state.keypair) 91 case .none: 92 ConnectWalletView(model: model, nav: damus_state.nav, userKeypair: self.damus_state.keypair) 93 case .existing(let nwc): 94 MainWalletView(nwc: nwc) 95 .toolbar { 96 ToolbarItem(placement: .navigationBarTrailing) { 97 Button( 98 action: { show_settings = true }, 99 label: { 100 Image("settings") 101 .foregroundColor(.gray) 102 } 103 ) 104 } 105 } 106 .onAppear() { 107 Task { await self.updateWalletInformation() } 108 } 109 .refreshable { 110 model.resetWalletStateInformation() 111 await self.updateWalletInformation() 112 } 113 .sheet(isPresented: $show_settings, onDismiss: { self.show_settings = false }) { 114 ScrollView { 115 NWCSettings(damus_state: damus_state, nwc: nwc, model: model, settings: settings) 116 .padding(.top, 30) 117 } 118 .presentationDragIndicator(.visible) 119 .presentationDetents([.large]) 120 } 121 .sheet(isPresented: $show_send_sheet) { 122 SendPaymentView(damus_state: damus_state, model: model, nwc: nwc) 123 .presentationDragIndicator(.visible) 124 .presentationDetents([.large]) 125 } 126 } 127 } 128 129 @MainActor 130 func updateWalletInformation() async { 131 await WalletConnect.update_wallet_information(damus_state: damus_state) 132 } 133 } 134 135 let test_wallet_connect_url = WalletConnectURL(pubkey: test_pubkey, relay: .init("wss://relay.damus.io")!, keypair: test_damus_state.keypair.to_full()!, lud16: "jb55@sendsats.com") 136 137 struct WalletView_Previews: PreviewProvider { 138 static let tds = test_damus_state 139 static var previews: some View { 140 WalletView(damus_state: tds, model: WalletModel(state: .existing(test_wallet_connect_url), settings: tds.settings)) 141 } 142 }