Skip to content

Commit

Permalink
Support play next and play last from web view.
Browse files Browse the repository at this point in the history
  • Loading branch information
aidewoode committed Dec 1, 2023
1 parent 79824cc commit 7bad3b0
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 10 deletions.
2 changes: 1 addition & 1 deletion BlackCandy/Clients/APIClient/APIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct APIClient {
var moveSongInCurrentPlaylist: (Int, Int) async throws -> NoContentResponse
var getSong: (Int) async throws -> Song
var getSystemInfo: (ServerAddressState) async throws -> SystemInfo
var addSongToCurrentPlaylist: (Int, Song?) async throws -> Song
var addSongToCurrentPlaylist: (Int, Song?, String?) async throws -> Song
var replaceCurrentPlaylistWithAlbumSongs: (Int) async throws -> [Song]
var replaceCurrentPlaylistWithPlaylistSongs: (Int) async throws -> [Song]
}
Expand Down
8 changes: 6 additions & 2 deletions BlackCandy/Clients/APIClient/LiveAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,17 @@ extension APIClient: DependencyKey {
}
},

addSongToCurrentPlaylist: { songId, currentSong in
var parameters = ["song_id": songId]
addSongToCurrentPlaylist: { songId, currentSong, location in
var parameters: [String: Any] = ["song_id": songId]

if let currentSongId = currentSong?.id {
parameters["current_song_id"] = currentSongId
}

if let location = location {
parameters["location"] = location
}

let request = AF.request(
requestURL("/current_playlist/songs"),
method: .post,
Expand Down
5 changes: 5 additions & 0 deletions BlackCandy/Models/Playlist.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@ struct Playlist: Equatable {
orderedSongs.insert(song, at: index)
shuffledSongs.insert(song, at: index)
}

mutating func append(_ song: Song) {
orderedSongs.append(song)
shuffledSongs.append(song)
}
}
45 changes: 41 additions & 4 deletions BlackCandy/Store/PlayerReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ struct PlayerReducer: Reducer {
var hasCurrentSong: Bool {
currentSong != nil
}

mutating func insertSongNextToCurrent(song: Song) -> Int {
let insertIndex = min(currentIndex + 1, playlist.songs.endIndex)
playlist.insert(song, at: insertIndex)

return insertIndex
}
}

enum Action: Equatable {
Expand Down Expand Up @@ -58,7 +65,11 @@ struct PlayerReducer: Reducer {
case playAll(String, Int)
case playAllResponse(TaskResult<[Song]>)
case playSong(Int)
case playNext(Int)
case playLast(Int)
case playSongResponse(TaskResult<Song>)
case playNextResponse(TaskResult<Song>)
case playLastResponse(TaskResult<Song>)
}

var body: some ReducerOf<Self> {
Expand Down Expand Up @@ -304,23 +315,49 @@ struct PlayerReducer: Reducer {
return .run { [currentSong = state.currentSong] send in
await send(
.playSongResponse(
TaskResult { try await apiClient.addSongToCurrentPlaylist(songId, currentSong) }
TaskResult { try await apiClient.addSongToCurrentPlaylist(songId, currentSong, nil) }
)
)
}
}

case let .playSongResponse(.success(song)):
let insertIndex = min(state.currentIndex + 1, state.playlist.songs.endIndex)
state.playlist.insert(song, at: insertIndex)
case let .playNext(songId):
return .run { [currentSong = state.currentSong] send in
await send(
.playNextResponse(
TaskResult { try await apiClient.addSongToCurrentPlaylist(songId, currentSong, nil) }
)
)
}

case let .playLast(songId):
return .run { send in
await send(
.playLastResponse(
TaskResult { try await apiClient.addSongToCurrentPlaylist(songId, nil, "last") }
)
)
}

case let .playSongResponse(.success(song)):
let insertIndex = state.insertSongNextToCurrent(song: song)
return self.playOn(state: &state, index: insertIndex)

case let .playNextResponse(.success(song)):
_ = state.insertSongNextToCurrent(song: song)
return .none

case let .playLastResponse(.success(song)):
state.playlist.append(song)
return .none

case let .deleteSongsResponse(.failure(error)),
let .moveSongsResponse(.failure(error)),
let .currentPlaylistResponse(.failure(error)),
let .playAllResponse(.failure(error)),
let .playSongResponse(.failure(error)),
let .playNextResponse(.failure(error)),
let .playLastResponse(.failure(error)),
let .toggleFavoriteResponse(.failure(error)):
guard let error = error as? APIClient.APIError else { return .none }

Expand Down
6 changes: 6 additions & 0 deletions BlackCandy/Turbo/TurboScriptMessageHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class TurboScriptMessageHandler: NSObject, WKScriptMessageHandler {
case "playSong":
guard let songId = body["songId"] as? Int else { return }
store.send(.player(.playSong(songId)))
case "playNext":
guard let songId = body["songId"] as? Int else { return }
store.send(.player(.playNext(songId)))
case "playLast":
guard let songId = body["songId"] as? Int else { return }
store.send(.player(.playLast(songId)))
case "updateTheme":
guard
let theme = body["theme"] as? String,
Expand Down
2 changes: 1 addition & 1 deletion BlackCandyTests/Clients/APIClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ final class APIClientTests: XCTestCase {
return .init(data: responseJSON, statusCode: 200, headers: nil)
}

let response = try await apiClient.addSongToCurrentPlaylist(1, currentSong)
let response = try await apiClient.addSongToCurrentPlaylist(1, currentSong, nil)

XCTAssertEqual(response.id, 1)
XCTAssertEqual(response.name, "sample1")
Expand Down
4 changes: 2 additions & 2 deletions BlackCandyTests/Store/PlayerReducerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ final class PlayerReducerTests: XCTestCase {

let store = withDependencies {
$0.apiClient.getSong = { _ in playingSong }
$0.apiClient.addSongToCurrentPlaylist = { _, _ in playingSong}
$0.apiClient.addSongToCurrentPlaylist = { _, _, _ in playingSong}
} operation: {
TestStore(
initialState: PlayerReducer.State(playlist: playlist),
Expand All @@ -611,7 +611,7 @@ final class PlayerReducerTests: XCTestCase {

let store = withDependencies {
$0.apiClient.getSong = { _ in song }
$0.apiClient.addSongToCurrentPlaylist = { _, _ in song }
$0.apiClient.addSongToCurrentPlaylist = { _, _, _ in song }
} operation: {
TestStore(
initialState: PlayerReducer.State(),
Expand Down

0 comments on commit 7bad3b0

Please sign in to comment.