damus

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

EventView.swift (4793B)


      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 {
     49                 TextEvent(damus: damus, event: event, pubkey: pubkey, options: options)
     50                     //.padding([.top], 6)
     51             }
     52         }
     53     }
     54 }
     55 
     56 // blame the porn bots for this code
     57 func should_blur_images(settings: UserSettingsStore, contacts: Contacts, ev: NostrEvent, our_pubkey: Pubkey, booster_pubkey: Pubkey? = nil) -> Bool {
     58     if !settings.blur_images {
     59         return false
     60     }
     61     
     62     if ev.pubkey == our_pubkey {
     63         return false
     64     }
     65     if contacts.is_in_friendosphere(ev.pubkey) {
     66         return false
     67     }
     68     if let boost_key = booster_pubkey, contacts.is_in_friendosphere(boost_key) {
     69         return false
     70     }
     71     return true
     72 }
     73 
     74 func format_relative_time(_ created_at: UInt32) -> String
     75 {
     76     return time_ago_since(Date(timeIntervalSince1970: Double(created_at)))
     77 }
     78 
     79 func format_date(created_at: UInt32) -> String {
     80     let date = Date(timeIntervalSince1970: TimeInterval(created_at))
     81     return format_date(date: date)
     82 }
     83 
     84 func format_date(date: Date, time_style: DateFormatter.Style = .short) -> String {
     85     let dateFormatter = DateFormatter()
     86     dateFormatter.timeStyle = time_style
     87     dateFormatter.dateStyle = .short
     88     return dateFormatter.string(from: date)
     89 }
     90 
     91 func make_actionbar_model(ev: NoteId, damus: DamusState) -> ActionBarModel {
     92     let model = ActionBarModel.empty()
     93     model.update(damus: damus, evid: ev)
     94     return model
     95 }
     96 
     97 func eventviewsize_to_font(_ size: EventViewKind, font_size: Double) -> Font {
     98     switch size {
     99     case .small:
    100         return Font.system(size: 12.0 * font_size)
    101     case .normal:
    102         return Font.system(size: 17.0 * font_size) // Assuming .body is 17pt by default
    103     case .selected:
    104         return .custom("selected", size: 21.0 * font_size)
    105     case .title:
    106         return Font.system(size: 24.0 * font_size) // Assuming .title is 24pt by default
    107     case .subheadline:
    108         return Font.system(size: 14.0 * font_size) // Assuming .subheadline is 14pt by default
    109     }
    110 }
    111 
    112 func eventviewsize_to_uifont(_ size: EventViewKind) -> UIFont {
    113     switch size {
    114     case .small:
    115         return .preferredFont(forTextStyle: .body)
    116     case .normal:
    117         return .preferredFont(forTextStyle: .body)
    118     case .selected:
    119         return .preferredFont(forTextStyle: .title2)
    120     case .subheadline:
    121         return .preferredFont(forTextStyle: .subheadline)
    122     case .title:
    123         return .preferredFont(forTextStyle: .title1)
    124     }
    125 }
    126 
    127 
    128 struct EventView_Previews: PreviewProvider {
    129     static var previews: some View {
    130         VStack {
    131             /*
    132             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)
    133             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)
    134             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)
    135             
    136              */
    137 
    138             EventView( damus: test_damus_state, event: test_note )
    139 
    140             EventView( damus: test_damus_state, event: test_longform_event.event, options: [.wide] )
    141         }
    142         .padding()
    143     }
    144 }
    145