From 80292ac90b5ed21dc34d4ecec6a022da3b9dc269 Mon Sep 17 00:00:00 2001 From: Julian Lechner Date: Mon, 23 Nov 2026 14:22:52 +0100 Subject: [PATCH] feat(schedule): rework the schedule page Adds the start time, unlimited mode, start from now, end now and the buttons to move, edit and remove timeline items. Rows update in place and slow controller calls run off the UI thread, with the buttons switching at once. Co-Authored-By: Claude Fable 5.2 --- src/Radiosender/Views/Pages/SchedulePage.xaml | 193 +++++++++----- .../Views/Pages/SchedulePage.xaml.cs | 246 ++++++++++++++---- .../Views/ScheduleItemEditWindow.xaml | 36 +++ .../Views/ScheduleItemEditWindow.xaml.cs | 46 ++++ 4 files changed, 401 insertions(+), 120 deletions(-) create mode 100644 src/Radiosender/Views/ScheduleItemEditWindow.xaml create mode 100644 src/Radiosender/Views/ScheduleItemEditWindow.xaml.cs diff --git a/src/Radiosender/Views/Pages/SchedulePage.xaml b/src/Radiosender/Views/Pages/SchedulePage.xaml index 0b90f55..a846fe2 100644 --- a/src/Radiosender/Views/Pages/SchedulePage.xaml +++ b/src/Radiosender/Views/Pages/SchedulePage.xaml @@ -2,7 +2,9 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml" - Title="Schedule" + Title="Sendeplan" + Foreground="{DynamicResource TextFillColorPrimaryBrush}" + ScrollViewer.CanContentScroll="False" Loaded="OnLoaded" Unloaded="OnUnloaded"> @@ -14,8 +16,8 @@ HorizontalAlignment="Left" BorderThickness="1" CornerRadius="4" - BorderBrush="{DynamicResource TextFillColorTertiaryBrush}"> - + BorderBrush="{DynamicResource ControlStrongStrokeColorDefaultBrush}"> + @@ -41,26 +43,50 @@ - - - - + + + + + - + - + + @@ -83,7 +111,7 @@ + Text="Läuft gerade" /> + Text="Nichts auf Sendung" /> - - + + + - + Click="OnPlayToggle" /> @@ -125,7 +156,7 @@ Width="140" Margin="8,0,0,0" VerticalAlignment="Center" - AutomationProperties.Name="Volume" + AutomationProperties.Name="Lautstärke" Minimum="0" Maximum="1" SmallChange="0.05" @@ -136,46 +167,77 @@ - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + - + TextWrapping="Wrap" + Visibility="Collapsed" /> + + AutomationProperties.Name="Sendeplan" + SelectionChanged="OnTimelineSelectionChanged" + MouseDoubleClick="OnEditItem"> @@ -185,7 +247,7 @@ Symbol="Play24" Filled="True" Foreground="{DynamicResource AccentTextFillColorPrimaryBrush}" - AutomationProperties.Name="On air" + AutomationProperties.Name="Auf Sendung" Visibility="Collapsed" /> @@ -200,15 +262,15 @@ - + - - + + @@ -222,7 +284,8 @@ Margin="0,48,0,0" HorizontalAlignment="Center" VerticalAlignment="Top" - Text="No schedule yet. Choose the hours and create one." + Foreground="{DynamicResource TextFillColorSecondaryBrush}" + Text="Noch kein Plan. Stunden wählen und Plan erstellen." Visibility="Visible" /> diff --git a/src/Radiosender/Views/Pages/SchedulePage.xaml.cs b/src/Radiosender/Views/Pages/SchedulePage.xaml.cs index 5480b1e..a0cd37a 100644 --- a/src/Radiosender/Views/Pages/SchedulePage.xaml.cs +++ b/src/Radiosender/Views/Pages/SchedulePage.xaml.cs @@ -1,3 +1,4 @@ +using System.Collections.ObjectModel; using System.Windows; using System.Windows.Controls; using System.Windows.Threading; @@ -14,10 +15,24 @@ public partial class SchedulePage : Page private readonly DispatcherTimer _progressTimer = new() { Interval = TimeSpan.FromMilliseconds(250) }; private bool _loading; private int _scrolledTo = -1; + private bool _shownPlaying; + + // Weekday names in the start list follow the interface language, not the Windows setting. + private static readonly System.Globalization.CultureInfo German = new("de-DE"); + + // Bound once and updated row by row, so a click never rebuilds the whole list. + private readonly ObservableCollection _rows = []; + + // A controller call is running on a pool thread; the buttons that would collide with it are off. + private bool _working; + + // Optimistic state: what the user just asked for, shown before the engine has caught up. + private bool? _expectPlaying; public SchedulePage() { InitializeComponent(); + Timeline.ItemsSource = _rows; // Also refreshes the buttons: Finished can arrive just before the engine reports it stopped. _progressTimer.Tick += (_, _) => { @@ -36,6 +51,9 @@ public partial class SchedulePage : Page _loading = true; VolumeSlider.Value = _controller.Volume; _loading = false; + FillStartTimes(); + if (_controller.Plan.Count > 0) + UnlimitedBox.IsChecked = _controller.IsUnlimited; // The library can change on other pages. RequestSong.ItemsSource = _controller.LibrarySongs() @@ -55,65 +73,148 @@ public partial class SchedulePage : Page _progressTimer.Stop(); } - private void OnCreate(object sender, RoutedEventArgs e) + // The next 12 full hours; rebuilt when the list opens, so it never offers a past hour. + private void FillStartTimes() { - var hours = (int)(HoursBox.Value ?? 1); - try - { - _controller.CreatePlan(hours); - var plan = _controller.Plan; - ShowMessage(InfoBarSeverity.Success, "Schedule created", - $"{hours} h from {plan[0].Start:HH:mm} to {plan[^1].End:HH:mm}, {plan.Count} items."); - } - catch (Exception ex) when (ex is ScheduleException or ArgumentOutOfRangeException) - { - ShowMessage(InfoBarSeverity.Error, "Could not create the schedule", ex.Message); - } - ShowCurrent(); + var selected = (StartBox.SelectedItem as StartOption)?.Time; + var now = DateTime.Now; + var next = now.Date.AddHours(now.Hour + 1); + var options = Enumerable.Range(0, 12) + .Select(i => next.AddHours(i)) + .Select(t => new StartOption(t, t.Date == now.Date ? $"{t:HH:mm}" : t.ToString("ddd HH:mm", German))) + .ToList(); + StartBox.ItemsSource = options; + StartBox.SelectedItem = options.FirstOrDefault(o => o.Time == selected) ?? options[0]; } - private void OnSwap(object sender, RoutedEventArgs e) + private void OnStartDropDownOpened(object? sender, EventArgs e) => FillStartTimes(); + + private void OnUnlimitedChanged(object sender, RoutedEventArgs e) => + HoursBox.IsEnabled = UnlimitedBox.IsChecked != true; + + private void OnCreate(object sender, RoutedEventArgs e) => CreatePlan(startNow: false); + + private void OnStartNow(object sender, RoutedEventArgs e) => CreatePlan(startNow: true); + + private async void CreatePlan(bool startNow) + { + int? hours = UnlimitedBox.IsChecked == true ? null : (int)(HoursBox.Value ?? 1); + var start = (StartBox.SelectedItem as StartOption)?.Time; + if (startNow) + _expectPlaying = true; + await RunAsync("Der Plan konnte nicht erstellt werden", () => + { + if (startNow) + _controller.StartNow(hours); + else + _controller.CreatePlan(hours, start); + }); + } + + /// + /// Runs a controller call that can take a moment (planning, waiting for the audio thread to stop) + /// off the UI thread, so the window stays responsive. Returns false after showing the error. + /// + private async Task RunAsync(string errorTitle, Action work) + { + _working = true; + UpdateButtons(); + try + { + await Task.Run(work); + Message.IsOpen = false; + return true; + } + catch (Exception ex) when (ex is ScheduleException or ArgumentOutOfRangeException or InvalidOperationException) + { + ShowMessage(InfoBarSeverity.Error, errorTitle, ex.Message); + return false; + } + finally + { + _working = false; + _expectPlaying = null; + ShowCurrent(); + } + } + + private async 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(); + ScheduleItem? placed = null; + if (await RunAsync("Der Songwunsch konnte nicht eingeplant werden", () => placed = _controller.Swap(row.Index, song.Id)[row.Index])) + ShowMessage(InfoBarSeverity.Success, "Songwunsch eingeplant", + $"\"{placed!.Title}\" läuft um {placed.Start:HH:mm:ss}."); } private void OnRequestChanged(object sender, SelectionChangedEventArgs e) => UpdateButtons(); - private void OnTimelineSelectionChanged(object sender, SelectionChangedEventArgs e) => UpdateButtons(); + private void OnMoveUp(object sender, RoutedEventArgs e) => MoveSelected(-1); - private void OnStart(object sender, RoutedEventArgs e) + private void OnMoveDown(object sender, RoutedEventArgs e) => MoveSelected(1); + + // The selection follows the item, so it can be moved several steps in a row. + private void MoveSelected(int offset) => ChangeSelected(row => + { + _controller.Move(row.Index, offset); + Timeline.SelectedIndex = row.Index + offset; + }); + + private void OnEndNow(object sender, RoutedEventArgs e) { - Message.IsOpen = false; try { - _controller.Start(); + _controller.EndCurrentNow(); + Message.IsOpen = false; } - catch (InvalidOperationException ex) + catch (ScheduleException ex) { - ShowMessage(InfoBarSeverity.Warning, "Could not start", ex.Message); + ShowMessage(InfoBarSeverity.Error, "Das Element konnte nicht beendet werden", ex.Message); } ShowCurrent(); } - private void OnStop(object sender, RoutedEventArgs e) + private void OnRemove(object sender, RoutedEventArgs e) => ChangeSelected(row => _controller.Remove(row.Index)); + + private void OnEditItem(object sender, RoutedEventArgs e) { - _controller.Stop(); + if (!EditItemButton.IsEnabled || Timeline.SelectedItem is not TimelineRow row) + return; + var dialog = new ScheduleItemEditWindow(row.Item) { Owner = Window.GetWindow(this) }; + if (dialog.ShowDialog() == true) + ChangeSelected(r => _controller.EditItem(r.Index, dialog.ItemTitle, dialog.Duration)); + } + + private void ChangeSelected(Action change) + { + if (Timeline.SelectedItem is not TimelineRow row) + return; + try + { + change(row); + Message.IsOpen = false; + } + catch (ScheduleException ex) + { + ShowMessage(InfoBarSeverity.Error, "Der Plan konnte nicht geändert werden", ex.Message); + } ShowCurrent(); } + private void OnTimelineSelectionChanged(object sender, SelectionChangedEventArgs e) => UpdateButtons(); + + private async void OnPlayToggle(object sender, RoutedEventArgs e) + { + var stop = _controller.IsPlaying; + // The button and the card switch at once; stopping waits for the audio thread in the background. + _expectPlaying = !stop; + if (stop) + NowKind.Visibility = Visibility.Collapsed; + UpdateProgress(); + await RunAsync("Start nicht möglich", stop ? _controller.Stop : _controller.Start); + } + private void OnVolumeChanged(object sender, RoutedPropertyChangedEventArgs e) { if (!_loading) _controller.Volume = (float)e.NewValue; @@ -123,15 +224,15 @@ public partial class SchedulePage : Page private void OnItemStarted(object? sender, int index) => Dispatcher.BeginInvoke(ShowCurrent); private void OnItemFailed(object? sender, string message) => - Dispatcher.BeginInvoke(() => ShowMessage(InfoBarSeverity.Warning, "Item skipped", message)); + Dispatcher.BeginInvoke(() => ShowMessage(InfoBarSeverity.Warning, "Element übersprungen", message)); private void OnFinished(object? sender, EventArgs e) => Dispatcher.BeginInvoke(() => { - ShowMessage(InfoBarSeverity.Informational, "Broadcast finished", "The whole schedule has been played."); + ShowMessage(InfoBarSeverity.Informational, "Sendung beendet", "Der ganze Plan wurde gespielt."); ShowCurrent(); }); - // Rebuilds the rows from the controller; the selection is the user's swap target and is kept. + // Syncs the rows with the controller; the selection is the user's swap target and is kept. private void ShowCurrent() { var item = _controller.CurrentItem; @@ -139,8 +240,19 @@ public partial class SchedulePage : Page var plan = _controller.Plan; var selected = Timeline.SelectedIndex; - var rows = plan.Select((p, i) => new TimelineRow(i, p, i == current)).ToList(); - Timeline.ItemsSource = rows; + // Rows are records: only the ones whose content changed are replaced. + for (var i = 0; i < plan.Count; i++) + { + var updated = new TimelineRow(i, plan[i], i == current); + if (i == _rows.Count) + _rows.Add(updated); + else if (_rows[i] != updated) + _rows[i] = updated; + } + while (_rows.Count > plan.Count) + _rows.RemoveAt(_rows.Count - 1); + var rows = _rows; + GridColumns.Fit(Timeline); 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) @@ -152,7 +264,7 @@ public partial class SchedulePage : Page NowKind.Content = item; NowKind.Visibility = item is null ? Visibility.Collapsed : Visibility.Visible; - if (item is not null) NowTitle.Text = item.Title; + if (item is not null) NowTitle.Text = item.Title.Length > 0 ? item.Title : item.KindName; UpdateButtons(); UpdateProgress(); @@ -160,38 +272,60 @@ public partial class SchedulePage : Page private void UpdateButtons() { - var playing = _controller.IsPlaying; - StartButton.IsEnabled = !playing && _controller.Plan.Count > 0; - StopButton.IsEnabled = playing; + var playing = _expectPlaying ?? _controller.IsPlaying; + PlayButton.IsEnabled = !_working && (playing || _controller.Plan.Count > 0); + CreateButton.IsEnabled = StartNowButton.IsEnabled = !_working; + // Called every timer tick, so the label and icon change only with the state. + if (playing != _shownPlaying) + { + _shownPlaying = playing; + PlayButton.Content = playing ? "Stopp" : "Start"; + PlayButton.Icon = new SymbolIcon(playing ? SymbolRegular.Stop24 : SymbolRegular.Play24); + } var row = Timeline.SelectedItem as TimelineRow; + + // Only items that are not on air or over can be moved or removed; the one on air can still be edited. + var count = _controller.Plan.Count; + var current = _controller.CurrentIndex; + bool Editable(int i) => !_working && i >= 0 && i < count && (!playing || i > current); + var index = row?.Index ?? -1; + MoveUpButton.IsEnabled = Editable(index) && Editable(index - 1); + MoveDownButton.IsEnabled = Editable(index) && Editable(index + 1); + RemoveButton.IsEnabled = Editable(index); + var onAir = !_working && playing && index >= 0 && index == current; + EditItemButton.IsEnabled = (Editable(index) || onAir) && row!.Item.Kind != ScheduleItemKind.Song; + EndNowButton.IsEnabled = !_working && _expectPlaying != false && _controller.CurrentItem is not null; + 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 => "Ein kommendes Element im Plan auswählen.", + { Item.Kind: ScheduleItemKind.News } => "Nachrichten können nicht ersetzt werden.", + _ when playing && row.Index <= _controller.CurrentIndex => "Dieses Element ist bereits auf Sendung oder vorbei.", _ => null, }; - SwapHint.Text = blocker ?? $"Replaces the item at {row!.Item.Start:HH:mm:ss}."; - SwapButton.IsEnabled = blocker is null; + SwapHint.Text = blocker ?? $"Ersetzt das Element um {row!.Item.Start:HH:mm:ss}."; + // The hint only matters once a song is chosen. + var requested = RequestSong.SelectedItem is not null; + SwapHint.Visibility = requested ? Visibility.Visible : Visibility.Collapsed; + SwapButton.IsEnabled = !_working && requested && blocker is null; } private void UpdateProgress() { - if (_controller.CurrentItem is not { } item) + if (_expectPlaying == false || _controller.CurrentItem is not { } item) { NowProgress.Value = 0; // Checked here, not in ShowCurrent: the engine starts waiting a moment after Start. - var waiting = _controller.WaitingUntil; - NowTitle.Text = waiting is { } at ? $"On air at {at:HH:mm:ss}" : "Nothing on air"; - NowTime.Text = waiting is null ? " " : "Waiting for the planned start, so the news comes at :00 and :30."; + var waiting = _expectPlaying == false ? null : _controller.WaitingUntil; + NowTitle.Text = waiting is { } at ? $"Auf Sendung um {at:HH:mm:ss}" : "Nichts auf Sendung"; + NowTime.Text = " "; return; } var elapsed = _controller.CurrentElapsed; NowProgress.Value = item.Duration.Ticks == 0 ? 0 : (double)elapsed.Ticks / item.Duration.Ticks; - NowTime.Text = $"{elapsed:m\\:ss} / {item.Duration:m\\:ss} · planned {item.Start:HH:mm:ss}"; + NowTime.Text = $"{elapsed:m\\:ss} / {item.Duration:m\\:ss}"; } private void ShowMessage(InfoBarSeverity severity, string title, string text) @@ -205,4 +339,6 @@ public partial class SchedulePage : Page private sealed record TimelineRow(int Index, ScheduleItem Item, bool IsCurrent); private sealed record SongOption(long Id, string Label); + + private sealed record StartOption(DateTime Time, string Label); } diff --git a/src/Radiosender/Views/ScheduleItemEditWindow.xaml b/src/Radiosender/Views/ScheduleItemEditWindow.xaml new file mode 100644 index 0000000..eeb4627 --- /dev/null +++ b/src/Radiosender/Views/ScheduleItemEditWindow.xaml @@ -0,0 +1,36 @@ + + + + + + + + diff --git a/src/Radiosender/Views/ScheduleItemEditWindow.xaml.cs b/src/Radiosender/Views/ScheduleItemEditWindow.xaml.cs new file mode 100644 index 0000000..69100f5 --- /dev/null +++ b/src/Radiosender/Views/ScheduleItemEditWindow.xaml.cs @@ -0,0 +1,46 @@ +using System.Globalization; +using System.Windows; +using Radiosender.Models; +using Wpf.Ui.Controls; + +namespace Radiosender.Views; + +/// +/// Dialog for the title of a news, ad or moderator item and, for news and moderator items, its length. +/// Only collects input; the caller changes the schedule. +/// +public partial class ScheduleItemEditWindow : FluentWindow +{ + private static readonly string[] DurationFormats = [@"m\:ss", @"mm\:ss"]; + + public ScheduleItemEditWindow(ScheduleItem item) + { + InitializeComponent(); + Title = Bar.Title = $"{item.KindName} bearbeiten"; + TitleBox.Text = item.Title; + DurationBox.Text = item.Duration.ToString(@"m\:ss"); + Duration = item.Duration; + // Ads keep their length. + DurationLabel.IsEnabled = DurationBox.IsEnabled = item.Kind is ScheduleItemKind.News or ScheduleItemKind.Moderator; + } + + public string ItemTitle => TitleBox.Text; + + public TimeSpan Duration { get; private set; } + + private void OnSave(object sender, RoutedEventArgs e) + { + if (DurationBox.IsEnabled) + { + if (!TimeSpan.TryParseExact(DurationBox.Text.Trim(), DurationFormats, CultureInfo.InvariantCulture, out var duration) + || duration <= TimeSpan.Zero) + { + ErrorText.Visibility = Visibility.Visible; + return; + } + Duration = duration; + } + + DialogResult = true; + } +}