Browse Source

wallet: refactor, make it work with ln tip button

Changelog-Added: Added option to choose default wallet
post-button-style
William Casarin 2 years ago
parent
commit
6e709058c0
  1. 32
      damus/Components/InvoiceView.swift
  2. 16
      damus/Models/UserSettingsStore.swift
  3. 4
      damus/Models/Wallet.swift
  4. 7
      damus/Views/ConfigView.swift
  5. 22
      damus/Views/ProfileView.swift
  6. 6
      damus/Views/SelectWalletView.swift

32
damus/Components/InvoiceView.swift

@ -7,30 +7,30 @@
import SwiftUI import SwiftUI
func open_with_wallet(wallet: Wallet.Model, invoice: String) {
if let url = URL(string: "\(wallet.link)\(invoice)"), UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url)
} else {
if let url = URL(string: wallet.appStoreLink), UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url)
}
}
}
struct InvoiceView: View { struct InvoiceView: View {
@Environment(\.colorScheme) var colorScheme @Environment(\.colorScheme) var colorScheme
@Environment(\.openURL) private var openURL @Environment(\.openURL) private var openURL
let invoice: Invoice let invoice: Invoice
@State var showingSelectWallet: Bool = false @State var showing_select_wallet: Bool = false
@State var inv: String = ""
@ObservedObject var user_settings = UserSettingsStore() @ObservedObject var user_settings = UserSettingsStore()
var PayButton: some View { var PayButton: some View {
Button { Button {
inv = invoice.string if user_settings.show_wallet_selector {
if user_settings.showWalletSelector { showing_select_wallet = true
showingSelectWallet = true
} else { } else {
let walletModel = user_settings.defaultWallet.model open_with_wallet(wallet: user_settings.default_wallet.model, invoice: invoice.string)
if let url = URL(string: "\(walletModel.link)\(inv)"), UIApplication.shared.canOpenURL(url) {
openURL(url)
} else {
if let url = URL(string: walletModel.appStoreLink), UIApplication.shared.canOpenURL(url) {
openURL(url)
}
}
} }
} label: { } label: {
RoundedRectangle(cornerRadius: 20) RoundedRectangle(cornerRadius: 20)
@ -68,8 +68,8 @@ struct InvoiceView: View {
} }
.padding(30) .padding(30)
} }
.sheet(isPresented: $showingSelectWallet, onDismiss: {showingSelectWallet = false}) { .sheet(isPresented: $showing_select_wallet, onDismiss: {showing_select_wallet = false}) {
SelectWalletView(showingSelectWallet: $showingSelectWallet, invoice: $inv).environmentObject(user_settings) SelectWalletView(showingSelectWallet: $showing_select_wallet, invoice: invoice.string).environmentObject(user_settings)
} }
} }
} }

16
damus/Models/UserSettingsStore.swift

@ -8,25 +8,25 @@
import Foundation import Foundation
class UserSettingsStore: ObservableObject { class UserSettingsStore: ObservableObject {
@Published var defaultWallet: Wallet { @Published var default_wallet: Wallet {
didSet { didSet {
UserDefaults.standard.set(defaultWallet.rawValue, forKey: "default_wallet") UserDefaults.standard.set(default_wallet.rawValue, forKey: "default_wallet")
} }
} }
@Published var showWalletSelector: Bool { @Published var show_wallet_selector: Bool {
didSet { didSet {
UserDefaults.standard.set(showWalletSelector, forKey: "show_wallet_selector") UserDefaults.standard.set(show_wallet_selector, forKey: "show_wallet_selector")
} }
} }
init() { init() {
if let defaultWalletName = UserDefaults.standard.string(forKey: "default_wallet"), if let defaultWalletName = UserDefaults.standard.string(forKey: "default_wallet"),
let defaultWallet = Wallet(rawValue: defaultWalletName) { let default_wallet = Wallet(rawValue: defaultWalletName) {
self.defaultWallet = defaultWallet self.default_wallet = default_wallet
} else { } else {
self.defaultWallet = .systemdefaultwallet self.default_wallet = .system_default_wallet
} }
self.showWalletSelector = UserDefaults.standard.object(forKey: "show_wallet_selector") as? Bool ?? true self.show_wallet_selector = UserDefaults.standard.object(forKey: "show_wallet_selector") as? Bool ?? true
} }
} }

4
damus/Models/Wallet.swift

