feat: add song requests with hot swap

A listener request can now replace any upcoming item in the schedule,
also while the broadcast is running. The user picks a song from the
library, selects a row in the timeline and clicks "Play instead of
selected".

ScheduleGenerator.Replace puts the song at that position and rebuilds
only the rest of its half hour. Filler goes before the songs, so the
last song still ends exactly when the next news starts. Items before
the swap and all later half hours stay as they were. The filler budget
counts the ads and talk already planned earlier in that half hour, and
an ad never follows another ad.

PlaybackEngine.ReplaceUpcoming switches the running engine to the new
plan. It refuses when the item has already started, and the engine
reads each next item under a lock, so a swap cannot hit an item that
is just going on air.

The timeline marks the item on air with a play icon instead of
selecting it, so the selection is free for the swap target. A hint
next to the button says why a row cannot be replaced (news, already
played, no song chosen).

Refs #11

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
fraujulianandClaude Opus 5.5 committed 2026-11-23 11:10:38 +01:00
1 parent ce0bc03ef7
commit 667501e805
8 files changed
+387 -30

No files matched your search

+66 -7
View File
@@ -37,6 +37,7 @@
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
@@ -135,23 +136,81 @@
</Grid>
</Border>
<!-- Song request (hot swap) -->
<Grid Grid.Row="4" Margin="0,0,0,12">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" MaxWidth="420" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ui:TextBlock Grid.Column="0" VerticalAlignment="Center" Text="Song request" />
<ComboBox x:Name="RequestSong"
Grid.Column="1"
Margin="12,0,0,0"
AutomationProperties.Name="Requested song"
DisplayMemberPath="Label"
IsEditable="True"
IsTextSearchEnabled="True"
TextSearch.TextPath="Label"
SelectionChanged="OnRequestChanged" />
<ui:Button x:Name="SwapButton"
Grid.Column="2"
Margin="12,0,0,0"
Content="Play instead of selected"
Icon="{ui:SymbolIcon ArrowSwap24}"
IsEnabled="False"
Click="OnSwap" />
<ui:TextBlock x:Name="SwapHint"
Grid.Column="3"
Margin="12,0,0,0"
VerticalAlignment="Center"
Foreground="{DynamicResource TextFillColorSecondaryBrush}"
TextTrimming="CharacterEllipsis"
Text="Select an upcoming item in the schedule." />
</Grid>
<ui:ListView x:Name="Timeline"
Grid.Row="4"
Grid.Row="5"
SelectionMode="Single"
IsHitTestVisible="True"
AutomationProperties.Name="Schedule timeline">
AutomationProperties.Name="Schedule timeline"
SelectionChanged="OnTimelineSelectionChanged">
<ui:ListView.View>
<ui:GridView>
<ui:GridViewColumn Width="36">
<ui:GridViewColumn.CellTemplate>
<DataTemplate>
<ui:SymbolIcon x:Name="OnAir"
Symbol="Play24"
Filled="True"
Foreground="{DynamicResource AccentTextFillColorPrimaryBrush}"
AutomationProperties.Name="On air"
Visibility="Collapsed" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsCurrent}" Value="True">
<Setter TargetName="OnAir" Property="Visibility" Value="Visible" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ui:GridViewColumn.CellTemplate>
</ui:GridViewColumn>
<ui:GridViewColumn Width="90" Header="Start">
<ui:GridViewColumn.DisplayMemberBinding>
<Binding Path="Start" StringFormat="HH:mm:ss" />
<Binding Path="Item.Start" StringFormat="HH:mm:ss" />
</ui:GridViewColumn.DisplayMemberBinding>
</ui:GridViewColumn>
<ui:GridViewColumn Width="120" Header="Type" CellTemplate="{StaticResource KindChip}" />
<ui:GridViewColumn Width="420" Header="Title" DisplayMemberBinding="{Binding Title}" />
<ui:GridViewColumn Width="120" Header="Type">
<ui:GridViewColumn.CellTemplate>
<DataTemplate>
<ContentPresenter Content="{Binding Item}" ContentTemplate="{StaticResource KindChip}" />
</DataTemplate>
</ui:GridViewColumn.CellTemplate>
</ui:GridViewColumn>
<ui:GridViewColumn Width="420" Header="Title" DisplayMemberBinding="{Binding Item.Title}" />
<ui:GridViewColumn Width="90" Header="Duration">
<ui:GridViewColumn.DisplayMemberBinding>
<Binding Path="Duration" StringFormat="m\:ss" />
<Binding Path="Item.Duration" StringFormat="m\:ss" />
</ui:GridViewColumn.DisplayMemberBinding>
</ui:GridViewColumn>
</ui:GridView>
@@ -159,7 +218,7 @@
</ui:ListView>
<ui:TextBlock x:Name="EmptyText"
Grid.Row="4"
Grid.Row="5"
Margin="0,48,0,0"
HorizontalAlignment="Center"
VerticalAlignment="Top"
@@ -2,6 +2,7 @@ using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using Radiosender.Controllers;
using Radiosender.Models;
using Radiosender.Services;
using Wpf.Ui.Controls;
@@ -12,6 +13,7 @@ public partial class SchedulePage : Page
private readonly ScheduleController _controller = App.Schedule;
private readonly DispatcherTimer _progressTimer = new() { Interval = TimeSpan.FromMilliseconds(250) };
private bool _loading;
private int _scrolledTo = -1;
public SchedulePage()
{
@@ -35,7 +37,12 @@ public partial class SchedulePage : Page
VolumeSlider.Value = _controller.Volume;
_loading = false;
ShowPlan();
// The library can change on other pages.
RequestSong.ItemsSource = _controller.LibrarySongs()
.OrderBy(s => s.Artist).ThenBy(s => s.Title)
.Select(s => new SongOption(s.Id, $"{(string.IsNullOrWhiteSpace(s.Artist) ? s.Title : $"{s.Artist} - {s.Title}")} ({s.Duration:m\\:ss})"))
.ToList();
ShowCurrent();
_progressTimer.Start();
}
@@ -62,10 +69,31 @@ public partial class SchedulePage : Page
{
ShowMessage(InfoBarSeverity.Error, "Could not create the schedule", ex.Message);
}
ShowPlan();
ShowCurrent();
}
private void OnSwap(object sender, RoutedEventArgs e)
{
if (Timeline.SelectedItem is not TimelineRow row || RequestSong.SelectedItem is not SongOption song)
return;
try
{
var plan = _controller.Swap(row.Index, song.Id);
var placed = plan[row.Index];
ShowMessage(InfoBarSeverity.Success, "Song request planned",
$"\"{placed.Title}\" plays at {placed.Start:HH:mm:ss} instead of \"{row.Item.Title}\". The rest of that half hour was replanned, the news stays on time.");
}
catch (ScheduleException ex)
{
ShowMessage(InfoBarSeverity.Error, "Could not play the request", ex.Message);
}
ShowCurrent();
}
private void OnRequestChanged(object sender, SelectionChangedEventArgs e) => UpdateButtons();
private void OnTimelineSelectionChanged(object sender, SelectionChangedEventArgs e) => UpdateButtons();
private void OnStart(object sender, RoutedEventArgs e)
{
Message.IsOpen = false;
@@ -96,25 +124,30 @@ public partial class SchedulePage : Page
ShowCurrent();
});
private void ShowPlan()
{
Timeline.ItemsSource = _controller.Plan;
EmptyText.Visibility = _controller.Plan.Count == 0 ? Visibility.Visible : Visibility.Collapsed;
}
// Rebuilds the rows from the controller; the selection is the user's swap target and is kept.
private void ShowCurrent()
{
var item = _controller.CurrentItem;
UpdateButtons();
var current = _controller.CurrentIndex;
var plan = _controller.Plan;
var selected = Timeline.SelectedIndex;
var rows = plan.Select((p, i) => new TimelineRow(i, p, i == current)).ToList();
Timeline.ItemsSource = rows;
Timeline.SelectedIndex = selected < rows.Count ? selected : -1;
// Follow the broadcast when it moves on; otherwise keep the user's row in view.
if (current != _scrolledTo && current >= 0 && current < rows.Count)
Timeline.ScrollIntoView(rows[current]);
else if (Timeline.SelectedItem is { } row)
Timeline.ScrollIntoView(row);
_scrolledTo = current;
EmptyText.Visibility = plan.Count == 0 ? Visibility.Visible : Visibility.Collapsed;
NowKind.Content = item;
NowKind.Visibility = item is null ? Visibility.Collapsed : Visibility.Visible;
NowTitle.Text = item?.Title ?? "Nothing on air";
Timeline.SelectedIndex = _controller.CurrentIndex;
if (Timeline.SelectedItem is { } selected)
Timeline.ScrollIntoView(selected);
UpdateButtons();
UpdateProgress();
}
@@ -123,6 +156,18 @@ public partial class SchedulePage : Page
var playing = _controller.IsPlaying;
StartButton.IsEnabled = !playing && _controller.Plan.Count > 0;
StopButton.IsEnabled = playing;
var row = Timeline.SelectedItem as TimelineRow;
var blocker = row switch
{
null => "Select an upcoming item in the schedule.",
{ Item.Kind: ScheduleItemKind.News } => "News cannot be replaced.",
_ when playing && row.Index <= _controller.CurrentIndex => "This item is already on air or over.",
_ when RequestSong.SelectedItem is null => "Choose a song from the library.",
_ => null,
};
SwapHint.Text = blocker ?? $"Replaces the item at {row!.Item.Start:HH:mm:ss}.";
SwapButton.IsEnabled = blocker is null;
}
private void UpdateProgress()
@@ -146,4 +191,8 @@ public partial class SchedulePage : Page
Message.Message = text;
Message.IsOpen = true;
}
private sealed record TimelineRow(int Index, ScheduleItem Item, bool IsCurrent);
private sealed record SongOption(long Id, string Label);
}