damus

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

ProfilePictureSelector.swift (2123B)


      1 //
      2 //  EditProfilePictureView.swift
      3 //  damus
      4 //
      5 //  Created by William Casarin on 2022-05-20.
      6 //
      7 
      8 import SwiftUI
      9 import Kingfisher
     10 import Combine
     11 
     12 struct EditProfilePictureView: View {
     13     
     14     @State var profile_url: URL?
     15     
     16     let pubkey: Pubkey
     17     var damus_state: DamusState?
     18     var size: CGFloat = 80.0
     19     let highlight: Highlight = .custom(Color.white, 2.0)
     20     @ObservedObject var uploadObserver: ImageUploadingObserver
     21     let callback: (URL?) -> Void
     22 
     23     var body: some View {
     24         ZStack {
     25             Color(uiColor: .systemBackground)
     26     
     27             KFAnimatedImage(get_profile_url())
     28                 .imageContext(.pfp, disable_animation: damus_state?.settings.disable_animation == true)
     29                 .cancelOnDisappear(true)
     30                 .configure { view in
     31                     view.framePreloadCount = 3
     32                 }
     33                 .scaledToFill()
     34                 .kfClickable()
     35     
     36             EditPictureControl(
     37                 uploader: damus_state?.settings.default_media_uploader ?? .nostrBuild,
     38                 context: .profile_picture,
     39                 keypair: damus_state?.keypair,
     40                 pubkey: pubkey,
     41                 current_image_url: $profile_url,
     42                 upload_observer: uploadObserver,
     43                 callback: callback
     44             )
     45         }
     46         .frame(width: size, height: size)
     47         .clipShape(Circle())
     48         .overlay(Circle().stroke(highlight_color(highlight), lineWidth: pfp_line_width(highlight)))
     49     }
     50     
     51     private func get_profile_url() -> URL? {
     52         if let profile_url {
     53             return profile_url
     54         } else if let state = damus_state,
     55                   let picture = state.profiles.lookup(id: pubkey)?.map({ pr in pr?.picture }).value {
     56             return URL(string: picture)
     57         } else {
     58             return profile_url ?? URL(string: robohash(pubkey))
     59         }
     60     }
     61 }
     62 
     63 struct ProfilePictureSelector_Previews: PreviewProvider {
     64     static var previews: some View {
     65         EditProfilePictureView(pubkey: test_pubkey, uploadObserver: ImageUploadingObserver()) { _ in
     66             //
     67         }
     68     }
     69 }