@ -21,7 +21,7 @@ enum Wallet: String, CaseIterable, Identifiable {
} }
// New url prefixes needed to be added to LSApplicationQueriesSchemes // New url prefixes needed to be added to LSApplicationQueriesSchemes
case systemdefaultwallet case system_default_wallet
case strike case strike
case cashapp case cashapp
case muun case muun
@ -34,7 +34,7 @@ enum Wallet: String, CaseIterable, Identifiable {
var model: Model { var model: Model {
switch self { switch self {
case .systemdefaultwallet: case .system_default_wallet:
return .init(index: -1, tag: "systemdefaultwallet", displayName: "Local default", return .init(index: -1, tag: "systemdefaultwallet", displayName: "Local default",
link: "lightning:", appStoreLink: "lightning:", image: "") link: "lightning:", appStoreLink: "lightning:", image: "")
case .strike: case .strike:

7
damus/Views/ConfigView.swift

@ -17,7 +17,6 @@ struct ConfigView: View {
@State var privkey: String @State var privkey: String
@State var privkey_copied: Bool = false @State var privkey_copied: Bool = false
@State var pubkey_copied: Bool = false @State var pubkey_copied: Bool = false
@State var allWallets: [Wallet] = Wallet.allCases
@State var relays: [RelayDescriptor] @State var relays: [RelayDescriptor]
@EnvironmentObject var user_settings: UserSettingsStore @EnvironmentObject var user_settings: UserSettingsStore
@ -95,10 +94,10 @@ struct ConfigView: View {
} }
Section("Wallet Selector") { Section("Wallet Selector") {
Toggle("Show wallet selector", isOn: $user_settings.showWalletSelector).toggleStyle(.switch) Toggle("Show wallet selector", isOn: $user_settings.show_wallet_selector).toggleStyle(.switch)
Picker("Select default wallet", Picker("Select default wallet",
selection: $user_settings.defaultWallet) { selection: $user_settings.default_wallet) {
ForEach(allWallets, id: \.self) { wallet in ForEach(Wallet.allCases, id: \.self) { wallet in
Text(wallet.model.displayName) Text(wallet.model.displayName)
.tag(wallet.model.tag) .tag(wallet.model.tag)
} }

22
damus/Views/ProfileView.swift

@ -118,25 +118,23 @@ struct ProfileView: View {
@State private var selected_tab: ProfileTab = .posts @State private var selected_tab: ProfileTab = .posts
@StateObject var profile: ProfileModel @StateObject var profile: ProfileModel
@StateObject var followers: FollowersModel @StateObject var followers: FollowersModel
@StateObject var user_settings = UserSettingsStore()
@State private var showingEditProfile = false @State private var showingEditProfile = false
@State var showingSelectWallet: Bool = false @State var showing_select_wallet: Bool = false
@State var inv: String = ""
@State var is_zoomed: Bool = false @State var is_zoomed: Bool = false
@StateObject var user_settings = UserSettingsStore()
@Environment(\.dismiss) var dismiss @Environment(\.dismiss) var dismiss
@Environment(\.colorScheme) var colorScheme @Environment(\.colorScheme) var colorScheme
//@EnvironmentObject var profile: ProfileModel //@EnvironmentObject var profile: ProfileModel
func LNButton(lud06: String?, lud16: String?, profile: Profile) -> some View { func LNButton(lnurl: String, profile: Profile) -> some View {
Button(action: { Button(action: {
if let l = lud06 { if user_settings.show_wallet_selector {
inv = l showing_select_wallet = true
} else { } else {
inv = lud16 ?? "" open_with_wallet(wallet: user_settings.default_wallet.model, invoice: lnurl)
} }
showingSelectWallet = true
}) { }) {
Image(systemName: "bolt.circle") Image(systemName: "bolt.circle")
.symbolRenderingMode(.palette) .symbolRenderingMode(.palette)
@ -149,8 +147,8 @@ struct ProfileView: View {
Label("Copy LNURL", systemImage: "doc.on.doc") Label("Copy LNURL", systemImage: "doc.on.doc")
} }
} }
}.sheet(isPresented: $showingSelectWallet, onDismiss: {showingSelectWallet = false}) { }.sheet(isPresented: $showing_select_wallet, onDismiss: {showing_select_wallet = false}) {
SelectWalletView(showingSelectWallet: $showingSelectWallet, invoice: $inv) SelectWalletView(showingSelectWallet: $showing_select_wallet, invoice: lnurl)
.environmentObject(user_settings) .environmentObject(user_settings)
} }
} }
@ -183,8 +181,8 @@ struct ProfileView: View {
Spacer() Spacer()
if let profile = data { if let profile = data {
if (profile.lud06 != nil || profile.lud16 != nil) { if let lnurl = profile.lnurl {
LNButton(lud06: profile.lud06, lud16: profile.lud16, profile: profile) LNButton(lnurl: lnurl, profile: profile)
} }
} }

6
damus/Views/SelectWalletView.swift

@ -9,7 +9,7 @@ import SwiftUI
struct SelectWalletView: View { struct SelectWalletView: View {
@Binding var showingSelectWallet: Bool @Binding var showingSelectWallet: Bool
@Binding var invoice: String let invoice: String
@Environment(\.openURL) private var openURL @Environment(\.openURL) private var openURL
@State var invoice_copied: Bool = false @State var invoice_copied: Bool = false
@EnvironmentObject var user_settings: UserSettingsStore @EnvironmentObject var user_settings: UserSettingsStore
@ -38,7 +38,7 @@ struct SelectWalletView: View {
Section("Select a lightning wallet"){ Section("Select a lightning wallet"){
List{ List{
Button() { Button() {
let walletModel = user_settings.defaultWallet.model let walletModel = user_settings.default_wallet.model
if let url = URL(string: "\(walletModel.link)\(invoice)"), UIApplication.shared.canOpenURL(url) { if let url = URL(string: "\(walletModel.link)\(invoice)"), UIApplication.shared.canOpenURL(url) {
openURL(url) openURL(url)
} else { } else {
@ -85,6 +85,6 @@ struct SelectWalletView_Previews: PreviewProvider {
@State static var invoice: String = "" @State static var invoice: String = ""
static var previews: some View { static var previews: some View {
SelectWalletView(showingSelectWallet: $show, invoice: $invoice) SelectWalletView(showingSelectWallet: $show, invoice: "")
} }
} }

Loading…
Cancel
Save