forked from TelegramMessenger/Telegram-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTGBotKeyboardController.m
106 lines (79 loc) · 2.54 KB
/
TGBotKeyboardController.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
#import "TGBotKeyboardController.h"
#import "TGWatchCommon.h"
#import "TGBridgeBotReplyMarkup.h"
#import "WKInterfaceTable+TGDataDrivenTable.h"
#import "TGBotKeyboardButtonController.h"
NSString *const TGBotKeyboardControllerIdentifier = @"TGBotKeyboardController";
@implementation TGBotKeyboardControllerContext
@end
@interface TGBotKeyboardController () <TGTableDataSource>
{
TGBotKeyboardControllerContext *_context;
TGBridgeBotReplyMarkup *_replyMarkup;
}
@end
@implementation TGBotKeyboardController
- (instancetype)init
{
self = [super init];
if (self != nil)
{
[self.table _setInitialHidden:true];
self.table.tableDataSource = self;
}
return self;
}
- (void)configureWithContext:(TGBotKeyboardControllerContext *)context
{
_context = context;
_replyMarkup = context.replyMarkup;
[self.table reloadData];
}
#pragma mark -
- (NSUInteger)numberOfRowsInTable:(WKInterfaceTable *)table section:(NSUInteger)section
{
return [self numberOfAvailableButtons];
}
- (Class)table:(WKInterfaceTable *)table rowControllerClassAtIndexPath:(TGIndexPath *)indexPath
{
return [TGBotKeyboardButtonController class];
}
- (void)table:(WKInterfaceTable *)table updateRowController:(TGBotKeyboardButtonController *)controller forIndexPath:(TGIndexPath *)indexPath
{
TGBridgeBotReplyMarkupButton *button = [self buttonForRow:indexPath.row];
[controller updateWithButton:button];
}
- (void)table:(WKInterfaceTable *)table didSelectRowAtIndexPath:(TGIndexPath *)indexPath
{
[self dismissController];
TGBridgeBotReplyMarkupButton *button = [self buttonForRow:indexPath.row];
if (_context.completionBlock != nil)
_context.completionBlock(button.text);
}
#pragma mark -
- (TGBridgeBotReplyMarkupButton *)buttonForRow:(NSUInteger)row
{
NSRange currentRange = NSMakeRange(0, 0);
for (TGBridgeBotReplyMarkupRow *markupRow in _replyMarkup.rows)
{
NSArray *buttons = markupRow.buttons;
currentRange = NSMakeRange(currentRange.location + currentRange.length, buttons.count);
NSInteger transposedRow = row - currentRange.location;
if (transposedRow >= 0 && transposedRow < currentRange.length)
return buttons[transposedRow];
}
return nil;
}
- (NSUInteger)numberOfAvailableButtons
{
NSUInteger count = 0;
for (TGBridgeBotReplyMarkupRow *row in _replyMarkup.rows)
count += row.buttons.count;
return count;
}
#pragma mark -
+ (NSString *)identifier
{
return TGBotKeyboardControllerIdentifier;
}
@end