damus

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

NotificationItemView.swift (3054B)


      1 //
      2 //  NotificationItemView.swift
      3 //  damus
      4 //
      5 //  Created by William Casarin on 2023-02-21.
      6 //
      7 
      8 import SwiftUI
      9 
     10 enum ShowItem {
     11     case show(NostrEvent?)
     12     case dontshow(NostrEvent?)
     13     case show_damus_app_notification(DamusAppNotification)
     14 }
     15 
     16 func notification_item_event(events: EventCache, notif: NotificationItem) -> ShowItem {
     17     switch notif {
     18     case .repost(let evid, _):
     19         return .dontshow(events.lookup(evid))
     20     case .reply(let ev):
     21         return .show(ev)
     22     case .reaction(let evid, _):
     23         return .dontshow(events.lookup(evid))
     24     case .event_zap(let evid, _):
     25         return .dontshow(events.lookup(evid))
     26     case .profile_zap:
     27         return .show(nil)
     28     case .damus_app_notification(let app_notification):
     29         return .show_damus_app_notification(app_notification)
     30     }
     31 }
     32 
     33 struct NotificationItemView: View {
     34     let state: DamusState
     35     let item: NotificationItem
     36     
     37     var show_item: ShowItem {
     38         notification_item_event(events: state.events, notif: item)
     39     }
     40     
     41     var options: EventViewOptions {
     42         if state.settings.truncate_mention_text {
     43             return [.wide, .truncate_content]
     44         }
     45         
     46         return [.wide]
     47     }
     48     
     49     func Item(_ ev: NostrEvent?) -> some View {
     50         Group {
     51             switch item {
     52             case .repost(_, let evgrp):
     53                 EventGroupView(state: state, event: ev, group: .repost(evgrp))
     54                 
     55             case .event_zap(_, let zapgrp):
     56                 EventGroupView(state: state, event: ev, group: .zap(zapgrp))
     57                 
     58             case .profile_zap(let grp):
     59                 EventGroupView(state: state, event: nil, group: .profile_zap(grp))
     60             
     61             case .reaction(_, let evgrp):
     62                 EventGroupView(state: state, event: ev, group: .reaction(evgrp))
     63             
     64             case .reply(let ev):
     65                 NavigationLink(value: Route.Thread(thread: ThreadModel(event: ev, damus_state: state))) {
     66                     EventView(damus: state, event: ev, options: options)
     67                 }
     68                 .buttonStyle(.plain)
     69             case .damus_app_notification(let notification):
     70                 DamusAppNotificationView(damus_state: state, notification: notification)
     71             }
     72             
     73             ThiccDivider()
     74         }
     75     }
     76     
     77     var body: some View {
     78         Group {
     79             switch show_item {
     80             case .show(let ev):
     81                 Item(ev)
     82                 
     83             case .dontshow(let ev):
     84                 if let ev {
     85                     Item(ev)
     86                 }
     87             case .show_damus_app_notification(let notification):
     88                 DamusAppNotificationView(damus_state: state, notification: notification)
     89             }
     90         }
     91     }
     92 }
     93 
     94 let test_notification_item: NotificationItem = .repost(test_note.id, test_event_group)
     95 
     96 struct NotificationItemView_Previews: PreviewProvider {
     97     static var previews: some View {
     98         NotificationItemView(state: test_damus_state, item: test_notification_item)
     99     }
    100 }