commit b40c595a7c482e2f71ff0da82cec8959a6d4739e parent 80063af19a3fb83c690ae1cd1eadbde7dea2df4b Author: William Casarin <jb55@jb55.com> Date: Sun, 30 Jul 2023 10:50:41 -0700 notify: switch over to new typesafe notifications Diffstat:
39 files changed, 129 insertions(+), 289 deletions(-)
diff --git a/damus.xcodeproj/project.pbxproj b/damus.xcodeproj/project.pbxproj @@ -292,7 +292,6 @@ 4CE4F0F229D4FCFA005914DB /* DebouncedOnChange.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CE4F0F129D4FCFA005914DB /* DebouncedOnChange.swift */; }; 4CE4F0F429D779B5005914DB /* PostBox.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CE4F0F329D779B5005914DB /* PostBox.swift */; }; 4CE4F0F829DB7399005914DB /* ThiccDivider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CE4F0F729DB7399005914DB /* ThiccDivider.swift */; }; - 4CE4F8CD281352B30009DFBB /* Notifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CE4F8CC281352B30009DFBB /* Notifications.swift */; }; 4CE4F9DE2852768D00C00DD9 /* ConfigView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CE4F9DD2852768D00C00DD9 /* ConfigView.swift */; }; 4CE4F9E328528C5200C00DD9 /* AddRelayView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CE4F9E228528C5200C00DD9 /* AddRelayView.swift */; }; 4CE6DEE727F7A08100C66700 /* damusApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CE6DEE627F7A08100C66700 /* damusApp.swift */; }; @@ -829,7 +828,6 @@ 4CE4F0F129D4FCFA005914DB /* DebouncedOnChange.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DebouncedOnChange.swift; sourceTree = "<group>"; }; 4CE4F0F329D779B5005914DB /* PostBox.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PostBox.swift; sourceTree = "<group>"; }; 4CE4F0F729DB7399005914DB /* ThiccDivider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThiccDivider.swift; sourceTree = "<group>"; }; - 4CE4F8CC281352B30009DFBB /* Notifications.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Notifications.swift; sourceTree = "<group>"; }; 4CE4F9DD2852768D00C00DD9 /* ConfigView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfigView.swift; sourceTree = "<group>"; }; 4CE4F9E228528C5200C00DD9 /* AddRelayView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddRelayView.swift; sourceTree = "<group>"; }; 4CE6DEE327F7A08100C66700 /* damus.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = damus.app; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -1355,7 +1353,6 @@ 4CC7AAE6297EFA7B00430951 /* Zap.swift */, F7908E96298B1FDF00AB113A /* NIPURLBuilder.swift */, 4CEE2AF4280B29E600AB5EEF /* TimeAgo.swift */, - 4CE4F8CC281352B30009DFBB /* Notifications.swift */, 4C363A8328233689006E126D /* Parser.swift */, 4C363AA728297703006E126D /* InsertSort.swift */, 4C285C8B28398BC6008A31F1 /* Keys.swift */, @@ -2028,7 +2025,6 @@ 4C3AC79D2833036D00E1F516 /* FollowingView.swift in Sources */, 5CF72FC229B9142F00124A13 /* ShareAction.swift in Sources */, 4C8D1A6C29F1DFC200ACDF75 /* FriendIcon.swift in Sources */, - 4CE4F8CD281352B30009DFBB /* Notifications.swift in Sources */, 4C30AC7829A577AB00E2BD5A /* EventCache.swift in Sources */, 4C285C8428385690008A31F1 /* CreateAccountView.swift in Sources */, 4CDD1AE22A6B3074001CD4DF /* NdbTagsIterator.swift in Sources */, diff --git a/damus/Components/InvoiceView.swift b/damus/Components/InvoiceView.swift @@ -115,5 +115,5 @@ struct InvoiceView_Previews: PreviewProvider { func present_sheet(_ sheet: Sheets) { - notify(.present_sheet, sheet) + notify(.present_sheet(sheet)) } diff --git a/damus/Components/Search/SearchHeaderView.swift b/damus/Components/Search/SearchHeaderView.swift @@ -93,13 +93,11 @@ struct SearchHeaderView: View { } } } - .onReceive(handle_notify(.followed)) { notif in - let ref = notif.object as! ReferencedId + .onReceive(handle_notify(.followed)) { ref in guard hashtag_matches_search(desc: self.described, ref: ref) else { return } self.is_following = true } - .onReceive(handle_notify(.unfollowed)) { notif in - let ref = notif.object as! ReferencedId + .onReceive(handle_notify(.unfollowed)) { ref in guard hashtag_matches_search(desc: self.described, ref: ref) else { return } self.is_following = false } diff --git a/damus/Components/ZapButton.swift b/damus/Components/ZapButton.swift @@ -189,7 +189,7 @@ func send_zap(damus_state: DamusState, target: ZapTarget, lnurl: String, is_cust remove_zap(reqid: reqid, zapcache: damus_state.zaps, evcache: damus_state.events) let typ = ZappingEventType.failed(.bad_lnurl) let ev = ZappingEvent(is_custom: is_custom, type: typ, target: target) - notify(.zapping, ev) + notify(.zapping(ev)) return } @@ -197,7 +197,7 @@ func send_zap(damus_state: DamusState, target: ZapTarget, lnurl: String, is_cust remove_zap(reqid: reqid, zapcache: damus_state.zaps, evcache: damus_state.events) let typ = ZappingEventType.failed(.fetching_invoice) let ev = ZappingEvent(is_custom: is_custom, type: typ, target: target) - notify(.zapping, ev) + notify(.zapping(ev)) return } @@ -208,7 +208,7 @@ func send_zap(damus_state: DamusState, target: ZapTarget, lnurl: String, is_cust remove_zap(reqid: reqid, zapcache: damus_state.zaps, evcache: damus_state.events) let typ = ZappingEventType.failed(.canceled) let ev = ZappingEvent(is_custom: is_custom, type: typ, target: target) - notify(.zapping, ev) + notify(.zapping(ev)) return } @@ -234,7 +234,7 @@ func send_zap(damus_state: DamusState, target: ZapTarget, lnurl: String, is_cust let typ = ZappingEventType.failed(.send_failed) let ev = ZappingEvent(is_custom: is_custom, type: typ, target: target) - notify(.zapping, ev) + notify(.zapping(ev)) return } @@ -245,12 +245,12 @@ func send_zap(damus_state: DamusState, target: ZapTarget, lnurl: String, is_cust } let ev = ZappingEvent(is_custom: is_custom, type: .sent_from_nwc, target: target) - notify(.zapping, ev) + notify(.zapping(ev)) case .external(let pending_ext): pending_ext.state = .done let ev = ZappingEvent(is_custom: is_custom, type: .got_zap_invoice(inv), target: target) - notify(.zapping, ev) + notify(.zapping(ev)) } } diff --git a/damus/ContentView.swift b/damus/ContentView.swift @@ -349,26 +349,22 @@ struct ContentView: View { } } } - .onReceive(handle_notify(.compose)) { notif in - let action = notif.object as! PostAction + .onReceive(handle_notify(.compose)) { action in self.active_sheet = .post(action) } .onReceive(timer) { n in self.damus_state?.postbox.try_flushing_events() } - .onReceive(handle_notify(.report)) { notif in - let target = notif.object as! ReportTarget + .onReceive(handle_notify(.report)) { target in self.active_sheet = .report(target) } - .onReceive(handle_notify(.mute)) { notif in - let pubkey = notif.object as! String + .onReceive(handle_notify(.mute)) { pubkey in self.muting = pubkey self.confirm_mute = true } - .onReceive(handle_notify(.attached_wallet)) { notif in + .onReceive(handle_notify(.attached_wallet)) { nwc in // update the lightning address on our profile when we attach a // wallet with an associated - let nwc = notif.object as! WalletConnectURL guard let ds = self.damus_state, let lud16 = nwc.lud16, let keypair = ds.keypair.to_full(), @@ -390,8 +386,7 @@ struct ContentView: View { } ds.postbox.send(ev) } - .onReceive(handle_notify(.broadcast_event)) { obj in - let ev = obj.object as! NostrEvent + .onReceive(handle_notify(.broadcast)) { ev in guard let ds = self.damus_state else { return } @@ -400,47 +395,43 @@ struct ContentView: View { ds.postbox.send(profile.event) } } - .onReceive(handle_notify(.unfollow)) { notif in + .onReceive(handle_notify(.unfollow)) { target in guard let state = self.damus_state else { return } - _ = handle_unfollow_notif(state: state, notif: notif) + _ = handle_unfollow_notif(state: state, target: target) } - .onReceive(handle_notify(.unfollowed)) { notif in - let unfollow = notif.object as! ReferencedId + .onReceive(handle_notify(.unfollowed)) { unfollow in home.resubscribe(.unfollowing(unfollow)) } - .onReceive(handle_notify(.follow)) { notif in + .onReceive(handle_notify(.follow)) { target in guard let state = self.damus_state else { return } - guard handle_follow_notif(state: state, notif: notif) else { return } + guard handle_follow_notif(state: state, target: target) else { return } } - .onReceive(handle_notify(.followed)) { notif in + .onReceive(handle_notify(.followed)) { _ in home.resubscribe(.following) } - .onReceive(handle_notify(.post)) { notif in + .onReceive(handle_notify(.post)) { post in guard let state = self.damus_state, let keypair = state.keypair.to_full() else { return } - if !handle_post_notification(keypair: keypair, postbox: state.postbox, events: state.events, notif: notif) { + if !handle_post_notification(keypair: keypair, postbox: state.postbox, events: state.events, post: post) { self.active_sheet = nil } } - .onReceive(handle_notify(.new_mutes)) { notif in + .onReceive(handle_notify(.new_mutes)) { _ in home.filter_events() } - .onReceive(handle_notify(.mute_thread)) { notif in + .onReceive(handle_notify(.mute_thread)) { _ in home.filter_events() } - .onReceive(handle_notify(.unmute_thread)) { notif in + .onReceive(handle_notify(.unmute_thread)) { _ in home.filter_events() } - .onReceive(handle_notify(.present_sheet)) { notif in - let sheet = notif.object as! Sheets + .onReceive(handle_notify(.present_sheet)) { sheet in self.active_sheet = sheet } - .onReceive(handle_notify(.zapping)) { notif in - let zap_ev = notif.object as! ZappingEvent - + .onReceive(handle_notify(.zapping)) { zap_ev in guard !zap_ev.is_custom else { return } @@ -475,12 +466,8 @@ struct ContentView: View { break } } - .onReceive(handle_notify(.local_notification)) { notif in - - guard let local = notif.object as? LossyLocalNotification, - let damus_state else { - return - } + .onReceive(handle_notify(.local_notification)) { local in + guard let damus_state else { return } if local.type == .profile_zap { open_profile(id: local.event_id) @@ -503,8 +490,7 @@ struct ContentView: View { break } } - .onReceive(handle_notify(.onlyzaps_mode)) { notif in - let hide = notif.object as! Bool + .onReceive(handle_notify(.onlyzaps_mode)) { hide in home.filter_events() guard let damus_state, @@ -597,12 +583,12 @@ struct ContentView: View { func switch_timeline(_ timeline: Timeline) { self.isSideBarOpened = false - self.popToRoot() - NotificationCenter.default.post(name: .switched_timeline, object: timeline) - + + notify(.switched_timeline(timeline)) + if timeline == self.selected_timeline { - NotificationCenter.default.post(name: .scroll_to_top, object: nil) + notify(.scroll_to_top) return } @@ -894,7 +880,7 @@ func handle_unfollow(state: DamusState, unfollow: ReferencedId) -> Bool { return false } - notify(.unfollowed, unfollow) + notify(.unfollowed(unfollow)) state.contacts.event = ev @@ -906,8 +892,7 @@ func handle_unfollow(state: DamusState, unfollow: ReferencedId) -> Bool { return true } -func handle_unfollow_notif(state: DamusState, notif: Notification) -> ReferencedId? { - let target = notif.object as! FollowTarget +func handle_unfollow_notif(state: DamusState, target: FollowTarget) -> ReferencedId? { let pk = target.pubkey let ref = ReferencedId.p(pk) @@ -929,7 +914,7 @@ func handle_follow(state: DamusState, follow: ReferencedId) -> Bool { return false } - notify(.followed, follow) + notify(.followed(follow)) state.contacts.event = ev if follow.key == "p" { @@ -940,21 +925,19 @@ func handle_follow(state: DamusState, follow: ReferencedId) -> Bool { } @discardableResult -func handle_follow_notif(state: DamusState, notif: Notification) -> Bool { - let fnotify = notif.object as! FollowTarget - switch fnotify { +func handle_follow_notif(state: DamusState, target: FollowTarget) -> Bool { + switch target { case .pubkey(let pk): state.contacts.add_friend_pubkey(pk) case .contact(let ev): state.contacts.add_friend_contact(ev) } - return handle_follow(state: state, follow: .p(fnotify.pubkey)) + return handle_follow(state: state, follow: .p(target.pubkey)) } -func handle_post_notification(keypair: FullKeypair, postbox: PostBox, events: EventCache, notif: Notification) -> Bool { - let post_res = notif.object as! NostrPostResult - switch post_res { +func handle_post_notification(keypair: FullKeypair, postbox: PostBox, events: EventCache, post: NostrPostResult) -> Bool { + switch post { case .post(let post): //let post = tup.0 //let to_relays = tup.1 diff --git a/damus/Models/Contacts.swift b/damus/Models/Contacts.swift @@ -35,14 +35,14 @@ class Contacts { let new = Set(ev.referenced_pubkeys.map({ $0.ref_id })) let diff = old.symmetricDifference(new) - var new_mutes = Array<String>() - var new_unmutes = Array<String>() - + var new_mutes = Set<Pubkey>() + var new_unmutes = Set<Pubkey>() + for d in diff { if new.contains(d) { - new_mutes.append(d.string()) + new_mutes.insert(d.string()) } else { - new_unmutes.append(d.string()) + new_unmutes.insert(d.string()) } } @@ -50,11 +50,11 @@ class Contacts { self.muted = Set(ev.referenced_pubkeys.map({ $0.ref_id.string() })) if new_mutes.count > 0 { - notify(.new_mutes, new_mutes) + notify(.new_mutes(new_mutes)) } if new_unmutes.count > 0 { - notify(.new_unmutes, new_unmutes) + notify(.new_unmutes(new_unmutes)) } } diff --git a/damus/Models/FollowTarget.swift b/damus/Models/FollowTarget.swift @@ -7,17 +7,14 @@ import Foundation - enum FollowTarget { - case pubkey(String) + case pubkey(Pubkey) case contact(NostrEvent) - var pubkey: String { + var pubkey: Pubkey { switch self { - case .pubkey(let pk): - return pk - case .contact(let ev): - return ev.pubkey + case .pubkey(let pk): return pk + case .contact(let ev): return ev.pubkey } } } diff --git a/damus/Models/HomeModel.swift b/damus/Models/HomeModel.swift @@ -328,8 +328,8 @@ class HomeModel { break case .success(let n): let boosted = Counted(event: ev, id: e, total: n) - notify(.boosted, boosted) - notify(.update_stats, e) + notify(.reposted(boosted)) + notify(.update_stats(note_id: e)) } } @@ -349,8 +349,8 @@ class HomeModel { case .success(let n): handle_notification(ev: ev) let liked = Counted(event: ev, id: e.ref_id, total: n) - notify(.liked, liked) - notify(.update_stats, e.ref_id) + notify(.liked(liked)) + notify(.update_stats(note_id: e.ref_id)) } } @@ -716,12 +716,12 @@ func load_our_contacts(state: DamusState, m_old_ev: NostrEvent?, ev: NostrEvent) let diff = new_refs.symmetricDifference(old_refs) for ref in diff { if new_refs.contains(ref) { - notify(.followed, ref) + notify(.followed(ref)) if ref.key == "p" { contacts.add_friend_pubkey(ref.ref_id) } } else { - notify(.unfollowed, ref) + notify(.unfollowed(ref)) if ref.key == "p" { contacts.remove_friend(ref.ref_id) } @@ -817,7 +817,7 @@ func process_metadata_profile(our_pubkey: String, profiles: Profiles, profile: P } if changed { - notify(.profile_updated, ProfileUpdate(pubkey: ev.pubkey, profile: profile)) + notify(.profile_updated(pubkey: ev.pubkey, profile: profile)) } } @@ -933,7 +933,7 @@ func load_our_relays(state: DamusState, m_old_ev: NostrEvent?, ev: NostrEvent) { if changed { save_bootstrap_relays(pubkey: state.pubkey, relays: Array(new)) state.pool.connect() - notify(.relays_changed, ()) + notify(.relays_changed) } } diff --git a/damus/Models/MutedThreadsManager.swift b/damus/Models/MutedThreadsManager.swift @@ -60,11 +60,11 @@ class MutedThreadsManager: ObservableObject { if isMutedThread(ev, privkey: keypair.privkey) { mutedThreads = mutedThreads.filter { $0 != threadId } _mutedThreadsSet.remove(threadId) - notify(.unmute_thread, ev) + notify(.unmute_thread(ev)) } else { mutedThreads.append(threadId) _mutedThreadsSet.insert(threadId) - notify(.mute_thread, ev) + notify(.mute_thread(ev)) } } } diff --git a/damus/Models/Report.swift b/damus/Models/Report.swift @@ -38,12 +38,17 @@ struct ReportNoteTarget { enum ReportTarget { case user(String) case note(ReportNoteTarget) + + static func note(pubkey: Pubkey, note_id: NoteId) -> ReportTarget { + return .note(ReportNoteTarget(pubkey: pubkey, note_id: note_id)) + } } struct Report { let type: ReportType let target: ReportTarget let message: String + } func create_report_tags(target: ReportTarget, type: ReportType) -> [[String]] { diff --git a/damus/Models/WalletModel.swift b/damus/Models/WalletModel.swift @@ -57,7 +57,7 @@ class WalletModel: ObservableObject { func connect(_ nwc: WalletConnectURL) { self.settings.nostr_wallet_connect = nwc.to_url().absoluteString - notify(.attached_wallet, nwc) + notify(.attached_wallet(nwc)) self.connect_state = .existing(nwc) self.previous_state = .existing(nwc) } diff --git a/damus/Notify/Notify.swift b/damus/Notify/Notify.swift @@ -31,12 +31,12 @@ struct Notifications<T: Notify> { struct NotifyHandler<T> { } -func notify_safe<T: Notify>(_ notify: Notifications<T>) { +func notify<T: Notify>(_ notify: Notifications<T>) { let notify = notify.notify NotificationCenter.default.post(name: T.name, object: notify.payload) } -func handle_notify_safe<T: Notify>(_ handler: NotifyHandler<T>) -> AnyPublisher<T.Payload, Never> { +func handle_notify<T: Notify>(_ handler: NotifyHandler<T>) -> AnyPublisher<T.Payload, Never> { return NotificationCenter.default.publisher(for: T.name) //.compactMap { notification in notification.object as? T.Payload } .map { notification in notification.object as! T.Payload } diff --git a/damus/Util/Notifications.swift b/damus/Util/Notifications.swift @@ -1,109 +0,0 @@ -// -// Notifications.swift -// damus -// -// Created by William Casarin on 2022-04-22. -// - -import Foundation - -extension Notification.Name { - static var relays_changed: Notification.Name { - return Notification.Name("relays_changed") - } - static var profile_updated: Notification.Name { - return Notification.Name("profile_updated") - } - static var switched_timeline: Notification.Name { - return Notification.Name("switched_timeline") - } - static var liked: Notification.Name { - return Notification.Name("liked") - } - static var scroll_to_top: Notification.Name { - return Notification.Name("scroll_to_to") - } - static var broadcast_event: Notification.Name { - return Notification.Name("broadcast event") - } - static var notice: Notification.Name { - return Notification.Name("notice") - } - static var delete: Notification.Name { - return Notification.Name("delete note") - } - static var post: Notification.Name { - return Notification.Name("send post") - } - static var compose: Notification.Name { - return Notification.Name("compose") - } - static var boosted: Notification.Name { - return Notification.Name("boosted") - } - static var follow: Notification.Name { - return Notification.Name("follow") - } - static var unfollow: Notification.Name { - return Notification.Name("unfollow") - } - static var login: Notification.Name { - return Notification.Name("login") - } - static var logout: Notification.Name { - return Notification.Name("logout") - } - static var followed: Notification.Name { - return Notification.Name("followed") - } - static var unfollowed: Notification.Name { - return Notification.Name("unfollowed") - } - static var report: Notification.Name { - return Notification.Name("report") - } - static var mute: Notification.Name { - return Notification.Name("mute") - } - static var new_mutes: Notification.Name { - return Notification.Name("new_mutes") - } - static var new_unmutes: Notification.Name { - return Notification.Name("new_unmutes") - } - static var deleted_account: Notification.Name { - return Notification.Name("deleted_account") - } - static var update_stats: Notification.Name { - return Notification.Name("update_stats") - } - static var present_sheet: Notification.Name { - return Notification.Name("present_sheet") - } - static var zapping: Notification.Name { - return Notification.Name("zapping") - } - static var mute_thread: Notification.Name { - return Notification.Name("mute_thread") - } - static var unmute_thread: Notification.Name { - return Notification.Name("unmute_thread") - } - static var local_notification: Notification.Name { - return Notification.Name("local_notification") - } - static var onlyzaps_mode: Notification.Name { - return Notification.Name("hide_reactions") - } - static var attached_wallet: Notification.Name { - return Notification.Name("attached_wallet") - } -} - -func handle_notify(_ name: Notification.Name) -> NotificationCenter.Publisher { - return NotificationCenter.default.publisher(for: name) -} - -func notify(_ name: NSNotification.Name, _ object: Any?) { - NotificationCenter.default.post(name: name, object: object) -} diff --git a/damus/Util/Zaps.swift b/damus/Util/Zaps.swift @@ -89,8 +89,8 @@ class Zaps { event_counts[id] = event_counts[id]! + 1 event_totals[id] = event_totals[id]! + zap.amount - - notify(.update_stats, zap.target.id) + + notify(.update_stats(note_id: zap.target.id)) } } diff --git a/damus/Views/ActionBar/EventActionBar.swift b/damus/Views/ActionBar/EventActionBar.swift @@ -44,7 +44,7 @@ struct EventActionBar: View { if damus_state.keypair.privkey != nil { HStack(spacing: 4) { EventActionButton(img: "bubble2", col: bar.replied ? DamusColors.purple : Color.gray) { - notify(.compose, PostAction.replying_to(event)) + notify(.compose(.replying_to(event))) } .accessibilityLabel(NSLocalizedString("Reply", comment: "Accessibility label for reply button")) Text(verbatim: "\(bar.replies > 0 ? "\(bar.replies)" : "")") @@ -70,7 +70,7 @@ struct EventActionBar: View { HStack(spacing: 4) { LikeButton(damus_state: damus_state, liked: bar.liked, liked_emoji: bar.our_like != nil ? to_reaction_emoji(ev: bar.our_like!) : nil) { emoji in if bar.liked { - notify(.delete, bar.our_like) + //notify(.delete, bar.our_like) } else { send_like(emoji: emoji) } @@ -122,13 +122,11 @@ struct EventActionBar: View { RepostAction(damus_state: self.damus_state, event: event) } } - .onReceive(handle_notify(.update_stats)) { n in - let target = n.object as! String + .onReceive(handle_notify(.update_stats)) { target in guard target == self.event.id else { return } self.bar.update(damus: self.damus_state, evid: target) } - .onReceive(handle_notify(.liked)) { n in - let liked = n.object as! Counted + .onReceive(handle_notify(.liked)) { liked in if liked.id != event.id { return } diff --git a/damus/Views/ActionBar/RepostAction.swift b/damus/Views/ActionBar/RepostAction.swift @@ -38,7 +38,7 @@ struct RepostAction: View { Button { dismiss() DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { - notify(.compose, PostAction.quoting(self.event)) + notify(.compose(.quoting(self.event))) } } label: { diff --git a/damus/Views/ActionBar/ShareAction.swift b/damus/Views/ActionBar/ShareAction.swift @@ -51,7 +51,7 @@ struct ShareAction: View { ShareActionButton(img: "globe", text: NSLocalizedString("Broadcast", comment: "Button to broadcast note to all your relays")) { dismiss() - NotificationCenter.default.post(name: .broadcast_event, object: event) + notify(.broadcast(event)) } ShareActionButton(img: "upload", text: NSLocalizedString("Share Via...", comment: "Button to present iOS share sheet")) { diff --git a/damus/Views/BannerImageView.swift b/damus/Views/BannerImageView.swift @@ -77,9 +77,7 @@ struct BannerImageView: View { var body: some View { InnerBannerImageView(disable_animation: disable_animation, url: get_banner_url(banner: banner, pubkey: pubkey, profiles: profiles)) - .onReceive(handle_notify(.profile_updated)) { notif in - let updated = notif.object as! ProfileUpdate - + .onReceive(handle_notify(.profile_updated)) { updated in guard updated.pubkey == self.pubkey else { return } diff --git a/damus/Views/Buttons/GradientFollowButton.swift b/damus/Views/Buttons/GradientFollowButton.swift @@ -35,20 +35,12 @@ struct GradientFollowButton: View { .stroke(grayBorder, lineWidth: follow_state == .unfollows ? 0 : 1) ) } - .onReceive(handle_notify(.followed)) { notif in - let pk = notif.object as? ReferencedId - if pk?.ref_id != target.pubkey { - return - } - + .onReceive(handle_notify(.followed)) { ref in + guard target.pubkey == ref.ref_id else { return } self.follow_state = .follows } - .onReceive(handle_notify(.unfollowed)) { notif in - let pk = notif.object as? ReferencedId - if pk?.ref_id != target.pubkey { - return - } - + .onReceive(handle_notify(.unfollowed)) { ref in + guard target.pubkey == ref.ref_id else { return } self.follow_state = .unfollows } } diff --git a/damus/Views/ConfigView.swift b/damus/Views/ConfigView.swift @@ -72,7 +72,7 @@ struct ConfigView: View { Section(NSLocalizedString("Sign Out", comment: "Section title for signing out")) { Button(action: { if state.keypair.privkey == nil { - notify(.logout, ()) + notify(.logout) } else { confirm_logout = true } @@ -126,7 +126,7 @@ struct ConfigView: View { return } state.postbox.send(ev) - notify(.logout, ()) + notify(.logout) } } .alert(NSLocalizedString("Logout", comment: "Alert for logging out the user."), isPresented: $confirm_logout) { @@ -134,7 +134,7 @@ struct ConfigView: View { confirm_logout = false } Button(NSLocalizedString("Logout", comment: "Button for logging out the user."), role: .destructive) { - notify(.logout, ()) + notify(.logout) } } message: { Text("Make sure your nsec account key is saved before you logout or you will lose access to this account", comment: "Reminder message in alert to get customer to verify that their private security account key is saved saved before logging out.") diff --git a/damus/Views/Events/EventMenu.swift b/damus/Views/Events/EventMenu.swift @@ -120,7 +120,7 @@ struct MenuItems: View { } Button { - NotificationCenter.default.post(name: .broadcast_event, object: event) + notify(.broadcast(event)) } label: { Label(NSLocalizedString("Broadcast", comment: "Context menu option for broadcasting the user's note to all of the user's connected relay servers."), image: "globe") } @@ -128,14 +128,13 @@ struct MenuItems: View { // Only allow reporting if logged in with private key and the currently viewed profile is not the logged in profile. if keypair.pubkey != target_pubkey && keypair.privkey != nil { Button(role: .destructive) { - let target: ReportTarget = .note(ReportNoteTarget(pubkey: target_pubkey, note_id: event.id)) - notify(.report, target) + notify(.report(.note(pubkey: target_pubkey, note_id: event.id))) } label: { Label(NSLocalizedString("Report", comment: "Context menu option for reporting content."), image: "raising-hand") } Button(role: .destructive) { - notify(.mute, target_pubkey) + notify(.mute(target_pubkey)) } label: { Label(NSLocalizedString("Mute user", comment: "Context menu option for muting users."), image: "mute") } diff --git a/damus/Views/Events/MutedEventView.swift b/damus/Views/Events/MutedEventView.swift @@ -60,20 +60,12 @@ struct MutedEventView: View { Event } } - .onReceive(handle_notify(.new_mutes)) { notif in - guard let mutes = notif.object as? [String] else { - return - } - + .onReceive(handle_notify(.new_mutes)) { mutes in if mutes.contains(event.pubkey) { shown = false } } - .onReceive(handle_notify(.new_unmutes)) { notif in - guard let unmutes = notif.object as? [String] else { - return - } - + .onReceive(handle_notify(.new_unmutes)) { unmutes in if unmutes.contains(event.pubkey) { shown = true } diff --git a/damus/Views/Events/SelectedEventView.swift b/damus/Views/Events/SelectedEventView.swift @@ -75,8 +75,7 @@ struct SelectedEventView: View { Divider() .padding([.top], 4) } - .onReceive(handle_notify(.update_stats)) { n in - let target = n.object as! String + .onReceive(handle_notify(.update_stats)) { target in guard target == self.event.id else { return } self.bar.update(damus: self.damus, evid: target) } diff --git a/damus/Views/FollowButtonView.swift b/damus/Views/FollowButtonView.swift @@ -31,17 +31,15 @@ struct FollowButtonView: View { .stroke(follow_state == .unfollows ? .clear : borderColor(), lineWidth: 1) } } - .onReceive(handle_notify(.followed)) { notif in - let pk = notif.object as! ReferencedId - if pk.key == "p", pk.ref_id != target.pubkey { + .onReceive(handle_notify(.followed)) { pk in + guard pk.key == "p", target.pubkey == pk.ref_id else { return } self.follow_state = .follows } - .onReceive(handle_notify(.unfollowed)) { notif in - let pk = notif.object as! ReferencedId - if pk.key == "p", pk.ref_id != target.pubkey { + .onReceive(handle_notify(.unfollowed)) { pk in + guard pk.key == "p", target.pubkey == pk.ref_id else { return } @@ -97,14 +95,14 @@ struct FollowButtonView_Previews: PreviewProvider { func perform_follow_btn_action(_ fs: FollowState, target: FollowTarget) -> FollowState { switch fs { case .follows: - notify(.unfollow, target) + notify(.unfollow(target)) return .following case .following: return .following case .unfollowing: return .following case .unfollows: - notify(.follow, target) + notify(.follow(target)) return .unfollowing } } diff --git a/damus/Views/LoginView.swift b/damus/Views/LoginView.swift @@ -215,9 +215,13 @@ func process_login(_ key: ParsedKey, is_pubkey: Bool) async throws { } save_pubkey(pubkey: pk) } - + + guard let keypair = get_saved_keypair() else { + return + } + await MainActor.run { - notify(.login, ()) + notify(.login(keypair)) } } diff --git a/damus/Views/NoteContentView.swift b/damus/Views/NoteContentView.swift @@ -225,8 +225,7 @@ struct NoteContentView: View { var body: some View { ArtifactContent - .onReceive(handle_notify(.profile_updated)) { notif in - let profile = notif.object as! ProfileUpdate + .onReceive(handle_notify(.profile_updated)) { profile in let blocks = event.blocks(damus_state.keypair.privkey) for block in blocks.blocks { switch block { diff --git a/damus/Views/Onboarding/SuggestedUsersViewModel.swift b/damus/Views/Onboarding/SuggestedUsersViewModel.swift @@ -44,7 +44,7 @@ class SuggestedUsersViewModel: ObservableObject { func follow(pubkeys: [String]) { for pubkey in pubkeys { - notify(.follow, FollowTarget.pubkey(pubkey)) + notify(.follow(.pubkey(pubkey))) } } diff --git a/damus/Views/PostView.swift b/damus/Views/PostView.swift @@ -67,7 +67,7 @@ struct PostView: View { @Environment(\.presentationMode) var presentationMode func cancel() { - NotificationCenter.default.post(name: .post, object: NostrPostResult.cancel) + notify(.post(.cancel)) dismiss() } @@ -78,7 +78,7 @@ struct PostView: View { func send_post() { let new_post = build_post(post: self.post, action: action, uploadedMedias: uploadedMedias, references: references) - NotificationCenter.default.post(name: .post, object: NostrPostResult.post(new_post)) + notify(.post(.post(new_post))) clear_draft() diff --git a/damus/Views/Profile/EventProfileName.swift b/damus/Views/Profile/EventProfileName.swift @@ -92,8 +92,7 @@ struct EventProfileName: View { SupporterBadge(percent: supporter) } } - .onReceive(handle_notify(.profile_updated)) { notif in - let update = notif.object as! ProfileUpdate + .onReceive(handle_notify(.profile_updated)) { update in if update.pubkey != pubkey { return } diff --git a/damus/Views/Profile/ProfileName.swift b/damus/Views/Profile/ProfileName.swift @@ -98,8 +98,7 @@ struct ProfileName: View { SupporterBadge(percent: supporter) } } - .onReceive(handle_notify(.profile_updated)) { notif in - let update = notif.object as! ProfileUpdate + .onReceive(handle_notify(.profile_updated)) { update in if update.pubkey != pubkey { return } diff --git a/damus/Views/Profile/ProfilePicView.swift b/damus/Views/Profile/ProfilePicView.swift @@ -86,9 +86,7 @@ struct ProfilePicView: View { var body: some View { InnerProfilePicView(url: get_profile_url(picture: picture, pubkey: pubkey, profiles: profiles), fallbackUrl: URL(string: robohash(pubkey)), pubkey: pubkey, size: size, highlight: highlight, disable_animation: disable_animation) - .onReceive(handle_notify(.profile_updated)) { notif in - let updated = notif.object as! ProfileUpdate - + .onReceive(handle_notify(.profile_updated)) { updated in guard updated.pubkey == self.pubkey else { return } diff --git a/damus/Views/Profile/ProfileView.swift b/damus/Views/Profile/ProfileView.swift @@ -178,8 +178,7 @@ struct ProfileView: View { // Only allow reporting if logged in with private key and the currently viewed profile is not the logged in profile. if profile.pubkey != damus_state.pubkey && damus_state.is_privkey_user { Button(NSLocalizedString("Report", comment: "Button to report a profile."), role: .destructive) { - let target: ReportTarget = .user(profile.pubkey) - notify(.report, target) + notify(.report(.user(profile.pubkey))) } if damus_state.contacts.is_muted(profile.pubkey) { @@ -200,7 +199,7 @@ struct ProfileView: View { } } else { Button(NSLocalizedString("Mute", comment: "Button to mute a profile."), role: .destructive) { - notify(.mute, profile.pubkey) + notify(.mute(profile.pubkey)) } } } @@ -479,7 +478,7 @@ struct ProfileView: View { if damus_state.is_privkey_user { PostButtonContainer(is_left_handed: damus_state.settings.left_handed) { - notify(.compose, PostAction.posting(.user(profile.pubkey))) + notify(.compose(.posting(.user(profile.pubkey)))) } } } @@ -584,7 +583,7 @@ func check_nip05_validity(pubkey: String, profiles: Profiles) { Task { @MainActor in profiles.set_validated(pubkey, nip05: validated) profiles.nip05_pubkey[nip05] = pubkey - notify(.profile_updated, ProfileUpdate(pubkey: pubkey, profile: profile)) + notify(.profile_updated(pubkey: pubkey, profile: profile)) } } } diff --git a/damus/Views/SaveKeysView.swift b/damus/Views/SaveKeysView.swift @@ -141,7 +141,7 @@ struct SaveKeysView: View { do { try save_keypair(pubkey: account.pubkey, privkey: account.privkey) - notify(.login, account.keypair) + notify(.login(account.keypair)) } catch { self.error = "Failed to save keys" } diff --git a/damus/Views/Settings/ZapSettingsView.swift b/damus/Views/Settings/ZapSettingsView.swift @@ -29,7 +29,7 @@ struct ZapSettingsView: View { Toggle(NSLocalizedString("OnlyZaps mode", comment: "Setting toggle to hide reactions."), isOn: $settings.onlyzaps_mode) .toggleStyle(.switch) .onChange(of: settings.onlyzaps_mode) { newVal in - notify(.onlyzaps_mode, newVal) + notify(.onlyzaps_mode(newVal)) } } diff --git a/damus/Views/SideMenuView.swift b/damus/Views/SideMenuView.swift @@ -137,7 +137,7 @@ struct SideMenuView: View { Button(action: { //ConfigView(state: damus_state) if damus_state.keypair.privkey == nil { - notify(.logout, ()) + notify(.logout) } else { confirm_logout = true } @@ -175,7 +175,7 @@ struct SideMenuView: View { confirm_logout = false } Button(NSLocalizedString("Logout", comment: "Button for logging out the user."), role: .destructive) { - notify(.logout, ()) + notify(.logout) } } message: { Text("Make sure your nsec account key is saved before you logout or you will lose access to this account", comment: "Reminder message in alert to get customer to verify that their private security account key is saved saved before logging out.") diff --git a/damus/Views/TimelineView.swift b/damus/Views/TimelineView.swift @@ -51,7 +51,7 @@ struct TimelineView<Content: View>: View { } .buttonStyle(BorderlessButtonStyle()) .coordinateSpace(name: "scroll") - .onReceive(NotificationCenter.default.publisher(for: .scroll_to_top)) { _ in + .onReceive(handle_notify(.scroll_to_top)) { () in events.flush() self.events.should_queue = false scroll_to_event(scroller: scroller, id: "startblock", delay: 0.0, animate: true, anchor: .top) diff --git a/damus/Views/Wallet/WalletView.swift b/damus/Views/Wallet/WalletView.swift @@ -169,8 +169,8 @@ struct WalletView: View { } profile.damus_donation = p - - notify(.profile_updated, ProfileUpdate(pubkey: damus_state.pubkey, profile: profile)) + + notify(.profile_updated(pubkey: damus_state.pubkey, profile: profile)) } .onDisappear { guard let keypair = damus_state.keypair.to_full(), diff --git a/damus/Views/Zaps/CustomizeZapView.swift b/damus/Views/Zaps/CustomizeZapView.swift @@ -185,12 +185,8 @@ struct CustomizeZapView: View { } } - func receive_zap(notif: Notification) { - let zap_ev = notif.object as! ZappingEvent - guard zap_ev.is_custom else { - return - } - guard zap_ev.target.id == target.id else { + func receive_zap(zap_ev: ZappingEvent) { + guard zap_ev.is_custom, zap_ev.target.id == target.id else { return } @@ -257,8 +253,8 @@ struct CustomizeZapView: View { .onAppear { model.set_defaults(settings: state.settings) } - .onReceive(handle_notify(.zapping)) { notif in - receive_zap(notif: notif) + .onReceive(handle_notify(.zapping)) { zap_ev in + receive_zap(zap_ev: zap_ev) } .background(fillColor().edgesIgnoringSafeArea(.all)) .onTapGesture { diff --git a/damus/damusApp.swift b/damus/damusApp.swift @@ -34,7 +34,7 @@ struct MainView: View { } } .dynamicTypeSize(.xSmall ... .xxxLarge) - .onReceive(handle_notify(.logout)) { _ in + .onReceive(handle_notify(.logout)) { () in try? clear_keypair() keypair = nil } @@ -59,7 +59,7 @@ class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDele func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { let userInfo = response.notification.request.content.userInfo let notification = LossyLocalNotification.from_user_info(user_info: userInfo) - notify(.local_notification, notification) + notify(.local_notification(notification)) completionHandler() } }