-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTABaseHeaderView.m
More file actions
132 lines (108 loc) · 4.23 KB
/
STABaseHeaderView.m
File metadata and controls
132 lines (108 loc) · 4.23 KB
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
//
// HeaderView.m
// STACollapsableTable
//
// Created by Aaron Jubbal on 5/17/16.
// Copyright © 2016 Aaron Jubbal. All rights reserved.
//
#import "STABaseHeaderView.h"
#import <ReactiveCocoa/ReactiveCocoa.h>
@interface STABaseHeaderView ()
@property (nonatomic, strong) UIImageView *collapsedStatusImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, weak) STACollapsableTableModel *tableModel;
@property (nonatomic, assign, readwrite) NSInteger section;
@end
@implementation STABaseHeaderView
+ (instancetype)createHeaderInSection:(NSInteger)section
fromModel:(STACellModel *)cellModel
tableModel:(STACollapsableTableModel *)tableModel
nibName:(NSString *)nibName
userInfo:(NSDictionary *)userInfo
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil];
STABaseHeaderView *headerView = [topLevelObjects objectAtIndex:0];
headerView.section = section;
headerView.cellModel = cellModel;
headerView.titleLabel.text = cellModel.title;
headerView.tableModel = tableModel;
if (!tableModel.isSearching) {
cellModel.isSearchResult = YES;
}
return headerView;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
UITapGestureRecognizer *singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(tapGestureHandler:)];
singleTapRecognizer.numberOfTouchesRequired = 1;
singleTapRecognizer.numberOfTapsRequired = 1;
[self addGestureRecognizer:singleTapRecognizer];
}
return self;
}
#pragma mark - Setters
- (void)setCellModel:(STACellModel *)cellModel {
_cellModel = cellModel;
@weakify(self);
[[RACObserve(self.cellModel, isSearchResult)
combinePreviousWithStart:@(self.cellModel.isSearchResult)
reduce:^id(NSNumber *previousValue, NSNumber *currentValue)
{
return @(([previousValue boolValue] != [currentValue boolValue]));
}] subscribeNext:^(NSNumber *statusChanged) {
@strongify(self);
if ([statusChanged boolValue]) {
dispatch_async(dispatch_get_main_queue(), ^{
[self isSearchResultStateChanged:cellModel.isSearchResult];
});
}
}];
[self updateImageView];
[self isSearchResultStateChanged:self.cellModel.isSearchResult];
}
#pragma mark - Public Methods
- (void)headerTapped {
[self animateUpdateOfImageViewWithDuration:0.33];
}
- (void)animateUpdateOfImageViewWithDuration:(NSTimeInterval)duration {
[UIView animateWithDuration:duration animations:^{
[self updateImageView];
} completion:^(BOOL finished) {
if (finished) {
[self updateImageView];
}
}];
}
- (void)updateImageView {
if (self.cellModel.isExpanded) {
self.collapsedStatusImageView.transform = CGAffineTransformMakeRotation(M_PI / 2);
} else {
self.collapsedStatusImageView.transform = CGAffineTransformMakeRotation(0);
}
}
- (void)isSearchResultStateChanged:(BOOL)isSearchResult {
if (self.titleLabel) {
self.titleLabel.alpha = isSearchResult ? 1.0 : 0.5;
}
}
#pragma mark - Private Methods
- (void)tapGestureHandler:(UITapGestureRecognizer *)gesture {
if (!self.cellModel.children.count) {
return; // collapsing/expansion can't be done on a cell without children
}
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:NSNotFound inSection:self.section];
if (self.cellModel.isExpanded) { // collapse
if (self.cellModel.tableModel.isSearching) {
if (self.cellModel.descendantsInSearchResults < self.cellModel.children.count) {
[self.tableModel collapse:self.cellModel fromRowFromIndexPath:indexPath];
}
} else {
[self.tableModel collapse:self.cellModel fromRowFromIndexPath:indexPath];
}
} else { // expand
[self.tableModel expand:self.cellModel fromRowFromIndexPath:indexPath];
}
[self headerTapped];
}
@end