[ios] Compressed empty reviews

This commit is contained in:
VladiMihaylenko 2017-10-17 18:04:25 +03:00 committed by r.kuznetsov
parent c29b1deb65
commit 8d904e9ffa
8 changed files with 39 additions and 16 deletions

View file

@ -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]

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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))

View file

@ -111,6 +111,7 @@
<outlet property="dateLabel" destination="qZ5-I9-7dJ" id="Hkf-h6-ybf"/>
<outlet property="ratingCollectionView" destination="w6j-GC-6Bv" id="SbY-HY-zXO"/>
<outlet property="ratingCollectionViewHeight" destination="2hn-mr-UAw" id="xbR-Dl-OR3"/>
<outlet property="reviewBottomOffset" destination="NW0-Eu-IsI" id="emL-VB-8lD"/>
<outlet property="reviewLabel" destination="G4x-pj-fmv" id="pWq-RG-xv0"/>
<outlet property="titleLabel" destination="nht-6C-S3i" id="1lQ-nA-n7z"/>
</connections>

View file

@ -4,6 +4,8 @@
#include "ugc/types.hpp"
#include <chrono>
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<seconds>(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

View file

@ -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