From 8d904e9ffa026c30a570fc4207a2615c9174e912 Mon Sep 17 00:00:00 2001 From: VladiMihaylenko Date: Tue, 17 Oct 2017 18:04:25 +0300 Subject: [PATCH] [ios] Compressed empty reviews --- .../Maps/Categories/String+BoundingRect.swift | 4 +++ .../Content/UGC/UGCReview.swift | 4 +-- .../Content/UGC/UGCReviewCell.swift | 5 +--- .../Content/UGC/UGCYourReview.swift | 4 +-- .../Content/UGC/UGCYourReviewCell.swift | 8 +++--- .../Content/UGC/UGCYourReviewCell.xib | 1 + .../PlacePage/UGCViewModel/MWMUGCViewModel.mm | 27 ++++++++++++++++--- iphone/Maps/UI/Reviews/MWMReviewProtocol.h | 2 +- 8 files changed, 39 insertions(+), 16 deletions(-) diff --git a/iphone/Maps/Categories/String+BoundingRect.swift b/iphone/Maps/Categories/String+BoundingRect.swift index f3f2a004df..66dcb88b5e 100644 --- a/iphone/Maps/Categories/String+BoundingRect.swift +++ b/iphone/Maps/Categories/String+BoundingRect.swift @@ -2,6 +2,10 @@ import UIKit extension String { func size(width: CGFloat, font: UIFont, maxNumberOfLines: Int = 0) -> CGSize { + if (isEmpty) { + return CGSize(width: width, height: 0); + } + let maximumHeight = maxNumberOfLines == 0 ? CGFloat.greatestFiniteMagnitude : font.lineHeight * CGFloat(maxNumberOfLines + 1) let constraintSize = CGSize(width: width, height: maximumHeight) let options: NSStringDrawingOptions = [.usesLineFragmentOrigin, .usesFontLeading] diff --git a/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/UGC/UGCReview.swift b/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/UGC/UGCReview.swift index bb11c9e958..f0bf756d2e 100644 --- a/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/UGC/UGCReview.swift +++ b/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/UGC/UGCReview.swift @@ -1,11 +1,11 @@ @objc(MWMUGCReview) final class UGCReview: NSObject, MWMReviewProtocol { let title: String - let date: Date + let date: String let text: String let rating: UGCRatingValueType - @objc init(title: String, date: Date, text: String, rating: UGCRatingValueType) { + @objc init(title: String, date: String, text: String, rating: UGCRatingValueType) { self.title = title self.date = date self.text = text diff --git a/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/UGC/UGCReviewCell.swift b/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/UGC/UGCReviewCell.swift index cc95f4cddb..5209343f81 100644 --- a/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/UGC/UGCReviewCell.swift +++ b/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/UGC/UGCReviewCell.swift @@ -37,10 +37,7 @@ final class UGCReviewCell: MWMTableViewCell { @objc func config(review: UGCReview, onUpdate: @escaping () -> Void) { titleLabel.text = review.title - let dateFormatter = DateFormatter() - dateFormatter.dateStyle = .medium - dateFormatter.timeStyle = .none - dateLabel.text = dateFormatter.string(from: review.date) + dateLabel.text = review.date reviewLabel.text = review.text reviewLabel.onUpdate = onUpdate ratingView.value = review.rating.value diff --git a/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/UGC/UGCYourReview.swift b/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/UGC/UGCYourReview.swift index 9391193ae0..3c3b2f5ec4 100644 --- a/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/UGC/UGCYourReview.swift +++ b/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/UGC/UGCYourReview.swift @@ -1,10 +1,10 @@ @objc(MWMUGCYourReview) final class UGCYourReview: NSObject, MWMReviewProtocol { - let date: Date + let date: String let text: String let ratings: [UGCRatingStars] - @objc init(date: Date, text: String, ratings: [UGCRatingStars]) { + @objc init(date: String, text: String, ratings: [UGCRatingStars]) { self.date = date self.text = text self.ratings = ratings diff --git a/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/UGC/UGCYourReviewCell.swift b/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/UGC/UGCYourReviewCell.swift index fe7583950a..4f23f6ac72 100644 --- a/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/UGC/UGCYourReviewCell.swift +++ b/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/UGC/UGCYourReviewCell.swift @@ -1,6 +1,7 @@ @objc(MWMUGCYourReviewCell) final class UGCYourReviewCell: MWMTableViewCell { private enum Config { + static let defaultReviewBottomOffset: CGFloat = 16 static let minimumInteritemSpacing: CGFloat = 16 static let minItemsPerRow: CGFloat = 3 static let estimatedItemSize = CGSize(width: 96, height: 32) @@ -30,6 +31,7 @@ final class UGCYourReviewCell: MWMTableViewCell { } } + @IBOutlet private weak var reviewBottomOffset: NSLayoutConstraint! @IBOutlet private weak var ratingCollectionViewHeight: NSLayoutConstraint! @IBOutlet private weak var ratingCollectionView: UICollectionView! { didSet { @@ -50,12 +52,10 @@ final class UGCYourReviewCell: MWMTableViewCell { } @objc func config(yourReview: UGCYourReview, onUpdate: @escaping () -> Void) { - let dateFormatter = DateFormatter() - dateFormatter.dateStyle = .medium - dateFormatter.timeStyle = .none - dateLabel.text = dateFormatter.string(from: yourReview.date) + dateLabel.text = yourReview.date self.yourReview = yourReview reviewLabel.text = yourReview.text + reviewBottomOffset.constant = yourReview.text.isEmpty ? 0 : Config.defaultReviewBottomOffset reviewLabel.onUpdate = onUpdate updateCollectionView { [weak self] in self?.ratingCollectionView.reloadSections(IndexSet(integer: 0)) diff --git a/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/UGC/UGCYourReviewCell.xib b/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/UGC/UGCYourReviewCell.xib index 5ea2a7cb5b..50e78095d7 100644 --- a/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/UGC/UGCYourReviewCell.xib +++ b/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/UGC/UGCYourReviewCell.xib @@ -111,6 +111,7 @@ + diff --git a/iphone/Maps/UI/PlacePage/UGCViewModel/MWMUGCViewModel.mm b/iphone/Maps/UI/PlacePage/UGCViewModel/MWMUGCViewModel.mm index f9dc3b90ac..66f623c5f1 100644 --- a/iphone/Maps/UI/PlacePage/UGCViewModel/MWMUGCViewModel.mm +++ b/iphone/Maps/UI/PlacePage/UGCViewModel/MWMUGCViewModel.mm @@ -4,6 +4,8 @@ #include "ugc/types.hpp" +#include + using namespace place_page; namespace @@ -28,6 +30,7 @@ MWMUGCRatingValueType * ratingValueType(float rating) @interface MWMUGCViewModel () @property(copy, nonatomic) MWMVoidBlock refreshCallback; +@property(nonatomic) NSDateComponentsFormatter * formatter; @end @implementation MWMUGCViewModel @@ -96,8 +99,7 @@ MWMUGCRatingValueType * ratingValueType(float rating) { auto const & review = m_ugcUpdate; return [[MWMUGCYourReview alloc] - initWithDate:[NSDate - dateWithTimeIntervalSince1970:review.m_time.time_since_epoch().count()] + initWithDate:[self daysAgo:review.m_time] text:@(review.m_text.m_text.c_str()) ratings:starsRatings(review.m_ratings)]; } @@ -107,9 +109,28 @@ MWMUGCRatingValueType * ratingValueType(float rating) auto const & review = m_ugc.m_reviews[idx]; return [[MWMUGCReview alloc] initWithTitle:@(review.m_author.c_str()) - date:[NSDate dateWithTimeIntervalSince1970:review.m_time.time_since_epoch().count()] + date:[self daysAgo:review.m_time] text:@(review.m_text.m_text.c_str()) rating:ratingValueType(review.m_rating)]; } +#pragma mark - Propertis + +- (NSString *)daysAgo:(ugc::Time const &) time +{ + using namespace std::chrono; + NSDate * reviewDate = [NSDate dateWithTimeIntervalSince1970:duration_cast(time.time_since_epoch()).count()]; + return [self.formatter stringFromDate:reviewDate toDate:[NSDate date]]; +} + +- (NSDateComponentsFormatter *)formatter +{ + if (!_formatter) + { + _formatter = [[NSDateComponentsFormatter alloc] init]; + _formatter.unitsStyle = NSDateComponentsFormatterUnitsStyleFull; + _formatter.allowedUnits = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay; + } + return _formatter; +} @end diff --git a/iphone/Maps/UI/Reviews/MWMReviewProtocol.h b/iphone/Maps/UI/Reviews/MWMReviewProtocol.h index 9189030b9d..11303b5e22 100644 --- a/iphone/Maps/UI/Reviews/MWMReviewProtocol.h +++ b/iphone/Maps/UI/Reviews/MWMReviewProtocol.h @@ -1,4 +1,4 @@ @protocol MWMReviewProtocol -@property(nonatomic, readonly, copy) NSDate * _Nonnull date; +@property(nonatomic, readonly, copy) NSString * _Nonnull date; @property(nonatomic, readonly, copy) NSString * _Nonnull text; @end