diff --git a/app/controllers/current_playlist/songs/albums_controller.rb b/app/controllers/current_playlist/songs/albums_controller.rb index 84b7953c..e07060ee 100644 --- a/app/controllers/current_playlist/songs/albums_controller.rb +++ b/app/controllers/current_playlist/songs/albums_controller.rb @@ -7,7 +7,11 @@ class CurrentPlaylist::Songs::AlbumsController < ApplicationController def update @current_playlist.replace(@album.song_ids) - redirect_to current_playlist_songs_path(should_play_all: true) + + redirect_to current_playlist_songs_path( + should_play: params[:should_play], + song_id: params[:song_id] + ) end private diff --git a/app/controllers/current_playlist/songs/playlists_controller.rb b/app/controllers/current_playlist/songs/playlists_controller.rb index 00f4998a..f6fb54c9 100644 --- a/app/controllers/current_playlist/songs/playlists_controller.rb +++ b/app/controllers/current_playlist/songs/playlists_controller.rb @@ -6,7 +6,10 @@ class CurrentPlaylist::Songs::PlaylistsController < ApplicationController def update @current_playlist.replace(@playlist.song_ids) - redirect_to current_playlist_songs_path(should_play_all: true) + redirect_to current_playlist_songs_path( + should_play: params[:should_play], + song_id: params[:song_id] + ) end private diff --git a/app/controllers/current_playlist/songs_controller.rb b/app/controllers/current_playlist/songs_controller.rb index b3e0d8d5..9c1744fe 100644 --- a/app/controllers/current_playlist/songs_controller.rb +++ b/app/controllers/current_playlist/songs_controller.rb @@ -7,6 +7,8 @@ class CurrentPlaylist::SongsController < Playlists::SongsController def index @songs = @playlist.songs_with_favorite + @should_play = params[:should_play] == "true" + @should_play_song_id = params[:song_id].to_i if @should_play end def create @@ -21,7 +23,7 @@ def create flash.now[:success] = t("notice.added_to_playlist") - redirect_to action: "index", should_play_all: params[:should_play] if @playlist.songs.count == 1 + redirect_to action: "index", should_play: params[:should_play] if @playlist.songs.count == 1 rescue ActiveRecord::RecordNotUnique flash.now[:error] = t("error.already_in_playlist") render turbo_stream: render_flash diff --git a/app/javascript/controllers/current_playlist_songs_controller.js b/app/javascript/controllers/current_playlist_songs_controller.js index 8ce612fa..2f01982a 100644 --- a/app/javascript/controllers/current_playlist_songs_controller.js +++ b/app/javascript/controllers/current_playlist_songs_controller.js @@ -6,7 +6,7 @@ export default class extends Controller { static targets = ['item'] static values = { - shouldPlayAll: Boolean + shouldPlay: Boolean } initialize () { @@ -16,21 +16,24 @@ export default class extends Controller { itemTargetConnected (target) { const song = JSON.parse(target.dataset.songJson) - const shouldPlay = target.dataset.shouldPlay === 'true' + const songShouldPlay = target.dataset.shouldPlay === 'true' const targetIndex = this.itemTargets.indexOf(target) this.playlist.insert(targetIndex, song) - if (shouldPlay) { + if (songShouldPlay) { this.player.skipTo(targetIndex) + delete target.dataset.shouldPlay + this.shouldPlayValue = false } } connect () { - if (this.shouldPlayAllValue) { + if (this.shouldPlayValue) { + // If no particular song is set to play, play the first song this.player.skipTo(0) - this.shouldPlayAllValue = false + this.shouldPlayValue = false } this.handleEvent('click', { diff --git a/app/javascript/controllers/playlist_songs_bridge_controller.js b/app/javascript/controllers/playlist_songs_bridge_controller.js index ee943f6b..db771d97 100644 --- a/app/javascript/controllers/playlist_songs_bridge_controller.js +++ b/app/javascript/controllers/playlist_songs_bridge_controller.js @@ -7,6 +7,11 @@ export default class extends Controller { return isNativeApp() } + static values = { + resourceType: String, + resourceId: Number + } + initialize () { installEventHandler(this) } @@ -14,7 +19,13 @@ export default class extends Controller { connect () { this.handleEvent('click', { on: this.element, - with: this.playSong, + with: this.playResourceBeginWith, + delegation: true + }) + + this.handleEvent('click', { + on: this.element, + with: this.playNow, delegation: true }) @@ -31,13 +42,18 @@ export default class extends Controller { }) } - playAll ({ params }) { - App.nativeBridge.playAll(params.resourceType, params.resourceId) + playResource () { + App.nativeBridge.playResource(this.resourceTypeValue, this.resourceIdValue) + } + + playResourceBeginWith = (event) => { + const { songId } = event.target.closest('[data-song-id]').dataset + App.nativeBridge.playResourceBeginWith(this.resourceTypeValue, this.resourceIdValue, songId) } - playSong (event) { + playNow (event) { const { songId } = event.target.closest('[data-song-id]').dataset - App.nativeBridge.playSong(songId) + App.nativeBridge.playNow(songId) } playNext (event) { diff --git a/app/javascript/native_bridge.js b/app/javascript/native_bridge.js index 3a9c9752..dbbbc148 100644 --- a/app/javascript/native_bridge.js +++ b/app/javascript/native_bridge.js @@ -1,30 +1,45 @@ import { isAndroidApp, isiOSApp } from './helper' class NativeBridge { - playAll (resourceType, resourceId) { + playResource (resourceType, resourceId) { if (isiOSApp()) { window.webkit.messageHandlers.nativeApp.postMessage({ - name: 'playAll', + name: 'playResource', resourceType, resourceId: Number(resourceId) }) } if (isAndroidApp()) { - window.NativeBridge.playAll(resourceType, Number(resourceId)) + window.NativeBridge.playResource(resourceType, Number(resourceId)) } } - playSong (songId) { + playResourceBeginWith (resourceType, resourceId, songId) { if (isiOSApp()) { window.webkit.messageHandlers.nativeApp.postMessage({ - name: 'playSong', + name: 'playResourceBeginWith', + resourceType, + resourceId: Number(resourceId), + songId: Number(songId) + }) + } + + if (isAndroidApp()) { + window.NativeBridge.playResourceBeginWith(resourceType, Number(resourceId), Number(songId)) + } + } + + playNow (songId) { + if (isiOSApp()) { + window.webkit.messageHandlers.nativeApp.postMessage({ + name: 'playNow', songId: Number(songId) }) } if (isAndroidApp()) { - window.NativeBridge.playSong(Number(songId)) + window.NativeBridge.playNow(Number(songId)) } } diff --git a/app/views/albums/show.html.erb b/app/views/albums/show.html.erb index 1f4dadcf..c523406a 100644 --- a/app/views/albums/show.html.erb +++ b/app/views/albums/show.html.erb @@ -1,6 +1,6 @@ <% page_title_tag @album.name %> -