Skip to content

Commit

Permalink
Merge pull request #372 from blackcandy-org/playing-behavior
Browse files Browse the repository at this point in the history
Add new playing begin with behavior on songs in album or playlist
  • Loading branch information
aidewoode authored Apr 29, 2024
2 parents dcbe0a6 + b2c4b8b commit 9fa1159
Show file tree
Hide file tree
Showing 29 changed files with 249 additions and 103 deletions.
6 changes: 5 additions & 1 deletion app/controllers/current_playlist/songs/albums_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/current_playlist/songs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
34 changes: 34 additions & 0 deletions app/javascript/controllers/album_bridge_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Controller } from '@hotwired/stimulus'
import { installEventHandler } from './mixins/event_handler'
import { isNativeApp } from '../helper'

export default class extends Controller {
static get shouldLoad () {
return isNativeApp()
}

static values = {
id: Number
}

initialize () {
installEventHandler(this)
}

connect () {
this.handleEvent('click', {
on: this.element,
with: this.playBeginWith,
delegation: true
})
}

play () {
App.nativeBridge.playAlbum(this.idValue)
}

playBeginWith = (event) => {
const { songId } = event.target.closest('[data-song-id]').dataset
App.nativeBridge.playAlbumBeginWith(this.idValue, songId)
}
}
13 changes: 8 additions & 5 deletions app/javascript/controllers/current_playlist_songs_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default class extends Controller {
static targets = ['item']

static values = {
shouldPlayAll: Boolean
shouldPlay: Boolean
}

initialize () {
Expand All @@ -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', {
Expand Down
16 changes: 12 additions & 4 deletions app/javascript/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ import MiniPlayerController from './mini_player_controller.js'

import PlayerController from './player_controller.js'

import PlaylistSongsController from './playlist_songs_controller.js'
import SongsController from './songs_controller.js'

import CurrentPlaylistSongsController from './current_playlist_songs_controller.js'

import PlaylistSortableController from './playlist_sortable_controller.js'

import SearchController from './search_controller.js'

import PlaylistSongsBridgeController from './playlist_songs_bridge_controller.js'
import SongsBridgeController from './songs_bridge_controller.js'

import AlbumBridgeController from './album_bridge_controller.js'

import PlaylistBridgeController from './playlist_bridge_controller.js'

import FlashBridgeController from './flash_bridge_controller.js'

Expand All @@ -52,15 +56,19 @@ application.register('mini-player', MiniPlayerController)

application.register('player', PlayerController)

application.register('playlist-songs', PlaylistSongsController)
application.register('songs', SongsController)

application.register('current-playlist-songs', CurrentPlaylistSongsController)

application.register('playlist-sortable', PlaylistSortableController)

application.register('search', SearchController)

application.register('playlist-songs-bridge', PlaylistSongsBridgeController)
application.register('songs-bridge', SongsBridgeController)

application.register('album-bridge', AlbumBridgeController)

application.register('playlist-bridge', PlaylistBridgeController)

application.register('flash-bridge', FlashBridgeController)

Expand Down
8 changes: 4 additions & 4 deletions app/javascript/controllers/mixins/playing_song_indicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ export const installPlayingSongIndicator = (controller, getSongElements = () =>
}

const addPlayingSongIndicatorEventListener = () => {
document.addEventListener('playlistSongs:showPlaying', showPlayingSong)
document.addEventListener('playlistSongs:hidePlaying', hidePlayingSong)
document.addEventListener('songs:showPlaying', showPlayingSong)
document.addEventListener('songs:hidePlaying', hidePlayingSong)
}

const removePlayingSongIndicatorEventListener = () => {
document.removeEventListener('playlistSongs:showPlaying', showPlayingSong)
document.removeEventListener('playlistSongs:hidePlaying', hidePlayingSong)
document.removeEventListener('songs:showPlaying', showPlayingSong)
document.removeEventListener('songs:hidePlaying', hidePlayingSong)
}

const controllerConnectCallback = controller.connect.bind(controller)
Expand Down
4 changes: 2 additions & 2 deletions app/javascript/controllers/player_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export default class extends Controller {
this.timerInterval = setInterval(this.#setTimer.bind(this), 1000)

// let playlist can show current playing song
dispatchEvent(document, 'playlistSongs:showPlaying')
dispatchEvent(document, 'songs:showPlaying')
}

#setPauseStatus = () => {
Expand All @@ -162,7 +162,7 @@ export default class extends Controller {

if (!this.currentSong.id) {
this.headerTarget.classList.remove('is-expanded')
dispatchEvent(document, 'playlistSongs:hidePlaying')
dispatchEvent(document, 'songs:hidePlaying')
}
}

Expand Down
34 changes: 34 additions & 0 deletions app/javascript/controllers/playlist_bridge_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Controller } from '@hotwired/stimulus'
import { installEventHandler } from './mixins/event_handler'
import { isNativeApp } from '../helper'

export default class extends Controller {
static get shouldLoad () {
return isNativeApp()
}

static values = {
id: Number
}

initialize () {
installEventHandler(this)
}

connect () {
this.handleEvent('click', {
on: this.element,
with: this.playBeginWith,
delegation: true
})
}

play () {
App.nativeBridge.playPlaylist(this.idValue)
}

playBeginWith = (event) => {
const { songId } = event.target.closest('[data-song-id]').dataset
App.nativeBridge.playPlaylistBeginWith(this.idValue, songId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class extends Controller {
connect () {
this.handleEvent('click', {
on: this.element,
with: this.playSong,
with: this.playNow,
delegation: true
})

Expand All @@ -31,13 +31,9 @@ export default class extends Controller {
})
}

playAll ({ params }) {
App.nativeBridge.playAll(params.resourceType, params.resourceId)
}

playSong (event) {
playNow (event) {
const { songId } = event.target.closest('[data-song-id]').dataset
App.nativeBridge.playSong(songId)
App.nativeBridge.playNow(songId)
}

playNext (event) {
Expand Down
56 changes: 48 additions & 8 deletions app/javascript/native_bridge.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,70 @@
import { isAndroidApp, isiOSApp } from './helper'

class NativeBridge {
playAll (resourceType, resourceId) {
playAlbum (albumId) {
if (isiOSApp()) {
window.webkit.messageHandlers.nativeApp.postMessage({
name: 'playAll',
resourceType,
resourceId: Number(resourceId)
name: 'playAlbum',
albumId: Number(albumId)
})
}

if (isAndroidApp()) {
window.NativeBridge.playAll(resourceType, Number(resourceId))
window.NativeBridge.playAlbum(Number(albumId))
}
}

playSong (songId) {
playAlbumBeginWith (albumId, songId) {
if (isiOSApp()) {
window.webkit.messageHandlers.nativeApp.postMessage({
name: 'playSong',
name: 'playAlbumBeginWith',
albumId: Number(albumId),
songId: Number(songId)
})
}

if (isAndroidApp()) {
window.NativeBridge.playSong(Number(songId))
window.NativeBridge.playAlbumBeginWith(Number(albumId), Number(songId))
}
}

playPlaylist (playlistId) {
if (isiOSApp()) {
window.webkit.messageHandlers.nativeApp.postMessage({
name: 'playPlaylist',
playlistId: Number(playlistId)
})
}

if (isAndroidApp()) {
window.NativeBridge.playPlaylist(Number(playlistId))
}
}

playPlaylistBeginWith (playlistId, songId) {
if (isiOSApp()) {
window.webkit.messageHandlers.nativeApp.postMessage({
name: 'playPlaylistBeginWith',
playlistId: Number(playlistId),
songId: Number(songId)
})
}

if (isAndroidApp()) {
window.NativeBridge.playPlaylistBeginWith(Number(playlistId), Number(songId))
}
}

playNow (songId) {
if (isiOSApp()) {
window.webkit.messageHandlers.nativeApp.postMessage({
name: 'playNow',
songId: Number(songId)
})
}

if (isAndroidApp()) {
window.NativeBridge.playNow(Number(songId))
}
}

Expand Down
Loading

0 comments on commit 9fa1159

Please sign in to comment.