diff --git a/resources/views/livewire/devices/configure.blade.php b/resources/views/livewire/devices/configure.blade.php index 2ee3aad..b6d2da3 100644 --- a/resources/views/livewire/devices/configure.blade.php +++ b/resources/views/livewire/devices/configure.blade.php @@ -1,5 +1,7 @@ user()->devices->contains($device), 403); @@ -19,11 +28,13 @@ new class extends Component { $current_image_uuid = $device->current_screen_image; $current_image_path = 'images/generated/' . $current_image_uuid . '.png'; + $this->device = $device; $this->name = $device->name; $this->api_key = $device->api_key; $this->friendly_id = $device->friendly_id; $this->mac_address = $device->mac_address; $this->default_refresh_interval = $device->default_refresh_interval; + $this->playlists = $device->playlists()->with('items.plugin')->orderBy('created_at')->get(); return view('livewire.devices.configure', [ 'image' => ($current_image_uuid) ? url($current_image_path) : null, @@ -58,6 +69,115 @@ new class extends Component { Flux::modal('edit-device')->close(); } + + public function createPlaylist() + { + $this->validate([ + 'playlist_name' => 'required|string|max:255', + 'selected_weekdays' => 'array', + 'active_from' => 'nullable|date_format:H:i', + 'active_until' => 'nullable|date_format:H:i', + ]); + + $this->device->playlists()->create([ + 'name' => $this->playlist_name, + 'weekdays' => $this->selected_weekdays, + 'active_from' => $this->active_from, + 'active_until' => $this->active_until, + 'is_active' => true, + ]); + + $this->playlists = $this->device->playlists()->with('items.plugin')->orderBy('created_at')->get(); + $this->reset(['playlist_name', 'selected_weekdays', 'active_from', 'active_until']); + Flux::modal('create-playlist')->close(); + } + + public function togglePlaylistActive(Playlist $playlist) + { + $playlist->update(['is_active' => !$playlist->is_active]); + $this->playlists = $this->device->playlists()->with('items.plugin')->orderBy('created_at')->get(); + } + + public function movePlaylistItemUp(PlaylistItem $item) + { + $previousItem = $item->playlist->items() + ->where('order', '<', $item->order) + ->orderBy('order', 'desc') + ->first(); + + if ($previousItem) { + $tempOrder = $previousItem->order; + $previousItem->update(['order' => $item->order]); + $item->update(['order' => $tempOrder]); + $this->playlists = $this->device->playlists()->with('items.plugin')->orderBy('created_at')->get(); + } + } + + public function movePlaylistItemDown(PlaylistItem $item) + { + $nextItem = $item->playlist->items() + ->where('order', '>', $item->order) + ->orderBy('order') + ->first(); + + if ($nextItem) { + $tempOrder = $nextItem->order; + $nextItem->update(['order' => $item->order]); + $item->update(['order' => $tempOrder]); + $this->playlists = $this->device->playlists()->with('items.plugin')->orderBy('created_at')->get(); + } + } + + public function togglePlaylistItemActive(PlaylistItem $item) + { + $item->update(['is_active' => !$item->is_active]); + $this->playlists = $this->device->playlists()->with('items.plugin')->orderBy('created_at')->get(); + } + + public function deletePlaylist(Playlist $playlist) + { + abort_unless(auth()->user()->devices->contains($playlist->device), 403); + $playlist->delete(); + $this->playlists = $this->device->playlists()->with('items.plugin')->orderBy('created_at')->get(); + Flux::modal('delete-playlist-' . $playlist->id)->close(); + } + + public function deletePlaylistItem(PlaylistItem $item) + { + abort_unless(auth()->user()->devices->contains($item->playlist->device), 403); + $item->delete(); + $this->playlists = $this->device->playlists()->with('items.plugin')->orderBy('created_at')->get(); + Flux::modal('delete-playlist-item-' . $item->id)->close(); + } + + public function editPlaylist(Playlist $playlist) + { + $this->validate([ + 'playlist_name' => 'required|string|max:255', + 'selected_weekdays' => 'array', + 'active_from' => 'nullable|date_format:H:i', + 'active_until' => 'nullable|date_format:H:i', + ]); + + $playlist->update([ + 'name' => $this->playlist_name, + 'weekdays' => $this->selected_weekdays, + 'active_from' => $this->active_from, + 'active_until' => $this->active_until, + ]); + + $this->playlists = $this->device->playlists()->with('items.plugin')->orderBy('created_at')->get(); + $this->reset(['playlist_name', 'selected_weekdays', 'active_from', 'active_until']); + Flux::modal('edit-playlist-' . $playlist->id)->close(); + } + + public function preparePlaylistEdit(Playlist $playlist) + { + $this->playlist_name = $playlist->name; + $this->selected_weekdays = $playlist->weekdays ?? []; + $this->active_from = optional($playlist->active_from)->format('H:i'); + $this->active_until = optional($playlist->active_until)->format('H:i'); + } } ?> @@ -129,7 +249,8 @@ new class extends Component { - +
@@ -161,6 +282,203 @@ new class extends Component { Next Image @endif + + + +
+

Device Playlists

+ + Create Playlist + +
+ + +
+
+ Create Playlist +
+ +
+
+ +
+ +
+ + + + + + + + + +
+ +
+ +
+ +
+ +
+ +
+ + Create Playlist +
+
+
+
+ + @foreach($playlists as $playlist) +
+
+
+

{{ $playlist->name }}

+ +
+
+
+ @if($playlist->weekdays) + {{ implode(', ', collect($playlist->weekdays)->map(fn($day) => ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'][$day])->toArray()) }} + @endif + @if($playlist->active_from && $playlist->active_until) + + {{ $playlist->active_from->format('H:i') }} - {{ $playlist->active_until->format('H:i') }} + @endif +
+
+ + + + + + +
+
+
+ + +
+
+ Edit Playlist +
+ +
+
+ +
+ +
+ + + + + + + + + +
+ +
+ +
+ +
+ +
+ +
+ + Save Changes +
+
+
+
+ + +
+ Delete {{ $playlist->name }}? +

This will permanently delete this playlist and all its items.

+
+ +
+ + + Cancel + + Delete playlist +
+
+ + + + + + + + + + + @foreach($playlist->items->sortBy('order') as $item) + + + + + + @endforeach + +
+
Plugin
+
+
Status
+
+
Actions
+
+ {{ $item->plugin->name }} + + + +
+ @if(!$loop->first) + + @endif + @if(!$loop->last) + + @endif + + + +
+ + +
+ Delete {{ $item->plugin->name }}? +

This will remove this item from the playlist.

+
+ +
+ + + Cancel + + Delete item +
+
+
+
+ @endforeach
diff --git a/tests/Unit/Models/PlaylistItemTest.php b/tests/Unit/Models/PlaylistItemTest.php index 482549a..9c00a3a 100644 --- a/tests/Unit/Models/PlaylistItemTest.php +++ b/tests/Unit/Models/PlaylistItemTest.php @@ -4,7 +4,6 @@ use App\Models\Playlist; use App\Models\PlaylistItem; use App\Models\Plugin; - test('playlist item belongs to playlist', function () { $playlist = Playlist::factory()->create(); $playlistItem = PlaylistItem::factory()->create(['playlist_id' => $playlist->id]);