UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Firebase

Forums > SwiftUI


import SwiftUI
import Firebase

class SearchAnuncioUsuarioViewModel: ObservableObject {
    @EnvironmentObject var viewModel: SearchViewModel
    let user: User
    @Published var userAnuncios = [Anuncio]()

    init(user:User) {
        self.user = user
        fetchUserAnuncios()
    }

    func fetchUserAnuncios() {
        COLLECTION_ANUNCIOS.whereField("uid", isEqualTo: user.id).getDocuments { snapshot, _ in

            guard let documents = snapshot?.documents else { return }
            documents.forEach { document in
                self.userAnuncios = documents.map({ Anuncio(dictionary: $0.data()) })
            }
        }
    }

}

Models:

import Firebase

struct User: Identifiable {
    let id: String
    let username: String
    let profileImageUrl: String
    let fullname: String
    let email: String

    init(dictionary: [String: Any]) {
        self.id = dictionary["uid"] as? String ?? ""
        self.username = dictionary["username"] as? String ?? ""
        self.profileImageUrl = dictionary["profileImageUrl"] as? String ?? ""
        self.email = dictionary["email"] as? String ?? ""
        self.fullname = dictionary["fullname"] as? String ?? ""
    }
}

Meus anuncios views (select "anuncios from a anuncios uid "

struct MeusAnunciosView: View {
    let user: User
    @ObservedObject var viewModel3: SearchAnuncioUsuarioViewModel

    init(user: User) {
       self.user = user
        self.viewModel3 = SearchAnuncioUsuarioViewModel(user: user)
    }

    @EnvironmentObject var viewModel: AuthViewModel  //pra fazer logout

    var body: some View {

                VStack {

                    HStack {

                        KFImage(URL(string: AuthViewModel.shared.user?.profileImageUrl ?? "foto..."))
                            .resizable()
                            .scaledToFit()
                            .frame(width:100, height:100)
                            .clipShape(Circle())
                                        .shadow(radius: 2)
                                        .overlay(Circle().stroke(Color.yellow, lineWidth: 3))
                            .padding(.leading,25)

                        Text("@")
                            .foregroundColor(.gray)
                        + Text(AuthViewModel.shared.user?.username ?? "Usuário..") //carrega nome do usuario
                            .foregroundColor(.black)

                        Spacer()
                        Button(action: {
                            viewModel.signOut()

                        }, label: {
                            Image(systemName: "person.fill.xmark").imageScale(.large)
                                .foregroundColor(.yellow)
                            Text("Sair")
                                .foregroundColor(.gray)
                                .padding(.trailing,25)
                        })
                    }
                    Divider()
                    NavigationLink(
                        destination: CriarAnuncio(),
                        label: {
                            Image(systemName: "square.and.pencil")
                                .foregroundColor(.gray)
                                .font(.system(size: 19, weight: .regular))
                            Text("Criar Anúncio")
                                .foregroundColor(.yellow)
                                .fontWeight(.heavy)
                                .font(.title3)

                        })
                    Divider()

                }.navigationTitle("Meus Anúncios")

                VStack {
                    ScrollView {

//                        if viewModel.user.isCurrentUser {

                        ForEach(viewModel3.userAnuncios) { anuncio in

                            NavigationLink(destination:ClassDetailView(anuncio: anuncio)) {
                            AnuncioRowView(anuncio: anuncio)
                                .foregroundColor(.black)
                            }

                        }

//                        }

                    }

                    }.padding(.top,20)
                .padding(.bottom,20)

//                    ForEach(viewModel3.userAnuncios) { anuncio in // USERS é o MODEL OBJECT
//                        Text(anuncio.titulo)
//
//                    }

                    } }

struct AnuncioRowView: View {
    let anuncio: Anuncio

    var body: some View {

        HStack {
            VStack (alignment:.leading) {
                HStack {
            Image(systemName: "note")
                .foregroundColor(.gray)
                .font(.system(size:15))
            Text(anuncio.titulo)
                .font(.system(size:15))
                }

            Text(anuncio.detailedTimestampString)
                .font(.system(size: 10))
                .foregroundColor(.gray)
            }
            Spacer()
        Button(action: /*@START_MENU_TOKEN@*/{}/*@END_MENU_TOKEN@*/) {
                Text("Encerrar ")
                    .foregroundColor(.red)
            }
        }.padding(.horizontal,20)
        .padding(.vertical,5)

}
}

ALWAYS DE SAME ERROR, SearchAnuncioUsuarioView.type cannot conform to ObservableObjetct

Content View: @EnvironmentObject var viewModel: AuthViewModel @EnvironmentObject var viewModel3: SearchAnuncioUsuarioViewModel

NavigationLink(destination: MeusAnunciosView(user: AuthViewModel.shared.user!)) {

2      

struct Anuncio: Identifiable {
    let id: String
    let titulo: String
    let anuncioImageUrl: String
    let anuncioImageUrl2: String
    let anuncioImageUrl3: String
    let descricao: String
    let nomeanunciante: String
    let whatsapp: String
    let preco: String
    let categoria: String
    -> let uid: String
    let timestamp: Timestamp

2      

I try put .environmentObject(SearchAnuncioUsuarioViewModel(user: <#User#>)) but apper: Missing argument for parameter 'user' in call

2      

can you help me? maybe i can describe this better...

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.