Skip to content

Commit 12c66af

Browse files
committed
fetch data from firestore, update real time in UI; UI in progress
1 parent 4a7a01f commit 12c66af

File tree

5 files changed

+60
-11
lines changed

5 files changed

+60
-11
lines changed

firestore/FirestoreExample.xcodeproj/project.pbxproj

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
8E4C62D525E9CFE1001678A1 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 8E4C62D425E9CFE1001678A1 /* Assets.xcassets */; };
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 */; };
32-
8E4C630125EDB1CD001678A1 /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 8E4C630025EDB1CD001678A1 /* GoogleService-Info.plist */; };
3332
8E4C634925EDB793001678A1 /* Restaurant.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8E4C634825EDB793001678A1 /* Restaurant.swift */; };
33+
8E4C635725EDD58F001678A1 /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 8E4C635625EDD58F001678A1 /* GoogleService-Info.plist */; };
34+
8E4C637825EEF4CA001678A1 /* RestaurantListViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8E4C637725EEF4CA001678A1 /* RestaurantListViewModel.swift */; };
3435
928F7382D38D4F9DB48A15EF /* Pods_FirestoreExample.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 541C1C9953818121C760549F /* Pods_FirestoreExample.framework */; };
3536
DE8564AE23AFBF8000611383 /* FirestoreUITest.m in Sources */ = {isa = PBXBuildFile; fileRef = DE8564AD23AFBF8000611383 /* FirestoreUITest.m */; };
3637
DE8564B423AFBFA700611383 /* FIREGSignInHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = DE8564B123AFBFA700611383 /* FIREGSignInHelper.m */; };
@@ -90,8 +91,9 @@
9091
8E4C62D725E9CFE1001678A1 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
9192
8E4C62D925E9CFE1001678A1 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
9293
8E4C62E625E9D191001678A1 /* RestaurantItemView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RestaurantItemView.swift; sourceTree = "<group>"; };
93-
8E4C630025EDB1CD001678A1 /* GoogleService-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = "GoogleService-Info.plist"; path = "../../../SwiftUIRewrite/GoogleService-Info.plist"; sourceTree = "<group>"; };
9494
8E4C634825EDB793001678A1 /* Restaurant.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Restaurant.swift; sourceTree = "<group>"; };
95+
8E4C635625EDD58F001678A1 /* GoogleService-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = "GoogleService-Info.plist"; path = "../../../FolderForGoogleService-Info/GoogleService-Info.plist"; sourceTree = "<group>"; };
96+
8E4C637725EEF4CA001678A1 /* RestaurantListViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RestaurantListViewModel.swift; sourceTree = "<group>"; };
9597
B1197C1340BA7906456ECA9D /* Pods_FirestoreSwiftUIExample.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_FirestoreSwiftUIExample.framework; sourceTree = BUILT_PRODUCTS_DIR; };
9698
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>"; };
9799
DE8564AC23AFBF8000611383 /* FirestoreExampleUITests-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "FirestoreExampleUITests-Bridging-Header.h"; sourceTree = "<group>"; };
@@ -220,12 +222,13 @@
220222
8E4C62CF25E9CFE0001678A1 /* FirestoreSwiftUIExample */ = {
221223
isa = PBXGroup;
222224
children = (
225+
8E4C637625EEF4B6001678A1 /* ViewModels */,
223226
8E4C634725EDB762001678A1 /* Model */,
224227
8E4C62E125E9D137001678A1 /* Views */,
225228
8E4C62D025E9CFE0001678A1 /* FirestoreSwiftUIExampleApp.swift */,
226229
8E4C62D425E9CFE1001678A1 /* Assets.xcassets */,
227230
8E4C62D925E9CFE1001678A1 /* Info.plist */,
228-
8E4C630025EDB1CD001678A1 /* GoogleService-Info.plist */,
231+
8E4C635625EDD58F001678A1 /* GoogleService-Info.plist */,
229232
8E4C62D625E9CFE1001678A1 /* Preview Content */,
230233
);
231234
path = FirestoreSwiftUIExample;
@@ -256,6 +259,14 @@
256259
path = Model;
257260
sourceTree = "<group>";
258261
};
262+
8E4C637625EEF4B6001678A1 /* ViewModels */ = {
263+
isa = PBXGroup;
264+
children = (
265+
8E4C637725EEF4CA001678A1 /* RestaurantListViewModel.swift */,
266+
);
267+
path = ViewModels;
268+
sourceTree = "<group>";
269+
};
259270
DE8564AF23AFBFA700611383 /* TestUtils */ = {
260271
isa = PBXGroup;
261272
children = (
@@ -445,7 +456,7 @@
445456
files = (
446457
8E4C62D825E9CFE1001678A1 /* Preview Assets.xcassets in Resources */,
447458
8E4C62D525E9CFE1001678A1 /* Assets.xcassets in Resources */,
448-
8E4C630125EDB1CD001678A1 /* GoogleService-Info.plist in Resources */,
459+
8E4C635725EDD58F001678A1 /* GoogleService-Info.plist in Resources */,
449460
);
450461
runOnlyForDeploymentPostprocessing = 0;
451462
};
@@ -681,6 +692,7 @@
681692
files = (
682693
8E4C62D325E9CFE0001678A1 /* ContentView.swift in Sources */,
683694
8E4C634925EDB793001678A1 /* Restaurant.swift in Sources */,
695+
8E4C637825EEF4CA001678A1 /* RestaurantListViewModel.swift in Sources */,
684696
8E4C62D125E9CFE0001678A1 /* FirestoreSwiftUIExampleApp.swift in Sources */,
685697
8E4C62E725E9D191001678A1 /* RestaurantItemView.swift in Sources */,
686698
);

firestore/FirestoreSwiftUIExample/Model/Restaurant.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
import Firebase
1010
import FirebaseFirestoreSwift
1111

12-
struct Restaurant: Codable {
12+
struct Restaurant: Identifiable, Codable {
13+
var id: String = UUID().uuidString
1314

1415
var name: String
1516
var category: String // Could become an enum
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// RestaurantListViewModel.swift
3+
// FirestoreSwiftUIExample
4+
//
5+
// Created by Rachel Collins on 3/2/21.
6+
// Copyright © 2021 Firebase. All rights reserved.
7+
//
8+
9+
import Combine
10+
import Firebase
11+
12+
class RestaurantListViewModel: ObservableObject {
13+
14+
@Published var restaurants: [Restaurant] = []
15+
private var db = Firestore.firestore()
16+
17+
func fetchData() {
18+
db.collection("restaurants").addSnapshotListener { (querySnapshot, error) in
19+
if let error = error {
20+
print("Error getting restaurants: \(error.localizedDescription)")
21+
return
22+
}
23+
24+
self.restaurants = querySnapshot?.documents.compactMap { document in
25+
try? document.data(as: Restaurant.self)
26+
} ?? []
27+
}
28+
}
29+
}

firestore/FirestoreSwiftUIExample/Views/ContentView.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,19 @@ import Firebase
1111

1212
struct ContentView: View {
1313
let db = Firestore.firestore()
14+
@ObservedObject var restaurantListViewModel = RestaurantListViewModel()
1415

1516
var body: some View {
1617
NavigationView {
17-
List(/*@START_MENU_TOKEN@*/0 ..< 5/*@END_MENU_TOKEN@*/) { item in
18+
List(restaurantListViewModel.restaurants) { restaurant in
1819
NavigationLink(destination: RestaurantDetailView()) {
19-
RestaurantItemView()
20+
RestaurantItemView(restaurant: restaurant)
2021
}
2122
}
2223
.navigationBarTitle("Friendly Eats", displayMode: .inline)
24+
.onAppear() {
25+
self.restaurantListViewModel.fetchData()
26+
}
2327
.toolbar {
2428
ToolbarItem(placement: .navigationBarLeading) {
2529
Button("Populate") {
@@ -29,7 +33,7 @@ struct ContentView: View {
2933

3034
ToolbarItem(placement: .navigationBarTrailing) {
3135
Button("Filter") {
32-
print("filtering")
36+
print(restaurantListViewModel.restaurants.count)
3337
}
3438
}
3539
}
@@ -115,6 +119,6 @@ struct RestaurantDetailView: View {
115119

116120
struct ContentView_Previews: PreviewProvider {
117121
static var previews: some View {
118-
ContentView()
122+
ContentView(restaurantListViewModel: RestaurantListViewModel())
119123
}
120124
}

firestore/FirestoreSwiftUIExample/Views/RestaurantItemView.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
import SwiftUI
1010

1111
struct RestaurantItemView: View {
12+
var restaurant: Restaurant
13+
1214
var body: some View {
1315
HStack {
1416
Image(systemName: "circle")
1517
VStack(alignment: .leading) {
16-
Text("Restaurant Name")
18+
Text(restaurant.name)
1719
Text("Star Placeholder")
1820
Spacer()
1921
Text("Type and location")
@@ -27,6 +29,7 @@ struct RestaurantItemView: View {
2729

2830
struct RestaurantItemView_Previews: PreviewProvider {
2931
static var previews: some View {
30-
RestaurantItemView()
32+
let data = Restaurant(id: .init(), name: "test123", category: "test123", city: "test123", price: 0, ratingCount: 0, averageRating: 0, photo: Restaurant.imageURL(forName: "test"))
33+
RestaurantItemView(restaurant: data)
3134
}
3235
}

0 commit comments

Comments
 (0)