@@ -7,115 +7,114 @@ import SwiftUI
7
7
import Firebase
8
8
9
9
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)
37
18
}
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 ( )
74
28
}
29
+ }
75
30
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)
100
34
}
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
35
}
36
+ }
107
37
}
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
+ }
109
108
}
110
109
111
110
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
+ }
115
114
}
116
115
117
116
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
+ }
121
120
}
0 commit comments