-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasources.py
331 lines (279 loc) · 12.5 KB
/
datasources.py
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import json
import ui
import sound
import dialogs
import requests
import console
sound.set_honors_silent_switch(False)
def save_tags(tags):
with open('tags.json', 'w') as f:
json.dump(tags, f, indent=4)
def save_history(history):
with open('history.json', 'w') as f:
json.dump({d[0].isoformat(): d[1] for d in history}, f, indent=4)
def get_hymn_cell(hymn, tags):
cell = ui.TableViewCell('subtitle')
cell.text_label.text = hymn['title']
cell.detail_text_label.text = hymn['#']
cell.accessory_type = 'disclosure_indicator'
for tag in tags:
if hymn['#'] in tag['hymns']:
cell.background_color = tag['color']
break
return cell
@ui.in_background
def load_mp3(hymn, btn):
with open('tmp.mp3', 'wb') as tmp:
try:
console.show_activity()
mp3 = requests.get(f'https://hymnsing.ivoah.net/audio/Th2_{hymn:0>3}.mp3', verify=False)
tmp.write(mp3.content)
except requests.ConnectionError:
return
finally:
console.hide_activity()
player = sound.Player('tmp.mp3')
def play_pause(sender):
if player.playing:
player.pause()
sender.image = ui.Image('iob:play_32')
else:
player.play()
sender.image = ui.Image('iob:pause_32')
def finished():
btn.image = ui.Image('iob:play_32')
#player.finished_handler = finished
btn.action = play_pause
btn.enabled = True
class HymnsDataSource:
def __init__(self, hymns, tags, history):
self.set_hymns(hymns)
self.tags = tags
self.history = history
def set_hymns(self, hymns):
self.sections = []
for hymn in hymns:
if len(self.sections) == 0 or '{section}: {subsection}'.format(**hymn) != self.sections[-1][0]:
self.sections.append(('{section}: {subsection}'.format(**hymn), []))
self.sections[-1][1].append(hymn)
def tableview_number_of_sections(self, tableview):
# Return the number of sections (defaults to 1)
return len(self.sections)
def tableview_number_of_rows(self, tableview, section):
# Return the number of rows in the section
return len(self.sections[section][1])
def tableview_cell_for_row(self, tableview, section, row):
# Create and return a cell for the given section/row
hymn = self.sections[section][1][row]
cell = get_hymn_cell(hymn, self.tags)
return cell
def tableview_title_for_header(self, tableview, section):
# Return a title for the given section.
# If this is not implemented, no section headers will be shown.
return self.sections[section][0]
def tableview_did_select(self, tableview, section, row):
# Called when a row was selected.
hymn = self.sections[section][1][row]
tableview.selected_row = -1
detail_view = ui.TableView('grouped')
detail_view.name = hymn['#']
detail_view.data_source = DetailDataSource(hymn, self.tags, self.history, tableview.reload_data)
detail_view.delegate = detail_view.data_source
btn = ui.ButtonItem(
image=ui.Image('iob:play_32'),
enabled=False
)
# load_mp3(hymn['#'], btn)
detail_view.right_button_items = [btn]
tableview.navigation_view.push_view(detail_view)
class DetailDataSource:
def __init__(self, hymn, tags, history, reload_parent):
self.hymn = hymn
self.tags = tags
self.history = history
self.reload_parent = reload_parent
def tableview_number_of_sections(self, tableview):
# Return the number of sections (defaults to 1)
return 3
def tableview_number_of_rows(self, tableview, section):
# Return the number of rows in the section
return [
0,
len(self.tags) + 1,
len(list(filter(lambda d: self.hymn['#'] in d[1], self.history))) + 1
][section]
def tableview_cell_for_row(self, tableview, section, row):
# Create and return a cell for the given section/row
hist = [date[0] for date in self.history if self.hymn['#'] in date[1]]
cell = ui.TableViewCell()
if section == 1:
if row == len(self.tags):
cell.text_label.text = 'Edit tags...'
cell.accessory_type = 'disclosure_indicator'
else:
tag = self.tags[row]
cell.text_label.text = tag['name']
cell.background_color = tag['color']
if self.hymn['#'] in tag['hymns']:
cell.accessory_type = 'checkmark'
elif section == 2:
if row == 0:
cell.text_label.text = 'Add to history...'
else:
cell.text_label.text = hist[row - 1].strftime('%A %B %d, %Y')
cell.selectable = False
return cell
def tableview_title_for_header(self, tableview, section):
# Return a title for the given section.
# If this is not implemented, no section headers will be shown.
return [self.hymn['title'], 'Tags', 'History'][section]
@ui.in_background
def tableview_did_select(self, tableview, section, row):
# Called when a row was selected.
tableview.selected_row = -1
if section == 1:
if row == len(self.tags):
detail_view = ui.TableView('grouped')
def reload_parent():
tableview.reload_data()
self.reload_parent()
def toggle_editing(s):
detail_view.set_editing(not detail_view.editing, True)
@ui.in_background
def new_tag(s):
name = dialogs.input_alert('Tag name')
color = dialogs.input_alert('Tag color')
self.tags.append({'name': name, 'color': color, 'hymns': []})
save_tags(self.tags)
detail_view.reload_data()
reload_parent()
detail_view.name = 'Edit Tags'
detail_view.data_source = EditTagsDataSource(self.tags, reload_parent)
detail_view.delegate = detail_view.data_source
detail_view.left_button_items = [ui.ButtonItem('Edit', action=toggle_editing)]
detail_view.right_button_items = [ui.ButtonItem('New tag', action=new_tag)]
tableview.navigation_view.push_view(detail_view)
else:
tag = self.tags[row]
if self.hymn['#'] in tag['hymns']:
tag['hymns'].remove(self.hymn['#'])
else:
tag['hymns'].append(self.hymn['#'])
save_tags(self.tags)
elif section == 2 and row == 0:
date = dialogs.date_dialog()
if date is not None:
i = 0
while i < len(self.history) and self.history[i][0] > date:
i += 1
if i < len(self.history) and self.history[i][0] == date:
self.history[i][1].append(self.hymn['#'])
else:
self.history.insert(i, (date, [self.hymn['#']]))
save_history(self.history)
tableview.reload_data()
self.reload_parent()
class EditTagsDataSource:
def __init__(self, tags, reload_parent):
self.tags = tags
self.reload_parent = reload_parent
def tableview_number_of_sections(self, tableview):
# Return the number of sections (defaults to 1)
return 1
def tableview_number_of_rows(self, tableview, section):
# Return the number of rows in the section
return len(self.tags)
def tableview_cell_for_row(self, tableview, section, row):
# Create and return a cell for the given section/row
cell = ui.TableViewCell()
tag = self.tags[row]
cell.text_label.text = tag['name']
cell.background_color = tag['color']
return cell
def tableview_title_for_header(self, tableview, section):
# Return a title for the given section.
# If this is not implemented, no section headers will be shown.
return 'Tags'
def tableview_can_delete(self, tableview, section, row):
# Return True if the user should be able to delete the given row.
return True
def tableview_can_move(self, tableview, section, row):
# Return True if a reordering control should be shown for the given row (in editing mode).
return True
@ui.in_background
def tableview_delete(self, tableview, section, row):
# Called when the user confirms deletion of the given row.
dialogs.alert('Confirm delete', 'Are you sure you want to delete the tag?', 'Delete')
del self.tags[row]
save_tags(self.tags)
tableview.reload_data()
self.reload_parent()
def tableview_move_row(self, tableview, from_section, from_row, to_section, to_row):
# Called when the user moves a row with the reordering control (in editing mode).
self.tags.insert(to_row, self.tags.pop(from_row))
save_tags(self.tags)
self.reload_parent()
@ui.in_background
def tableview_did_select(self, tableview, section, row):
# Called when a row was selected.
tableview.selected_row = -1
name_or_color = dialogs.alert('Edit', '', 'Name', 'Color')
if name_or_color == 1:
name = dialogs.input_alert('Tag name', '', self.tags[row]['name'])
self.tags[row]['name'] = name
elif name_or_color == 2:
color = dialogs.input_alert('Tag color', '', self.tags[row]['color'])
self.tags[row]['color'] = color
save_tags(self.tags)
tableview.reload_data()
self.reload_parent()
class HistoryDataSource:
def __init__(self, hymns, tags, history, reload_parent):
self.hymns = hymns
self.tags = tags
self.history = history
self.reload_parent = reload_parent
def tableview_number_of_sections(self, tableview):
# Return the number of sections (defaults to 1)
return len(self.history)
def tableview_number_of_rows(self, tableview, section):
# Return the number of rows in the section
return len(self.history[section][1])
def tableview_cell_for_row(self, tableview, section, row):
# Create and return a cell for the given section/row
cell = get_hymn_cell(self.hymns[int(self.history[section][1][row]) - 1], self.tags)
return cell
def tableview_title_for_header(self, tableview, section):
# Return a title for the given section.
# If this is not implemented, no section headers will be shown.
return self.history[section][0].strftime('%A %B %d, %Y')
def tableview_can_delete(self, tableview, section, row):
# Return True if the user should be able to delete the given row.
return True
def tableview_can_move(self, tableview, section, row):
# Return True if a reordering control should be shown for the given row (in editing mode).
return True
def tableview_delete(self, tableview, section, row):
# Called when the user confirms deletion of the given row.
del self.history[section][1][row]
if not self.history[section][1]: del self.history[section]
save_history(self.history)
tableview.reload_data()
def tableview_move_row(self, tableview, from_section, from_row, to_section, to_row):
# Called when the user moves a row with the reordering control (in editing mode).
self.history[to_section][1].insert(to_row, self.history[from_section][1].pop(from_row))
if not self.history[from_section][1]: del self.history[from_section]
save_history(self.history)
tableview.reload_data()
def tableview_did_select(self, tableview, section, row):
# Called when a row was selected.
def reload_parent():
tableview.reload_data()
self.reload_parent()
hymn = self.hymns[int(self.history[section][1][row]) - 1]
tableview.selected_row = -1
detail_view = ui.TableView('grouped')
detail_view.name = hymn['title']
detail_view.data_source = DetailDataSource(hymn, self.tags, self.history, reload_parent)
detail_view.delegate = detail_view.data_source
tableview.navigation_view.push_view(detail_view)