Refactoring Edit Picture Views
This commit is contained in:
committed by
William Casarin
parent
bf95a8b328
commit
9e7e128d9a
@@ -89,7 +89,7 @@ struct EditMetadataView: View {
|
||||
let pfp_size: CGFloat = 90.0
|
||||
|
||||
HStack(alignment: .center) {
|
||||
ProfilePictureSelector(pubkey: damus_state.pubkey, damus_state: damus_state, uploadObserver: profileUploadObserver, callback: uploadedProfilePicture(image_url:))
|
||||
EditProfilePictureView(pubkey: damus_state.pubkey, damus_state: damus_state, size: pfp_size, uploadObserver: profileUploadObserver, callback: uploadedProfilePicture(image_url:))
|
||||
.offset(y: -(pfp_size/2.0)) // Increase if set a frame
|
||||
|
||||
Spacer()
|
||||
|
||||
@@ -7,6 +7,10 @@
|
||||
|
||||
import SwiftUI
|
||||
|
||||
class ImageUploadingObserver: ObservableObject {
|
||||
@Published var isLoading: Bool = false
|
||||
}
|
||||
|
||||
struct EditPictureControl: View {
|
||||
let uploader: MediaUploader
|
||||
let pubkey: String
|
||||
@@ -104,3 +108,17 @@ struct EditPictureControl: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct EditPictureControl_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
let pubkey = "123"
|
||||
let url = Binding<URL?>.constant(URL(string: "https://damus.io")!)
|
||||
let observer = ImageUploadingObserver()
|
||||
ZStack {
|
||||
Color.gray
|
||||
EditPictureControl(uploader: .nostrBuild, pubkey: pubkey, image_url: url, uploadObserver: observer) { _ in
|
||||
//
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,59 +28,6 @@ func pfp_line_width(_ h: Highlight) -> CGFloat {
|
||||
}
|
||||
}
|
||||
|
||||
struct EditProfilePictureView: View {
|
||||
|
||||
@Binding var url: URL?
|
||||
|
||||
let pubkey: String
|
||||
let size: CGFloat
|
||||
let highlight: Highlight
|
||||
|
||||
var damus_state: DamusState?
|
||||
|
||||
var Placeholder: some View {
|
||||
Circle()
|
||||
.frame(width: size, height: size)
|
||||
.overlay(Circle().stroke(highlight_color(highlight), lineWidth: pfp_line_width(highlight)))
|
||||
.padding(2)
|
||||
}
|
||||
|
||||
var disable_animation: Bool {
|
||||
damus_state?.settings.disable_animation ?? false
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
ZStack {
|
||||
Color(uiColor: .systemBackground)
|
||||
|
||||
KFAnimatedImage(get_profile_url())
|
||||
.imageContext(.pfp, disable_animation: disable_animation)
|
||||
.cancelOnDisappear(true)
|
||||
.configure { view in
|
||||
view.framePreloadCount = 3
|
||||
}
|
||||
.placeholder { _ in
|
||||
Placeholder
|
||||
}
|
||||
.scaledToFill()
|
||||
.opacity(0.5)
|
||||
}
|
||||
.frame(width: size, height: size)
|
||||
.clipShape(Circle())
|
||||
.overlay(Circle().stroke(highlight_color(highlight), lineWidth: pfp_line_width(highlight)))
|
||||
}
|
||||
|
||||
private func get_profile_url() -> URL? {
|
||||
if let url {
|
||||
return url
|
||||
} else if let state = damus_state, let picture = state.profiles.lookup(id: pubkey)?.picture {
|
||||
return URL(string: picture)
|
||||
} else {
|
||||
return url ?? URL(string: robohash(pubkey))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct InnerProfilePicView: View {
|
||||
let url: URL?
|
||||
let fallbackUrl: URL?
|
||||
|
||||
@@ -6,32 +6,47 @@
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
import Kingfisher
|
||||
import Combine
|
||||
|
||||
class ImageUploadingObserver: ObservableObject {
|
||||
@Published var isLoading: Bool = false
|
||||
}
|
||||
|
||||
struct ProfilePictureSelector: View {
|
||||
|
||||
struct EditProfilePictureView: View {
|
||||
|
||||
@State var profile_url: URL?
|
||||
|
||||
let pubkey: String
|
||||
var size: CGFloat = 80.0
|
||||
var damus_state: DamusState?
|
||||
var size: CGFloat = 80.0
|
||||
let highlight: Highlight = .custom(Color.white, 2.0)
|
||||
@ObservedObject var uploadObserver: ImageUploadingObserver
|
||||
let callback: (URL?) -> Void
|
||||
|
||||
var body: some View {
|
||||
ZStack {
|
||||
Color(uiColor: .systemBackground)
|
||||
|
||||
@State var profile_image: URL? = nil
|
||||
KFAnimatedImage(get_profile_url())
|
||||
.imageContext(.pfp, disable_animation: damus_state?.settings.disable_animation == true)
|
||||
.cancelOnDisappear(true)
|
||||
.configure { view in
|
||||
view.framePreloadCount = 3
|
||||
}
|
||||
.scaledToFill()
|
||||
.opacity(0.5)
|
||||
|
||||
var uploader: MediaUploader {
|
||||
damus_state?.settings.default_media_uploader ?? .nostrBuild
|
||||
EditPictureControl(uploader: damus_state?.settings.default_media_uploader ?? .nostrBuild, pubkey: pubkey, image_url: $profile_url, uploadObserver: uploadObserver, callback: callback)
|
||||
}
|
||||
.frame(width: size, height: size)
|
||||
.clipShape(Circle())
|
||||
.overlay(Circle().stroke(highlight_color(highlight), lineWidth: pfp_line_width(highlight)))
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
let highlight: Highlight = .custom(Color.white, 2.0)
|
||||
ZStack {
|
||||
EditProfilePictureView(url: $profile_image, pubkey: pubkey, size: size, highlight: highlight, damus_state: damus_state)
|
||||
EditPictureControl(pubkey: pubkey, image_url: $profile_image, uploadObserver: uploadObserver, callback: callback)
|
||||
private func get_profile_url() -> URL? {
|
||||
if let profile_url {
|
||||
return profile_url
|
||||
} else if let state = damus_state, let picture = state.profiles.lookup(id: pubkey)?.picture {
|
||||
return URL(string: picture)
|
||||
} else {
|
||||
return profile_url ?? URL(string: robohash(pubkey))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -39,7 +54,7 @@ struct ProfilePictureSelector: View {
|
||||
struct ProfilePictureSelector_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
let test_pubkey = "ff48854ac6555fed8e439ebb4fa2d928410e0eef13fa41164ec45aaaa132d846"
|
||||
ProfilePictureSelector(pubkey: test_pubkey, uploadObserver: ImageUploadingObserver()) { _ in
|
||||
EditProfilePictureView(pubkey: test_pubkey, uploadObserver: ImageUploadingObserver()) { _ in
|
||||
//
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user