Skip to content

Commit 9ee1704

Browse files
committed
updated UI to display star rating, price indicator
1 parent 7e2a833 commit 9ee1704

File tree

3 files changed

+91
-10
lines changed

3 files changed

+91
-10
lines changed

firestore/FirestoreExample.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
8E4C634925EDB793001678A1 /* Restaurant.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8E4C634825EDB793001678A1 /* Restaurant.swift */; };
3333
8E4C635725EDD58F001678A1 /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 8E4C635625EDD58F001678A1 /* GoogleService-Info.plist */; };
3434
8E4C637825EEF4CA001678A1 /* RestaurantListViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8E4C637725EEF4CA001678A1 /* RestaurantListViewModel.swift */; };
35+
8E4C63B225F05E76001678A1 /* StarsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8E4C63B125F05E76001678A1 /* StarsView.swift */; };
3536
928F7382D38D4F9DB48A15EF /* Pods_FirestoreExample.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 541C1C9953818121C760549F /* Pods_FirestoreExample.framework */; };
3637
DE8564AE23AFBF8000611383 /* FirestoreUITest.m in Sources */ = {isa = PBXBuildFile; fileRef = DE8564AD23AFBF8000611383 /* FirestoreUITest.m */; };
3738
DE8564B423AFBFA700611383 /* FIREGSignInHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = DE8564B123AFBFA700611383 /* FIREGSignInHelper.m */; };
@@ -94,6 +95,7 @@
9495
8E4C634825EDB793001678A1 /* Restaurant.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Restaurant.swift; sourceTree = "<group>"; };
9596
8E4C635625EDD58F001678A1 /* GoogleService-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = "GoogleService-Info.plist"; path = "../../../FolderForGoogleService-Info/GoogleService-Info.plist"; sourceTree = "<group>"; };
9697
8E4C637725EEF4CA001678A1 /* RestaurantListViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RestaurantListViewModel.swift; sourceTree = "<group>"; };
98+
8E4C63B125F05E76001678A1 /* StarsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StarsView.swift; sourceTree = "<group>"; };
9799
B1197C1340BA7906456ECA9D /* Pods_FirestoreSwiftUIExample.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_FirestoreSwiftUIExample.framework; sourceTree = BUILT_PRODUCTS_DIR; };
98100
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>"; };
99101
DE8564AC23AFBF8000611383 /* FirestoreExampleUITests-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "FirestoreExampleUITests-Bridging-Header.h"; sourceTree = "<group>"; };
@@ -247,6 +249,7 @@
247249
children = (
248250
8E4C62D225E9CFE0001678A1 /* ContentView.swift */,
249251
8E4C62E625E9D191001678A1 /* RestaurantItemView.swift */,
252+
8E4C63B125F05E76001678A1 /* StarsView.swift */,
250253
);
251254
path = Views;
252255
sourceTree = "<group>";
@@ -696,6 +699,7 @@
696699
8E4C634925EDB793001678A1 /* Restaurant.swift in Sources */,
697700
8E4C637825EEF4CA001678A1 /* RestaurantListViewModel.swift in Sources */,
698701
8E4C62D125E9CFE0001678A1 /* FirestoreSwiftUIExampleApp.swift in Sources */,
702+
8E4C63B225F05E76001678A1 /* StarsView.swift in Sources */,
699703
8E4C62E725E9D191001678A1 /* RestaurantItemView.swift in Sources */,
700704
);
701705
runOnlyForDeploymentPostprocessing = 0;

firestore/FirestoreSwiftUIExample/Views/RestaurantItemView.swift

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,71 @@ struct RestaurantItemView: View {
1414

1515
var body: some View {
1616
HStack {
17-
WebImage(url: restaurant.photo)
18-
.resizable()
19-
.placeholder(Image(systemName: "photo"))
20-
.aspectRatio(1, contentMode: .fill)
21-
.frame(width: 100, height: 100, alignment: .leading)
17+
ImageThumbnail(imageURL: restaurant.photo)
2218
VStack(alignment: .leading) {
23-
Text(restaurant.name)
24-
Text("Star Placeholder")
19+
HStack {
20+
Text(restaurant.name)
21+
.frame(alignment: .leading)
22+
Spacer()
23+
Price(price: restaurant.price)
24+
}
25+
StarsView(rating: Int(restaurant.averageRating.rounded()))
2526
Spacer()
2627
HStack {
2728
Text(restaurant.category)
2829
Text("")
2930
Text(restaurant.city)
3031
}
32+
.foregroundColor(Color.gray)
33+
.font(.footnote)
3134
}
32-
Spacer()
33-
Text("$$$")
3435
}
3536
.padding([.bottom, .trailing])
3637
}
3738
}
3839

3940
struct RestaurantItemView_Previews: PreviewProvider {
4041
static var previews: some View {
41-
let data = Restaurant(id: .init(), name: "Pizza Place", category: "Pizza", city: "Austin", price: 0, ratingCount: 0, averageRating: 0, photo: Restaurant.imageURL(forName: "Place Place"))
42+
let data = Restaurant(id: .init(), name: "Pizza Place", category: "Pizza", city: "Austin", price: 2, ratingCount: 1, averageRating: 4, photo: Restaurant.imageURL(forName: "Place Place"))
4243
RestaurantItemView(restaurant: data)
4344
}
4445
}
46+
47+
struct ImageThumbnail: View {
48+
var imageURL: URL
49+
50+
var body: some View {
51+
WebImage(url: imageURL)
52+
.resizable()
53+
.placeholder(Image(systemName: "photo"))
54+
.aspectRatio(1, contentMode: .fill)
55+
.frame(width: 100, height: 100, alignment: .leading)
56+
}
57+
}
58+
59+
struct Price: View {
60+
var price: Int
61+
62+
var body: some View {
63+
Text(priceString(from: price))
64+
.font(.footnote)
65+
.foregroundColor(Color.gray)
66+
.multilineTextAlignment(.trailing)
67+
}
68+
69+
func priceString(from price: Int) -> String {
70+
let priceText: String
71+
switch price {
72+
case 1:
73+
priceText = "$"
74+
case 2:
75+
priceText = "$$"
76+
case 3:
77+
priceText = "$$$"
78+
case _:
79+
fatalError("price must be between one and three")
80+
}
81+
82+
return priceText
83+
}
84+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// StarsView.swift
3+
// FirestoreSwiftUIExample
4+
//
5+
// Created by Rachel Collins on 3/3/21.
6+
// Copyright © 2021 Firebase. All rights reserved.
7+
//
8+
9+
import SwiftUI
10+
11+
struct StarsView: View {
12+
var rating: Int
13+
14+
var body: some View {
15+
HStack {
16+
ForEach(1..<6) { i in
17+
getStar(num: i, rating: rating)
18+
.frame(width: 12, height: 20)
19+
.foregroundColor(getStarColor(num: i, rating: rating))
20+
}
21+
}
22+
}
23+
24+
func getStar(num: Int, rating: Int) -> Image {
25+
num > rating ? Image(systemName: "star") : Image(systemName: "star.fill")
26+
}
27+
28+
func getStarColor(num: Int, rating: Int) -> Color {
29+
num > rating ? Color.gray : Color.yellow
30+
}
31+
}
32+
33+
struct StarsView_Previews: PreviewProvider {
34+
static var previews: some View {
35+
StarsView(rating: 3)
36+
}
37+
}

0 commit comments

Comments
 (0)