diff options
author | VladiMihaylenko <vxmihaylenko@gmail.com> | 2018-02-05 17:10:46 +0300 |
---|---|---|
committer | Arsentiy Milchakov <milcars@mapswithme.com> | 2018-02-06 14:44:49 +0300 |
commit | 2be0781941a5b9be7e0209bcbe72a2e623cad546 (patch) | |
tree | fded72bbb2930a132589476cdbbcf4290aecfabc | |
parent | b378c80fe3062fba83330158a3498c9105a6ac76 (diff) |
Using the same constant for an incorrect rating in all places.
-rw-r--r-- | android/jni/com/mapswithme/maps/SearchEngine.cpp | 15 | ||||
-rw-r--r-- | android/src/com/mapswithme/maps/gallery/Holders.java | 18 | ||||
-rw-r--r-- | android/src/com/mapswithme/maps/gallery/Items.java | 7 | ||||
-rw-r--r-- | android/src/com/mapswithme/maps/search/SearchAdapter.java | 10 | ||||
-rw-r--r-- | android/src/com/mapswithme/maps/search/SearchResult.java | 6 | ||||
-rw-r--r-- | android/src/com/mapswithme/util/Constants.java | 7 | ||||
-rw-r--r-- | defines.hpp | 4 | ||||
-rw-r--r-- | iphone/Maps/UI/Discovery/MWMDiscoveryTableManager.mm | 34 | ||||
-rw-r--r-- | iphone/Maps/UI/Search/TableView/MWMSearchCommonCell.mm | 23 | ||||
-rw-r--r-- | map/discovery/discovery_search_params.hpp | 5 | ||||
-rw-r--r-- | map/place_page_info.hpp | 4 | ||||
-rw-r--r-- | partners_api/locals_api.cpp | 5 | ||||
-rw-r--r-- | search/everywhere_search_callback.hpp | 2 | ||||
-rw-r--r-- | search/intermediate_result.cpp | 10 | ||||
-rw-r--r-- | search/result.hpp | 6 | ||||
-rw-r--r-- | ugc/types.hpp | 4 |
16 files changed, 97 insertions, 63 deletions
diff --git a/android/jni/com/mapswithme/maps/SearchEngine.cpp b/android/jni/com/mapswithme/maps/SearchEngine.cpp index d42a8d5d2b..8b26cd03d6 100644 --- a/android/jni/com/mapswithme/maps/SearchEngine.cpp +++ b/android/jni/com/mapswithme/maps/SearchEngine.cpp @@ -14,6 +14,8 @@ #include "base/assert.hpp" #include "base/logging.hpp" +#include "defines.hpp" + #include <chrono> #include <cstdint> #include <memory> @@ -377,15 +379,16 @@ jobject ToJavaResult(Result & result, search::ProductInfo const & productInfo, b jni::TScopedLocalRef cuisine(env, jni::ToJavaString(env, result.GetCuisine())); jni::TScopedLocalRef pricing(env, jni::ToJavaString(env, result.GetHotelApproximatePricing())); - string ratingStr = result.GetHotelRating(); - if (ratingStr.empty() && productInfo.m_ugcRating != search::ProductInfo::kInvalidRating) - ratingStr = place_page::rating::GetRatingFormatted(productInfo.m_ugcRating); - jni::TScopedLocalRef rating(env, jni::ToJavaString(env, ratingStr)); + + auto const hotelRating = result.GetHotelRating(); + auto const ugcRating = productInfo.m_ugcRating; + auto const rating = static_cast<jfloat>(hotelRating == kInvalidRatingValue ? ugcRating + : hotelRating); jni::TScopedLocalRef desc(env, env->NewObject(g_descriptionClass, g_descriptionConstructor, featureId.get(), featureType.get(), address.get(), dist.get(), cuisine.get(), - rating.get(), pricing.get(), + pricing.get(), rating, result.GetStarsCount(), static_cast<jint>(result.IsOpenNow()))); @@ -505,7 +508,7 @@ extern "C" g_descriptionConstructor = jni::GetConstructorID(env, g_descriptionClass, "(Lcom/mapswithme/maps/bookmarks/data/FeatureId;" "Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;" - "Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;II)V"); + "Ljava/lang/String;Ljava/lang/String;FII)V"); g_mapResultsMethod = jni::GetMethodID(env, g_javaListener, "onMapSearchResults", "([Lcom/mapswithme/maps/search/NativeMapSearchListener$Result;JZ)V"); diff --git a/android/src/com/mapswithme/maps/gallery/Holders.java b/android/src/com/mapswithme/maps/gallery/Holders.java index 2fcfd9fdca..6e10a5b440 100644 --- a/android/src/com/mapswithme/maps/gallery/Holders.java +++ b/android/src/com/mapswithme/maps/gallery/Holders.java @@ -276,28 +276,12 @@ public class Holders item.getPrice(), mSubtitle.getResources())); - float rating = formatRating(item.getRating()); + float rating = item.getRating(); Impress impress = Impress.values()[UGC.nativeToImpress(rating)]; mRatingView.setRating(impress, UGC.nativeFormatRating(rating)); UiUtils.setTextAndHideIfEmpty(mDistance, item.getDistance()); } - //TODO: refactor rating to float in core - private static float formatRating(@Nullable String rating) - { - if (TextUtils.isEmpty(rating)) - return -1; - - try - { - return Float.valueOf(rating); - } - catch (NumberFormatException e) - { - return -1; - } - } - @NonNull private static CharSequence formatDescription(int stars, @Nullable String type, @Nullable String priceCategory, diff --git a/android/src/com/mapswithme/maps/gallery/Items.java b/android/src/com/mapswithme/maps/gallery/Items.java index edf19b941f..262bbb679b 100644 --- a/android/src/com/mapswithme/maps/gallery/Items.java +++ b/android/src/com/mapswithme/maps/gallery/Items.java @@ -10,6 +10,8 @@ import com.mapswithme.maps.search.SearchResult; import static com.mapswithme.maps.gallery.Constants.TYPE_MORE; import static com.mapswithme.maps.gallery.Constants.TYPE_PRODUCT; +import static com.mapswithme.util.Constants.Rating.RATING_INCORRECT_VALUE; + public class Items { public static class ViatorItem extends RegularAdapterStrategy.Item @@ -147,11 +149,10 @@ public class Items return mResult.description.stars; } - @Nullable - public String getRating() + public float getRating() { if (mResult.description == null) - return null; + return RATING_INCORRECT_VALUE; return mResult.description.rating; } diff --git a/android/src/com/mapswithme/maps/search/SearchAdapter.java b/android/src/com/mapswithme/maps/search/SearchAdapter.java index a0e53b1c6a..1fae353859 100644 --- a/android/src/com/mapswithme/maps/search/SearchAdapter.java +++ b/android/src/com/mapswithme/maps/search/SearchAdapter.java @@ -23,6 +23,7 @@ import com.mapswithme.HotelUtils; import com.mapswithme.maps.R; import com.mapswithme.maps.bookmarks.data.FeatureId; import com.mapswithme.maps.routing.RoutingController; +import com.mapswithme.maps.ugc.UGC; import com.mapswithme.util.Graphics; import com.mapswithme.util.ThemeUtils; import com.mapswithme.util.UiUtils; @@ -31,6 +32,8 @@ import java.util.Arrays; import java.util.HashSet; import java.util.Set; +import static com.mapswithme.util.Constants.Rating.RATING_INCORRECT_VALUE; + class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.SearchDataViewHolder> { private final SearchFragment mSearchFragment; @@ -172,7 +175,7 @@ class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.SearchDataViewHol final SpannableStringBuilder tail = new SpannableStringBuilder(); int stars = result.description.stars; - if (stars > 0 || !result.description.rating.isEmpty() || isHotelAvailable) + if (stars > 0 || result.description.rating != RATING_INCORRECT_VALUE || isHotelAvailable) { if (stars > 0) { @@ -180,10 +183,11 @@ class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.SearchDataViewHol tail.append(HotelUtils.formatStars(stars, itemView.getResources())); } - if (!result.description.rating.isEmpty()) + if (result.description.rating != RATING_INCORRECT_VALUE) { Resources rs = itemView.getResources(); - String s = rs.getString(R.string.place_page_booking_rating, result.description.rating); + String s = rs.getString(R.string.place_page_booking_rating, + UGC.nativeFormatRating(result.description.rating)); tail .append(" • ") .append(colorizeString(s, rs.getColor(R.color.base_green))); diff --git a/android/src/com/mapswithme/maps/search/SearchResult.java b/android/src/com/mapswithme/maps/search/SearchResult.java index 7f7d7439f0..eb6ef1996b 100644 --- a/android/src/com/mapswithme/maps/search/SearchResult.java +++ b/android/src/com/mapswithme/maps/search/SearchResult.java @@ -27,21 +27,21 @@ public class SearchResult implements SearchData public final String region; public final String distance; public final String cuisine; - public final String rating; public final String pricing; + public final float rating; public final int stars; public final int openNow; public Description(FeatureId featureId, String featureType, String region, String distance, - String cuisine, String rating, String pricing, int stars, int openNow) + String cuisine, String pricing, float rating, int stars, int openNow) { this.featureId = featureId; this.featureType = featureType; this.region = region; this.distance = distance; this.cuisine = cuisine; - this.rating = rating; this.pricing = pricing; + this.rating = rating; this.stars = stars; this.openNow = openNow; } diff --git a/android/src/com/mapswithme/util/Constants.java b/android/src/com/mapswithme/util/Constants.java index a89856bd6a..2f9b5cb1cc 100644 --- a/android/src/com/mapswithme/util/Constants.java +++ b/android/src/com/mapswithme/util/Constants.java @@ -61,6 +61,13 @@ public final class Constants private Package() {} } + public static class Rating + { + public static final float RATING_INCORRECT_VALUE = -1.0f; + + private Rating() {}; + } + public static final String MWM_DIR_POSTFIX = "/MapsWithMe/"; public static final String CACHE_DIR = "cache"; diff --git a/defines.hpp b/defines.hpp index 93b3f55f65..eed20fce9f 100644 --- a/defines.hpp +++ b/defines.hpp @@ -1,5 +1,7 @@ #pragma once +// Only defines and constexprs are allowed in this file. + #define DATA_FILE_EXTENSION ".mwm" #define DATA_FILE_EXTENSION_TMP ".mwm.tmp" #define DIFF_FILE_EXTENSION ".mwmdiff" @@ -94,3 +96,5 @@ #define MIXED_TAGS_FILE "mixed_tags.txt" #define LOCALIZATION_DESCRIPTION_SUFFIX " Description" + +auto constexpr kInvalidRatingValue = -1.0f; diff --git a/iphone/Maps/UI/Discovery/MWMDiscoveryTableManager.mm b/iphone/Maps/UI/Discovery/MWMDiscoveryTableManager.mm index 0b1bc94e9c..d3ad33533d 100644 --- a/iphone/Maps/UI/Discovery/MWMDiscoveryTableManager.mm +++ b/iphone/Maps/UI/Discovery/MWMDiscoveryTableManager.mm @@ -24,6 +24,7 @@ #include <iterator> #include <string> #include <utility> +#include <tuple> using namespace std; @@ -40,6 +41,13 @@ NSString * StatProvider(ItemType const type) case ItemType::Hotels: return kStatBooking; } } + +pair<NSString *, MWMRatingSummaryViewValueType> FormattedRating(float const rawValue) +{ + auto const str = place_page::rating::GetRatingFormatted(rawValue); + auto const impress = static_cast<MWMRatingSummaryViewValueType>(place_page::rating::GetImpress(rawValue)); + return {@(str.c_str()), impress}; +}; } // namespace discovery using namespace discovery; @@ -341,6 +349,8 @@ string GetDistance(m2::PointD const & from, m2::PointD const & to) { auto const type = collectionView.itemType; auto const & model = self.model(); + NSString * ratingValue; + MWMRatingSummaryViewValueType ratingType; switch (type) { case ItemType::Attractions: @@ -369,14 +379,15 @@ string GetDistance(m2::PointD const & from, m2::PointD const & to) auto const & v = model.GetViatorAt(indexPath.row); auto imageURL = [NSURL URLWithString:@(v.m_photoUrl.c_str())]; auto pageURL = [NSURL URLWithString:@(v.m_pageUrl.c_str())]; - string const ratingFormatted = place_page::rating::GetRatingFormatted(v.m_rating); - auto const ratingValue = static_cast<int>(place_page::rating::GetImpress(v.m_rating)); + + tie(ratingValue, ratingType) = FormattedRating(v.m_rating); + auto viatorModel = [[MWMViatorItemModel alloc] initWithImageURL:imageURL pageURL:pageURL title:@(v.m_title.c_str()) - ratingFormatted:@(ratingFormatted.c_str()) - ratingType:static_cast<MWMRatingSummaryViewValueType>(ratingValue) + ratingFormatted:ratingValue + ratingType:ratingType duration:@(v.m_duration.c_str()) price:@(v.m_priceFormatted.c_str())]; cell.model = viatorModel; @@ -388,14 +399,12 @@ string GetDistance(m2::PointD const & from, m2::PointD const & to) auto cell = static_cast<MWMDiscoveryLocalExpertCell *>( [collectionView dequeueReusableCellWithCellClass:cls indexPath:indexPath]); auto const & expert = model.GetExpertAt(indexPath.row); - - string const ratingFormatted = place_page::rating::GetRatingFormatted(expert.m_rating); - auto const ratingValue = static_cast<int>(place_page::rating::GetImpress(expert.m_rating)); + tie(ratingValue, ratingType) = FormattedRating(expert.m_rating); [cell configWithAvatarURL:@(expert.m_photoUrl.c_str()) name:@(expert.m_name.c_str()) - ratingValue:@(ratingFormatted.c_str()) - ratingType:static_cast<MWMRatingSummaryViewValueType>(ratingValue) + ratingValue:ratingValue + ratingType:ratingType price:expert.m_pricePerHour currency:@(expert.m_currency.c_str()) tap:^{ @@ -423,12 +432,15 @@ string GetDistance(m2::PointD const & from, m2::PointD const & to) for (int i = 0; i < starsCount; ++i) [subtitle appendString:@"★"]; } + + tie(ratingValue, ratingType) = FormattedRating(sr.GetHotelRating()); + [cell configWithAvatarURL:nil title:@(sr.GetString().c_str()) subtitle:[subtitle copy] price:@(sr.GetHotelApproximatePricing().c_str()) - ratingValue:@(sr.GetHotelRating().c_str()) - ratingType:MWMRatingSummaryViewValueTypeGood + ratingValue:ratingValue + ratingType:ratingType distance:@(GetDistance(pt, sr.GetFeatureCenter()).c_str()) onBuildRoute:^{ [self.delegate routeToItem:type atIndex:indexPath.row]; diff --git a/iphone/Maps/UI/Search/TableView/MWMSearchCommonCell.mm b/iphone/Maps/UI/Search/TableView/MWMSearchCommonCell.mm index f306be4e96..8595708845 100644 --- a/iphone/Maps/UI/Search/TableView/MWMSearchCommonCell.mm +++ b/iphone/Maps/UI/Search/TableView/MWMSearchCommonCell.mm @@ -8,6 +8,8 @@ #include "platform/measurement_utils.hpp" +#include "defines.hpp" + @interface MWMSearchCommonCell () @property(nonatomic) IBOutletCollection(UIImageView) NSArray * infoRatingStars; @@ -34,12 +36,21 @@ { [super config:result]; self.typeLabel.text = @(result.GetFeatureTypeName().c_str()).capitalizedString; - auto ratingStr = result.GetHotelRating(); - if (ratingStr.empty() && productInfo.m_ugcRating != search::ProductInfo::kInvalidRating) - ratingStr = place_page::rating::GetRatingFormatted(productInfo.m_ugcRating); - self.ratingLabel.text = - ratingStr.empty() ? @"" : [NSString stringWithFormat:L(@"place_page_booking_rating"), - ratingStr.c_str()]; + + + auto const hotelRating = result.GetHotelRating(); + auto const ugcRating = productInfo.m_ugcRating; + auto const rating = hotelRating != kInvalidRatingValue ? hotelRating : ugcRating; + if (rating != kInvalidRatingValue) + { + auto const str = place_page::rating::GetRatingFormatted(rating); + self.ratingLabel.text = [NSString stringWithFormat:L(@"place_page_booking_rating"), str.c_str()]; + } + else + { + self.ratingLabel.text = @""; + } + self.priceLabel.text = @(result.GetHotelApproximatePricing().c_str()); self.locationLabel.text = @(result.GetAddress().c_str()); [self.locationLabel sizeToFit]; diff --git a/map/discovery/discovery_search_params.hpp b/map/discovery/discovery_search_params.hpp index ac3c124731..cf2d2a217e 100644 --- a/map/discovery/discovery_search_params.hpp +++ b/map/discovery/discovery_search_params.hpp @@ -25,10 +25,7 @@ struct DiscoverySearchParams { bool operator()(Result const & lhs, Result const & rhs) const { - float l = -1, r = -1; - UNUSED_VALUE(strings::to_float(lhs.m_metadata.m_hotelRating, l)); - UNUSED_VALUE(strings::to_float(rhs.m_metadata.m_hotelRating, r)); - return l > r; + return lhs.m_metadata.m_hotelRating > rhs.m_metadata.m_hotelRating; } }; diff --git a/map/place_page_info.hpp b/map/place_page_info.hpp index a58b4247fa..be0fead870 100644 --- a/map/place_page_info.hpp +++ b/map/place_page_info.hpp @@ -19,6 +19,8 @@ #include "geometry/mercator.hpp" #include "geometry/point2d.hpp" +#include "defines.hpp" + #include <memory> #include <string> #include <utility> @@ -55,7 +57,7 @@ enum class LocalsStatus Available }; -auto constexpr kIncorrectRating = -1.0f; +auto constexpr kIncorrectRating = kInvalidRatingValue; class Info : public osm::MapObject { diff --git a/partners_api/locals_api.cpp b/partners_api/locals_api.cpp index 18e6eb47e2..4fd2c04ce4 100644 --- a/partners_api/locals_api.cpp +++ b/partners_api/locals_api.cpp @@ -10,6 +10,8 @@ #include "base/logging.hpp" #include "base/string_utils.hpp" +#include "defines.hpp" + #include "3party/jansson/myjansson.hpp" #include "private.h" @@ -57,9 +59,8 @@ void ParseLocals(std::string const & src, std::vector<LocalExpert> & locals, FromJSONObject(item, "i_will_show_you", local.m_offerDescription); // Rescale rating to (0.0; 10.0] range. Rating 0.0 is invalid. - static double const kInvalidRating = -1.0; if (local.m_rating == 0.0) - local.m_rating = kInvalidRating; + local.m_rating = kInvalidRatingValue; else local.m_rating *= 2.0; diff --git a/search/everywhere_search_callback.hpp b/search/everywhere_search_callback.hpp index 73b617351e..55820f592f 100644 --- a/search/everywhere_search_callback.hpp +++ b/search/everywhere_search_callback.hpp @@ -9,7 +9,7 @@ namespace search { struct ProductInfo { - static float constexpr kInvalidRating = -1; + static auto constexpr kInvalidRating = kInvalidRatingValue; bool m_isLocalAdsCustomer = false; float m_ugcRating = kInvalidRating; diff --git a/search/intermediate_result.cpp b/search/intermediate_result.cpp index 374f42243f..411add4374 100644 --- a/search/intermediate_result.cpp +++ b/search/intermediate_result.cpp @@ -28,7 +28,6 @@ namespace search { namespace { -char const * const kEmptyRatingSymbol = "-"; char const * const kPricingSymbol = "$"; class SkipRegionInfo @@ -202,8 +201,13 @@ void ProcessMetadata(FeatureType const & ft, Result::Metadata & meta) if (isSponsoredHotel) { auto const r = src.Get(feature::Metadata::FMD_RATING); - char const * const rating = r.empty() ? kEmptyRatingSymbol : r.c_str(); - meta.m_hotelRating = rating; + if (!r.empty()) + { + float raw; + if (strings::to_float(r.c_str(), raw)) + meta.m_hotelRating = raw; + } + int pricing; if (!strings::to_int(src.Get(feature::Metadata::FMD_PRICE_RATE), pricing)) diff --git a/search/result.hpp b/search/result.hpp index 3f10bf496b..e35ad3d01e 100644 --- a/search/result.hpp +++ b/search/result.hpp @@ -11,6 +11,8 @@ #include "base/assert.hpp" #include "base/buffer_vector.hpp" +#include "defines.hpp" + #include <algorithm> #include <cstddef> #include <cstdint> @@ -43,7 +45,7 @@ public: // Following fields are used for hotels only. std::string m_hotelApproximatePricing; - std::string m_hotelRating; + float m_hotelRating = kInvalidRatingValue; int m_stars = 0; bool m_isSponsoredHotel = false; bool m_isHotel = false; @@ -74,7 +76,7 @@ public: std::string const & GetAddress() const { return m_address; } std::string const & GetFeatureTypeName() const { return m_featureTypeName; } std::string const & GetCuisine() const { return m_metadata.m_cuisine; } - std::string const & GetHotelRating() const { return m_metadata.m_hotelRating; } + float GetHotelRating() const { return m_metadata.m_hotelRating; } std::string const & GetHotelApproximatePricing() const { return m_metadata.m_hotelApproximatePricing; diff --git a/ugc/types.hpp b/ugc/types.hpp index 3817dc6b3f..1c748100a5 100644 --- a/ugc/types.hpp +++ b/ugc/types.hpp @@ -7,6 +7,8 @@ #include "base/math.hpp" #include "base/visitor.hpp" +#include "defines.hpp" + #include <chrono> #include <cstdint> #include <memory> @@ -338,7 +340,7 @@ struct UGC Ratings m_ratings; Reviews m_reviews; - float m_totalRating = -1.f; + float m_totalRating = kInvalidRatingValue; uint32_t m_basedOn{}; }; |