16.3 C
London
Monday, September 9, 2024

ios – The right way to add a put up


I’m attempting to add a put up with simply textwords however my code solely uploads the put up that has a picture hooked up to it and the put up with simply phrases do not present up in firebase

How do I repair this code to permit my person add their put up with out photographs to firebase

That is the uploadPostViewModel I’ve the textual content as a string however but the information shouldn’t be being uploaded as if it not listed however the picture is what am I lacking from this code that is not permitting my textual content knowledge to be uploaded to firebase

class UploadPostViewModel: ObservableObject {
    @Printed var didUploadPost = false
    @Printed var error: Error?
    @Printed var profileImage: Picture?
    @Printed var caption = ""
    @Printed var textual content = ""
    @Printed var selectedImage: PhotosPickerItem? {
        didSet { Process { await loadImage(fromItem: selectedImage) } }
    }
    
    non-public var uiImage: UIImage?
    
    func uploadPost() async throws {
        guard let uid = Auth.auth().currentUser?.uid else { return }
        guard let picture = uiImage else { return }
        
        do {
            guard let picture = attempt await ImageUploader.uploadImage(picture: picture, kind: .put up) else { return }
            let put up = Submit(ownerUid: uid,textual content: textual content, caption: caption, likes: 0, replyCount: 67, picture: picture, timestamp: Timestamp())

            
            attempt await PostService.uploadPost(put up)
            self.didUploadPost = true
        } catch {
            print("DEBUG: Did not add picture with error (error.localizedDescription)")
            self.error = error
        }
    }
    
    func loadImage(fromItem merchandise: PhotosPickerItem?) async {
        guard let merchandise = merchandise else { return }
        
        guard let knowledge = attempt? await merchandise.loadTransferable(kind: Knowledge.self) else { return }
        guard let uiImage = UIImage(knowledge: knowledge) else { return }
        self.uiImage = uiImage
        self.profileImage = Picture(uiImage: uiImage)
    }
}
 @State var captionText = ""
       
       @StateObject var viewModel = UploadPostViewModel()
       @Surroundings(.dismiss) var dismiss
      
       @Binding var tabIndex: Int
       
       non-public var person: Userss? {
           return ThirdUserService.shared.currentUser
       }
       var physique: some View {
           NavigationStack {
               VStack {
                   HStack(alignment: .prime) {
                       RectangularImageSize(person: person, measurement: .small)
                       
                       VStack(alignment: .main, spacing: 4) {
                           Textual content(person?.username ?? "")
                               .fontWeight(.semibold)
                           
                           TextField("Kind One thing", textual content: $viewModel.textual content, axis: .vertical)
                       }
                       .font(.footnote)
                       
                       Spacer()
                       
                       if !viewModel.textual content.isEmpty {
                           Button {
                               viewModel.textual content = ""
                           } label: {
                               Picture(systemName: "xmark")
                                   .resizable()
                                   .body(width: 12, top: 12)
                                   .foregroundColor(.grey)
                           }
                       }
                   }
                   
                   Spacer()
               }
               .padding()
               .toolbar {
                   ToolbarItem(placement: .navigationBarLeading) {
                       Button("Cancel") {
                           dismiss()
                       }
                       .font(.subheadline)
                       .foregroundStyle(Colour.black)
                   }
                   
                   ToolbarItem(placement: .navigationBarTrailing) {
                       Button("Submit") {
                           Process {
                               attempt await viewModel.uploadPost()
                               dismiss()
                           }
                       }
                       .opacity(viewModel.textual content.isEmpty ? 0.5 : 1.0)
                       .disabled(viewModel.textual content.isEmpty)
                       .font(.subheadline)
                       .fontWeight(.semibold)
                       .foregroundStyle(Colour.inexperienced)
                   }
               }
               .onDisappear { tabIndex = 0 }
               
           }
       }
   }

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here