damus

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

commit ed73899e5ba92ccd9234ee9e9a4384ad6bbe598e
parent d0eb86dfa3d034ecf5ef92b641455db77b186c01
Author: William Casarin <jb55@jb55.com>
Date:   Fri, 23 Jun 2023 12:04:54 +0200

Revert "Initial actionbar model refactor"

This reverts commit d0eb86dfa3d034ecf5ef92b641455db77b186c01.

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, 38 insertions(+), 41 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 - @ObservedObject var button: ZapButtonModel + @StateObject var button: ZapButtonModel = 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, button: ZapButtonModel()) + ZapButton(damus_state: test_damus_state(), target: ZapTarget.note(id: test_event.id, author: test_event.pubkey), lnurl: "lnurl", zaps: zaps) } } diff --git a/damus/Models/ActionBarModel.swift b/damus/Models/ActionBarModel.swift @@ -14,15 +14,15 @@ enum Zapped { } class ActionBarModel: ObservableObject { - 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 + @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 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,20 +65,6 @@ 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) - self.damus_state.events.get_cache_data(ev.id).bar_model.update(damus: self.damus_state, evid: ev.id) + notify(.update_stats, e) } } diff --git a/damus/Util/EventCache.swift b/damus/Util/EventCache.swift @@ -113,8 +113,6 @@ 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 @@ -135,8 +133,6 @@ 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,6 +21,12 @@ 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 } @@ -82,7 +88,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, button: damus_state.events.get_cache_data(event.id).zap_button_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) } Spacer() @@ -130,9 +136,9 @@ struct EventActionBar: View { if liked.id != event.id { return } - self.bar.set_likes(likes: liked.total) + self.bar.likes = liked.total if liked.event.pubkey == damus_state.keypair.pubkey { - self.bar.set_our_like(our_like: liked.event) + self.bar.our_like = liked.event } } } @@ -144,7 +150,7 @@ struct EventActionBar: View { let like_ev = make_like_event(pubkey: damus_state.pubkey, privkey: privkey, liked: event) - self.bar.set_our_like(our_like: like_ev) + self.bar.our_like = like_ev generator.impactOccurred() diff --git a/damus/Views/Events/SelectedEventView.swift b/damus/Views/Events/SelectedEventView.swift @@ -16,6 +16,15 @@ 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) @@ -54,20 +63,24 @@ 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, bar: bar) + EventActionBar(damus_state: damus, event: event) .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, bar: get_actionbar(state: damus, evid: event.id)) + return EventActionBar(damus_state: damus, event: event) .padding([.top], 4) } @@ -229,7 +229,3 @@ struct TextEvent_Previews: PreviewProvider { } } - -func get_actionbar(state: DamusState, evid: String) -> ActionBarModel { - return state.events.get_cache_data(evid).bar_model -}