damus

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

commit d0eb86dfa3d034ecf5ef92b641455db77b186c01
parent 337c4de33732292f64eac7bc9ec921083145c19d
Author: William Casarin <jb55@jb55.com>
Date:   Thu, 22 Jun 2023 14:10:16 +0200

Initial actionbar model refactor

Diffstat:
Mdamus/Components/ZapButton.swift | 4++--
Mdamus/Models/ActionBarModel.swift | 32+++++++++++++++++++++++---------
Mdamus/Models/HomeModel.swift | 2+-
Mdamus/Util/EventCache.swift | 4++++
Mdamus/Views/ActionBar/EventActionBar.swift | 14++++----------
Mdamus/Views/Events/SelectedEventView.swift | 17++---------------
Mdamus/Views/Events/TextEvent.swift | 6+++++-
7 files changed, 41 insertions(+), 38 deletions(-)

diff --git a/damus/Components/ZapButton.swift b/damus/Components/ZapButton.swift @@ -32,7 +32,7 @@ struct ZapButton: View { let lnurl: String @ObservedObject var zaps: ZapsDataModel - @StateObject var button: ZapButtonModel = ZapButtonModel() + @ObservedObject var button: ZapButtonModel var our_zap: Zapping? { zaps.zaps.first(where: { z in z.request.ev.pubkey == damus_state.pubkey }) @@ -178,7 +178,7 @@ struct ZapButton_Previews: PreviewProvider { let pending_zap = PendingZap(amount_msat: 1000, target: ZapTarget.note(id: "noteid", author: "author"), request: .normal(test_zap_request), type: .pub, state: .external(.init(state: .fetching_invoice))) let zaps = ZapsDataModel([.pending(pending_zap)]) - ZapButton(damus_state: test_damus_state(), target: ZapTarget.note(id: test_event.id, author: test_event.pubkey), lnurl: "lnurl", zaps: zaps) + ZapButton(damus_state: test_damus_state(), target: ZapTarget.note(id: test_event.id, author: test_event.pubkey), lnurl: "lnurl", zaps: zaps, button: ZapButtonModel()) } } diff --git a/damus/Models/ActionBarModel.swift b/damus/Models/ActionBarModel.swift @@ -14,15 +14,15 @@ enum Zapped { } class ActionBarModel: ObservableObject { - @Published var our_like: NostrEvent? - @Published var our_boost: NostrEvent? - @Published var our_reply: NostrEvent? - @Published var our_zap: Zapping? - @Published var likes: Int - @Published var boosts: Int - @Published private(set) var zaps: Int - @Published var zap_total: Int64 - @Published var replies: Int + private(set) var our_like: NostrEvent? + private(set) var our_boost: NostrEvent? + private(set) var our_reply: NostrEvent? + private(set) var our_zap: Zapping? + private(set) var likes: Int + private(set) var boosts: Int + private(set) var zaps: Int + private(set) var zap_total: Int64 + private(set) var replies: Int static func empty() -> ActionBarModel { 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) @@ -65,6 +65,20 @@ class ActionBarModel: ObservableObject { self.objectWillChange.send() } + func set_likes(likes: Int) { + guard likes != self.likes else { return } + + self.likes = likes + self.objectWillChange.send() + } + + func set_our_like(our_like: NostrEvent) { + guard self.our_like == nil else { return } + + self.our_like = our_like + self.objectWillChange.send() + } + var is_empty: Bool { return likes == 0 && boosts == 0 && zaps == 0 } diff --git a/damus/Models/HomeModel.swift b/damus/Models/HomeModel.swift @@ -269,7 +269,7 @@ class HomeModel { case .success(let n): let boosted = Counted(event: ev, id: e, total: n) notify(.boosted, boosted) - notify(.update_stats, e) + self.damus_state.events.get_cache_data(ev.id).bar_model.update(damus: self.damus_state, evid: ev.id) } } diff --git a/damus/Util/EventCache.swift b/damus/Util/EventCache.swift @@ -113,6 +113,8 @@ class EventData { var relative_time: RelativeTimeModel = RelativeTimeModel() var validated: ValidationResult var media_metadata_model: MediaMetaModel + var bar_model: ActionBarModel + var zap_button_model: ZapButtonModel var translations: TranslateStatus { return translations_model.state @@ -133,6 +135,8 @@ class EventData { self.validated = .unknown self.media_metadata_model = MediaMetaModel() self.preview_model = .init(state: .not_loaded) + self.bar_model = ActionBarModel() + self.zap_button_model = ZapButtonModel() } } diff --git a/damus/Views/ActionBar/EventActionBar.swift b/damus/Views/ActionBar/EventActionBar.swift @@ -21,12 +21,6 @@ struct EventActionBar: View { @ObservedObject var bar: ActionBarModel - init(damus_state: DamusState, event: NostrEvent, bar: ActionBarModel? = nil) { - self.damus_state = damus_state - self.event = event - _bar = ObservedObject(wrappedValue: bar ?? make_actionbar_model(ev: event.id, damus: damus_state)) - } - var lnurl: String? { damus_state.profiles.lookup(id: event.pubkey)?.lnurl } @@ -88,7 +82,7 @@ struct EventActionBar: View { if let lnurl = self.lnurl { Spacer() - ZapButton(damus_state: damus_state, target: ZapTarget.note(id: event.id, author: event.pubkey), lnurl: lnurl, zaps: self.damus_state.events.get_cache_data(self.event.id).zaps_model) + ZapButton(damus_state: damus_state, target: ZapTarget.note(id: event.id, author: event.pubkey), lnurl: lnurl, zaps: self.damus_state.events.get_cache_data(self.event.id).zaps_model, button: damus_state.events.get_cache_data(event.id).zap_button_model) } Spacer() @@ -136,9 +130,9 @@ struct EventActionBar: View { if liked.id != event.id { return } - self.bar.likes = liked.total + self.bar.set_likes(likes: liked.total) if liked.event.pubkey == damus_state.keypair.pubkey { - self.bar.our_like = liked.event + self.bar.set_our_like(our_like: liked.event) } } } @@ -150,7 +144,7 @@ struct EventActionBar: View { let like_ev = make_like_event(pubkey: damus_state.pubkey, privkey: privkey, liked: event) - self.bar.our_like = like_ev + self.bar.set_our_like(our_like: like_ev) generator.impactOccurred() diff --git a/damus/Views/Events/SelectedEventView.swift b/damus/Views/Events/SelectedEventView.swift @@ -16,15 +16,6 @@ struct SelectedEventView: View { event.pubkey } - @StateObject var bar: ActionBarModel - - init(damus: DamusState, event: NostrEvent, size: EventViewKind) { - self.damus = damus - self.event = event - self.size = size - self._bar = StateObject(wrappedValue: make_actionbar_model(ev: event.id, damus: damus)) - } - var body: some View { HStack(alignment: .top) { let profile = damus.profiles.lookup(id: pubkey) @@ -63,24 +54,20 @@ struct SelectedEventView: View { Divider() .padding([.bottom], 4) + let bar = damus.events.get_cache_data(event.id).bar_model if !bar.is_empty { EventDetailBar(state: damus, target: event.id, target_pk: event.pubkey) .padding(.horizontal) Divider() } - EventActionBar(damus_state: damus, event: event) + EventActionBar(damus_state: damus, event: event, bar: bar) .padding([.top], 4) .padding(.horizontal) Divider() .padding([.top], 4) } - .onReceive(handle_notify(.update_stats)) { n in - let target = n.object as! String - guard target == self.event.id else { return } - self.bar.update(damus: self.damus, evid: target) - } .compositingGroup() } } diff --git a/damus/Views/Events/TextEvent.swift b/damus/Views/Events/TextEvent.swift @@ -153,7 +153,7 @@ struct TextEvent: View { } var ActionBar: some View { - return EventActionBar(damus_state: damus, event: event) + return EventActionBar(damus_state: damus, event: event, bar: get_actionbar(state: damus, evid: event.id)) .padding([.top], 4) } @@ -229,3 +229,7 @@ struct TextEvent_Previews: PreviewProvider { } } + +func get_actionbar(state: DamusState, evid: String) -> ActionBarModel { + return state.events.get_cache_data(evid).bar_model +}