This repository was archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmusic_picker.py
364 lines (299 loc) · 10.3 KB
/
music_picker.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
from kivy.lang import Builder
from kivy.metrics import sp
from kivy.utils import rgba
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.recycleview import RecycleView
from kivy.uix.modalview import ModalView
from kivy.properties import NumericProperty, StringProperty, ObjectProperty
from string_lines import StringLines
from mediastore_utils import MediastoreUtils, MS_FAIL
##################################################################
# MusicPicker is the root_class, it displays a list of Genres.
# On genre selection MusicPicker opens AlbumPicker passing genre_id,
# AlbumPicker displays a list of Albums.
# On album selection AlbumPicker opens TrackPicker passing album_id,
# TrackPicker displays a list of Tracks.
# On track selection TrackPicker updates the temp track list in MusicPicker.
# On MusicPicker close the track list is converted to a Uri list
# and returned in a callback.
##################################################################
#######################
# Music Picker
#######################
Builder.load_string('''
<MusicPicker>:
# RecycleView incorrectly ignores ModalView border,
# so explicity set border to zero.
border: (0,0,0,0)
MenuBackground
BoxLayout:
orientation: 'vertical'
GenresRV:
picker_root: root.picker_root
MenuRelativeImageButton:
size_hint_y: 0.1
pressed: root.quit_picker
source: 'icons/arrow_left_white.png'
<GenresRV>:
viewclass: 'GenreSelection'
RecycleBoxLayout:
default_size: None, dp(56)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
<GenreSelection>:
MenuBackGesturePadding
MenuTextButton:
text: root.genre_name
on_press: root.show_albums()
MenuBackGesturePadding
<MenuBackground@Widget>:
canvas.before:
Color:
rgba: rgba('#708090')
Rectangle:
pos: self.pos
size: self.size
<MenuTextButton@ButtonBehavior+Label>:
<MenuImageButton@ButtonBehavior+Image>:
<MenuRelativeImageButton@RelativeLayout>:
pressed: None
source: ''
MenuImageButton:
source: root.source
on_press: root.pressed()
fit_mode: 'contain'
size_hint: (None, 0.8)
width: self.height
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
# A Button Press may occur (usually occurs?) before a Back Gesture :(
# So a back gesture is passed to the Button's new child ModalView which
# immediately responds to the parent's gesture and closes.
# Creating an unexpected NOP. So pad viewclass edges.
<MenuBackGesturePadding@MenuBackground>:
width: self.height // 3
size_hint: (None, 1)
''')
class MusicPicker(ModalView):
picker_root = ObjectProperty()
def __init__(self, callback, **args):
super().__init__(**args)
self.picker_root = self
self.callback = callback
self.temp_track_list = []
if MS_FAIL:
self.quit_picker()
def on_pre_dismiss(self,*args):
uris = []
for track_id in self.temp_track_list:
uris.append(MediastoreUtils().id_to_uri(track_id))
self.callback(uris)
def quit_picker(self):
self.dismiss()
class GenresRV(RecycleView):
picker_root = ObjectProperty()
def on_picker_root(self, obj, items):
genres = MediastoreUtils().list_genres()
for g in genres:
g['picker_root'] = self.picker_root
self.data = genres
class GenreSelection(BoxLayout):
genre_id = NumericProperty()
genre_name = StringProperty()
picker_root = ObjectProperty()
def show_albums(self):
AlbumPicker(self.genre_id, self.picker_root).open()
#######################
# Album Picker
#######################
Builder.load_string('''
<AlbumPicker>:
border: (0,0,0,0)
MenuBackground
BoxLayout:
orientation: 'vertical'
Albums:
picker_root: root.picker_root
genre_id: root.genre_id
MenuRelativeImageButton:
size_hint_y: 0.1
pressed: root.quit_picker
source: 'icons/arrow_left_white.png'
<Albums>:
viewclass: 'AlbumSelection'
RecycleBoxLayout:
id: rbl
default_size: None, dp(56)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
<AlbumSelection>:
Image:
texture: root.album_art
fit_mode: 'contain'
width: self.height
size_hint: (None, 1)
MenuTextButton:
text: root.album_name
on_press: root.show_tracks()
MenuBackGesturePadding
''')
class AlbumPicker(ModalView):
genre_id = NumericProperty()
picker_root = ObjectProperty()
def __init__(self, genre_id, picker_root, **args):
super().__init__(**args)
self.genre_id = genre_id
self.picker_root = picker_root
def quit_picker(self):
self.dismiss()
class Albums(RecycleView):
genre_id = NumericProperty()
picker_root = ObjectProperty()
def on_picker_root(self, obj, items):
if self.genre_id:
self.read_mediastore()
def on_genre_id(self, obj, items):
if self.picker_root:
self.read_mediastore()
def art_callback(self, index, texture):
self.data[index]['album_art'] = texture
def read_mediastore(self):
# Don't know how long the label will be, so guess 85% of Window
# based on image on left and padding on right.
sl = StringLines()
max_cols = sl.estimate_columns(sp(16),round(Window.width * 0.85))
albums, arts = MediastoreUtils().list_albums_in_genre(self.genre_id)
for a in albums:
a['picker_root'] = self.picker_root
a['album_name'] = sl.multiline_string(a['album_name'], max_cols, 2)
self.data = albums
# resolution of 160 is picked for speed
MediastoreUtils().add_many_thumbnails(self.art_callback, arts, 160, 0)
class AlbumSelection(BoxLayout):
album_id = NumericProperty()
album_name = StringProperty()
album_art = ObjectProperty()
picker_root = ObjectProperty()
def show_tracks(self):
TrackPicker(self.album_id, self.picker_root).open()
#######################
# Track Picker
#######################
Builder.load_string('''
<TrackPicker>:
border: (0,0,0,0)
MenuBackground
BoxLayout:
orientation: 'vertical'
Label:
size_hint_y: 0.05
TrackHeader:
album_id: root.album_id
size_hint_y: 0.15
Label:
size_hint_y: 0.05
TracksRV:
picker_root: root.picker_root
album_id: root.album_id
MenuRelativeImageButton:
size_hint_y: 0.1
pressed: root.quit_picker
source: 'icons/arrow_left_white.png'
<TracksRV>:
viewclass: 'TrackSelection'
RecycleBoxLayout:
default_size: None, dp(56)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
<TrackSelection>:
MenuBackGesturePadding
MenuTextButton:
id: mtb
text: root.track_name
on_press: root.pick_item()
MenuBackGesturePadding
<TrackHeader>:
orientation: 'horizontal'
MenuBackGesturePadding
Image:
texture: root.album_art
fit_mode: 'contain'
width: self.height
size_hint: (None, 1)
BoxLayout:
orientation: 'vertical'
Label:
text: root.album_title
bold : True
Label:
text: root.album_artist
bold : True
MenuBackGesturePadding
''')
class TrackPicker(ModalView):
album_id = NumericProperty()
picker_root = ObjectProperty()
def __init__(self, album_id, picker_root, **args):
super().__init__(**args)
self.album_id = album_id
self.picker_root = picker_root
self.selected = []
def quit_picker(self):
self.dismiss()
class TrackHeader(BoxLayout):
album_id = NumericProperty()
album_title = StringProperty()
album_artist = StringProperty()
album_art = ObjectProperty()
def art_callback(self, texture):
self.album_art = texture
def on_album_id(self, album_id, items):
album, artist = MediastoreUtils().list_album_info(self.art_callback,
self.album_id)
sl = StringLines() # another estimate..
max_cols = sl.estimate_columns(sp(16),round(Window.width * 0.8))
self.album_title = sl.multiline_string(album, max_cols, 2)
self.album_artist = sl.multiline_string(artist, max_cols, 2)
class TracksRV(RecycleView):
album_id = NumericProperty()
picker_root = ObjectProperty()
def on_picker_root(self, obj, items):
if self.album_id:
self.read_mediastore()
def on_album_id(self, obj, items):
if self.picker_root:
self.read_mediastore()
def read_mediastore(self):
tracks = MediastoreUtils().list_tracks_in_album(self.album_id)
sl = StringLines() # another estimate..
max_cols = sl.estimate_columns(sp(16),round(Window.width * 0.9))
for t in tracks:
t['picker_root'] = self.picker_root
t['track_name'] = sl.multiline_string(t['track_name'], max_cols, 2)
self.data = tracks
class TrackSelection(BoxLayout):
track_id = NumericProperty()
track_name = StringProperty()
picker_root = ObjectProperty()
def on_picker_root(self, obj, items):
if self.track_id:
self.update_selected()
def on_track_id(self, obj, items):
if self.picker_root:
self.update_selected()
def update_selected(self):
if self.track_id in self.picker_root.temp_track_list:
self.ids.mtb.color = rgba('#800000')
def pick_item(self):
if self.track_id in self.picker_root.temp_track_list:
self.picker_root.temp_track_list.remove(self.track_id)
self.ids.mtb.color = rgba('#FFFFFF')
else:
self.picker_root.temp_track_list.append(self.track_id)
self.ids.mtb.color = rgba('#800000')