forked from TelegramMessenger/Telegram-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTGInputController.m
178 lines (154 loc) · 5.01 KB
/
TGInputController.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
177
178
#import "TGInputController.h"
#import <WatchCommonWatch/WatchCommonWatch.h>
#import "TGWatchCommon.h"
#import "TGInterfaceController.h"
#import "TGFileCache.h"
#import "TGExtensionDelegate.h"
#import "TGBridgePresetsSignals.h"
@implementation TGInputController
+ (void)presentPlainInputControllerForInterfaceController:(TGInterfaceController *)interfaceController completion:(void (^)(NSString *))completion;
{
[interfaceController presentTextInputControllerWithSuggestions:nil allowedInputMode:WKTextInputModePlain completion:^(NSArray *results)
{
if (completion != nil && results.count > 0 && [results.firstObject isKindOfClass:[NSString class]])
completion(results.firstObject);
}];
}
+ (void)presentInputControllerForInterfaceController:(TGInterfaceController *)interfaceController suggestionsForText:(NSString *)text completion:(void (^)(NSString *))completion
{
[interfaceController presentTextInputControllerWithSuggestions:[self suggestionsForText:text] allowedInputMode:WKTextInputModeAllowEmoji completion:^(NSArray *results)
{
if (completion != nil && results.count > 0 && [results.firstObject isKindOfClass:[NSString class]])
completion(results.firstObject);
}];
}
+ (void)presentAudioControllerForInterfaceController:(TGInterfaceController *)interfaceController completion:(void (^)(int64_t, int32_t, NSURL *))completion
{
NSDictionary *options = @
{
WKAudioRecorderControllerOptionsActionTitleKey: TGLocalized(@"Watch.Compose.Send"),
};
int64_t randomId = 0;
arc4random_buf(&randomId, sizeof(int64_t));
NSURL *url = [[TGExtensionDelegate instance].audioCache urlForKey:[NSString stringWithFormat:@"%lld", randomId]];
[interfaceController presentAudioRecorderControllerWithOutputURL:url preset:WKAudioRecorderPresetWideBandSpeech options:options completion:^(BOOL didSave, NSError * _Nullable error)
{
WKAudioFileAsset *asset = [WKAudioFileAsset assetWithURL:url];
if (didSave && !error)
completion(randomId, (int32_t)asset.duration, url);
}];
}
+ (NSArray *)suggestionsForText:(NSString *)text
{
return [self customSuggestions];
}
+ (NSArray *)customSuggestions
{
NSArray *presetIdentifiers = [self presetIdentifiers];
NSMutableArray *suggestions = [[NSMutableArray alloc] init];
NSDictionary *customPresets = [self customPresets];
for (NSString *identifier in presetIdentifiers)
{
NSString *preset = customPresets[identifier];
if (preset == nil)
preset = TGLocalized([NSString stringWithFormat:@"Watch.Suggestion.%@", identifier]);
[suggestions addObject:preset];
}
return suggestions;
}
+ (NSDictionary *)customPresets
{
NSData *data = [NSData dataWithContentsOfURL:[TGBridgePresetsSignals presetsURL]];
@try
{
id presets = [NSKeyedUnarchiver unarchiveObjectWithData:data];
if ([presets isKindOfClass:[NSDictionary class]])
return presets;
return nil;
}
@catch (NSException *exception)
{
return nil;
}
}
+ (NSArray *)presetIdentifiers
{
return @
[
@"OK",
@"Thanks",
@"WhatsUp",
@"TalkLater",
@"CantTalk",
@"HoldOn",
@"BRB",
@"OnMyWay"
];
}
+ (NSArray *)composeSuggestions
{
static NSArray *suggestions;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^
{
suggestions = @
[
TGLocalized(@"Watch.Suggestion.WhatsUp"),
TGLocalized(@"Watch.Suggestion.OnMyWay"),
TGLocalized(@"Watch.Suggestion.OK"),
TGLocalized(@"Watch.Suggestion.CantTalk"),
TGLocalized(@"Watch.Suggestion.CallSoon"),
TGLocalized(@"Watch.Suggestion.Thanks")
];
});
return suggestions;
}
+ (NSArray *)generalSuggestions
{
static NSArray *suggestions;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^
{
suggestions = @
[
TGLocalized(@"Watch.Suggestion.OK"),
TGLocalized(@"Watch.Suggestion.Thanks"),
TGLocalized(@"Watch.Suggestion.WhatsUp")
];
});
return suggestions;
}
+ (NSArray *)yesNoSuggestions
{
static NSArray *suggestions;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^
{
suggestions = @
[
TGLocalized(@"Watch.Suggestion.Yes"),
TGLocalized(@"Watch.Suggestion.No"),
TGLocalized(@"Watch.Suggestion.Absolutely"),
TGLocalized(@"Watch.Suggestion.Nope")
];
});
return suggestions;
}
+ (NSArray *)laterSuggestions
{
static NSArray *suggestions;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^
{
suggestions = @
[
TGLocalized(@"Watch.Suggestion.TalkLater"),
TGLocalized(@"Watch.Suggestion.CantTalk"),
TGLocalized(@"Watch.Suggestion.HoldOn"),
TGLocalized(@"Watch.Suggestion.BRB"),
TGLocalized(@"Watch.Suggestion.OnMyWay")
];
});
return suggestions;
}
@end