I’m utilizing kingfisher to load my person photos however I’m going through an issue updating my person picture The picture updates however solely after I shut out the app and rebuild it . the pictures does not replace mechanically I’ve to shut my construct and relaunch it to view the brand new picture. How do I repair this code to replace my photos mechanically to make them seem as soon as the person decides to vary the selectedimage
EditProfile
@EnvironmentObject var viewModel: CurrentUserProfileViewModel
@Atmosphere(.dismiss) var dismiss
personal var person: Userss? {
return viewModel.currentUser
}
var physique: some View {
NavigationStack {
VStack {
VStack(spacing: 8) {
PhotosPicker(choice: $viewModel.selectedImage) {
VStack {
if let picture = viewModel.profileImage {
picture
.resizable()
.scaledToFill()
.body(width: 192, peak: 192)
.clipShape(Rectangle())
.foregroundColor(Shade(.systemGray4))
} else {
RectangularImageSize(person: person, measurement: .giant)
}
Textual content("Edit profile image")
.font(.footnote)
.fontWeight(.semibold)
.foregroundColor(.white)
}
}
.padding(.vertical, 8)
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button("Cancel") {
dismiss()
}
.font(.subheadline)
.foregroundColor(Shade.blue)
}
ToolbarItem(placement: .navigationBarTrailing) {
Button("Finished") {
Activity {
strive await viewModel.updateUserData()
dismiss()
}
}
CurrentUserProfileViewModel
class CurrentUserProfileViewModel: ObservableObject {
@Printed var selectedImage: PhotosPickerItem? {
didSet { Activity { await loadImage(fromItem: selectedImage) } }
}
@Printed var profileImage: Picture?
personal var uiImage: UIImage?
}
extension CurrentUserProfileViewModel {
func loadUserData() {
self.bio = currentUser?.bio ?? ""
guard let currentUid = Auth.auth().currentUser?.uid else { return }
Activity {
ThirdUserService.shared.currentUser?.stats = strive await ThirdUserService.fetchUserStats(uid: currentUid)
}
}
func updateUserData() async throws {
guard let person = currentUser else { return }
var knowledge: [String: String] = [:]
if let uiImage = uiImage {
strive await updateProfileImage(uiImage)
knowledge["profileImageUrl"] = currentUser?.profileImageUrl
}
strive await FirestoreConstants.UserCollection.doc(person.id).updateData(knowledge)
}
}
// MARK: - Subscribers
extension CurrentUserProfileViewModel {
@MainActor
personal func setupSubcribers() {
ThirdUserService.shared.$currentUser.sink { [weak self] person in
self?.currentUser = person
}.retailer(in: &cancellables)
}
}
// MARK: - Picture Loading
extension CurrentUserProfileViewModel {
func loadImage(fromItem merchandise: PhotosPickerItem?) async {
guard let merchandise = merchandise else { return }
guard let knowledge = strive? await merchandise.loadTransferable(kind: Information.self) else { return }
guard let uiImage = UIImage(knowledge: knowledge) else { return }
self.uiImage = uiImage
self.profileImage = Picture(uiImage: uiImage)
}
func updateProfileImage(_ uiImage: UIImage) async throws {
let imageUrl = strive? await ImageUploader.uploadImage(picture: uiImage, kind: .profile)
currentUser?.profileImageUrl = imageUrl
}
}