From d10ee72fec9f5ff2bc53d465e46423763495f213 Mon Sep 17 00:00:00 2001 From: Suhail Saqan Date: Thu, 29 Dec 2022 06:04:58 -0600 Subject: [PATCH] add show toggle and default wallet selector --- damus.xcodeproj/project.pbxproj | 4 + damus/Components/InvoiceView.swift | 17 +++- damus/ContentView.swift | 3 +- damus/Models/UserSettings.swift | 124 +++++++++++++++++++++++++++++ damus/Util/Constants.swift | 15 ---- damus/Views/ConfigView.swift | 16 +++- damus/Views/ProfileView.swift | 3 +- damus/Views/SelectWalletView.swift | 46 +++++------ 8 files changed, 184 insertions(+), 44 deletions(-) create mode 100644 damus/Models/UserSettings.swift diff --git a/damus.xcodeproj/project.pbxproj b/damus.xcodeproj/project.pbxproj index 0a549b9e..da6d621e 100644 --- a/damus.xcodeproj/project.pbxproj +++ b/damus.xcodeproj/project.pbxproj @@ -131,6 +131,7 @@ 4CEE2AF9280B2EAC00AB5EEF /* PowView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CEE2AF8280B2EAC00AB5EEF /* PowView.swift */; }; 4CEE2B02280B39E800AB5EEF /* EventActionBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CEE2B01280B39E800AB5EEF /* EventActionBar.swift */; }; 6C7DE41F2955169800E66263 /* Vault in Frameworks */ = {isa = PBXBuildFile; productRef = 6C7DE41E2955169800E66263 /* Vault */; }; + BA693074295D649800ADDB87 /* UserSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA693073295D649800ADDB87 /* UserSettings.swift */; }; BAB68BED29543FA3007BA466 /* SelectWalletView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAB68BEC29543FA3007BA466 /* SelectWalletView.swift */; }; E990020F2955F837003BBC5A /* EditMetadataView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E990020E2955F837003BBC5A /* EditMetadataView.swift */; }; E9E4ED0B295867B900DD7078 /* ThreadV2View.swift in Sources */ = {isa = PBXBuildFile; fileRef = E9E4ED0A295867B900DD7078 /* ThreadV2View.swift */; }; @@ -312,6 +313,7 @@ 4CEE2AF6280B2DEA00AB5EEF /* ProfileName.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileName.swift; sourceTree = ""; }; 4CEE2AF8280B2EAC00AB5EEF /* PowView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PowView.swift; sourceTree = ""; }; 4CEE2B01280B39E800AB5EEF /* EventActionBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventActionBar.swift; sourceTree = ""; }; + BA693073295D649800ADDB87 /* UserSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserSettings.swift; sourceTree = ""; }; BAB68BEC29543FA3007BA466 /* SelectWalletView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelectWalletView.swift; sourceTree = ""; }; E990020E2955F837003BBC5A /* EditMetadataView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditMetadataView.swift; sourceTree = ""; }; E9E4ED0A295867B900DD7078 /* ThreadV2View.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThreadV2View.swift; sourceTree = ""; }; @@ -438,6 +440,7 @@ 4C64987D286D082C00EAE2B3 /* DirectMessagesModel.swift */, 4C216F372871EDE300040376 /* DirectMessageModel.swift */, 4C99737A28C92A9200E53835 /* ChatroomMetadata.swift */, + BA693073295D649800ADDB87 /* UserSettings.swift */, ); path = Models; sourceTree = ""; @@ -799,6 +802,7 @@ 4C363A8428233689006E126D /* Parser.swift in Sources */, 4CE4F9E328528C5200C00DD9 /* AddRelayView.swift in Sources */, 4C363A9A28283854006E126D /* Reply.swift in Sources */, + BA693074295D649800ADDB87 /* UserSettings.swift in Sources */, 4C90BD18283A9EE5008EE7EF /* LoginView.swift in Sources */, 4C3EA66828FF5F9900C48A62 /* hex.c in Sources */, E9E4ED0B295867B900DD7078 /* ThreadV2View.swift in Sources */, diff --git a/damus/Components/InvoiceView.swift b/damus/Components/InvoiceView.swift index 21bd0a73..83ad3fda 100644 --- a/damus/Components/InvoiceView.swift +++ b/damus/Components/InvoiceView.swift @@ -10,15 +10,28 @@ import SwiftUI struct InvoiceView: View { @Environment(\.colorScheme) var colorScheme + @Environment(\.openURL) private var openURL let invoice: Invoice @State var showingSelectWallet: Bool = false @State var inv: String = "" + @StateObject var user_settings = UserSettingsStore() var PayButton: some View { Button { inv = invoice.string - showingSelectWallet = true + if (user_settings.showwalletselector){ + showingSelectWallet = true + } else { + let wallet = get_default_wallet(user_settings.defaultwallet.rawValue) + if let url = URL(string: "\(wallet.link)\(inv)"), UIApplication.shared.canOpenURL(url) { + openURL(url) + } else { + if let url = URL(string: wallet.appStoreLink), UIApplication.shared.canOpenURL(url) { + openURL(url) + } + } + } } label: { RoundedRectangle(cornerRadius: 20) .foregroundColor(colorScheme == .light ? .black : .white) @@ -56,7 +69,7 @@ struct InvoiceView: View { .padding(30) } .sheet(isPresented: $showingSelectWallet, onDismiss: {showingSelectWallet = false}) { - SelectWalletView(showingSelectWallet: $showingSelectWallet, invoice: $inv) + SelectWalletView(showingSelectWallet: $showingSelectWallet, invoice: $inv).environmentObject(user_settings) } } } diff --git a/damus/ContentView.swift b/damus/ContentView.swift index a99006a2..1e885706 100644 --- a/damus/ContentView.swift +++ b/damus/ContentView.swift @@ -69,6 +69,7 @@ struct ContentView: View { @State var search_open: Bool = false @State var filter_state : FilterState = .posts_and_replies @StateObject var home: HomeModel = HomeModel() + @StateObject var user_settings = UserSettingsStore() // connect retry timer let timer = Timer.publish(every: 4, on: .main, in: .common).autoconnect() @@ -216,7 +217,7 @@ struct ContentView: View { .foregroundColor(.gray) } - NavigationLink(destination: ConfigView(state: damus_state!)) { + NavigationLink(destination: ConfigView(state: damus_state!).environmentObject(user_settings)) { Label("", systemImage: "gear") } .buttonStyle(PlainButtonStyle()) diff --git a/damus/Models/UserSettings.swift b/damus/Models/UserSettings.swift new file mode 100644 index 00000000..61364d1a --- /dev/null +++ b/damus/Models/UserSettings.swift @@ -0,0 +1,124 @@ +// +// UserSettings.swift +// damus +// +// Created by Suhail Saqan on 12/29/22. +// + +import Foundation + +struct WalletItem : Decodable, Identifiable, Hashable { + var id: Int + var tag: String + var name : String + var link : String + var appStoreLink : String + var image: String +} + +// New url prefixes needed to be added to LSApplicationQueriesSchemes +enum Wallet: String, CaseIterable { + case defaultwallet = """ + {"id": -1, "tag": "defaultwallet", "name": "Local default", "link": "lightning:", "appStoreLink": "lightning:", "image": ""} + """ + case strike = """ + {"id": 0, "tag": "strike", "name": "Strike", "link": "strike:", "appStoreLink": "https://apps.apple.com/us/app/strike-bitcoin-payments/id1488724463", "image": "strike"} + """ + case cashapp = """ + {"id": 1, "tag": "cashapp", "name": "Cash App", "link": "squarecash://", "appStoreLink": "https://apps.apple.com/us/app/cash-app/id711923939", "image": "cashapp"} + """ + case muun = """ + {"id": 2, "tag": "muun", "name": "Muun", "link": "muun:", "appStoreLink": "https://apps.apple.com/us/app/muun-wallet/id1482037683", "image": "muun"} + """ + case bluewallet = """ + {"id": 3, "tag": "bluewallet", "name": "Blue Wallet", "link": "bluewallet:lightning:", "appStoreLink": "https://apps.apple.com/us/app/bluewallet-bitcoin-wallet/id1376878040", "image": "bluewallet"} + """ + case walletofsatoshi = """ + {"id": 4, "tag": "walletofsatoshi", "name": "Wallet Of Satoshi", "link": "walletofsatoshi:lightning:", "appStoreLink": "https://apps.apple.com/us/app/wallet-of-satoshi/id1438599608", "image": "walletofsatoshi"} + """ + case zebedee = """ + {"id": 5, "tag": "zebedee", "name": "Zebedee", "link": "zebedee:lightning:", "appStoreLink": "https://apps.apple.com/us/app/zebedee-wallet/id1484394401", "image": "zebedee"} + """ + case zeusln = """ + {"id": 6, "tag": "zeusln", "name": "Zeus LN", "link": "zeusln:lightning:", "appStoreLink": "https://apps.apple.com/us/app/zeus-ln/id1456038895", "image": "zeusln"} + """ + case lnlink = """ + {"id": 7, "tag": "lnlink", "name": "LNLink", "link": "lnlink:lightning:", "appStoreLink": "https://testflight.apple.com/join/aNY4yuuZ", "image": "lnlink"} + """ + case phoenix = """ + {"id": 8, "tag": "phoenix", "name": "Phoenix", "link": "phoenix://", "appStoreLink": "https://apps.apple.com/us/app/phoenix-wallet/id1544097028", "image": "phoenix"} + """ +} + +class UserSettingsStore: ObservableObject { + @Published var defaultwallet: Wallet { + didSet { + UserDefaults.standard.set(defaultwallet.rawValue, forKey: "defaultwallet") + } + } + + @Published var showwalletselector: Bool { + didSet { + UserDefaults.standard.set(showwalletselector, forKey: "showwalletselector") + } + } + + init() { + self.defaultwallet = (UserDefaults.standard.object(forKey: "defaultwallet") == nil ? Wallet.defaultwallet : Wallet(rawValue: UserDefaults.standard.object(forKey: "defaultwallet") as! String)) ?? Wallet.defaultwallet + self.showwalletselector = UserDefaults.standard.object(forKey: "showwalletselector") == nil ? true : UserDefaults.standard.object(forKey: "showwalletselector") as! Bool + } +} + +func get_wallet_list() -> [WalletItem] { + let values: [String] = Wallet.allCases.map { $0.rawValue } + + var walletList: [WalletItem] = [] + + for value in values { + let data = value.data(using: .utf8)! + do { + let wallet = try JSONDecoder().decode(WalletItem.self, from: data) + walletList.append(wallet) + } catch { + return [] + } + } + return walletList +} + +func get_wallet_tag(_ tag: String) -> Wallet { + switch tag { + case "defaultwallet": + return Wallet.defaultwallet + case "strike": + return Wallet.strike + case "cashapp": + return Wallet.cashapp + case "muun": + return Wallet.muun + case "bluewallet": + return Wallet.bluewallet + case "walletofsatoshi": + return Wallet.walletofsatoshi + case "zebedee": + return Wallet.zebedee + case "zeusln": + return Wallet.zeusln + case "lnlink": + return Wallet.lnlink + case "phoenix": + return Wallet.phoenix + default: + return Wallet.defaultwallet + } +} + +func get_default_wallet(_ us: String) -> WalletItem { + let data = us.data(using: .utf8)! + do { + return try JSONDecoder().decode(WalletItem.self, from: data) + } catch { + return get_wallet_list()[0] + } + +} diff --git a/damus/Util/Constants.swift b/damus/Util/Constants.swift index 07b5edd5..63199b11 100644 --- a/damus/Util/Constants.swift +++ b/damus/Util/Constants.swift @@ -24,19 +24,4 @@ public class Constants { NostrEvent(id: UUID().description, content: "Hello World! This is so cool!", pubkey: "3efdaebb1d8923ebd99c9e7ace3b4194ab45512e2be79c1b7d68d9243e0d2681"), NostrEvent(id: UUID().description, content: "Bonjour Le Monde", pubkey: "3efdaebb1d8923ebd99c9e7ace3b4194ab45512e2be79c1b7d68d9243e0d2681"), ] - - // New url prefixes needed to be added to LSApplicationQueriesSchemes - static let WALLETS = """ - [ - {"id": 0, "name": "Strike", "link": "strike:", "appStoreLink": "https://apps.apple.com/us/app/strike-bitcoin-payments/id1488724463", "image": "strike"}, - {"id": 1, "name": "Cash App", "link": "squarecash://", "appStoreLink": "https://apps.apple.com/us/app/cash-app/id711923939", "image": "cashapp"}, - {"id": 2, "name": "Muun", "link": "muun:", "appStoreLink": "https://apps.apple.com/us/app/muun-wallet/id1482037683", "image": "muun"}, - {"id": 3, "name": "Blue Wallet", "link": "bluewallet:lightning:", "appStoreLink": "https://apps.apple.com/us/app/bluewallet-bitcoin-wallet/id1376878040", "image": "bluewallet"}, - {"id": 4, "name": "Wallet Of Satoshi", "link": "walletofsatoshi:lightning:", "appStoreLink": "https://apps.apple.com/us/app/wallet-of-satoshi/id1438599608", "image": "walletofsatoshi"}, - {"id": 5, "name": "Zebedee", "link": "zebedee:lightning:", "appStoreLink": "https://apps.apple.com/us/app/zebedee-wallet/id1484394401", "image": "zebedee"}, - {"id": 6, "name": "Zeus LN", "link": "zeusln:lightning:", "appStoreLink": "https://apps.apple.com/us/app/zeus-ln/id1456038895", "image": "zeusln"}, - {"id": 7, "name": "LNLink", "link": "lnlink:lightning:", "appStoreLink": "https://testflight.apple.com/join/aNY4yuuZ", "image": "lnlink"}, - {"id": 8, "name": "Phoenix", "link": "phoenix://", "appStoreLink": "https://apps.apple.com/us/app/phoenix-wallet/id1544097028", "image": "phoenix"}, - ] - """.data(using: .utf8)! } diff --git a/damus/Views/ConfigView.swift b/damus/Views/ConfigView.swift index 88798102..4dd8cc4c 100644 --- a/damus/Views/ConfigView.swift +++ b/damus/Views/ConfigView.swift @@ -17,7 +17,10 @@ struct ConfigView: View { @State var privkey: String @State var privkey_copied: Bool = false @State var pubkey_copied: Bool = false - + @EnvironmentObject var user_settings: UserSettingsStore + + let walletItems: [WalletItem] = get_wallet_list() + let generator = UIImpactFeedbackGenerator(style: .light) init(state: DamusState) { @@ -78,6 +81,17 @@ struct ConfigView: View { } } + Section("Wallet Selector") { + Toggle("Show wallet selector", isOn: $user_settings.showwalletselector).toggleStyle(.switch) + if walletItems != [] { + Picker(selection: $user_settings.defaultwallet, label: Text("Select default wallet"), content: { + ForEach(walletItems, id: \.self) { wallet in + Text(wallet.name).tag(get_wallet_tag(wallet.tag)) + } + }) + } + } + Section("Reset") { Button("Logout") { confirm_logout = true diff --git a/damus/Views/ProfileView.swift b/damus/Views/ProfileView.swift index 0b820b75..1614a36e 100644 --- a/damus/Views/ProfileView.swift +++ b/damus/Views/ProfileView.swift @@ -118,6 +118,7 @@ struct ProfileView: View { @State private var selected_tab: ProfileTab = .posts @StateObject var profile: ProfileModel @StateObject var followers: FollowersModel + @StateObject var user_settings = UserSettingsStore() @State private var showingEditProfile = false @State var showingSelectWallet: Bool = false @State var inv: String = "" @@ -149,7 +150,7 @@ struct ProfileView: View { } } }.sheet(isPresented: $showingSelectWallet, onDismiss: {showingSelectWallet = false}) { - SelectWalletView(showingSelectWallet: $showingSelectWallet, invoice: $inv) + SelectWalletView(showingSelectWallet: $showingSelectWallet, invoice: $inv).environmentObject(user_settings) } } diff --git a/damus/Views/SelectWalletView.swift b/damus/Views/SelectWalletView.swift index 5573324f..fa42d7ec 100644 --- a/damus/Views/SelectWalletView.swift +++ b/damus/Views/SelectWalletView.swift @@ -7,23 +7,15 @@ import SwiftUI -struct WalletItem : Decodable, Identifiable, Hashable { - var id: Int - var name : String - var link : String - var appStoreLink : String - var image: String -} - struct SelectWalletView: View { @Binding var showingSelectWallet: Bool @Binding var invoice: String @Environment(\.openURL) private var openURL @State var invoice_copied: Bool = false + @EnvironmentObject var user_settings: UserSettingsStore let generator = UIImpactFeedbackGenerator(style: .light) - - let walletItems = try! JSONDecoder().decode([WalletItem].self, from: Constants.WALLETS) + let walletItems: [WalletItem] = get_wallet_list() var body: some View { NavigationView { @@ -46,8 +38,13 @@ struct SelectWalletView: View { Section("Select a lightning wallet"){ List{ Button() { - if let url = URL(string: "lightning:\(invoice)"), UIApplication.shared.canOpenURL(url) { + let wallet = get_default_wallet(user_settings.defaultwallet.rawValue) + if let url = URL(string: "\(wallet.link)\(invoice)"), UIApplication.shared.canOpenURL(url) { openURL(url) + } else { + if let url = URL(string: wallet.appStoreLink), UIApplication.shared.canOpenURL(url) { + openURL(url) + } } } label: { HStack { @@ -55,21 +52,22 @@ struct SelectWalletView: View { } }.buttonStyle(.plain) ForEach(walletItems, id: \.self) { wallet in - Button() { - if let url = URL(string: "\(wallet.link)\(invoice)"), UIApplication.shared.canOpenURL(url) { - print("opening wallet url \(url)") - openURL(url) - } else { - if let url = URL(string: wallet.appStoreLink), UIApplication.shared.canOpenURL(url) { + if (wallet.id >= 0){ + Button() { + if let url = URL(string: "\(wallet.link)\(invoice)"), UIApplication.shared.canOpenURL(url) { openURL(url) + } else { + if let url = URL(string: wallet.appStoreLink), UIApplication.shared.canOpenURL(url) { + openURL(url) + } } - } - } label: { - HStack { - Image(wallet.image).resizable().frame(width: 32.0, height: 32.0,alignment: .center).cornerRadius(5) - Text(wallet.name).font(.body) - } - }.buttonStyle(.plain) + } label: { + HStack { + Image(wallet.image).resizable().frame(width: 32.0, height: 32.0,alignment: .center).cornerRadius(5) + Text(wallet.name).font(.body) + } + }.buttonStyle(.plain) + } } }.padding(.vertical, 2.5) }