Calculating a restaurant's overall rating

The Restaurant Detail screen's overall rating label displays 0.0, and the ratings view displays 3.5 stars, regardless of the actual rating. To add an overall rating, you need to get the ratings from all the reviews and average them. Let's add a new method to CoreDataManager to do this. Do the following steps:

  1. Click CoreDataManager.swift inside the Project navigator (inside the Core Data folder in the Misc folder), and add the following method before the addReview(_:) method:
func fetchRestaurantRating(by identifier:Int) -> Float {
let reviews = fetchReviews(by: identifier).map({ $0 })
let sum = reviews.reduce(0, {$0 + ($1.rating ?? 0)})
return sum / Float(reviews.count)
}

In this method, all reviews for a particular restaurant are fetched from Core Data. The reduce() method takes a closure, which is used to add all the review ratings together, and calculate the average rating value.

  1. Click RestaurantDetailViewController.swift inside the Project navigator (inside the RestaurantDetail folder), and add the following code under the selectedRestaurant property to create an instance of CoreDataManager:
let manager = CoreDataManager()
  1. Update the createRating() method, as follows:
func createRating() {
ratingView.isEnabled = false
if let id = selectedRestaurant?.restaurantID {
let value = manager.fetchRestaurantRating(by: id)
ratingView.rating = CGFloat(value)
if value.isNaN {
lblOverallRating.text = "0.0"
} else {
let roundedValue = ((value * 10).rounded()/10)
lblOverallRating.text = "(roundedValue)"
}
}
}

The method first assigns the selectedRestaurant instance's restaurantID property to id. If successful, the manager.fetchRestaurantRating() method is called which gets all the reviews with id's restaurant identifier value, and calculates the average rating. value is then set to the average rating and used to update the ratings view's rating property, which determines the number of stars displayed in the Restaurant Detail screen. roundedValue is then calculated from value to return a number with 1 decimal point, and is used to set the text property for lblOverallRating.

  1. Build and run your project, and you should now see an overall rating for restaurants that have reviews, as well as a corresponding star rating as shown in the screenshot:

There's still one thing left to do, and that's adding photo reviews. Your challenge is to add photo reviews and to display them in the collection view just under the collection view used for reviews. The way to do this is very similar to the way you used to add reviews. This chapter covers all you need to know, and if you get stuck, feel free to use the completed project files for this chapter, which you will find in the Chapter22 folder of the code bundle of this book downloadable from https://github.com/PacktPublishing/iOS-13-Programming-for-Beginners.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.178.165