diff --git a/src/Radiosender/App.xaml.cs b/src/Radiosender/App.xaml.cs index d33ba2d..212796f 100644 --- a/src/Radiosender/App.xaml.cs +++ b/src/Radiosender/App.xaml.cs @@ -11,11 +11,29 @@ public partial class App : Application /// App-wide, so a running broadcast keeps going while the user switches pages. public static ScheduleController Schedule { get; private set; } = null!; + public static AppSettings Settings { get; private set; } = new(); + + private static readonly ScheduleGenerator Generator = new(); + + /// Stores the settings and applies them: alerts at once, minutes per hour from the next plan on. + /// The settings file cannot be written. + /// The settings file cannot be written. + public static void SaveSettings(AppSettings settings) + { + settings.Save(AppSettings.DefaultPath); + Settings = settings; + Generator.Options = settings.ToScheduleOptions(); + } + protected override void OnStartup(StartupEventArgs e) { + Settings = AppSettings.Load(AppSettings.DefaultPath); + Generator.Options = Settings.ToScheduleOptions(); // Creates the database file and table on first start. Songs = new SongRepository(SongRepository.DefaultDatabasePath); - Schedule = new ScheduleController(Songs, new ScheduleGenerator(), new PlaybackEngine(), () => DateTime.Now); + Schedule = new ScheduleController(Songs, Generator, new PlaybackEngine(), () => DateTime.Now); + // Follow the Windows setting: light, dark or a high-contrast theme. + Wpf.Ui.Appearance.ApplicationThemeManager.ApplySystemTheme(); base.OnStartup(e); } diff --git a/src/Radiosender/Services/AppSettings.cs b/src/Radiosender/Services/AppSettings.cs new file mode 100644 index 0000000..c309ecc --- /dev/null +++ b/src/Radiosender/Services/AppSettings.cs @@ -0,0 +1,80 @@ +using System.IO; +using System.Text.Json; + +namespace Radiosender.Services; + +/// What the user can change on the Settings page. The defaults are the values the app shipped with. +public record AppSettings +{ + public const int MinAlertSeconds = 5; + public const int MaxAlertSeconds = 300; + + public bool NewsAlert { get; init; } = true; + + /// How long before a news item the reminder appears. + public int NewsAlertSeconds { get; init; } = 30; + + public bool NewsAlertSound { get; init; } = true; + + public bool ModeratorAlert { get; init; } = true; + + /// How long before a moderator item the reminder appears. + public int ModeratorAlertSeconds { get; init; } = 30; + + public bool ModeratorAlertSound { get; init; } = true; + + /// News time per hour, split over the two news at :00 and :30. + public int NewsMinutesPerHour { get; init; } = 10; + + /// Target; the plan gets as close as the song lengths allow. + public int ModeratorMinutesPerHour { get; init; } = 10; + + /// Target; the plan gets as close as the song lengths allow. + public int AdMinutesPerHour { get; init; } = 20; + + public static string DefaultPath { get; } = Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Radiosender", "settings.json"); + + /// The first problem with these values as a message for the user, or null when they are fine. + public string? Validate() + { + if (NewsAlertSeconds is < MinAlertSeconds or > MaxAlertSeconds || ModeratorAlertSeconds is < MinAlertSeconds or > MaxAlertSeconds) + return $"Die Hinweiszeit muss zwischen {MinAlertSeconds} und {MaxAlertSeconds} Sekunden liegen."; + if (NewsMinutesPerHour < 2) + return "Nachrichten brauchen mindestens 2 Minuten pro Stunde."; + if (ModeratorMinutesPerHour < 0 || AdMinutesPerHour < 0) + return "Minuten pro Stunde dürfen nicht negativ sein."; + if (NewsMinutesPerHour + ModeratorMinutesPerHour + AdMinutesPerHour > 60) + return "Nachrichten, Moderation und Werbung dürfen zusammen höchstens 60 Minuten pro Stunde dauern."; + return null; + } + + public ScheduleOptions ToScheduleOptions() => new() + { + NewsDuration = TimeSpan.FromMinutes(NewsMinutesPerHour / 2.0), + AdsPerHour = TimeSpan.FromMinutes(AdMinutesPerHour), + ModeratorPerHour = TimeSpan.FromMinutes(ModeratorMinutesPerHour), + }; + + /// Reads the settings; a missing, unreadable or invalid file gives the defaults. + public static AppSettings Load(string path) + { + try + { + var settings = JsonSerializer.Deserialize(File.ReadAllText(path)); + return settings is not null && settings.Validate() is null ? settings : new AppSettings(); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or JsonException) + { + return new AppSettings(); + } + } + + /// The file cannot be written. + /// The file cannot be written. + public void Save(string path) + { + Directory.CreateDirectory(Path.GetDirectoryName(path)!); + File.WriteAllText(path, JsonSerializer.Serialize(this, new JsonSerializerOptions { WriteIndented = true })); + } +} diff --git a/tests/Radiosender.Tests/AppSettingsTests.cs b/tests/Radiosender.Tests/AppSettingsTests.cs new file mode 100644 index 0000000..b393ae9 --- /dev/null +++ b/tests/Radiosender.Tests/AppSettingsTests.cs @@ -0,0 +1,81 @@ +using Radiosender.Services; + +namespace Radiosender.Tests; + +public sealed class AppSettingsTests : IDisposable +{ + private readonly string _directory = Path.Combine(Path.GetTempPath(), "radiosender-tests", Guid.NewGuid().ToString("N")); + + private string File => Path.Combine(_directory, "settings.json"); + + public void Dispose() + { + if (Directory.Exists(_directory)) + Directory.Delete(_directory, recursive: true); + } + + [Fact] + public void Defaults_AreValidAndMatchTheOriginalSchedule() + { + var defaults = new AppSettings(); + + Assert.Null(defaults.Validate()); + Assert.Equal(TimeSpan.FromMinutes(5), defaults.ToScheduleOptions().NewsDuration); + Assert.Equal(TimeSpan.FromMinutes(20), defaults.ToScheduleOptions().AdsPerHour); + Assert.Equal(TimeSpan.FromMinutes(10), defaults.ToScheduleOptions().ModeratorPerHour); + Assert.True(defaults.NewsAlert && defaults.ModeratorAlert); + Assert.True(defaults.NewsAlertSound && defaults.ModeratorAlertSound); + Assert.Equal(30, defaults.NewsAlertSeconds); + } + + [Fact] + public void SaveAndLoad_RoundTrip() + { + var settings = new AppSettings { NewsAlert = false, ModeratorAlertSeconds = 45, ModeratorAlertSound = true, AdMinutesPerHour = 6 }; + + settings.Save(File); + + Assert.Equal(settings, AppSettings.Load(File)); + } + + [Fact] + public void Load_FallsBackToDefaultsForMissingBrokenOrInvalidFiles() + { + Assert.Equal(new AppSettings(), AppSettings.Load(File)); + + Directory.CreateDirectory(_directory); + System.IO.File.WriteAllText(File, "{ not json"); + Assert.Equal(new AppSettings(), AppSettings.Load(File)); + + System.IO.File.WriteAllText(File, """{"AdMinutesPerHour":500}"""); + Assert.Equal(new AppSettings(), AppSettings.Load(File)); + } + + [Theory] + [InlineData(10, 30, 20)] // exactly one hour + [InlineData(2, 0, 0)] + public void Validate_AcceptsUpToSixtyMinutesTogether(int news, int moderator, int ads) + { + Assert.Null(new AppSettings { NewsMinutesPerHour = news, ModeratorMinutesPerHour = moderator, AdMinutesPerHour = ads }.Validate()); + } + + [Theory] + [InlineData(4, 30, 10, 10, 10)] // alert too early + [InlineData(30, 301, 10, 10, 10)] // alert too late + [InlineData(30, 30, 1, 10, 10)] // too little news + [InlineData(30, 30, 10, 25, 26)] // more than 60 minutes together + [InlineData(30, 30, 10, -1, 5)] // negative + public void Validate_ReportsValuesOutOfRange(int newsSeconds, int moderatorSeconds, int news, int moderator, int ads) + { + var settings = new AppSettings + { + NewsAlertSeconds = newsSeconds, + ModeratorAlertSeconds = moderatorSeconds, + NewsMinutesPerHour = news, + ModeratorMinutesPerHour = moderator, + AdMinutesPerHour = ads, + }; + + Assert.NotNull(settings.Validate()); + } +}