From 36a9b5571609e1be541a8065c13546fd1d1b255a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Mon, 8 Sep 2014 22:46:30 +0200 Subject: [PATCH] HaikuDepot: Rating related fixes. * Fixed retrieving rating summary for the list view. * Rating and command are optional in the web app (or probably you need to specify at least one of them). Handle ratings with just the comment but no rating in average calculation. --- src/apps/haikudepot/Model.cpp | 30 +++++++++++-------------- src/apps/haikudepot/PackageInfo.cpp | 17 ++++++++++---- src/apps/haikudepot/PackageInfoView.cpp | 3 ++- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/apps/haikudepot/Model.cpp b/src/apps/haikudepot/Model.cpp index a05e71fc24..9462c93ffa 100644 --- a/src/apps/haikudepot/Model.cpp +++ b/src/apps/haikudepot/Model.cpp @@ -578,17 +578,19 @@ Model::PopulatePackage(const PackageInfoRef& package, uint32 flags) continue; } - // Extract basic info + // Extract basic info, all items are optional BString languageCode; BString comment; double rating; - if (item.FindString("naturalLanguageCode", - &languageCode) != B_OK - || item.FindString("comment", &comment) != B_OK - || item.FindDouble("rating", &rating) != B_OK) { - // Ignore this entry, we need the basics + item.FindString("naturalLanguageCode", &languageCode); + item.FindString("comment", &comment); + if (item.FindDouble("rating", &rating) != B_OK) + rating = -1; + if (comment.Length() == 0 && rating == -1) { + // No useful information given. continue; } + // For which version of the package was the rating? BString major = "?"; BString minor = "?"; @@ -927,18 +929,12 @@ Model::_PopulatePackageInfo(const PackageInfoRef& package, const BMessage& data) } double derivedRating; - double derivedRatingSampleSize; - if (data.FindDouble("derivedRating", &derivedRating) == B_OK - && data.FindDouble("derivedRatingSampleSize", - &derivedRatingSampleSize) == B_OK) { - if (derivedRatingSampleSize > 0) { - RatingSummary summary; - summary.averageRating = derivedRating; - summary.ratingCount = (int)derivedRatingSampleSize; - package->SetRatingSummary(summary); + if (data.FindDouble("derivedRating", &derivedRating) == B_OK) { + RatingSummary summary; + summary.averageRating = derivedRating; + package->SetRatingSummary(summary); - append_word_list(foundInfo, "rating"); - } + append_word_list(foundInfo, "rating"); } BMessage screenshots; diff --git a/src/apps/haikudepot/PackageInfo.cpp b/src/apps/haikudepot/PackageInfo.cpp index 4277bc06bc..b63447d999 100644 --- a/src/apps/haikudepot/PackageInfo.cpp +++ b/src/apps/haikudepot/PackageInfo.cpp @@ -923,17 +923,21 @@ PackageInfo::CalculateRatingSummary() const float ratingSum = 0.0f; + int ratingsSpecified = summary.ratingCount; for (int i = 0; i < summary.ratingCount; i++) { float rating = fUserRatings.ItemAtFast(i).Rating(); if (rating < 0.0f) - rating = 0.0f; + rating = -1.0f; else if (rating > 5.0f) rating = 5.0f; - ratingSum += rating; + if (rating >= 0.0f) + ratingSum += rating; - if (rating <= 1.0f) + if (rating <= 0.0f) + ratingsSpecified--; // No rating specified by user + else if (rating <= 1.0f) summary.ratingCountByStar[0]++; else if (rating <= 2.0f) summary.ratingCountByStar[1]++; @@ -945,7 +949,12 @@ PackageInfo::CalculateRatingSummary() const summary.ratingCountByStar[4]++; } - summary.averageRating = ratingSum / summary.ratingCount; + if (ratingsSpecified > 1) + ratingSum /= ratingsSpecified; + + summary.averageRating = ratingSum; + summary.ratingCount = ratingsSpecified; + return summary; } diff --git a/src/apps/haikudepot/PackageInfoView.cpp b/src/apps/haikudepot/PackageInfoView.cpp index d7062bc001..65acb59459 100644 --- a/src/apps/haikudepot/PackageInfoView.cpp +++ b/src/apps/haikudepot/PackageInfoView.cpp @@ -1030,7 +1030,8 @@ public: fRatingView->SetRating(rating.Rating()); BString ratingLabel; - ratingLabel.SetToFormat("%.1f", rating.Rating()); + if (rating.Rating() >= 0.0f) + ratingLabel.SetToFormat("%.1f", rating.Rating()); fRatingLabelView = new BStringView("rating label", ratingLabel); BString versionLabel(B_TRANSLATE("for %Version%"));