Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MWMUGCViewModel.mm « UGCViewModel « PlacePage « UI « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a90f21f538ac311bf55993f24a20a8529ff67525 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#import "MWMUGCViewModel.h"
#import "MWMPlacePageData.h"
#import "SwiftBridge.h"

#include "ugc/types.hpp"

#include <chrono>

using namespace place_page;

namespace
{
NSArray<MWMUGCRatingStars *> * starsRatings(ugc::Ratings const & ratings)
{
  NSMutableArray<MWMUGCRatingStars *> * mwmRatings = [@[] mutableCopy];
  for (auto const & rec : ratings)
    [mwmRatings addObject:[[MWMUGCRatingStars alloc] initWithTitle:@(rec.m_key.m_key.c_str())
                                                             value:rec.m_value
                                                          maxValue:5]];
  return [mwmRatings copy];
}

MWMUGCRatingValueType * ratingValueType(float rating)
{
  return [[MWMUGCRatingValueType alloc]
      initWithValue:@(rating::GetRatingFormatted(rating).c_str())
               type:[MWMPlacePageData ratingValueType:rating::GetImpress(rating)]];
}
}  // namespace

@interface MWMUGCViewModel ()
@property(copy, nonatomic) MWMVoidBlock refreshCallback;
@end

@implementation MWMUGCViewModel
{
  ugc::UGC m_ugc;
  ugc::UGCUpdate m_ugcUpdate;
  std::vector<ugc::view_model::ReviewRow> m_reviewRows;
}

- (instancetype)initWithUGC:(ugc::UGC const &)ugc update:(ugc::UGCUpdate const &)update
{
  self = [super init];
  if (self)
  {
    m_ugc = ugc;
    m_ugcUpdate = update;
    [self fillReviewRows];
  }
  return self;
}

- (void)fillReviewRows
{
  using namespace ugc::view_model;
  m_reviewRows.clear();
  if (!m_ugcUpdate.IsEmpty())
    m_reviewRows.push_back(ReviewRow::YourReview);

  if (m_ugc.IsEmpty())
    return;

  auto const reviewsSize = m_ugc.m_reviews.size();
  auto constexpr kMaxSize = 3;
  if (reviewsSize > kMaxSize)
  {
    m_reviewRows.insert(m_reviewRows.end(), kMaxSize, ReviewRow::Review);
    m_reviewRows.push_back(ReviewRow::MoreReviews);
  }
  else
  {
    m_reviewRows.insert(m_reviewRows.end(), reviewsSize, ReviewRow::Review);
  }
}

- (BOOL)isUGCEmpty { return static_cast<BOOL>(m_ugc.IsEmpty()); }
- (BOOL)isUGCUpdateEmpty { return static_cast<BOOL>(m_ugcUpdate.IsEmpty()); }
- (NSUInteger)ratingCellsCount { return 1; }
- (NSUInteger)addReviewCellsCount { return 1; }
- (NSUInteger)totalReviewsCount { return static_cast<NSUInteger>(m_ugc.m_basedOn); }
- (MWMUGCRatingValueType *)summaryRating { return ratingValueType(m_ugc.m_totalRating); }
- (NSArray<MWMUGCRatingStars *> *)ratings { return starsRatings(m_ugc.m_ratings); }
- (std::vector<ugc::view_model::ReviewRow> const &)reviewRows { return m_reviewRows; }

#pragma mark - MWMReviewsViewModelProtocol

- (NSInteger)numberOfReviews { return m_ugc.m_reviews.size() + !self.isUGCUpdateEmpty; }

- (id<MWMReviewProtocol> _Nonnull)reviewWithIndex:(NSInteger)index
{
  auto idx = index;
  NSAssert(idx >= 0, @"Invalid index");
  if (!self.isUGCUpdateEmpty)
  {
    if (idx == 0)
    {
      auto const & review = m_ugcUpdate;
      return [[MWMUGCYourReview alloc]
              initWithDate:[self reviewDate:review.m_time]
                  text:@(review.m_text.m_text.c_str())
               ratings:starsRatings(review.m_ratings)];
    }
    idx -= 1;
  }
  NSAssert(idx < m_ugc.m_reviews.size(), @"Invalid index");
  auto const & review = m_ugc.m_reviews[idx];
  return [[MWMUGCReview alloc]
      initWithTitle:@(review.m_author.c_str())
               date:[self reviewDate:review.m_time]
               text:@(review.m_text.m_text.c_str())
             rating:ratingValueType(review.m_rating)];
}

#pragma mark - Propertis

- (NSString *)reviewDate:(ugc::Time const &) time
{
  using namespace std::chrono;
  auto reviewDate = [NSDate dateWithTimeIntervalSince1970:duration_cast<seconds>(time.time_since_epoch()).count()];
  auto formatter = [[NSDateFormatter alloc] init];
  formatter.dateStyle = NSDateFormatterLongStyle;
  formatter.timeStyle = NSDateFormatterNoStyle;
  return [formatter stringFromDate:reviewDate];
}

@end