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

MWMMapDownloaderTableViewCell.mm « Cells « MapDownloader « Classes « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 80a615c410564e82d0852a0d381a7f570b0bf169 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#import "Common.h"
#import "MWMCircularProgress.h"
#import "MWMMapDownloaderLargeCountryTableViewCell.h"
#import "MWMMapDownloaderTableViewCell.h"
#import "NSString+Categories.h"
#import "UIFont+MapsMeFonts.h"

#include "Framework.h"

namespace
{
  NSDictionary * const kSelectedTitleAttrs = @{ NSFontAttributeName : [UIFont bold17] };
  NSDictionary * const kUnselectedTitleAttrs = @{ NSFontAttributeName : [UIFont regular17] };
} // namespace

@interface MWMMapDownloaderTableViewCell () <MWMCircularProgressProtocol>

@property (nonatomic) MWMCircularProgress * progress;
@property (copy, nonatomic) NSString * searchQuery;

@property (weak, nonatomic) IBOutlet UIView * stateWrapper;
@property (weak, nonatomic) IBOutlet UILabel * title;
@property (weak, nonatomic) IBOutlet UILabel * downloadSize;

@end

@implementation MWMMapDownloaderTableViewCell
{
  storage::TCountryId m_countryId;
}

+ (CGFloat)estimatedHeight
{
  return 52.0;
}

- (void)awakeFromNib
{
  [super awakeFromNib];
  m_countryId = kInvalidCountryId;
}

- (void)prepareForReuse
{
  [super prepareForReuse];
  m_countryId = kInvalidCountryId;
}

- (void)layoutSubviews
{
  [super layoutSubviews];
  if (isIOS7)
  {
    self.title.preferredMaxLayoutWidth = floor(self.title.width);
    self.downloadSize.preferredMaxLayoutWidth = floor(self.downloadSize.width);
    [super layoutSubviews];
  }
}

#pragma mark - Search matching

- (NSAttributedString *)matchedString:(NSString *)str selectedAttrs:(NSDictionary *)selectedAttrs unselectedAttrs:(NSDictionary *)unselectedAttrs
{
  NSMutableAttributedString * attrTitle = [[NSMutableAttributedString alloc] initWithString:str];
  [attrTitle addAttributes:unselectedAttrs range:{0, str.length}];
  if (!self.searchQuery)
    return [attrTitle copy];
  for (auto const & range : [str rangesOfString:self.searchQuery])
    [attrTitle addAttributes:selectedAttrs range:range];
  return [attrTitle copy];
}

#pragma mark - Config

- (void)config:(storage::NodeAttrs const &)nodeAttrs
{
  [self configProgress:nodeAttrs];
  self.title.attributedText = [self matchedString:@(nodeAttrs.m_nodeLocalName.c_str())
                                    selectedAttrs:kSelectedTitleAttrs
                                  unselectedAttrs:kUnselectedTitleAttrs];
  BOOL const haveDownloadingCountries = nodeAttrs.m_downloadingMwmCounter != 0;
  self.downloadSize.text = formattedSize(haveDownloadingCountries ? nodeAttrs.m_downloadingMwmSize : nodeAttrs.m_mwmSize);
}

- (void)configProgress:(storage::NodeAttrs const &)nodeAttrs
{
  MWMCircularProgress * progress = self.progress;
  MWMButtonColoring const coloring = self.mode == TMWMMapDownloaderMode::Downloaded
                                         ? MWMButtonColoringBlack
                                         : MWMButtonColoringBlue;
  switch (nodeAttrs.m_status)
  {
    case NodeStatus::NotDownloaded:
    case NodeStatus::Partly:
    {
      auto const affectedStates = {MWMCircularProgressStateNormal,
                                   MWMCircularProgressStateSelected};
      UIImage * image = [self isKindOfClass:[MWMMapDownloaderLargeCountryTableViewCell class]]
                            ? [UIImage imageNamed:@"ic_folder"]
                            : [UIImage imageNamed:@"ic_download"];
      [progress setImage:image forStates:affectedStates];
      [progress setColoring:coloring forStates:affectedStates];
      progress.state = MWMCircularProgressStateNormal;
      break;
    }
    case NodeStatus::Downloading:
    {
      auto const & prg = nodeAttrs.m_downloadingProgress;
      progress.progress = static_cast<CGFloat>(prg.first) / prg.second;
      break;
    }
    case NodeStatus::InQueue:
      progress.state = MWMCircularProgressStateSpinner;
      break;
    case NodeStatus::Undefined:
    case NodeStatus::Error:
      progress.state = MWMCircularProgressStateFailed;
      break;
    case NodeStatus::OnDisk:
      progress.state = MWMCircularProgressStateCompleted;
      break;
    case NodeStatus::OnDiskOutOfDate:
    {
      auto const affectedStates = {MWMCircularProgressStateNormal, MWMCircularProgressStateSelected};
      [progress setImage:[UIImage imageNamed:@"ic_update"] forStates:affectedStates];
      [progress setColoring:MWMButtonColoringOther forStates:affectedStates];
      progress.state = MWMCircularProgressStateNormal;
      break;
    }
  }
}

#pragma mark - MWMFrameworkStorageObserver

- (void)processCountryEvent:(TCountryId const &)countryId
{
  if (countryId != m_countryId)
    return;
  storage::NodeAttrs nodeAttrs;
  GetFramework().Storage().GetNodeAttrs(m_countryId, nodeAttrs);
  [self config:nodeAttrs];
}

- (void)processCountry:(TCountryId const &)countryId progress:(MapFilesDownloader::TProgress const &)progress
{
  if (countryId != m_countryId)
    return;
  self.progress.progress = static_cast<CGFloat>(progress.first) / progress.second;
}

#pragma mark - MWMCircularProgressProtocol

- (void)progressButtonPressed:(nonnull MWMCircularProgress *)progress
{
  storage::NodeAttrs nodeAttrs;
  GetFramework().Storage().GetNodeAttrs(m_countryId, nodeAttrs);
  switch (nodeAttrs.m_status)
  {
    case NodeStatus::NotDownloaded:
    case NodeStatus::Partly:
      if ([self isKindOfClass:[MWMMapDownloaderLargeCountryTableViewCell class]])
        [self.delegate openNodeSubtree:m_countryId];
      else
        [self.delegate downloadNode:m_countryId];
      break;
    case NodeStatus::Undefined:
    case NodeStatus::Error:
      [self.delegate retryDownloadNode:m_countryId];
      break;
    case NodeStatus::OnDiskOutOfDate:
      [self.delegate updateNode:m_countryId];
      break;
    case NodeStatus::Downloading:
    case NodeStatus::InQueue:
      [self.delegate cancelNode:m_countryId];
      break;
    case NodeStatus::OnDisk:
      break;
  }
}

#pragma mark - Properties

- (void)setCountryId:(NSString *)countryId searchQuery:(NSString *)query
{
  if (m_countryId == countryId.UTF8String && [query isEqualToString:self.searchQuery])
    return;
  self.searchQuery = query;
  m_countryId = countryId.UTF8String;
  storage::NodeAttrs nodeAttrs;
  GetFramework().Storage().GetNodeAttrs(m_countryId, nodeAttrs);
  [self config:nodeAttrs];
}

- (MWMCircularProgress *)progress
{
  if (!_progress && !self.isHeightCell)
  {
    _progress = [MWMCircularProgress downloaderProgressForParentView:self.stateWrapper];
    _progress.delegate = self;
  }
  return _progress;
}

@end