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

MWMSearchTabbedViewController.mm « TabbedView « Search « MapViewControls « CustomViews « Classes « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 57162068fdf0de61728e7cf6a2642409ec0c81f8 (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#import "MWMSearchCategoriesManager.h"
#import "MWMSearchHistoryManager.h"
#import "MWMSearchTabbedCollectionViewCell.h"
#import "MWMSearchTabbedViewController.h"
#import "MWMSearchTabbedViewLayout.h"
#import "MWMSearchTabbedViewProtocol.h"
#import "UIKitCategories.h"

static NSString * const kCollectionCell = @"MWMSearchTabbedCollectionViewCell";

typedef NS_ENUM(NSInteger, MWMSearchTabbedViewCell)
{
  MWMSearchTabbedViewCellHistory,
  MWMSearchTabbedViewCellCategories,
  MWMSearchTabbedViewCellCount
};

BOOL isOffsetInButton(CGFloat offset, MWMSearchTabButtonsView * button)
{
  CGRect const frame = button.frame;
  CGFloat const left = frame.origin.x;
  CGFloat const right = left + frame.size.width;
  return left <= offset && offset <= right;
}

@interface MWMSearchTabbedViewController () <UICollectionViewDataSource, UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView * tablesCollectionView;

@property (weak, nonatomic) MWMSearchTabButtonsView * selectedButton;

@property (nonatomic) MWMSearchHistoryManager * historyManager;
@property (nonatomic) MWMSearchCategoriesManager * categoriesManager;

@property (nonatomic) BOOL isRotating;

@end

@implementation MWMSearchTabbedViewController

- (instancetype)init
{
  self = [super init];
  if (self)
    [self setupDataSources];
  return self;
}

- (void)viewDidLoad
{
  [super viewDidLoad];
  [self setupCollectionView];
  self.selectedButtonTag = 0;
}

- (void)viewWillAppear:(BOOL)animated
{
  [self.tablesCollectionView reloadData];
  [self refreshScrollPosition];
  [super viewWillAppear:animated];
}

#pragma mark - Layout

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                                duration:(NSTimeInterval)duration
{
  self.isRotating = YES;
  [UIView animateWithDuration:duration animations:^
  {
    [self refreshScrollPosition];
  }
  completion:^(BOOL finished)
  {
    [self refreshScrollPosition];
    self.isRotating = NO;
  }];
}

- (void)viewWillTransitionToSize:(CGSize)size
       withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
  self.isRotating = YES;
  [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
  {
    [self refreshScrollPosition];
  }
  completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
  {
    self.isRotating = NO;
  }];
}

#pragma mark - Setup

- (void)setupCollectionView
{
  [self.tablesCollectionView registerNib:[UINib nibWithNibName:kCollectionCell bundle:nil]
              forCellWithReuseIdentifier:kCollectionCell];
  ((MWMSearchTabbedViewLayout *)self.tablesCollectionView.collectionViewLayout).tablesCount =
      MWMSearchTabbedViewCellCount;
}

- (void)setupDataSources
{
  self.categoriesManager = [[MWMSearchCategoriesManager alloc] init];
  self.historyManager = [[MWMSearchHistoryManager alloc] init];
}

- (void)tabButtonPressed:(MWMSearchTabButtonsView *)sender
{
  dispatch_async(dispatch_get_main_queue(), ^
  {
    [self.tablesCollectionView
        scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:sender.tag inSection:0]
               atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
                       animated:YES];
  });
}

- (void)updateScrollPosition:(CGFloat)position
{
  self.scrollIndicatorOffset.constant = nearbyint(position);
  CGFloat const btnMid = position + 0.5 * self.scrollIndicator.width;
  if (self.selectedButton && isOffsetInButton(btnMid, self.selectedButton))
    return;
  [self.tabButtons enumerateObjectsUsingBlock:^(MWMSearchTabButtonsView * btn, NSUInteger idx, BOOL *stop)
  {
    if (isOffsetInButton(btnMid, btn))
    {
      self.selectedButton = btn;
      *stop = YES;
    }
  }];
}

- (void)refreshScrollPosition
{
  self.scrollIndicatorOffset.constant = nearbyint(self.selectedButton.minX);
  [self tabButtonPressed:self.selectedButton];
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView
     numberOfItemsInSection:(NSInteger)section
{
  return MWMSearchTabbedViewCellCount;
}

- (nonnull UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView
                          cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
  MWMSearchTabbedCollectionViewCell * cell =
      [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionCell
                                                forIndexPath:indexPath];
  MWMSearchTabbedViewCell cellType = static_cast<MWMSearchTabbedViewCell>(indexPath.item);
  switch (cellType)
  {
    case MWMSearchTabbedViewCellHistory:
      [self.historyManager attachCell:cell];
      break;
    case MWMSearchTabbedViewCellCategories:
      [self.categoriesManager attachCell:cell];
      break;
    default:
      break;
  }
  return cell;
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(nonnull UIScrollView *)scrollView
{
  if (self.isRotating)
    return;
  CGFloat const scrollOffset = scrollView.contentOffset.x;
  CGFloat const indWidth = self.scrollIndicator.width;
  CGFloat const scrWidth = self.view.width;
  [self updateScrollPosition:scrollOffset * indWidth / scrWidth];
}

#pragma mark - Properties

- (void)setSelectedButton:(MWMSearchTabButtonsView *)selectedButton
{
  [self.tabButtons enumerateObjectsUsingBlock:^(MWMSearchTabButtonsView * btn, NSUInteger idx, BOOL *stop)
  {
    btn.selected = NO;
  }];
  _selectedButton = selectedButton;
  selectedButton.selected = YES;
}

- (void)setDelegate:(id<MWMSearchTabbedViewProtocol>)delegate
{
  _delegate = self.categoriesManager.delegate = self.historyManager.delegate = delegate;
}

- (void)setSelectedButtonTag:(NSInteger)selectedButtonTag
{
  [self.tabButtons enumerateObjectsUsingBlock:^(MWMSearchTabButtonsView * btn, NSUInteger idx, BOOL * stop)
  {
    if (btn.tag == selectedButtonTag)
    {
      self.selectedButton = btn;
      *stop = YES;
    }
  }];
  [self updateScrollPosition:self.selectedButton.minX];
  [self.tablesCollectionView
      scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:selectedButtonTag inSection:0]
             atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
                     animated:NO];
}

- (NSInteger)selectedButtonTag
{
  return self.selectedButton.tag;
}

@end