forked from TelegramMessenger/Telegram-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTGBotCommandController.m
149 lines (114 loc) · 4.12 KB
/
TGBotCommandController.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
#import "TGBotCommandController.h"
#import <WatchCommonWatch/WatchCommonWatch.h>
#import "TGWatchCommon.h"
#import <SSignalKit/SSignalKit.h>
#import "WKInterfaceTable+TGDataDrivenTable.h"
#import "TGUserRowController.h"
NSString *const TGBotCommandControllerIdentifier = @"TGBotCommandController";
NSString *const TGBotCommandKey = @"command";
NSString *const TGBotCommandUserKey = @"user";
NSString *const TGBotCommandListKey = @"list";
@implementation TGBotCommandControllerContext
@end
@interface TGBotCommandController () <TGTableDataSource>
{
SMetaDisposable *_commandDisposable;
NSArray *_commandList;
TGBotCommandControllerContext *_context;
}
@end
@implementation TGBotCommandController
- (instancetype)init
{
self = [super init];
if (self != nil)
{
_commandDisposable = [[SMetaDisposable alloc] init];
[self.table _setInitialHidden:true];
self.table.tableDataSource = self;
}
return self;
}
- (void)dealloc
{
[_commandDisposable dispose];
}
- (void)configureWithContext:(TGBotCommandControllerContext *)context
{
_context = context;
__weak TGBotCommandController *weakSelf = self;
[_commandDisposable setDisposable:[[context.commandListSignal deliverOn:[SQueue mainQueue]] startWithNext:^(NSArray *next)
{
__strong TGBotCommandController *strongSelf = weakSelf;
if (strongSelf == nil)
return;
strongSelf->_commandList = next;
[strongSelf performInterfaceUpdate:^(bool animated)
{
__strong TGBotCommandController *strongSelf = weakSelf;
if (strongSelf == nil)
return;
strongSelf.activityIndicator.hidden = true;
[strongSelf.table reloadData];
strongSelf.table.hidden = false;
}];
}]];
}
#pragma mark -
- (Class)table:(WKInterfaceTable *)table rowControllerClassAtIndexPath:(TGIndexPath *)indexPath
{
return [TGUserRowController class];
}
- (NSUInteger)numberOfRowsInTable:(WKInterfaceTable *)table section:(NSUInteger)section
{
return [self numberOfAvailableCommands];
}
- (void)table:(WKInterfaceTable *)table updateRowController:(TGUserRowController *)controller forIndexPath:(TGIndexPath *)indexPath
{
NSDictionary *dict = [self dictionaryForRow:indexPath.row];
TGBridgeBotCommandInfo *commandInfo = dict[TGBotCommandKey];
TGBridgeUser *botUser = dict[TGBotCommandUserKey];
[controller updateWithBotCommandInfo:commandInfo botUser:botUser context:_context.context];
}
- (void)table:(WKInterfaceTable *)table didSelectRowAtIndexPath:(TGIndexPath *)indexPath
{
[self dismissController];
NSDictionary *dict = [self dictionaryForRow:indexPath.row];
TGBridgeBotCommandInfo *commandInfo = dict[TGBotCommandKey];
TGBridgeUser *botUser = dict[TGBotCommandUserKey];
bool isSingleBot = (_commandList.count == 1);
NSString *mention = isSingleBot ? @"" : [NSString stringWithFormat:@"@%@", botUser.userName];
NSString *command = [NSString stringWithFormat:@"/%@%@", commandInfo.command, mention];
if (_context.completionBlock != nil)
_context.completionBlock(command);
}
- (NSDictionary *)dictionaryForRow:(NSUInteger)row
{
NSRange currentRange = NSMakeRange(0, 0);
for (NSDictionary *dict in _commandList)
{
NSArray *commandList = dict[TGBotCommandListKey];
currentRange = NSMakeRange(currentRange.location + currentRange.length, commandList.count);
NSInteger transposedRow = row - currentRange.location;
if (transposedRow >= 0 && transposedRow < currentRange.length)
return @{ TGBotCommandUserKey: dict[TGBotCommandUserKey], TGBotCommandKey: commandList[transposedRow]};
}
return nil;
}
- (NSUInteger)numberOfAvailableCommands
{
NSUInteger count = 0;
for (NSDictionary *dict in _commandList)
{
id commandList = dict[TGBotCommandListKey];
if ([commandList isKindOfClass:[NSArray class]])
count += [commandList count];
}
return count;
}
#pragma mark -
+ (NSString *)identifier
{
return TGBotCommandControllerIdentifier;
}
@end