damus

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

commit df3b94a1fc53226726f07b4ef51427781036e329
parent 06a66a3709d9deb69c6a0d1da1185cb5d3abeee3
Author: William Casarin <jb55@jb55.com>
Date:   Sun, 30 Jul 2023 10:47:53 -0700

notify: add typesafe notify class

Diffstat:
Mdamus.xcodeproj/project.pbxproj | 12++++++++++++
Adamus/Notify/Notify.swift | 44++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 56 insertions(+), 0 deletions(-)

diff --git a/damus.xcodeproj/project.pbxproj b/damus.xcodeproj/project.pbxproj @@ -202,6 +202,7 @@ 4C9F18E229AA9B6C008C55EC /* CustomizeZapView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C9F18E129AA9B6C008C55EC /* CustomizeZapView.swift */; }; 4C9F18E429ABDE6D008C55EC /* MaybeAnonPfpView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C9F18E329ABDE6D008C55EC /* MaybeAnonPfpView.swift */; }; 4CA2EFA0280E37AC0044ACD8 /* TimelineView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CA2EF9F280E37AC0044ACD8 /* TimelineView.swift */; }; + 4CA352A02A76AE80003BB08B /* Notify.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CA3529F2A76AE80003BB08B /* Notify.swift */; }; 4CA352A62A76B020003BB08B /* Pubkey.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CA352A52A76B020003BB08B /* Pubkey.swift */; }; 4CA3FA1029F593D000FDB3C3 /* ZapTypePicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CA3FA0F29F593D000FDB3C3 /* ZapTypePicker.swift */; }; 4CA5588329F33F5B00DC6A45 /* StringCodable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CA5588229F33F5B00DC6A45 /* StringCodable.swift */; }; @@ -704,6 +705,7 @@ 4C9F18E129AA9B6C008C55EC /* CustomizeZapView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomizeZapView.swift; sourceTree = "<group>"; }; 4C9F18E329ABDE6D008C55EC /* MaybeAnonPfpView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MaybeAnonPfpView.swift; sourceTree = "<group>"; }; 4CA2EF9F280E37AC0044ACD8 /* TimelineView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimelineView.swift; sourceTree = "<group>"; }; + 4CA3529F2A76AE80003BB08B /* Notify.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Notify.swift; sourceTree = "<group>"; }; 4CA352A52A76B020003BB08B /* Pubkey.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Pubkey.swift; sourceTree = "<group>"; }; 4CA3FA0F29F593D000FDB3C3 /* ZapTypePicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZapTypePicker.swift; sourceTree = "<group>"; }; 4CA5588229F33F5B00DC6A45 /* StringCodable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StringCodable.swift; sourceTree = "<group>"; }; @@ -1377,6 +1379,14 @@ path = Util; sourceTree = "<group>"; }; + 4CA3529C2A76AE47003BB08B /* Notify */ = { + isa = PBXGroup; + children = ( + 4CA3529F2A76AE80003BB08B /* Notify.swift */, + ); + path = Notify; + sourceTree = "<group>"; + }; 4CA9275B2A28FF570098A105 /* Longform */ = { isa = PBXGroup; children = ( @@ -1552,6 +1562,7 @@ 4CE6DEE527F7A08100C66700 /* damus */ = { isa = PBXGroup; children = ( + 4CA3529C2A76AE47003BB08B /* Notify */, F7F0BA23297892AE009531F3 /* Modifiers */, 4C4A3A5A288A1B2200453788 /* damus.entitlements */, 4CE4F9DF285287A000C00DD9 /* Components */, @@ -2172,6 +2183,7 @@ 4C3BEFD42819DE8F00B3DE84 /* NostrKind.swift in Sources */, 4C3EA66028FF5E7700C48A62 /* node_id.c in Sources */, 4C687C212A5F7ED00092C550 /* DamusBackground.swift in Sources */, + 4CA352A02A76AE80003BB08B /* Notify.swift in Sources */, 4CE6DEE727F7A08100C66700 /* damusApp.swift in Sources */, 4C363A962827096D006E126D /* PostBlock.swift in Sources */, 4CA9275F2A2902B20098A105 /* LongformPreview.swift in Sources */, diff --git a/damus/Notify/Notify.swift b/damus/Notify/Notify.swift @@ -0,0 +1,44 @@ +// +// Notify.swift +// damus +// +// Created by William Casarin on 2023-07-30. +// + +import Foundation +import Combine + +protocol Notify { + associatedtype Payload + static var name: Notification.Name { get } + var payload: Payload { get } +} + +extension Notify { + static var name: Notification.Name { + Notification.Name("\(Self.self)") + } +} + +// needed because static dispatch off protocol extensions doesn't work so well +struct Notifications<T: Notify> { + let notify: T + + init(_ notify: T) { + self.notify = notify + } +} + +struct NotifyHandler<T> { } + +func notify_safe<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> { + return NotificationCenter.default.publisher(for: T.name) + //.compactMap { notification in notification.object as? T.Payload } + .map { notification in notification.object as! T.Payload } + .eraseToAnyPublisher() +}