@@ -63,12 +63,16 @@ struct PlayerReducer: Reducer {
63
63
case moveSongsResponse( TaskResult < APIClient . NoContentResponse > )
64
64
case getCurrentPlaylist
65
65
case currentPlaylistResponse( TaskResult < [ Song ] > )
66
- case playAll( String , Int )
67
- case playAllResponse( TaskResult < [ Song ] > )
68
- case playSong( Int )
66
+ case playAlbum( Int )
67
+ case playAlbumBeginWith( Int , Int )
68
+ case playPlaylist( Int )
69
+ case playPlaylistBeginWith( Int , Int )
70
+ case playSongsResponse( TaskResult < [ Song ] > )
71
+ case playSongsBeginWithResponse( TaskResult < [ Song ] > , Int )
72
+ case playNow( Int )
69
73
case playNext( Int )
70
74
case playLast( Int )
71
- case playSongResponse ( TaskResult < Song > )
75
+ case playNowResponse ( TaskResult < Song > )
72
76
case playNextResponse( TaskResult < Song > )
73
77
case playLastResponse( TaskResult < Song > )
74
78
}
@@ -285,37 +289,72 @@ struct PlayerReducer: Reducer {
285
289
286
290
return . none
287
291
288
- case let . playAll ( resourceType , resourceId ) :
292
+ case let . playAlbum ( albumId ) :
289
293
return . run { send in
290
294
await send (
291
- . playAllResponse (
295
+ . playSongsResponse (
292
296
TaskResult {
293
- switch resourceType {
294
- case " album " :
295
- return try await apiClient. replaceCurrentPlaylistWithAlbumSongs ( resourceId)
296
- case " playlist " :
297
- return try await apiClient. replaceCurrentPlaylistWithPlaylistSongs ( resourceId)
298
- default :
299
- throw APIClient . APIError. invalidRequest
300
- }
297
+ try await apiClient. replaceCurrentPlaylistWithAlbumSongs ( albumId)
298
+ }
299
+ )
300
+ )
301
+ }
302
+
303
+ case let . playAlbumBeginWith( albumId, songId) :
304
+ return . run { send in
305
+ await send (
306
+ . playSongsBeginWithResponse(
307
+ TaskResult { try await apiClient. replaceCurrentPlaylistWithAlbumSongs ( albumId) } ,
308
+ songId
309
+ )
310
+ )
311
+ }
312
+
313
+ case let . playPlaylist( playlistId) :
314
+ return . run { send in
315
+ await send (
316
+ . playSongsResponse(
317
+ TaskResult {
318
+ try await apiClient. replaceCurrentPlaylistWithPlaylistSongs ( playlistId)
301
319
}
302
320
)
303
321
)
304
322
}
305
323
306
- case let . playAllResponse( . success( songs) ) :
324
+ case let . playPlaylistBeginWith( playlistId, songId) :
325
+ return . run { send in
326
+ await send (
327
+ . playSongsBeginWithResponse(
328
+ TaskResult { try await apiClient. replaceCurrentPlaylistWithPlaylistSongs ( playlistId) } ,
329
+ songId
330
+ )
331
+ )
332
+ }
333
+
334
+ case let . playSongsResponse( . success( songs) ) :
307
335
state. playlist. update ( songs: songs)
308
336
state. currentSong = songs. first
309
337
310
338
return self . playOn ( state: & state, index: 0 )
311
339
312
- case let . playSong( songId) :
340
+ case let . playSongsBeginWithResponse( . success( songs) , songId) :
341
+ state. playlist. update ( songs: songs)
342
+
343
+ if let songIndex = state. playlist. index ( by: songId) {
344
+ state. currentSong = state. playlist. find ( byIndex: songIndex)
345
+ return self . playOn ( state: & state, index: songIndex)
346
+ } else {
347
+ state. currentSong = songs. first
348
+ return self . playOn ( state: & state, index: 0 )
349
+ }
350
+
351
+ case let . playNow( songId) :
313
352
if let songIndex = state. playlist. index ( by: songId) {
314
353
return self . playOn ( state: & state, index: songIndex)
315
354
} else {
316
355
return . run { [ currentSong = state. currentSong] send in
317
356
await send (
318
- . playSongResponse (
357
+ . playNowResponse (
319
358
TaskResult { try await apiClient. addSongToCurrentPlaylist ( songId, currentSong, nil ) }
320
359
)
321
360
)
@@ -340,7 +379,7 @@ struct PlayerReducer: Reducer {
340
379
)
341
380
}
342
381
343
- case let . playSongResponse ( . success( song) ) :
382
+ case let . playNowResponse ( . success( song) ) :
344
383
let insertIndex = state. insertSongNextToCurrent ( song: song)
345
384
return self . playOn ( state: & state, index: insertIndex)
346
385
@@ -359,8 +398,9 @@ struct PlayerReducer: Reducer {
359
398
case let . deleteSongsResponse( . failure( error) ) ,
360
399
let . moveSongsResponse( . failure( error) ) ,
361
400
let . currentPlaylistResponse( . failure( error) ) ,
362
- let . playAllResponse( . failure( error) ) ,
363
- let . playSongResponse( . failure( error) ) ,
401
+ let . playSongsResponse( . failure( error) ) ,
402
+ let . playSongsBeginWithResponse( . failure( error) , _) ,
403
+ let . playNowResponse( . failure( error) ) ,
364
404
let . playNextResponse( . failure( error) ) ,
365
405
let . playLastResponse( . failure( error) ) ,
366
406
let . toggleFavoriteResponse( . failure( error) ) :
0 commit comments