forked from akahippac/Barmoji
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBarmojiCollectionView.m
176 lines (135 loc) · 7 KB
/
BarmojiCollectionView.m
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
//
// BarmojiCollectionView.m
// Barmoji
//
// Created by Juan Carlos Perez <[email protected]> 01/16/2018
// © CP Digital Darkroom <[email protected]> All rights reserved.
//
#import "Barmoji.h"
#import "BarmojiCollectionView.h"
#import "BarmojiHapticsManager.h"
#import "BarmojiEmojiCell.h"
@interface BarmojiCollectionView () <UICollectionViewDataSource, UICollectionViewDelegate>
@property (assign, nonatomic) UICollectionViewScrollDirection direction;
@property (assign, nonatomic) BOOL replacingPredictiveBar;
@property (assign, nonatomic) BOOL useCustomEmojis;
@property (strong, nonatomic) NSArray *customEmojis;
@property (strong, nonatomic) NSArray *recentEmojis;
@property (assign, nonatomic) int emojiPerRow;
@property (strong, nonatomic) UIKeyboardEmojiKeyDisplayController *emojiManager;
@end
@implementation BarmojiCollectionView
- (instancetype)initForPredictiveBar:(BOOL)forPredictive; {
NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/private/var/mobile/Library/Preferences/com.cpdigitaldarkroom.barmoji.plist"];
int emojiSource = ([prefs objectForKey:@"EmojiSource"] ? [[prefs objectForKey:@"EmojiSource"] intValue] : 1);
_emojiPerRow = ([prefs objectForKey:@"BarmojiEmojiPerRow"] ? [[prefs objectForKey:@"BarmojiEmojiPerRow"] intValue] : 6);
_direction = ([[prefs objectForKey:@"BarmojiScrollDirection"] ?: @(UICollectionViewScrollDirectionHorizontal) integerValue]);
_replacingPredictiveBar = forPredictive;
_useCustomEmojis = (emojiSource == 2);
if (_useCustomEmojis) {
NSString *emojiString = ([prefs objectForKey:@"CustomEmojis"] ? [prefs objectForKey:@"CustomEmojis"] : @"");
NSMutableArray *emojis = [NSMutableArray new];
[emojiString enumerateSubstringsInRange:NSMakeRange(0, emojiString.length)
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
[emojis addObject:substring];
}];
self.customEmojis = emojis;
} else {
self.emojiManager = [[NSClassFromString(@"UIKeyboardEmojiKeyDisplayController") alloc] init];
self.recentEmojis = [self.emojiManager recents];
}
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.scrollDirection = self.replacingPredictiveBar ? self.direction : UICollectionViewScrollDirectionHorizontal;
flowLayout.minimumInteritemSpacing = 0;
flowLayout.minimumLineSpacing = 0;
self = [super initWithFrame:CGRectZero collectionViewLayout:flowLayout];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.delegate = self;
self.dataSource = self;
self.showsVerticalScrollIndicator = NO;
self.showsHorizontalScrollIndicator = NO;
self.pagingEnabled = YES;
[self registerClass:NSClassFromString(@"BarmojiEmojiCell") forCellWithReuseIdentifier:@"kEmojiCellIdentifier"];
[self rotationUpdate:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rotationUpdate:) name:UIDeviceOrientationDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadLayout:) name:@"barmoji_reloadLayout" object:nil];
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)reloadLayout:(NSNotification *)note {
[self reloadData];
}
- (void)rotationUpdate:(NSNotification *)notification {
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
BOOL landscape = UIInterfaceOrientationIsLandscape(orientation);
/*
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
BOOL deviceLandscape = UIDeviceOrientationIsLandscape(deviceOrientation);
BOOL isSpringBoard = NO;
NSArray *args = [[NSClassFromString(@"NSProcessInfo") processInfo] arguments];
if (args.count != 0) {
NSString *executablePath = args[0];
if (executablePath) {
NSString *processName = [executablePath lastPathComponent];
isSpringBoard = [processName isEqualToString:@"SpringBoard"];
}
}
//NSLog(@"miroo: orientation is %d and deviceOrientation is %d", orientation, deviceOrientation);
// We don't want it to disappear in landscape if we're replacing the predictive bar
if (isSpringBoard && !self.replacingPredictiveBar && !deviceLandscape) {
self.alpha = 0;
} else if (landscape && !self.replacingPredictiveBar) {
self.alpha = 0;
} else {
self.alpha = 1;
}
*/
self.alpha = landscape && !self.replacingPredictiveBar ? 0 : 1;
// Landscape gets 12 emojis in the predictive bar,
// Portrait gets 8 in the predictive bar or 6 on the bottom
//self.emojiPerRow = (landscape) ? 12 : ((self.replacingPredictiveBar || self.fullWidth ) ? 8 : 6);
[self reloadData];
}
#pragma mark - UICollectionViewDelegate
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
BarmojiEmojiCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"kEmojiCellIdentifier" forIndexPath:indexPath];
if (_useCustomEmojis) {
NSString *emojiString = self.customEmojis[indexPath.row];
cell.emoji = [UIKeyboardEmoji emojiWithString:emojiString withVariantMask:0];
} else {
cell.emoji = _recentEmojis[indexPath.row];
}
return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return _useCustomEmojis ? self.customEmojis.count : _recentEmojis.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGFloat useableWidth = collectionView.frame.size.width / self.emojiPerRow;
return CGSizeMake(useableWidth, 30);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
if (self.replacingPredictiveBar) {
return UIEdgeInsetsZero;
}
return UIEdgeInsetsMake(22, 0, 0, 0);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if (self.feedbackType != 7) {
[[BarmojiHapticsManager sharedManager] actuateHapticsForType:self.feedbackType];
}
UIKeyboardEmoji *pressedEmoji;
if (_useCustomEmojis) {
NSString *emojiString = self.customEmojis[indexPath.row];
pressedEmoji = [UIKeyboardEmoji emojiWithString:emojiString withVariantMask:0];
} else {
pressedEmoji = [self.emojiManager recents][indexPath.row];
}
[[NSClassFromString(@"UIKeyboardImpl") activeInstance] insertText:pressedEmoji.emojiString];
}
@end