Skip to content

Commit fdc508b

Browse files
authored
Merge pull request #1127 from racheldoshcollins/master
Creating Firestore FriendlyEats sample with SwiftUI
2 parents ce13d39 + 23390e5 commit fdc508b

File tree

18 files changed

+1209
-3
lines changed

18 files changed

+1209
-3
lines changed

firestore/FirestoreExample.xcodeproj/project.pbxproj

Lines changed: 445 additions & 1 deletion
Large diffs are not rendered by default.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"colors" : [
3+
{
4+
"idiom" : "universal"
5+
}
6+
],
7+
"info" : {
8+
"author" : "xcode",
9+
"version" : 1
10+
}
11+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "iphone",
5+
"scale" : "2x",
6+
"size" : "20x20"
7+
},
8+
{
9+
"idiom" : "iphone",
10+
"scale" : "3x",
11+
"size" : "20x20"
12+
},
13+
{
14+
"idiom" : "iphone",
15+
"scale" : "2x",
16+
"size" : "29x29"
17+
},
18+
{
19+
"idiom" : "iphone",
20+
"scale" : "3x",
21+
"size" : "29x29"
22+
},
23+
{
24+
"idiom" : "iphone",
25+
"scale" : "2x",
26+
"size" : "40x40"
27+
},
28+
{
29+
"idiom" : "iphone",
30+
"scale" : "3x",
31+
"size" : "40x40"
32+
},
33+
{
34+
"idiom" : "iphone",
35+
"scale" : "2x",
36+
"size" : "60x60"
37+
},
38+
{
39+
"idiom" : "iphone",
40+
"scale" : "3x",
41+
"size" : "60x60"
42+
},
43+
{
44+
"idiom" : "ipad",
45+
"scale" : "1x",
46+
"size" : "20x20"
47+
},
48+
{
49+
"idiom" : "ipad",
50+
"scale" : "2x",
51+
"size" : "20x20"
52+
},
53+
{
54+
"idiom" : "ipad",
55+
"scale" : "1x",
56+
"size" : "29x29"
57+
},
58+
{
59+
"idiom" : "ipad",
60+
"scale" : "2x",
61+
"size" : "29x29"
62+
},
63+
{
64+
"idiom" : "ipad",
65+
"scale" : "1x",
66+
"size" : "40x40"
67+
},
68+
{
69+
"idiom" : "ipad",
70+
"scale" : "2x",
71+
"size" : "40x40"
72+
},
73+
{
74+
"idiom" : "ipad",
75+
"scale" : "1x",
76+
"size" : "76x76"
77+
},
78+
{
79+
"idiom" : "ipad",
80+
"scale" : "2x",
81+
"size" : "76x76"
82+
},
83+
{
84+
"idiom" : "ipad",
85+
"scale" : "2x",
86+
"size" : "83.5x83.5"
87+
},
88+
{
89+
"idiom" : "ios-marketing",
90+
"scale" : "1x",
91+
"size" : "1024x1024"
92+
}
93+
],
94+
"info" : {
95+
"author" : "xcode",
96+
"version" : 1
97+
}
98+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"info" : {
3+
"author" : "xcode",
4+
"version" : 1
5+
}
6+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//
2+
// Firestore+Extension.swift
3+
// FirestoreSwiftUIExample
4+
//
5+
// Copyright (c) 2021 Google Inc.
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
//
19+
20+
import Firebase
21+
22+
extension Firestore {
23+
func populate() {
24+
let words = ["Bar", "Fire", "Grill", "Drive Thru", "Place", "Best", "Spot", "Prime", "Eatin'"]
25+
26+
let cities = Restaurant.cities
27+
let categories = Restaurant.categories
28+
29+
for _ in 0 ..< 20 {
30+
let randomIndexes = (Int(arc4random_uniform(UInt32(words.count))),
31+
Int(arc4random_uniform(UInt32(words.count))))
32+
let name = words[randomIndexes.0] + " " + words[randomIndexes.1]
33+
let category = categories[Int(arc4random_uniform(UInt32(categories.count)))]
34+
let city = cities[Int(arc4random_uniform(UInt32(cities.count)))]
35+
let price = Int(arc4random_uniform(3)) + 1
36+
let photo = Restaurant.imageURL(forName: name)
37+
38+
// Basic writes
39+
40+
let collection = self.collection("restaurants")
41+
42+
let restaurant = Restaurant(
43+
name: name,
44+
category: category,
45+
city: city,
46+
price: price,
47+
ratingCount: 10,
48+
averageRating: 0,
49+
photo: photo
50+
)
51+
52+
let restaurantRef = collection.document()
53+
do {
54+
try restaurantRef.setData(from: restaurant)
55+
} catch {
56+
fatalError("Encoding Restaurant failed: \(error)")
57+
}
58+
59+
let batch = self.batch()
60+
var average: Float = 0
61+
for _ in 0 ..< 10 {
62+
let rating = Int(arc4random_uniform(5) + 1)
63+
average += Float(rating) / 10
64+
let text = rating > 3 ? "good" : "food was too spicy"
65+
66+
let review = Review(
67+
rating: rating,
68+
userID: "1234567890",
69+
username: "Anonymous",
70+
text: text,
71+
date: Timestamp()
72+
)
73+
74+
let ratingRef = restaurantRef.collection("ratings").document()
75+
do {
76+
try batch.setData(from: review, forDocument: ratingRef)
77+
} catch {
78+
fatalError("Encoding Rating failed: \(error)")
79+
}
80+
}
81+
batch.updateData(["avgRating": average], forDocument: restaurantRef)
82+
batch.commit(completion: { (error) in
83+
guard let error = error else { return }
84+
print("Error generating reviews: \(error). Check your Firestore permissions.")
85+
})
86+
}
87+
}
88+
89+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//
2+
// Restaurant+Extension.swift
3+
// FirestoreSwiftUIExample
4+
//
5+
// Copyright (c) 2021 Google Inc.
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
//
19+
20+
import Firebase
21+
22+
extension Restaurant {
23+
static let cities = [
24+
"Albuquerque",
25+
"Arlington",
26+
"Atlanta",
27+
"Austin",
28+
"Baltimore",
29+
"Boston",
30+
"Charlotte",
31+
"Chicago",
32+
"Cleveland",
33+
"Colorado Springs",
34+
"Columbus",
35+
"Dallas",
36+
"Denver",
37+
"Detroit",
38+
"El Paso",
39+
"Fort Worth",
40+
"Fresno",
41+
"Houston",
42+
"Indianapolis",
43+
"Jacksonville",
44+
"Kansas City",
45+
"Las Vegas",
46+
"Long Beach",
47+
"Los Angeles",
48+
"Louisville",
49+
"Memphis",
50+
"Mesa",
51+
"Miami",
52+
"Milwaukee",
53+
"Nashville",
54+
"New York",
55+
"Oakland",
56+
"Oklahoma",
57+
"Omaha",
58+
"Philadelphia",
59+
"Phoenix",
60+
"Portland",
61+
"Raleigh",
62+
"Sacramento",
63+
"San Antonio",
64+
"San Diego",
65+
"San Francisco",
66+
"San Jose",
67+
"Tucson",
68+
"Tulsa",
69+
"Virginia Beach",
70+
"Washington"
71+
]
72+
73+
static let categories = [
74+
"Brunch", "Burgers", "Coffee", "Deli", "Dim Sum", "Indian", "Italian",
75+
"Mediterranean", "Mexican", "Pizza", "Ramen", "Sushi"
76+
]
77+
78+
static func imageURL(forName name: String) -> URL {
79+
let number = (abs(name.hashValue) % 22) + 1
80+
let URLString =
81+
"https://storage.googleapis.com/firestorequickstarts.appspot.com/food_\(number).png"
82+
return URL(string: URLString)!
83+
}
84+
85+
var imageURL: URL {
86+
return Restaurant.imageURL(forName: name)
87+
}
88+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// FirestoreSwiftUIExampleApp.swift
3+
// FirestoreSwiftUIExample
4+
//
5+
// Copyright (c) 2021 Google Inc.
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
//
19+
20+
import SwiftUI
21+
import Firebase
22+
23+
@main
24+
struct FirestoreSwiftUIExampleApp: App {
25+
init() {
26+
FirebaseApp.configure()
27+
}
28+
29+
var body: some Scene {
30+
WindowGroup {
31+
RestaurantListView()
32+
}
33+
}
34+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>$(DEVELOPMENT_LANGUAGE)</string>
7+
<key>CFBundleExecutable</key>
8+
<string>$(EXECUTABLE_NAME)</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
11+
<key>CFBundleInfoDictionaryVersion</key>
12+
<string>6.0</string>
13+
<key>CFBundleName</key>
14+
<string>$(PRODUCT_NAME)</string>
15+
<key>CFBundlePackageType</key>
16+
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
17+
<key>CFBundleShortVersionString</key>
18+
<string>1.0</string>
19+
<key>CFBundleVersion</key>
20+
<string>1</string>
21+
<key>LSRequiresIPhoneOS</key>
22+
<true/>
23+
<key>UIApplicationSceneManifest</key>
24+
<dict>
25+
<key>UIApplicationSupportsMultipleScenes</key>
26+
<true/>
27+
</dict>
28+
<key>UIApplicationSupportsIndirectInputEvents</key>
29+
<true/>
30+
<key>UILaunchScreen</key>
31+
<dict/>
32+
<key>UIRequiredDeviceCapabilities</key>
33+
<array>
34+
<string>armv7</string>
35+
</array>
36+
<key>UISupportedInterfaceOrientations</key>
37+
<array>
38+
<string>UIInterfaceOrientationPortrait</string>
39+
<string>UIInterfaceOrientationLandscapeLeft</string>
40+
<string>UIInterfaceOrientationLandscapeRight</string>
41+
</array>
42+
<key>UISupportedInterfaceOrientations~ipad</key>
43+
<array>
44+
<string>UIInterfaceOrientationPortrait</string>
45+
<string>UIInterfaceOrientationPortraitUpsideDown</string>
46+
<string>UIInterfaceOrientationLandscapeLeft</string>
47+
<string>UIInterfaceOrientationLandscapeRight</string>
48+
</array>
49+
</dict>
50+
</plist>

0 commit comments

Comments
 (0)