damus

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

LinkView.swift (999B)


      1 //
      2 //  LinkView.swift
      3 //  damus
      4 //
      5 //  Created by Sam DuBois on 12/27/22.
      6 //
      7 
      8 import SwiftUI
      9 import LinkPresentation
     10 
     11 class CustomLinkView: LPLinkView {
     12     override var intrinsicContentSize: CGSize { CGSize(width: 0, height: super.intrinsicContentSize.height) }
     13     
     14 }
     15 
     16 enum Metadata {
     17     case linkmeta(CachedMetadata)
     18     case url(URL)
     19 }
     20 
     21 struct LinkViewRepresentable: UIViewRepresentable {
     22  
     23     typealias UIViewType = CustomLinkView
     24     
     25     let meta: Metadata
     26  
     27     func makeUIView(context: Context) -> CustomLinkView {
     28         switch meta {
     29         case .linkmeta(let linkmeta):
     30             return CustomLinkView(metadata: linkmeta.meta)
     31         case .url(let url):
     32             return CustomLinkView(url: url)
     33         }
     34     }
     35  
     36     func updateUIView(_ uiView: CustomLinkView, context: Context) {
     37         switch meta {
     38         case .linkmeta(let cached):
     39             cached.intrinsic_height = uiView.intrinsicContentSize.height
     40         case .url:
     41             return
     42         }
     43         
     44     }
     45 }