commit 9e7e128d9a18e52a32b54bac6cbe47fd848e6fa3
parent bf95a8b328e88a898a9e393c893dc79c2dcbfaa5
Author: Joel Klabo <joelklabo@gmail.com>
Date: Mon, 3 Apr 2023 14:23:36 -0700
Refactoring Edit Picture Views
Diffstat:
8 files changed, 61 insertions(+), 81 deletions(-)
diff --git a/damus/Models/CreateAccountModel.swift b/damus/Models/CreateAccountModel.swift
@@ -14,8 +14,8 @@ class CreateAccountModel: ObservableObject {
@Published var about: String = ""
@Published var pubkey: String = ""
@Published var privkey: String = ""
- @Published var profile_image: String? = nil
-
+ @Published var profile_image: URL? = nil
+
var pubkey_bech32: String {
return bech32_pubkey(self.pubkey) ?? ""
}
diff --git a/damus/Util/AnyCodable/AnyEncodable.swift b/damus/Util/AnyCodable/AnyEncodable.swift
@@ -91,8 +91,8 @@ extension _AnyEncodable {
try encode(nsnumber: number, into: &container)
case let date as Date:
try container.encode(date)
- case let url as URL:
- try container.encode(url)
+ case let profile_url as URL:
+ try container.encode(profile_url)
#endif
case let array as [Any?]:
try container.encode(array.map { AnyEncodable($0) })
diff --git a/damus/Views/CreateAccountView.swift b/damus/Views/CreateAccountView.swift
@@ -9,7 +9,7 @@ import SwiftUI
struct CreateAccountView: View {
@StateObject var account: CreateAccountModel = CreateAccountModel()
- @StateObject var profileUploadViewModel = ProfileUploadingViewModel()
+ @StateObject var profileUploadObserver = ImageUploadingObserver()
var nav: NavigationCoordinator
func SignupForm<FormContent: View>(@ViewBuilder content: () -> FormContent) -> some View {
@@ -26,7 +26,7 @@ struct CreateAccountView: View {
ZStack(alignment: .top) {
VStack {
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."))
.bold()
@@ -67,8 +67,8 @@ struct CreateAccountView: View {
.frame(minWidth: 300, maxWidth: .infinity, maxHeight: 12, alignment: .center)
}
.buttonStyle(GradientButtonStyle())
- .disabled(profileUploadViewModel.isLoading)
- .opacity(profileUploadViewModel.isLoading ? 0.5 : 1)
+ .disabled(profileUploadObserver.isLoading)
+ .opacity(profileUploadObserver.isLoading ? 0.5 : 1)
.padding(.top, 20)
LoginPrompt()
@@ -83,7 +83,7 @@ struct CreateAccountView: View {
}
func uploadedProfilePicture(image_url: URL?) {
- account.profile_image = image_url?.absoluteString
+ account.profile_image = image_url
}
}
diff --git a/damus/Views/Profile/EditMetadataView.swift b/damus/Views/Profile/EditMetadataView.swift
@@ -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()
diff --git a/damus/Views/Profile/EditPictureControl.swift b/damus/Views/Profile/EditPictureControl.swift
@@ -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
+ //
+ }
+ }
+ }
+}
diff --git a/damus/Views/Profile/ProfilePicView.swift b/damus/Views/Profile/ProfilePicView.swift
@@ -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?
diff --git a/damus/Views/Profile/ProfilePictureSelector.swift b/damus/Views/Profile/ProfilePictureSelector.swift
@@ -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
//
}
}
diff --git a/damus/Views/SaveKeysView.swift b/damus/Views/SaveKeysView.swift
@@ -239,5 +239,5 @@ struct SaveKeysView_Previews: PreviewProvider {
}
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)
}