From 403a22125aac121bb31c3d219119213e7f078507 Mon Sep 17 00:00:00 2001 From: Suhail Saqan <43693074+suhailsaqan@users.noreply.github.com> Date: Thu, 29 Dec 2022 23:30:56 -0800 Subject: [PATCH] Revert "Bh updates to improve wallet selector" --- damus.xcodeproj/project.pbxproj | 12 +-- damus/Components/InvoiceView.swift | 10 +-- damus/Models/UserSettings.swift | 124 +++++++++++++++++++++++++++ damus/Models/UserSettingsStore.swift | 32 ------- damus/Models/Wallet.swift | 72 ---------------- damus/Views/ConfigView.swift | 18 ++-- damus/Views/ProfileView.swift | 3 +- damus/Views/SelectWalletView.swift | 64 +++++++------- 8 files changed, 175 insertions(+), 160 deletions(-) create mode 100644 damus/Models/UserSettings.swift delete mode 100644 damus/Models/UserSettingsStore.swift delete mode 100644 damus/Models/Wallet.swift diff --git a/damus.xcodeproj/project.pbxproj b/damus.xcodeproj/project.pbxproj index 05ef357..da6d621 100644 --- a/damus.xcodeproj/project.pbxproj +++ b/damus.xcodeproj/project.pbxproj @@ -130,9 +130,8 @@ 4CEE2AF7280B2DEA00AB5EEF /* ProfileName.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CEE2AF6280B2DEA00AB5EEF /* ProfileName.swift */; }; 4CEE2AF9280B2EAC00AB5EEF /* PowView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CEE2AF8280B2EAC00AB5EEF /* PowView.swift */; }; 4CEE2B02280B39E800AB5EEF /* EventActionBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CEE2B01280B39E800AB5EEF /* EventActionBar.swift */; }; - 4FE60CDD295E1C5E00105A1F /* Wallet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4FE60CDC295E1C5E00105A1F /* Wallet.swift */; }; 6C7DE41F2955169800E66263 /* Vault in Frameworks */ = {isa = PBXBuildFile; productRef = 6C7DE41E2955169800E66263 /* Vault */; }; - BA693074295D649800ADDB87 /* UserSettingsStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA693073295D649800ADDB87 /* UserSettingsStore.swift */; }; + 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 */; }; @@ -314,8 +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 = ""; }; - 4FE60CDC295E1C5E00105A1F /* Wallet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Wallet.swift; sourceTree = ""; }; - BA693073295D649800ADDB87 /* UserSettingsStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserSettingsStore.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 = ""; }; @@ -442,8 +440,7 @@ 4C64987D286D082C00EAE2B3 /* DirectMessagesModel.swift */, 4C216F372871EDE300040376 /* DirectMessageModel.swift */, 4C99737A28C92A9200E53835 /* ChatroomMetadata.swift */, - BA693073295D649800ADDB87 /* UserSettingsStore.swift */, - 4FE60CDC295E1C5E00105A1F /* Wallet.swift */, + BA693073295D649800ADDB87 /* UserSettings.swift */, ); path = Models; sourceTree = ""; @@ -805,7 +802,7 @@ 4C363A8428233689006E126D /* Parser.swift in Sources */, 4CE4F9E328528C5200C00DD9 /* AddRelayView.swift in Sources */, 4C363A9A28283854006E126D /* Reply.swift in Sources */, - BA693074295D649800ADDB87 /* UserSettingsStore.swift in Sources */, + BA693074295D649800ADDB87 /* UserSettings.swift in Sources */, 4C90BD18283A9EE5008EE7EF /* LoginView.swift in Sources */, 4C3EA66828FF5F9900C48A62 /* hex.c in Sources */, E9E4ED0B295867B900DD7078 /* ThreadV2View.swift in Sources */, @@ -875,7 +872,6 @@ 4CACA9D5280C31E100D9BBE8 /* ReplyView.swift in Sources */, 4C0A3F97280F8E02000448DE /* ThreadView.swift in Sources */, 4C06670B28FDE64700038D2A /* damus.c in Sources */, - 4FE60CDD295E1C5E00105A1F /* Wallet.swift in Sources */, 4C99737B28C92A9200E53835 /* ChatroomMetadata.swift in Sources */, 4C75EFA427FA577B0006080F /* PostView.swift in Sources */, 4C75EFB528049D790006080F /* Relay.swift in Sources */, diff --git a/damus/Components/InvoiceView.swift b/damus/Components/InvoiceView.swift index 14cba43..83ad3fd 100644 --- a/damus/Components/InvoiceView.swift +++ b/damus/Components/InvoiceView.swift @@ -15,19 +15,19 @@ struct InvoiceView: View { let invoice: Invoice @State var showingSelectWallet: Bool = false @State var inv: String = "" - @ObservedObject var user_settings = UserSettingsStore() + @StateObject var user_settings = UserSettingsStore() var PayButton: some View { Button { inv = invoice.string - if user_settings.showWalletSelector { + if (user_settings.showwalletselector){ showingSelectWallet = true } else { - let walletModel = user_settings.defaultWallet.model - if let url = URL(string: "\(walletModel.link)\(inv)"), UIApplication.shared.canOpenURL(url) { + 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: walletModel.appStoreLink), UIApplication.shared.canOpenURL(url) { + if let url = URL(string: wallet.appStoreLink), UIApplication.shared.canOpenURL(url) { openURL(url) } } diff --git a/damus/Models/UserSettings.swift b/damus/Models/UserSettings.swift new file mode 100644 index 0000000..61364d1 --- /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/Models/UserSettingsStore.swift b/damus/Models/UserSettingsStore.swift deleted file mode 100644 index 7cd767e..0000000 --- a/damus/Models/UserSettingsStore.swift +++ /dev/null @@ -1,32 +0,0 @@ -// -// UserSettingsStore.swift -// damus -// -// Created by Suhail Saqan on 12/29/22. -// - -import Foundation - -class UserSettingsStore: ObservableObject { - @Published var defaultWallet: Wallet { - didSet { - UserDefaults.standard.set(defaultWallet.rawValue, forKey: "default_wallet") - } - } - - @Published var showWalletSelector: Bool { - didSet { - UserDefaults.standard.set(showWalletSelector, forKey: "show_wallet_selector") - } - } - - init() { - if let defaultWalletName = UserDefaults.standard.string(forKey: "default_wallet"), - let defaultWallet = Wallet(rawValue: defaultWalletName) { - self.defaultWallet = defaultWallet - } else { - self.defaultWallet = .systemdefaultwallet - } - self.showWalletSelector = UserDefaults.standard.object(forKey: "show_wallet_selector") as? Bool ?? true - } -} diff --git a/damus/Models/Wallet.swift b/damus/Models/Wallet.swift deleted file mode 100644 index 3ede2ae..0000000 --- a/damus/Models/Wallet.swift +++ /dev/null @@ -1,72 +0,0 @@ -// -// Wallet.swift -// damus -// -// Created by Benjamin Hakes on 12/29/22. -// - -import Foundation - -enum Wallet: String, CaseIterable, Identifiable { - var id: String { self.rawValue } - - struct Model: Identifiable, Hashable { - var id: String { self.tag } - var index: Int - var tag: String - var displayName : String - var link : String - var appStoreLink : String - var image: String - } - - // New url prefixes needed to be added to LSApplicationQueriesSchemes - case systemdefaultwallet - case strike - case cashapp - case muun - case bluewallet - case walletofsatoshi - case zebedee - case zeusln - case lnlink - case phoenix - - var model: Model { - switch self { - case .systemdefaultwallet: - return .init(index: -1, tag: "systemdefaultwallet", displayName: "Local default", - link: "lightning:", appStoreLink: "lightning:", image: "") - case .strike: - return .init(index: 0, tag: "strike", displayName: "Strike", link: "strike:", - appStoreLink: "https://apps.apple.com/us/app/strike-bitcoin-payments/id1488724463", image: "strike") - case .cashapp: - return .init(index: 1, tag: "cashapp", displayName: "Cash App", link: "squarecash://", - appStoreLink: "https://apps.apple.com/us/app/cash-app/id711923939", image: "cashapp") - case .muun: - return .init(index: 2, tag: "muun", displayName: "Muun", link: "muun:", appStoreLink: "https://apps.apple.com/us/app/muun-wallet/id1482037683", image: "muun") - case .bluewallet: - return .init(index: 3, tag: "bluewallet", displayName: "Blue Wallet", link: "bluewallet:lightning:", - appStoreLink: "https://apps.apple.com/us/app/bluewallet-bitcoin-wallet/id1376878040", image: "bluewallet") - case .walletofsatoshi: - return .init(index: 4, tag: "walletofsatoshi", displayName: "Wallet Of Satoshi", link: "walletofsatoshi:lightning:", - appStoreLink: "https://apps.apple.com/us/app/wallet-of-satoshi/id1438599608", image: "walletofsatoshi") - case .zebedee: - return .init(index: 5, tag: "zebedee", displayName: "Zebedee", link: "zebedee:lightning:", - appStoreLink: "https://apps.apple.com/us/app/zebedee-wallet/id1484394401", image: "zebedee") - case .zeusln: - return .init(index: 6, tag: "zeusln", displayName: "Zeus LN", link: "zeusln:lightning:", - appStoreLink: "https://apps.apple.com/us/app/zeus-ln/id1456038895", image: "zeusln") - case .lnlink: - return .init(index: 7, tag: "lnlink", displayName: "LNLink", link: "lnlink:lightning:", - appStoreLink: "https://testflight.apple.com/join/aNY4yuuZ", image: "lnlink") - case .phoenix: - return .init(index: 8, tag: "phoenix", displayName: "Phoenix", link: "phoenix://", - appStoreLink: "https://apps.apple.com/us/app/phoenix-wallet/id1544097028", image: "phoenix") - } - } - - static var allModels: [Model] { - return Self.allCases.map { $0.model } - } -} diff --git a/damus/Views/ConfigView.swift b/damus/Views/ConfigView.swift index 52a86a7..4dd8cc4 100644 --- a/damus/Views/ConfigView.swift +++ b/damus/Views/ConfigView.swift @@ -18,8 +18,8 @@ struct ConfigView: View { @State var privkey_copied: Bool = false @State var pubkey_copied: Bool = false @EnvironmentObject var user_settings: UserSettingsStore - - @State var allWallets: [Wallet] = Wallet.allCases + + let walletItems: [WalletItem] = get_wallet_list() let generator = UIImpactFeedbackGenerator(style: .light) @@ -82,13 +82,13 @@ struct ConfigView: View { } Section("Wallet Selector") { - Toggle("Show wallet selector", isOn: $user_settings.showWalletSelector).toggleStyle(.switch) - Picker("Select default wallet", - selection: $user_settings.defaultWallet) { - ForEach(allWallets, id: \.self) { wallet in - Text(wallet.model.displayName) - .tag(wallet.model.tag) - } + 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)) + } + }) } } diff --git a/damus/Views/ProfileView.swift b/damus/Views/ProfileView.swift index fd3471b..1614a36 100644 --- a/damus/Views/ProfileView.swift +++ b/damus/Views/ProfileView.swift @@ -150,8 +150,7 @@ struct ProfileView: View { } } }.sheet(isPresented: $showingSelectWallet, onDismiss: {showingSelectWallet = false}) { - SelectWalletView(showingSelectWallet: $showingSelectWallet, invoice: $inv) - .environmentObject(user_settings) + SelectWalletView(showingSelectWallet: $showingSelectWallet, invoice: $inv).environmentObject(user_settings) } } diff --git a/damus/Views/SelectWalletView.swift b/damus/Views/SelectWalletView.swift index 83c8362..fa42d7e 100644 --- a/damus/Views/SelectWalletView.swift +++ b/damus/Views/SelectWalletView.swift @@ -14,8 +14,8 @@ struct SelectWalletView: View { @State var invoice_copied: Bool = false @EnvironmentObject var user_settings: UserSettingsStore - @State var allWalletModels: [Wallet.Model] = Wallet.allModels let generator = UIImpactFeedbackGenerator(style: .light) + let walletItems: [WalletItem] = get_wallet_list() var body: some View { NavigationView { @@ -35,42 +35,42 @@ struct SelectWalletView: View { generator.impactOccurred() } } - Section("Select a lightning wallet"){ - List{ - Button() { - let walletModel = user_settings.defaultWallet.model - if let url = URL(string: "\(walletModel.link)\(invoice)"), UIApplication.shared.canOpenURL(url) { - openURL(url) - } else { - if let url = URL(string: walletModel.appStoreLink), UIApplication.shared.canOpenURL(url) { + Section("Select a lightning wallet"){ + List{ + Button() { + let wallet = get_default_wallet(user_settings.defaultwallet.rawValue) + if let url = URL(string: "\(wallet.link)\(invoice)"), UIApplication.shared.canOpenURL(url) { openURL(url) - } - } - } label: { - HStack { - Text("Default Wallet").font(.body).foregroundColor(.blue) - } - }.buttonStyle(.plain) - List($allWalletModels) { $wallet in - if wallet.index >= 0 { - Button() { - if let url = URL(string: "\(wallet.link)\(invoice)"), UIApplication.shared.canOpenURL(url) { + } else { + if let url = URL(string: wallet.appStoreLink), UIApplication.shared.canOpenURL(url) { openURL(url) - } else { - if let url = URL(string: wallet.appStoreLink), UIApplication.shared.canOpenURL(url) { + } + } + } label: { + HStack { + Text("Default Wallet").font(.body).foregroundColor(.blue) + } + }.buttonStyle(.plain) + ForEach(walletItems, id: \.self) { wallet in + 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.displayName).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) - } + }.padding(.vertical, 2.5) + } }.navigationBarTitle(Text("Pay the lightning invoice"), displayMode: .inline).navigationBarItems(trailing: Button(action: { self.showingSelectWallet = false }) {