ActionBarModel.swift (2588B)
1 // 2 // ActionBarModel.swift 3 // damus 4 // 5 // Created by William Casarin on 2022-04-30. 6 // 7 8 import Foundation 9 10 enum Zapped { 11 case not_zapped 12 case pending 13 case zapped 14 } 15 16 class ActionBarModel: ObservableObject { 17 @Published var our_like: NostrEvent? 18 @Published var our_boost: NostrEvent? 19 @Published var our_quote_repost: NostrEvent? 20 @Published var our_reply: NostrEvent? 21 @Published var our_zap: Zapping? 22 @Published var likes: Int 23 @Published var boosts: Int 24 @Published var quote_reposts: Int 25 @Published private(set) var zaps: Int 26 @Published var zap_total: Int64 27 @Published var replies: Int 28 29 static func empty() -> ActionBarModel { 30 return ActionBarModel(likes: 0, boosts: 0, zaps: 0, zap_total: 0, replies: 0, our_like: nil, our_boost: nil, our_zap: nil, our_reply: nil) 31 } 32 33 init(likes: Int = 0, boosts: Int = 0, zaps: Int = 0, zap_total: Int64 = 0, replies: Int = 0, our_like: NostrEvent? = nil, our_boost: NostrEvent? = nil, our_zap: Zapping? = nil, our_reply: NostrEvent? = nil, our_quote_repost: NostrEvent? = nil, quote_reposts: Int = 0) { 34 self.likes = likes 35 self.boosts = boosts 36 self.zaps = zaps 37 self.replies = replies 38 self.zap_total = zap_total 39 self.our_like = our_like 40 self.our_boost = our_boost 41 self.our_zap = our_zap 42 self.our_reply = our_reply 43 self.our_quote_repost = our_quote_repost 44 self.quote_reposts = quote_reposts 45 } 46 47 func update(damus: DamusState, evid: NoteId) { 48 self.likes = damus.likes.counts[evid] ?? 0 49 self.boosts = damus.boosts.counts[evid] ?? 0 50 self.zaps = damus.zaps.event_counts[evid] ?? 0 51 self.replies = damus.replies.get_replies(evid) 52 self.quote_reposts = damus.quote_reposts.counts[evid] ?? 0 53 self.zap_total = damus.zaps.event_totals[evid] ?? 0 54 self.our_like = damus.likes.our_events[evid] 55 self.our_boost = damus.boosts.our_events[evid] 56 self.our_zap = damus.zaps.our_zaps[evid]?.first 57 self.our_reply = damus.replies.our_reply(evid) 58 self.our_quote_repost = damus.quote_reposts.our_events[evid] 59 self.objectWillChange.send() 60 } 61 62 var is_empty: Bool { 63 return likes == 0 && boosts == 0 && zaps == 0 64 } 65 66 var liked: Bool { 67 return our_like != nil 68 } 69 70 var replied: Bool { 71 return our_reply != nil 72 } 73 74 var boosted: Bool { 75 return our_boost != nil 76 } 77 78 var quoted: Bool { 79 return our_quote_repost != nil 80 } 81 }