damus

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

EventView.swift (5279B)


      1 //
      2 //  EventView.swift
      3 //  damus
      4 //
      5 //  Created by William Casarin on 2022-04-11.
      6 //
      7 
      8 import Foundation
      9 import SwiftUI
     10 
     11 enum EventViewKind {
     12     case small
     13     case normal
     14     case selected
     15     case title
     16     case subheadline
     17 }
     18 
     19 struct EventView: View {
     20     let event: NostrEvent
     21     let options: EventViewOptions
     22     let damus: DamusState
     23     let pubkey: Pubkey
     24 
     25     init(damus: DamusState, event: NostrEvent, pubkey: Pubkey? = nil, options: EventViewOptions = []) {
     26         self.event = event
     27         self.options = options
     28         self.damus = damus
     29         self.pubkey = pubkey ?? event.pubkey
     30     }
     31 
     32     var body: some View {
     33         VStack {
     34             if event.known_kind == .boost {
     35                 if let inner_ev = event.get_inner_event(cache: damus.events) {
     36                     RepostedEvent(damus: damus, event: event, inner_ev: inner_ev, options: options)
     37                 } else {
     38                     EmptyView()
     39                 }
     40             } else if event.known_kind == .zap {
     41                 if let zap = damus.zaps.zaps[event.id] {
     42                     ZapEvent(damus: damus, zap: zap, is_top_zap: options.contains(.top_zap))
     43                 } else {
     44                     EmptyView()
     45                 }
     46             } else if event.known_kind == .longform {
     47                 LongformPreview(state: damus, ev: event, options: options)
     48             } else if event.known_kind == .highlight {
     49                 HighlightView(state: damus, event: event, options: options)
     50             } else {
     51                 TextEvent(damus: damus, event: event, pubkey: pubkey, options: options)
     52                     //.padding([.top], 6)
     53             }
     54         }
     55     }
     56 }
     57 
     58 // blame the porn bots for this code
     59 func should_blur_images(settings: UserSettingsStore, contacts: Contacts, ev: NostrEvent, our_pubkey: Pubkey, booster_pubkey: Pubkey? = nil) -> Bool {
     60     if settings.undistractMode {
     61         return true
     62     }
     63     
     64     if !settings.blur_images {
     65         return false
     66     }
     67     
     68     if ev.pubkey == our_pubkey {
     69         return false
     70     }
     71     if contacts.is_in_friendosphere(ev.pubkey) {
     72         return false
     73     }
     74     if let boost_key = booster_pubkey, contacts.is_in_friendosphere(boost_key) {
     75         return false
     76     }
     77     return true
     78 }
     79 
     80 // blame the porn bots for this code too
     81 func should_blur_images(damus_state: DamusState, ev: NostrEvent) -> Bool {
     82     return should_blur_images(
     83         settings: damus_state.settings,
     84         contacts: damus_state.contacts,
     85         ev: ev,
     86         our_pubkey: damus_state.pubkey
     87     )
     88 }
     89 
     90 func format_relative_time(_ created_at: UInt32) -> String
     91 {
     92     return time_ago_since(Date(timeIntervalSince1970: Double(created_at)))
     93 }
     94 
     95 func format_date(created_at: UInt32) -> String {
     96     let date = Date(timeIntervalSince1970: TimeInterval(created_at))
     97     return format_date(date: date)
     98 }
     99 
    100 func format_date(date: Date, time_style: DateFormatter.Style = .short) -> String {
    101     let dateFormatter = DateFormatter()
    102     dateFormatter.timeStyle = time_style
    103     dateFormatter.dateStyle = .short
    104     return dateFormatter.string(from: date)
    105 }
    106 
    107 func make_actionbar_model(ev: NoteId, damus: DamusState) -> ActionBarModel {
    108     let model = ActionBarModel.empty()
    109     model.update(damus: damus, evid: ev)
    110     return model
    111 }
    112 
    113 func eventviewsize_to_font(_ size: EventViewKind, font_size: Double) -> Font {
    114     switch size {
    115     case .small:
    116         return Font.system(size: 12.0 * font_size)
    117     case .normal:
    118         return Font.system(size: 17.0 * font_size) // Assuming .body is 17pt by default
    119     case .selected:
    120         return .custom("selected", size: 21.0 * font_size)
    121     case .title:
    122         return Font.system(size: 24.0 * font_size) // Assuming .title is 24pt by default
    123     case .subheadline:
    124         return Font.system(size: 14.0 * font_size) // Assuming .subheadline is 14pt by default
    125     }
    126 }
    127 
    128 func eventviewsize_to_uifont(_ size: EventViewKind) -> UIFont {
    129     switch size {
    130     case .small:
    131         return .preferredFont(forTextStyle: .body)
    132     case .normal:
    133         return .preferredFont(forTextStyle: .body)
    134     case .selected:
    135         return .preferredFont(forTextStyle: .title2)
    136     case .subheadline:
    137         return .preferredFont(forTextStyle: .subheadline)
    138     case .title:
    139         return .preferredFont(forTextStyle: .title1)
    140     }
    141 }
    142 
    143 
    144 struct EventView_Previews: PreviewProvider {
    145     static var previews: some View {
    146         VStack {
    147             /*
    148             EventView(damus: test_damus_state(), event: NostrEvent(content: "hello there https://jb55.com/s/Oct12-150217.png https://jb55.com/red-me.jb55 cool", pubkey: "pk"), show_friend_icon: true, size: .small)
    149             EventView(damus: test_damus_state(), event: NostrEvent(content: "hello there https://jb55.com/s/Oct12-150217.png https://jb55.com/red-me.jb55 cool", pubkey: "pk"), show_friend_icon: true, size: .normal)
    150             EventView(damus: test_damus_state(), event: NostrEvent(content: "hello there https://jb55.com/s/Oct12-150217.png https://jb55.com/red-me.jb55 cool", pubkey: "pk"), show_friend_icon: true, size: .big)
    151             
    152              */
    153 
    154             EventView( damus: test_damus_state, event: test_note )
    155 
    156             EventView( damus: test_damus_state, event: test_longform_event.event, options: [.wide] )
    157         }
    158         .padding()
    159     }
    160 }
    161