@MainActor class AddCarViewModel: ObservableObject {
@Published var photoUrls : [String] = []
func uploadImages(images: [Image], customerId: String) async throws {
let subFolderId = UUID().uuidString
let pictureFolderRef = Storage.storage().reference().child("CarPhotos").child(customerId).child("\(subFolderId)")
images.enumerated().forEach { index, image in
guard let imageData = image.asUIImage().jpegData(compressionQuality: 0.5) else {
return
}
let pictureReference = pictureFolderRef.child("image_\(index).jpeg")
pictureReference.putData(imageData, metadata: nil) {
metadata, error in
if let error = error {
print("failed to put data")
}
pictureReference.downloadURL { url, error in
if let error = error {
print("error while downloading url ")
}
guard let urlString = url?.absoluteString else {
return
}
self.photoUrls.append(urlString)
}
}
}
}
func addCar(carInfo: Car) async throws {
try await Firestore.firestore().collection("cars").document().setData(from: carInfo)
}
}
VStack {
Button {
Task {
do {
try await viewModel.uploadImages(images: selectedImages, customerId: viewModel.user!.uid)
try await viewModel.addCar(carInfo: Car(photoUrls: viewModel.photoUrls))
} catch {
print(error)
}
}
dismiss()
} label: {
Text("Post")
}
}
Post
Replies
Boosts
Views
Activity
struct Question {
var title: String
var options : [String]
}
struct SimpleQuestions: View {
@State var selectedOption = ""
@State var question = Question(title: "question title", options: \["one", "two", "three"\])
var body: some View {
VStack {
Text(question.title)
ForEach(question.options, id: \.self) { option in
HStack {
Button {
selectedOption = option
} label: {
Circle()
.foregroundStyle(selectedOption == option ? .blue : .gray.opacity(0.5))
.frame(width: 23, height: 23)
}}}}}
my question is how to filter in search text for city.name in Concert ? i tried to reach city name using nested compact map but couldn't handle it.
class Concert {
var kindOf : String
var city : [City]
}
class City {
var name : String
@Relationship(inverse: \Concert.city) var concert : [Concert]
}
@Query var concerts : [Concert]
@State var searchQuery : String = ""
var filteredConcert : [Concert] {
if searchQuery.isEmpty {
return concerts
}
let filteredConcerts = concerts.compactMap { concert in
let kindOfContainsSearch = concert.kindOf.range(of: searchQuery) != nil
return (kindOfContainsSearch ) ? concert : nil
}
return filteredConcerts
}