Refactoring Edit Picture Views

This commit is contained in:
Joel Klabo
2023-04-03 14:23:36 -07:00
committed by William Casarin
parent bf95a8b328
commit 9e7e128d9a
8 changed files with 61 additions and 81 deletions

View File

@@ -14,8 +14,8 @@ class CreateAccountModel: ObservableObject {
@Published var about: String = "" @Published var about: String = ""
@Published var pubkey: String = "" @Published var pubkey: String = ""
@Published var privkey: String = "" @Published var privkey: String = ""
@Published var profile_image: String? = nil @Published var profile_image: URL? = nil
var pubkey_bech32: String { var pubkey_bech32: String {
return bech32_pubkey(self.pubkey) ?? "" return bech32_pubkey(self.pubkey) ?? ""
} }

View File

@@ -91,8 +91,8 @@ extension _AnyEncodable {
try encode(nsnumber: number, into: &container) try encode(nsnumber: number, into: &container)
case let date as Date: case let date as Date:
try container.encode(date) try container.encode(date)
case let url as URL: case let profile_url as URL:
try container.encode(url) try container.encode(profile_url)
#endif #endif
case let array as [Any?]: case let array as [Any?]:
try container.encode(array.map { AnyEncodable($0) }) try container.encode(array.map { AnyEncodable($0) })

View File

@@ -9,7 +9,7 @@ import SwiftUI
struct CreateAccountView: View { struct CreateAccountView: View {
@StateObject var account: CreateAccountModel = CreateAccountModel() @StateObject var account: CreateAccountModel = CreateAccountModel()
@StateObject var profileUploadViewModel = ProfileUploadingViewModel() @StateObject var profileUploadObserver = ImageUploadingObserver()
var nav: NavigationCoordinator var nav: NavigationCoordinator
func SignupForm<FormContent: View>(@ViewBuilder content: () -> FormContent) -> some View { func SignupForm<FormContent: View>(@ViewBuilder content: () -> FormContent) -> some View {
@@ -26,7 +26,7 @@ struct CreateAccountView: View {
ZStack(alignment: .top) { ZStack(alignment: .top) {
VStack { VStack {
VStack(alignment: .center) { VStack(alignment: .center) {
ProfilePictureSelector(pubkey: account.pubkey, viewModel: profileUploadViewModel, callback: uploadedProfilePicture(image_url:)) EditPictureControl(uploader: .nostrBuild, pubkey: account.pubkey, image_url: $account.profile_image , uploadObserver: profileUploadObserver, callback: uploadedProfilePicture)
Text(NSLocalizedString("Public Key", comment: "Label to indicate the public key of the account.")) Text(NSLocalizedString("Public Key", comment: "Label to indicate the public key of the account."))
.bold() .bold()
@@ -67,8 +67,8 @@ struct CreateAccountView: View {
.frame(minWidth: 300, maxWidth: .infinity, maxHeight: 12, alignment: .center) .frame(minWidth: 300, maxWidth: .infinity, maxHeight: 12, alignment: .center)
} }
.buttonStyle(GradientButtonStyle()) .buttonStyle(GradientButtonStyle())
.disabled(profileUploadViewModel.isLoading) .disabled(profileUploadObserver.isLoading)
.opacity(profileUploadViewModel.isLoading ? 0.5 : 1) .opacity(profileUploadObserver.isLoading ? 0.5 : 1)
.padding(.top, 20) .padding(.top, 20)
LoginPrompt() LoginPrompt()
@@ -83,7 +83,7 @@ struct CreateAccountView: View {
} }
func uploadedProfilePicture(image_url: URL?) { func uploadedProfilePicture(image_url: URL?) {
account.profile_image = image_url?.absoluteString account.profile_image = image_url
} }
} }

View File

@@ -89,7 +89,7 @@ struct EditMetadataView: View {
let pfp_size: CGFloat = 90.0 let pfp_size: CGFloat = 90.0
HStack(alignment: .center) { 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 .offset(y: -(pfp_size/2.0)) // Increase if set a frame
Spacer() Spacer()

View File

@@ -7,6 +7,10 @@
import SwiftUI import SwiftUI
class ImageUploadingObserver: ObservableObject {
@Published var isLoading: Bool = false
}
struct EditPictureControl: View { struct EditPictureControl: View {
let uploader: MediaUploader let uploader: MediaUploader
let pubkey: String 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
//
}
}
}
}

View File

@@ -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 { struct InnerProfilePicView: View {
let url: URL? let url: URL?
let fallbackUrl: URL? let fallbackUrl: URL?

View File

@@ -6,32 +6,47 @@
// //
import SwiftUI import SwiftUI
import Kingfisher
import Combine import Combine
class ImageUploadingObserver: ObservableObject { struct EditProfilePictureView: View {
@Published var isLoading: Bool = false
} @State var profile_url: URL?
struct ProfilePictureSelector: View {
let pubkey: String let pubkey: String
var size: CGFloat = 80.0
var damus_state: DamusState? var damus_state: DamusState?
var size: CGFloat = 80.0
let highlight: Highlight = .custom(Color.white, 2.0)
@ObservedObject var uploadObserver: ImageUploadingObserver @ObservedObject var uploadObserver: ImageUploadingObserver
let callback: (URL?) -> Void 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 { EditPictureControl(uploader: damus_state?.settings.default_media_uploader ?? .nostrBuild, pubkey: pubkey, image_url: $profile_url, uploadObserver: uploadObserver, callback: callback)
damus_state?.settings.default_media_uploader ?? .nostrBuild }
.frame(width: size, height: size)
.clipShape(Circle())
.overlay(Circle().stroke(highlight_color(highlight), lineWidth: pfp_line_width(highlight)))
} }
var body: some View { private func get_profile_url() -> URL? {
let highlight: Highlight = .custom(Color.white, 2.0) if let profile_url {
ZStack { return profile_url
EditProfilePictureView(url: $profile_image, pubkey: pubkey, size: size, highlight: highlight, damus_state: damus_state) } else if let state = damus_state, let picture = state.profiles.lookup(id: pubkey)?.picture {
EditPictureControl(pubkey: pubkey, image_url: $profile_image, uploadObserver: uploadObserver, callback: callback) return URL(string: picture)
} else {
return profile_url ?? URL(string: robohash(pubkey))
} }
} }
} }
@@ -39,7 +54,7 @@ struct ProfilePictureSelector: View {
struct ProfilePictureSelector_Previews: PreviewProvider { struct ProfilePictureSelector_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
let test_pubkey = "ff48854ac6555fed8e439ebb4fa2d928410e0eef13fa41164ec45aaaa132d846" let test_pubkey = "ff48854ac6555fed8e439ebb4fa2d928410e0eef13fa41164ec45aaaa132d846"
ProfilePictureSelector(pubkey: test_pubkey, uploadObserver: ImageUploadingObserver()) { _ in EditProfilePictureView(pubkey: test_pubkey, uploadObserver: ImageUploadingObserver()) { _ in
// //
} }
} }

View File

@@ -239,5 +239,5 @@ struct SaveKeysView_Previews: PreviewProvider {
} }
func create_account_to_metadata(_ model: CreateAccountModel) -> Profile { func create_account_to_metadata(_ model: CreateAccountModel) -> Profile {
return Profile(name: model.nick_name, display_name: model.real_name, about: model.about, picture: model.profile_image, banner: nil, website: nil, lud06: nil, lud16: nil, nip05: nil, damus_donation: nil) return Profile(name: model.nick_name, display_name: model.real_name, about: model.about, picture: model.profile_image?.absoluteString, banner: nil, website: nil, lud06: nil, lud16: nil, nip05: nil, damus_donation: nil)
} }