damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit 73f7b696549e82b7a32a189ae6d1c496e25e5b22
parent d982bb886e3edd973e2bd55f1187234138e290f3
Author: Swift <scoder1747@gmail.com>
Date:   Thu,  9 Mar 2023 13:36:42 -0500

Add vibrate on zap

Changelog-Added: Vibrate when a zap is received
Closes: #768

Diffstat:
Mdamus/Models/HomeModel.swift | 31++++++++++++++++++++++++++-----
Mdamus/Models/UserSettingsStore.swift | 7+++++++
Mdamus/Views/ConfigView.swift | 4+++-
3 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/damus/Models/HomeModel.swift b/damus/Models/HomeModel.swift @@ -144,11 +144,15 @@ class HomeModel: ObservableObject { if !notifications.insert_zap(zap) { return } - - handle_last_event(ev: ev, timeline: .notifications) + + if handle_last_event(ev: ev, timeline: .notifications) && damus_state.settings.zap_vibration { + // Generate zap vibration + zap_vibrate(zap_amount: zap.invoice.amount) + } + return } - + func handle_zap_event(_ ev: NostrEvent) { // These are zap notifications guard let ptag = event_tag(ev, name: "p") else { @@ -477,10 +481,14 @@ class HomeModel: ObservableObject { handle_last_event(ev: ev, timeline: .notifications) } - - func handle_last_event(ev: NostrEvent, timeline: Timeline, shouldNotify: Bool = true) { + + @discardableResult + func handle_last_event(ev: NostrEvent, timeline: Timeline, shouldNotify: Bool = true) -> Bool { if let new_bits = handle_last_events(new_events: self.new_events, ev: ev, timeline: timeline, shouldNotify: shouldNotify) { new_events = new_bits + return true + } else { + return false } } @@ -907,3 +915,16 @@ func should_show_event(contacts: Contacts, ev: NostrEvent) -> Bool { return ev.should_show_event } +func zap_vibrate(zap_amount: Int64) { + let sats = zap_amount / 1000 + var vibration_generator: UIImpactFeedbackGenerator + if sats >= 10000 { + vibration_generator = UIImpactFeedbackGenerator(style: .heavy) + } else if sats >= 1000 { + vibration_generator = UIImpactFeedbackGenerator(style: .medium) + } else { + vibration_generator = UIImpactFeedbackGenerator(style: .light) + } + vibration_generator.impactOccurred() +} + diff --git a/damus/Models/UserSettingsStore.swift b/damus/Models/UserSettingsStore.swift @@ -101,6 +101,12 @@ class UserSettingsStore: ObservableObject { } } + @Published var zap_vibration: Bool { + didSet { + UserDefaults.standard.set(zap_vibration, forKey: "zap_vibration") + } + } + @Published var translation_service: TranslationService { didSet { UserDefaults.standard.set(translation_service.rawValue, forKey: "translation_service") @@ -178,6 +184,7 @@ class UserSettingsStore: ObservableObject { show_wallet_selector = should_show_wallet_selector(pubkey) left_handed = UserDefaults.standard.object(forKey: "left_handed") as? Bool ?? false + zap_vibration = UserDefaults.standard.object(forKey: "zap_vibration") as? Bool ?? false disable_animation = should_disable_image_animation() // Note from @tyiu: diff --git a/damus/Views/ConfigView.swift b/damus/Views/ConfigView.swift @@ -194,9 +194,11 @@ struct ConfigView: View { } } - Section(NSLocalizedString("Left Handed", comment: "Moves the post button to the left side of the screen")) { + Section(NSLocalizedString("Miscellaneous", comment: "Section header for miscellaneous user configuration")) { Toggle(NSLocalizedString("Left Handed", comment: "Moves the post button to the left side of the screen"), isOn: $settings.left_handed) .toggleStyle(.switch) + Toggle(NSLocalizedString("Zap Vibration", comment: "Setting to enable vibration on zap"), isOn: $settings.zap_vibration) + .toggleStyle(.switch) } Section(NSLocalizedString("Images", comment: "Section title for images configuration.")) {