Skip to content

Commit 4a7a01f

Browse files
committed
adding restaurant model and populate button functionality (randomly generates collection of restaurants and ratings and uploads to Firestore)
1 parent 10aecdc commit 4a7a01f

File tree

3 files changed

+208
-5
lines changed

3 files changed

+208
-5
lines changed

firestore/FirestoreExample.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
8E4C62D825E9CFE1001678A1 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 8E4C62D725E9CFE1001678A1 /* Preview Assets.xcassets */; };
3131
8E4C62E725E9D191001678A1 /* RestaurantItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8E4C62E625E9D191001678A1 /* RestaurantItemView.swift */; };
3232
8E4C630125EDB1CD001678A1 /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 8E4C630025EDB1CD001678A1 /* GoogleService-Info.plist */; };
33+
8E4C634925EDB793001678A1 /* Restaurant.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8E4C634825EDB793001678A1 /* Restaurant.swift */; };
3334
928F7382D38D4F9DB48A15EF /* Pods_FirestoreExample.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 541C1C9953818121C760549F /* Pods_FirestoreExample.framework */; };
3435
DE8564AE23AFBF8000611383 /* FirestoreUITest.m in Sources */ = {isa = PBXBuildFile; fileRef = DE8564AD23AFBF8000611383 /* FirestoreUITest.m */; };
3536
DE8564B423AFBFA700611383 /* FIREGSignInHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = DE8564B123AFBFA700611383 /* FIREGSignInHelper.m */; };
@@ -90,6 +91,7 @@
9091
8E4C62D925E9CFE1001678A1 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
9192
8E4C62E625E9D191001678A1 /* RestaurantItemView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RestaurantItemView.swift; sourceTree = "<group>"; };
9293
8E4C630025EDB1CD001678A1 /* GoogleService-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = "GoogleService-Info.plist"; path = "../../../SwiftUIRewrite/GoogleService-Info.plist"; sourceTree = "<group>"; };
94+
8E4C634825EDB793001678A1 /* Restaurant.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Restaurant.swift; sourceTree = "<group>"; };
9395
B1197C1340BA7906456ECA9D /* Pods_FirestoreSwiftUIExample.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_FirestoreSwiftUIExample.framework; sourceTree = BUILT_PRODUCTS_DIR; };
9496
D3859CC2659F6A304C12A137 /* Pods-FirestoreExample.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-FirestoreExample.release.xcconfig"; path = "Target Support Files/Pods-FirestoreExample/Pods-FirestoreExample.release.xcconfig"; sourceTree = "<group>"; };
9597
DE8564AC23AFBF8000611383 /* FirestoreExampleUITests-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "FirestoreExampleUITests-Bridging-Header.h"; sourceTree = "<group>"; };
@@ -218,6 +220,7 @@
218220
8E4C62CF25E9CFE0001678A1 /* FirestoreSwiftUIExample */ = {
219221
isa = PBXGroup;
220222
children = (
223+
8E4C634725EDB762001678A1 /* Model */,
221224
8E4C62E125E9D137001678A1 /* Views */,
222225
8E4C62D025E9CFE0001678A1 /* FirestoreSwiftUIExampleApp.swift */,
223226
8E4C62D425E9CFE1001678A1 /* Assets.xcassets */,
@@ -245,6 +248,14 @@
245248
path = Views;
246249
sourceTree = "<group>";
247250
};
251+
8E4C634725EDB762001678A1 /* Model */ = {
252+
isa = PBXGroup;
253+
children = (
254+
8E4C634825EDB793001678A1 /* Restaurant.swift */,
255+
);
256+
path = Model;
257+
sourceTree = "<group>";
258+
};
248259
DE8564AF23AFBFA700611383 /* TestUtils */ = {
249260
isa = PBXGroup;
250261
children = (
@@ -669,6 +680,7 @@
669680
buildActionMask = 2147483647;
670681
files = (
671682
8E4C62D325E9CFE0001678A1 /* ContentView.swift in Sources */,
683+
8E4C634925EDB793001678A1 /* Restaurant.swift in Sources */,
672684
8E4C62D125E9CFE0001678A1 /* FirestoreSwiftUIExampleApp.swift in Sources */,
673685
8E4C62E725E9D191001678A1 /* RestaurantItemView.swift in Sources */,
674686
);
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
//
2+
// File.swift
3+
// FirestoreSwiftUIExample
4+
//
5+
// Created by Rachel Collins on 3/1/21.
6+
// Copyright © 2021 Firebase. All rights reserved.
7+
//
8+
9+
import Firebase
10+
import FirebaseFirestoreSwift
11+
12+
struct Restaurant: Codable {
13+
14+
var name: String
15+
var category: String // Could become an enum
16+
var city: String
17+
var price: Int // from 1-3; could also be an enum
18+
var ratingCount: Int // numRatings
19+
var averageRating: Float
20+
var photo: URL
21+
22+
enum CodingKeys: String, CodingKey {
23+
case name
24+
case category
25+
case city
26+
case price
27+
case ratingCount = "numRatings"
28+
case averageRating = "avgRating"
29+
case photo
30+
}
31+
}
32+
33+
extension Restaurant {
34+
35+
static let cities = [
36+
"Albuquerque",
37+
"Arlington",
38+
"Atlanta",
39+
"Austin",
40+
"Baltimore",
41+
"Boston",
42+
"Charlotte",
43+
"Chicago",
44+
"Cleveland",
45+
"Colorado Springs",
46+
"Columbus",
47+
"Dallas",
48+
"Denver",
49+
"Detroit",
50+
"El Paso",
51+
"Fort Worth",
52+
"Fresno",
53+
"Houston",
54+
"Indianapolis",
55+
"Jacksonville",
56+
"Kansas City",
57+
"Las Vegas",
58+
"Long Beach",
59+
"Los Angeles",
60+
"Louisville",
61+
"Memphis",
62+
"Mesa",
63+
"Miami",
64+
"Milwaukee",
65+
"Nashville",
66+
"New York",
67+
"Oakland",
68+
"Oklahoma",
69+
"Omaha",
70+
"Philadelphia",
71+
"Phoenix",
72+
"Portland",
73+
"Raleigh",
74+
"Sacramento",
75+
"San Antonio",
76+
"San Diego",
77+
"San Francisco",
78+
"San Jose",
79+
"Tucson",
80+
"Tulsa",
81+
"Virginia Beach",
82+
"Washington"
83+
]
84+
85+
static let categories = [
86+
"Brunch", "Burgers", "Coffee", "Deli", "Dim Sum", "Indian", "Italian",
87+
"Mediterranean", "Mexican", "Pizza", "Ramen", "Sushi"
88+
]
89+
90+
static func imageURL(forName name: String) -> URL {
91+
let number = (abs(name.hashValue) % 22) + 1
92+
let URLString =
93+
"https://storage.googleapis.com/firestorequickstarts.appspot.com/food_\(number).png"
94+
return URL(string: URLString)!
95+
}
96+
97+
var imageURL: URL {
98+
return Restaurant.imageURL(forName: name)
99+
}
100+
}
101+
102+
struct Review: Codable {
103+
104+
var rating: Int // Can also be enum
105+
var userID: String
106+
var username: String
107+
var text: String
108+
var date: Timestamp
109+
110+
enum CodingKeys: String, CodingKey {
111+
case rating
112+
case userID = "userId"
113+
case username = "userName"
114+
case text
115+
case date
116+
}
117+
}

firestore/FirestoreSwiftUIExample/Views/ContentView.swift

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
//
88

99
import SwiftUI
10+
import Firebase
1011

1112
struct ContentView: View {
13+
let db = Firestore.firestore()
14+
1215
var body: some View {
1316
NavigationView {
1417
List(/*@START_MENU_TOKEN@*/0 ..< 5/*@END_MENU_TOKEN@*/) { item in
@@ -20,7 +23,7 @@ struct ContentView: View {
2023
.toolbar {
2124
ToolbarItem(placement: .navigationBarLeading) {
2225
Button("Populate") {
23-
print("populating")
26+
populate()
2427
}
2528
}
2629

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

42110
struct RestaurantDetailView: View {
43111
var body: some View {
44112
Text("Detail View Placeholder")
45113
}
46114
}
115+
116+
struct ContentView_Previews: PreviewProvider {
117+
static var previews: some View {
118+
ContentView()
119+
}
120+
}

0 commit comments

Comments
 (0)