1
|
|
|
from callbaker import callback_from_info |
2
|
|
|
from keyboa import Keyboa |
3
|
|
|
|
4
|
|
|
from app.bot.telegram import MIN_NUMBER_OF_BUTTONS |
5
|
|
|
from app.bot.telegram.variables import ( |
6
|
|
|
mark_entity, |
7
|
|
|
entity_predy, |
8
|
|
|
mark_action, |
9
|
|
|
action_predy_kb_cpx_show, |
10
|
|
|
mark_record_id, |
11
|
|
|
mark_slice_start, |
12
|
|
|
t, |
13
|
|
|
cbd, |
14
|
|
|
action_predy_kb_cpx_hide, |
15
|
|
|
action_predy_send_card, |
16
|
|
|
) |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
class WordKeyboard: |
20
|
|
|
def __init__(self, word): |
21
|
|
|
self.word = word |
22
|
|
|
|
23
|
|
|
def _keyboard_navi(self, index_start: int, index_end: int, delimiter: int): |
24
|
|
|
""" |
25
|
|
|
:param index_start: |
26
|
|
|
:param index_end: |
27
|
|
|
:param delimiter: |
28
|
|
|
:return: |
29
|
|
|
""" |
30
|
|
|
text_arrow_back = "\U0000276E" * 2 |
31
|
|
|
text_arrow_forward = "\U0000276F" * 2 |
32
|
|
|
button_back, button_forward = None, None |
33
|
|
|
|
34
|
|
|
common_data = { |
35
|
|
|
mark_entity: entity_predy, |
36
|
|
|
mark_action: action_predy_kb_cpx_show, |
37
|
|
|
mark_record_id: self.word.id, |
38
|
|
|
} |
39
|
|
|
if index_start != 0: |
40
|
|
|
cbd_predy_kb_cpx_back = { |
41
|
|
|
**common_data, |
42
|
|
|
mark_slice_start: index_start - delimiter, |
43
|
|
|
} |
44
|
|
|
button_back = { |
45
|
|
|
t: text_arrow_back, |
46
|
|
|
cbd: callback_from_info(cbd_predy_kb_cpx_back), |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if index_end != len(self.word.complexes): |
50
|
|
|
cbd_predy_kb_cpx_forward = { |
51
|
|
|
**common_data, |
52
|
|
|
mark_slice_start: index_end, |
53
|
|
|
} |
54
|
|
|
button_forward = { |
55
|
|
|
t: text_arrow_forward, |
56
|
|
|
cbd: callback_from_info(cbd_predy_kb_cpx_forward), |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
nav_row = [b for b in [button_back, button_forward] if b] |
60
|
|
|
return Keyboa(nav_row, items_in_row=2)() |
61
|
|
|
|
62
|
|
|
def _keyboard_hide(self, total_number_of_complexes: int): |
63
|
|
|
""" |
64
|
|
|
:param total_number_of_complexes: |
65
|
|
|
:return: |
66
|
|
|
""" |
67
|
|
|
text_cpx_hide = f"Hide Complex{'es' if total_number_of_complexes > 1 else ''}" |
68
|
|
|
cbd_predy_kb_cpx_hide = { |
69
|
|
|
mark_entity: entity_predy, |
70
|
|
|
mark_action: action_predy_kb_cpx_hide, |
71
|
|
|
mark_record_id: self.word.id, |
72
|
|
|
} |
73
|
|
|
button_predy_kb_cpx_hide = [ |
74
|
|
|
{t: text_cpx_hide, cbd: callback_from_info(cbd_predy_kb_cpx_hide)}, |
75
|
|
|
] |
76
|
|
|
return Keyboa(button_predy_kb_cpx_hide)() |
77
|
|
|
|
78
|
|
|
def _keyboard_show(self, total_number_of_complexes: int): |
79
|
|
|
""" |
80
|
|
|
:param total_number_of_complexes: |
81
|
|
|
:return: |
82
|
|
|
""" |
83
|
|
|
text_cpx_show = ( |
84
|
|
|
f"Show Complex{'es' if total_number_of_complexes > 1 else ''}" |
85
|
|
|
f" ({total_number_of_complexes})" |
86
|
|
|
) |
87
|
|
|
cbd_predy_kb_cpx_show = { |
88
|
|
|
mark_entity: entity_predy, |
89
|
|
|
mark_action: action_predy_kb_cpx_show, |
90
|
|
|
mark_record_id: self.word.id, |
91
|
|
|
} |
92
|
|
|
button_show = [ |
93
|
|
|
{t: text_cpx_show, cbd: callback_from_info(cbd_predy_kb_cpx_show)}, |
94
|
|
|
] |
95
|
|
|
return Keyboa.combine((Keyboa(button_show)(), kb_close())) |
96
|
|
|
|
97
|
|
|
@staticmethod |
98
|
|
|
def _get_delimiter(total_number_of_complexes: int): |
99
|
|
|
""" |
100
|
|
|
:param total_number_of_complexes: |
101
|
|
|
:return: |
102
|
|
|
""" |
103
|
|
|
allowed_range = list(range(MIN_NUMBER_OF_BUTTONS, MIN_NUMBER_OF_BUTTONS + 11)) |
104
|
|
|
lst = [(total_number_of_complexes % i, i) for i in allowed_range] |
105
|
|
|
delimiter = min(lst, key=lambda x: abs(x[0] - MIN_NUMBER_OF_BUTTONS))[1] |
106
|
|
|
for i in lst: |
107
|
|
|
if i[0] == 0: |
108
|
|
|
delimiter = i[1] |
109
|
|
|
break |
110
|
|
|
return delimiter |
111
|
|
|
|
112
|
|
|
@staticmethod |
113
|
|
|
def _keyboard_data(current_complexes: list): |
114
|
|
|
""" |
115
|
|
|
:param current_complexes: |
116
|
|
|
:return: |
117
|
|
|
""" |
118
|
|
|
cpx_items = [ |
119
|
|
|
{ |
120
|
|
|
t: cpx.name, |
121
|
|
|
cbd: callback_from_info( |
122
|
|
|
{ |
123
|
|
|
mark_entity: entity_predy, |
124
|
|
|
mark_action: action_predy_send_card, |
125
|
|
|
mark_record_id: cpx.id, |
126
|
|
|
} |
127
|
|
|
), |
128
|
|
|
} |
129
|
|
|
for cpx in current_complexes |
130
|
|
|
] |
131
|
|
|
return Keyboa(items=cpx_items, alignment=True, items_in_row=4)() |
132
|
|
|
|
133
|
|
|
def keyboard_cpx(self, show_list: bool = False, slice_start: int = 0): |
134
|
|
|
""" |
135
|
|
|
:param show_list: |
136
|
|
|
:param slice_start: |
137
|
|
|
:return: |
138
|
|
|
""" |
139
|
|
|
|
140
|
|
|
total_num_of_cpx = len(self.word.complexes) |
141
|
|
|
|
142
|
|
|
if not total_num_of_cpx: |
143
|
|
|
return kb_close() |
144
|
|
|
|
145
|
|
|
if not show_list: |
146
|
|
|
return self._keyboard_show(total_num_of_cpx) |
147
|
|
|
|
148
|
|
|
current_delimiter = self._get_delimiter(total_num_of_cpx) |
149
|
|
|
|
150
|
|
|
kb_cpx_hide = self._keyboard_hide(total_num_of_cpx) |
151
|
|
|
|
152
|
|
|
last_allowed_item = slice_start + current_delimiter |
153
|
|
|
slice_end = min(last_allowed_item, total_num_of_cpx) |
154
|
|
|
|
155
|
|
|
current_cpx_set = self.word.complexes[slice_start:slice_end] |
156
|
|
|
kb_cpx_data = self._keyboard_data(current_cpx_set) |
157
|
|
|
|
158
|
|
|
kb_cpx_nav = None |
159
|
|
|
|
160
|
|
|
if total_num_of_cpx > current_delimiter: |
161
|
|
|
kb_cpx_nav = self._keyboard_navi(slice_start, slice_end, current_delimiter) |
162
|
|
|
|
163
|
|
|
kb_combo = (kb_cpx_hide, kb_cpx_data, kb_cpx_nav, kb_close()) |
164
|
|
|
|
165
|
|
|
return Keyboa.combine(kb_combo) |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
def kb_close(): |
169
|
|
|
""" |
170
|
|
|
:return: |
171
|
|
|
""" |
172
|
|
|
return Keyboa({t: "Close", cbd: "close"})() |
173
|
|
|
|