Skip to content

Commit c40cfb8

Browse files
committed
fix formatting
1 parent e08d404 commit c40cfb8

File tree

6 files changed

+190
-191
lines changed

6 files changed

+190
-191
lines changed

firestore/FirestoreSwiftUIExample/FirestoreSwiftUIExampleApp.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import Firebase
99
@main
1010
struct FirestoreSwiftUIExampleApp: App {
1111

12-
init() {
13-
FirebaseApp.configure()
14-
}
12+
init() {
13+
FirebaseApp.configure()
14+
}
1515

16-
var body: some Scene {
17-
WindowGroup {
18-
ContentView()
19-
}
16+
var body: some Scene {
17+
WindowGroup {
18+
ContentView()
2019
}
20+
}
2121
}

firestore/FirestoreSwiftUIExample/Model/Restaurant.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ extension Restaurant {
8888
static func imageURL(forName name: String) -> URL {
8989
let number = (abs(name.hashValue) % 22) + 1
9090
let URLString =
91-
"https://storage.googleapis.com/firestorequickstarts.appspot.com/food_\(number).png"
91+
"https://storage.googleapis.com/firestorequickstarts.appspot.com/food_\(number).png"
9292
return URL(string: URLString)!
9393
}
9494

firestore/FirestoreSwiftUIExample/ViewModels/RestaurantListViewModel.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ import Firebase
88

99
class RestaurantListViewModel: ObservableObject {
1010

11-
@Published var restaurants: [Restaurant] = []
12-
private var db = Firestore.firestore()
11+
@Published var restaurants: [Restaurant] = []
12+
private var db = Firestore.firestore()
1313

14-
func fetchData() {
15-
db.collection("restaurants").addSnapshotListener { (querySnapshot, error) in
16-
if let error = error {
17-
print("Error getting restaurants: \(error.localizedDescription)")
18-
return
19-
}
14+
func fetchData() {
15+
db.collection("restaurants").addSnapshotListener { (querySnapshot, error) in
16+
if let error = error {
17+
print("Error getting restaurants: \(error.localizedDescription)")
18+
return
19+
}
2020

21-
self.restaurants = querySnapshot?.documents.compactMap { document in
22-
try? document.data(as: Restaurant.self)
23-
} ?? []
24-
}
21+
self.restaurants = querySnapshot?.documents.compactMap { document in
22+
try? document.data(as: Restaurant.self)
23+
} ?? []
2524
}
25+
}
2626
}

firestore/FirestoreSwiftUIExample/Views/ContentView.swift

Lines changed: 98 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -7,115 +7,114 @@ import SwiftUI
77
import Firebase
88

99
struct ContentView: View {
10-
let db = Firestore.firestore()
11-
@ObservedObject var restaurantListViewModel = RestaurantListViewModel()
12-
13-
var body: some View {
14-
NavigationView {
15-
List(restaurantListViewModel.restaurants) { restaurant in
16-
NavigationLink(destination: RestaurantDetailView()) {
17-
RestaurantItemView(restaurant: restaurant)
18-
}
19-
}
20-
.navigationBarTitle("Friendly Eats", displayMode: .inline)
21-
.onAppear() {
22-
self.restaurantListViewModel.fetchData()
23-
}
24-
.toolbar {
25-
ToolbarItem(placement: .navigationBarLeading) {
26-
Button("Populate") {
27-
populate()
28-
}
29-
}
30-
31-
ToolbarItem(placement: .navigationBarTrailing) {
32-
Button("Filter") {
33-
print(restaurantListViewModel.restaurants.count)
34-
}
35-
}
36-
}
10+
let db = Firestore.firestore()
11+
@ObservedObject var restaurantListViewModel = RestaurantListViewModel()
12+
13+
var body: some View {
14+
NavigationView {
15+
List(restaurantListViewModel.restaurants) { restaurant in
16+
NavigationLink(destination: RestaurantDetailView()) {
17+
RestaurantItemView(restaurant: restaurant)
3718
}
38-
}
39-
40-
func populate() {
41-
let words = ["Bar", "Fire", "Grill", "Drive Thru", "Place", "Best", "Spot", "Prime", "Eatin'"]
42-
43-
let cities = Restaurant.cities
44-
let categories = Restaurant.categories
45-
46-
for _ in 0 ..< 20 {
47-
let randomIndexes = (Int(arc4random_uniform(UInt32(words.count))),
48-
Int(arc4random_uniform(UInt32(words.count))))
49-
let name = words[randomIndexes.0] + " " + words[randomIndexes.1]
50-
let category = categories[Int(arc4random_uniform(UInt32(categories.count)))]
51-
let city = cities[Int(arc4random_uniform(UInt32(cities.count)))]
52-
let price = Int(arc4random_uniform(3)) + 1
53-
let photo = Restaurant.imageURL(forName: name)
54-
55-
// Basic writes
56-
57-
let collection = db.collection("restaurants")
58-
59-
let restaurant = Restaurant(
60-
name: name,
61-
category: category,
62-
city: city,
63-
price: price,
64-
ratingCount: 10,
65-
averageRating: 0,
66-
photo: photo
67-
)
68-
69-
let restaurantRef = collection.document()
70-
do {
71-
try restaurantRef.setData(from: restaurant)
72-
} catch {
73-
fatalError("Encoding Restaurant failed: \(error)")
19+
}
20+
.navigationBarTitle("Friendly Eats", displayMode: .inline)
21+
.onAppear() {
22+
self.restaurantListViewModel.fetchData()
23+
}
24+
.toolbar {
25+
ToolbarItem(placement: .navigationBarLeading) {
26+
Button("Populate") {
27+
populate()
7428
}
29+
}
7530

76-
let batch = db.batch()
77-
//TODO: guard let user = Auth.auth().currentUser else { continue }
78-
var average: Float = 0
79-
for _ in 0 ..< 10 {
80-
let rating = Int(arc4random_uniform(5) + 1)
81-
average += Float(rating) / 10
82-
let text = rating > 3 ? "good" : "food was too spicy"
83-
84-
//TODO: userID: user.uid,
85-
//TODO: username: user.displayName ?? "Anonymous",
86-
let review = Review(
87-
rating: rating,
88-
userID: "1234567890",
89-
username: "Anonymous",
90-
text: text,
91-
date: Timestamp()
92-
)
93-
94-
let ratingRef = restaurantRef.collection("ratings").document()
95-
do {
96-
try batch.setData(from: review, forDocument: ratingRef)
97-
} catch {
98-
fatalError("Encoding Rating failed: \(error)")
99-
}
31+
ToolbarItem(placement: .navigationBarTrailing) {
32+
Button("Filter") {
33+
print(restaurantListViewModel.restaurants.count)
10034
}
101-
batch.updateData(["avgRating": average], forDocument: restaurantRef)
102-
batch.commit(completion: { (error) in
103-
guard let error = error else { return }
104-
print("Error generating reviews: \(error). Check your Firestore permissions.")
105-
})
10635
}
36+
}
10737
}
108-
38+
}
39+
40+
func populate() {
41+
let words = ["Bar", "Fire", "Grill", "Drive Thru", "Place", "Best", "Spot", "Prime", "Eatin'"]
42+
43+
let cities = Restaurant.cities
44+
let categories = Restaurant.categories
45+
46+
for _ in 0 ..< 20 {
47+
let randomIndexes = (Int(arc4random_uniform(UInt32(words.count))),
48+
Int(arc4random_uniform(UInt32(words.count))))
49+
let name = words[randomIndexes.0] + " " + words[randomIndexes.1]
50+
let category = categories[Int(arc4random_uniform(UInt32(categories.count)))]
51+
let city = cities[Int(arc4random_uniform(UInt32(cities.count)))]
52+
let price = Int(arc4random_uniform(3)) + 1
53+
let photo = Restaurant.imageURL(forName: name)
54+
55+
// Basic writes
56+
57+
let collection = db.collection("restaurants")
58+
59+
let restaurant = Restaurant(
60+
name: name,
61+
category: category,
62+
city: city,
63+
price: price,
64+
ratingCount: 10,
65+
averageRating: 0,
66+
photo: photo
67+
)
68+
69+
let restaurantRef = collection.document()
70+
do {
71+
try restaurantRef.setData(from: restaurant)
72+
} catch {
73+
fatalError("Encoding Restaurant failed: \(error)")
74+
}
75+
76+
let batch = db.batch()
77+
//TODO: guard let user = Auth.auth().currentUser else { continue }
78+
var average: Float = 0
79+
for _ in 0 ..< 10 {
80+
let rating = Int(arc4random_uniform(5) + 1)
81+
average += Float(rating) / 10
82+
let text = rating > 3 ? "good" : "food was too spicy"
83+
84+
//TODO: userID: user.uid,
85+
//TODO: username: user.displayName ?? "Anonymous",
86+
let review = Review(
87+
rating: rating,
88+
userID: "1234567890",
89+
username: "Anonymous",
90+
text: text,
91+
date: Timestamp()
92+
)
93+
94+
let ratingRef = restaurantRef.collection("ratings").document()
95+
do {
96+
try batch.setData(from: review, forDocument: ratingRef)
97+
} catch {
98+
fatalError("Encoding Rating failed: \(error)")
99+
}
100+
}
101+
batch.updateData(["avgRating": average], forDocument: restaurantRef)
102+
batch.commit(completion: { (error) in
103+
guard let error = error else { return }
104+
print("Error generating reviews: \(error). Check your Firestore permissions.")
105+
})
106+
}
107+
}
109108
}
110109

111110
struct RestaurantDetailView: View {
112-
var body: some View {
113-
Text("Detail View Placeholder")
114-
}
111+
var body: some View {
112+
Text("Detail View Placeholder")
113+
}
115114
}
116115

117116
struct ContentView_Previews: PreviewProvider {
118-
static var previews: some View {
119-
ContentView(restaurantListViewModel: RestaurantListViewModel())
120-
}
117+
static var previews: some View {
118+
ContentView(restaurantListViewModel: RestaurantListViewModel())
119+
}
121120
}

0 commit comments

Comments
 (0)