Add vibrate on zap

Changelog-Added: Vibrate when a zap is received
Closes: #768
This commit is contained in:
Swift
2023-03-09 13:36:42 -05:00
committed by William Casarin
parent d982bb886e
commit 73f7b69654
3 changed files with 36 additions and 6 deletions

View File

@@ -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()
}