Skip to content

Commit 51300b2

Browse files
committed
Add shuffle functionality to local queue and update UI controls
1 parent 6918d69 commit 51300b2

3 files changed

Lines changed: 47 additions & 29 deletions

File tree

HomeSpeaker.WebAssembly/Components/Music/Player/LocalAudioPlayer.razor

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,6 @@
3535
<i class="fas fa-step-forward"></i>
3636
</button>
3737
</div>
38-
39-
<div class="flex-grow-1 me-3">
40-
<div class="d-flex align-items-center">
41-
<small class="text-muted me-2">Vol:</small>
42-
<input type="range" class="form-range"
43-
min="0" max="100"
44-
value="@volumeLevel"
45-
@onchange="OnVolumeChange" />
46-
<small class="text-muted ms-2">@volumeLevel%</small>
47-
</div>
48-
</div>
4938
</div>
5039

5140
<div class="current-song-info mb-2">
@@ -64,7 +53,6 @@
6453
}
6554

6655
@code {
67-
private int volumeLevel = 50;
6856
private Timer? statusTimer;
6957
private bool isPlaying = false;
7058
private double currentTime = 0;
@@ -100,7 +88,6 @@
10088
isPlaying = status.IsPlaying;
10189
currentTime = status.CurrentTime;
10290
duration = status.Duration;
103-
volumeLevel = (int)(status.Volume * 100);
10491

10592
currentTimeDisplay = TimeSpan.FromSeconds(currentTime).ToString(@"mm\:ss");
10693
durationDisplay = TimeSpan.FromSeconds(duration).ToString(@"mm\:ss");
@@ -174,22 +161,6 @@
174161
}
175162
}
176163

177-
private async Task OnVolumeChange(ChangeEventArgs e)
178-
{
179-
if (int.TryParse(e.Value?.ToString(), out var newVolume))
180-
{
181-
volumeLevel = newVolume;
182-
try
183-
{
184-
await BrowserAudioService.SetVolumeAsync(newVolume / 100.0f);
185-
}
186-
catch (Exception ex)
187-
{
188-
Logger.LogError(ex, "Error setting volume");
189-
}
190-
}
191-
}
192-
193164
private void OnQueueChanged(object? sender, EventArgs e)
194165
{
195166
InvokeAsync(StateHasChanged);

HomeSpeaker.WebAssembly/Components/Music/Player/LocalQueueDisplay.razor

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
<div class="fs-6 mt-0 text-muted">(@LocalQueueService.Queue.Count songs • drag to reorder)</div>
99
</div>
1010
<div>
11+
<button @onclick="ShuffleQueue"
12+
class="btn btn-outline-secondary btn-sm me-2"
13+
disabled="@(LocalQueueService.Queue.Count <= 1)"
14+
title="Shuffle queue">
15+
<i class="fas fa-random me-1"></i>Shuffle
16+
</button>
1117
<button @onclick="ClearQueue"
1218
class="btn btn-outline-danger btn-sm"
1319
disabled="@(LocalQueueService.Queue.Count == 0)">
@@ -167,6 +173,18 @@
167173
}
168174
}
169175

176+
private async Task ShuffleQueue()
177+
{
178+
try
179+
{
180+
await LocalQueueService.ShuffleQueueAsync();
181+
}
182+
catch (Exception ex)
183+
{
184+
Logger.LogError(ex, "Error shuffling local queue");
185+
}
186+
}
187+
170188
private void OnQueueChanged(object? sender, EventArgs e)
171189
{
172190
InvokeAsync(StateHasChanged);

HomeSpeaker.WebAssembly/Services/LocalQueueService.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public interface ILocalQueueService
2121
Task PlayNextAsync();
2222
Task PlayPreviousAsync();
2323
Task PlaySongAtIndexAsync(int index);
24+
Task ShuffleQueueAsync();
2425
}
2526

2627
public class LocalQueueService : ILocalQueueService
@@ -204,4 +205,32 @@ private async void OnAudioStatusChanged(object? sender, BrowserPlayerStatus stat
204205
await PlayNextAsync();
205206
}
206207
}
208+
209+
public Task ShuffleQueueAsync()
210+
{
211+
if (queue.Count <= 1)
212+
return Task.CompletedTask;
213+
214+
var currentSong = CurrentSong;
215+
var random = new Random();
216+
217+
// Shuffle the queue using Fisher-Yates algorithm
218+
for (int i = queue.Count - 1; i > 0; i--)
219+
{
220+
int j = random.Next(i + 1);
221+
var temp = queue[i];
222+
queue[i] = queue[j];
223+
queue[j] = temp;
224+
}
225+
226+
// Update current index to point to the currently playing song if there is one
227+
if (currentSong != null)
228+
{
229+
currentIndex = queue.FindIndex(s => s.SongId == currentSong.SongId);
230+
}
231+
232+
logger.LogInformation("Shuffled local queue with {count} songs", queue.Count);
233+
QueueChanged?.Invoke(this, EventArgs.Empty);
234+
return Task.CompletedTask;
235+
}
207236
}

0 commit comments

Comments
 (0)