ProfilePictureSelector.swift (1899B)
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 35 EditPictureControl(uploader: damus_state?.settings.default_media_uploader ?? .nostrBuild, pubkey: pubkey, image_url: $profile_url, uploadObserver: uploadObserver, callback: callback) 36 } 37 .frame(width: size, height: size) 38 .clipShape(Circle()) 39 .overlay(Circle().stroke(highlight_color(highlight), lineWidth: pfp_line_width(highlight))) 40 } 41 42 private func get_profile_url() -> URL? { 43 if let profile_url { 44 return profile_url 45 } else if let state = damus_state, 46 let picture = state.profiles.lookup(id: pubkey)?.map({ pr in pr?.picture }).value { 47 return URL(string: picture) 48 } else { 49 return profile_url ?? URL(string: robohash(pubkey)) 50 } 51 } 52 } 53 54 struct ProfilePictureSelector_Previews: PreviewProvider { 55 static var previews: some View { 56 EditProfilePictureView(pubkey: test_pubkey, uploadObserver: ImageUploadingObserver()) { _ in 57 // 58 } 59 } 60 }