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
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 {
@Environment(\.colorScheme) var colorScheme
@Environment(\.openURL) private var openURL
let invoice: Invoice
@State var showingSelectWallet: Bool = false
@State var inv: String = ""
@State var showing_select_wallet: Bool = false
@ObservedObject var user_settings = UserSettingsStore()
var PayButton: some View {
Button {
inv = invoice.string
if user_settings.showWalletSelector {
showingSelectWallet = true
if user_settings.show_wallet_selector {
showing_select_wallet = true
} else {
let walletModel = user_settings.defaultWallet.model
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)
}
}
open_with_wallet(wallet: user_settings.default_wallet.model, invoice: invoice.string)
}
} label: {
RoundedRectangle(cornerRadius: 20)
@ -68,8 +68,8 @@ struct InvoiceView: View {
}
.padding(30)
}
.sheet(isPresented: $showingSelectWallet, onDismiss: {showingSelectWallet = false}) {
SelectWalletView(showingSelectWallet: $showingSelectWallet, invoice: $inv).environmentObject(user_settings)
.sheet(isPresented: $showing_select_wallet, onDismiss: {showing_select_wallet = false}) {
SelectWalletView(showingSelectWallet: $showing_select_wallet, invoice: invoice.string).environmentObject(user_settings)
}
}
}

16
damus/Models/UserSettingsStore.swift

@ -8,25 +8,25 @@
import Foundation
class UserSettingsStore: ObservableObject {
@Published var defaultWallet: Wallet {
@Published var default_wallet: Wallet {
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 {
UserDefaults.standard.set(showWalletSelector, forKey: "show_wallet_selector")
UserDefaults.standard.set(show_wallet_selector, forKey: "show_wallet_selector")
}
}
init() {
if let defaultWalletName = UserDefaults.standard.string(forKey: "default_wallet"),
let defaultWallet = Wallet(rawValue: defaultWalletName) {
self.defaultWallet = defaultWallet
let default_wallet = Wallet(rawValue: defaultWalletName) {
self.default_wallet = default_wallet
} 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
case systemdefaultwallet
case system_default_wallet
case strike
case cashapp
case muun
@ -34,7 +34,7 @@ enum Wallet: String, CaseIterable, Identifiable {
var model: Model {
switch self {
case .systemdefaultwallet:
case .system_default_wallet:
return .init(index: -1, tag: "systemdefaultwallet", displayName: "Local default",
link: "lightning:", appStoreLink: "lightning:", image: "")
case .strike:

7
damus/Views/ConfigView.swift

@ -17,7 +17,6 @@ struct ConfigView: View {
@State var privkey: String
@State var privkey_copied: Bool = false
@State var pubkey_copied: Bool = false
@State var allWallets: [Wallet] = Wallet.allCases
@State var relays: [RelayDescriptor]
@EnvironmentObject var user_settings: UserSettingsStore
@ -95,10 +94,10 @@ struct ConfigView: View {
}
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",
selection: $user_settings.defaultWallet) {
ForEach(allWallets, id: \.self) { wallet in
selection: $user_settings.default_wallet) {
ForEach(Wallet.allCases, id: \.self) { wallet in
Text(wallet.model.displayName)
.tag(wallet.model.tag)
}

22
damus/Views/ProfileView.swift

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

6
damus/Views/SelectWalletView.swift

@ -9,7 +9,7 @@ import SwiftUI
struct SelectWalletView: View {
@Binding var showingSelectWallet: Bool
@Binding var invoice: String
let invoice: String
@Environment(\.openURL) private var openURL
@State var invoice_copied: Bool = false
@EnvironmentObject var user_settings: UserSettingsStore
@ -38,7 +38,7 @@ struct SelectWalletView: View {
Section("Select a lightning wallet"){
List{
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) {
openURL(url)
} else {
@ -85,6 +85,6 @@ struct SelectWalletView_Previews: PreviewProvider {
@State static var invoice: String = ""
static var previews: some View {
SelectWalletView(showingSelectWallet: $show, invoice: $invoice)
SelectWalletView(showingSelectWallet: $show, invoice: "")
}
}

Loading…
Cancel
Save