notify: switch over to new typesafe notifications

This commit is contained in:
William Casarin
2023-07-30 10:50:41 -07:00
parent 80063af19a
commit b40c595a7c
39 changed files with 129 additions and 289 deletions

View File

@@ -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
}

View File

@@ -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: {

View File

@@ -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")) {

View File

@@ -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
}

View File

@@ -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
}
}

View File

@@ -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.")

View File

@@ -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")
}

View File

@@ -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
}

View File

@@ -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)
}

View File

@@ -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
}
}

View File

@@ -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))
}
}

View File

@@ -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 {

View File

@@ -44,7 +44,7 @@ class SuggestedUsersViewModel: ObservableObject {
func follow(pubkeys: [String]) {
for pubkey in pubkeys {
notify(.follow, FollowTarget.pubkey(pubkey))
notify(.follow(.pubkey(pubkey)))
}
}

View File

@@ -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()

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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))
}
}
}

View File

@@ -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"
}

View File

@@ -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))
}
}

View File

@@ -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.")

View File

@@ -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)

View File

@@ -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(),

View File

@@ -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 {