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!)) {