refactor: build services with dependency injection
App registers the services, controllers, the main window and the pages in a container, and the navigation view asks it for each page. Pages get their controllers through the constructor instead of reading statics from App. SettingsStore takes over the current settings from App. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
65743279b1
commit
c6e9bf4e82
9 files changed
+112
-35
No files matched your search
@@ -1,7 +1,9 @@
|
||||
using System.Windows.Threading;
|
||||
using NAudio.Wave;
|
||||
using NAudio.Wave.SampleProviders;
|
||||
using Radiosender.Controllers;
|
||||
using Radiosender.Models;
|
||||
using Radiosender.Services;
|
||||
using Radiosender.Views.Pages;
|
||||
using Wpf.Ui.Controls;
|
||||
|
||||
@@ -10,11 +12,17 @@ namespace Radiosender.Views;
|
||||
public partial class MainWindow : FluentWindow
|
||||
{
|
||||
private readonly DispatcherTimer _reminderTimer = new() { Interval = TimeSpan.FromSeconds(1) };
|
||||
private readonly ScheduleController _schedule;
|
||||
private readonly SettingsStore _settings;
|
||||
private DateTime _remindedFor;
|
||||
|
||||
public MainWindow()
|
||||
public MainWindow(IServiceProvider services, ScheduleController schedule, SettingsStore settings)
|
||||
{
|
||||
_schedule = schedule;
|
||||
_settings = settings;
|
||||
InitializeComponent();
|
||||
// The navigation view asks the container for each page, so pages get their controllers injected.
|
||||
RootNavigation.SetServiceProvider(services);
|
||||
// Re-themes the app when the user switches the Windows theme while it runs.
|
||||
Wpf.Ui.Appearance.SystemThemeWatcher.Watch(this);
|
||||
Loaded += (_, _) => RootNavigation.Navigate(typeof(SchedulePage));
|
||||
@@ -25,10 +33,10 @@ public partial class MainWindow : FluentWindow
|
||||
// Shortly before news or moderator talk goes on air, the person at the microphone gets a heads-up.
|
||||
private void RemindSpeaker()
|
||||
{
|
||||
var settings = App.Settings;
|
||||
var settings = _settings.Current;
|
||||
TimeSpan? newsLead = settings.NewsAlert ? TimeSpan.FromSeconds(settings.NewsAlertSeconds) : null;
|
||||
TimeSpan? moderatorLead = settings.ModeratorAlert ? TimeSpan.FromSeconds(settings.ModeratorAlertSeconds) : null;
|
||||
if (App.Schedule.UpcomingSpeech(newsLead, moderatorLead) is not { } item || item.Start == _remindedFor)
|
||||
if (_schedule.UpcomingSpeech(newsLead, moderatorLead) is not { } item || item.Start == _remindedFor)
|
||||
return;
|
||||
_remindedFor = item.Start; // once per item
|
||||
var isNews = item.Kind == ScheduleItemKind.News;
|
||||
|
||||
@@ -9,9 +9,13 @@ namespace Radiosender.Views.Pages;
|
||||
|
||||
public partial class LibraryPage : Page
|
||||
{
|
||||
private readonly LibraryController _controller = new(App.Songs);
|
||||
private readonly LibraryController _controller;
|
||||
|
||||
public LibraryPage() => InitializeComponent();
|
||||
public LibraryPage(LibraryController controller)
|
||||
{
|
||||
_controller = controller;
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private Song? Selected => SongList.SelectedItem as Song;
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace Radiosender.Views.Pages;
|
||||
|
||||
public partial class SchedulePage : Page
|
||||
{
|
||||
private readonly ScheduleController _controller = App.Schedule;
|
||||
private readonly ScheduleController _controller;
|
||||
private readonly DispatcherTimer _progressTimer = new() { Interval = TimeSpan.FromMilliseconds(250) };
|
||||
private bool _loading;
|
||||
private int _scrolledTo = -1;
|
||||
@@ -29,8 +29,9 @@ public partial class SchedulePage : Page
|
||||
// Optimistic state: what the user just asked for, shown before the engine has caught up.
|
||||
private bool? _expectPlaying;
|
||||
|
||||
public SchedulePage()
|
||||
public SchedulePage(ScheduleController controller)
|
||||
{
|
||||
_controller = controller;
|
||||
InitializeComponent();
|
||||
Timeline.ItemsSource = _rows;
|
||||
// Also refreshes the buttons: Finished can arrive just before the engine reports it stopped.
|
||||
|
||||
@@ -9,10 +9,16 @@ namespace Radiosender.Views.Pages;
|
||||
/// <summary>Edits <see cref="AppSettings"/>. Nothing changes until Save.</summary>
|
||||
public partial class SettingsPage : Page
|
||||
{
|
||||
public SettingsPage() => InitializeComponent();
|
||||
private readonly SettingsStore _store;
|
||||
|
||||
public SettingsPage(SettingsStore store)
|
||||
{
|
||||
_store = store;
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
// Loaded fires on every navigation to the page, so unsaved edits from an earlier visit are dropped.
|
||||
private void OnLoaded(object sender, RoutedEventArgs e) => Show(App.Settings);
|
||||
private void OnLoaded(object sender, RoutedEventArgs e) => Show(_store.Current);
|
||||
|
||||
private void Show(AppSettings settings)
|
||||
{
|
||||
@@ -54,7 +60,7 @@ public partial class SettingsPage : Page
|
||||
|
||||
try
|
||||
{
|
||||
App.SaveSettings(settings);
|
||||
_store.Save(settings);
|
||||
ShowMessage(InfoBarSeverity.Success, "Gespeichert", "Minuten pro Stunde gelten für den nächsten Plan.");
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
|
||||
Reference in new issue
Block a user