diff --git a/KoeBook/ViewModels/MainViewModel.cs b/KoeBook/ViewModels/MainViewModel.cs index 14f9ac4..bda5ed5 100644 --- a/KoeBook/ViewModels/MainViewModel.cs +++ b/KoeBook/ViewModels/MainViewModel.cs @@ -4,6 +4,7 @@ using KoeBook.Core.Contracts.Services; using KoeBook.Core.Models; using KoeBook.Services; +using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Windows.Storage.Pickers; using WinRT.Interop; @@ -27,7 +28,14 @@ public sealed partial class MainViewModel : ObservableRecipient [NotifyCanExecuteChangedFor(nameof(StartProcessCommand))] private string? _ebookUrl; - private bool CanExecuteStartProcess => EbookFilePath is not null || !string.IsNullOrEmpty(EbookUrl); + [ObservableProperty] + [NotifyPropertyChangedFor(nameof(ErrorTextVisibility))] + [NotifyPropertyChangedFor(nameof(CanExecuteStartProcess))] + private bool _ebookIsValid = true; + + public Visibility ErrorTextVisibility => EbookIsValid ? Visibility.Collapsed : Visibility.Visible; + + private bool CanExecuteStartProcess => EbookFilePath is not null || !string.IsNullOrEmpty(EbookUrl) && EbookIsValid; [ObservableProperty] private bool _skipEdit = true; @@ -135,4 +143,28 @@ static async void CoreAsync(ILocalSettingsService settingsService, bool skipEdit await settingsService.SaveSettingAsync(SkipEditSettingsKey, skipEdit); } } + + partial void OnEbookUrlChanged(string? value) + { + EbookIsValid = IsValid(value); + + static bool IsValid(string? value) + { + if (string.IsNullOrEmpty(value)) + return true; + ReadOnlySpan allowedOrigins = [ + "https://www.aozora.gr.jp" + ]; + + try + { + var uri = new Uri(value); + return allowedOrigins.Contains(uri.GetLeftPart(UriPartial.Authority)); + } + catch (UriFormatException) + { + return false; + } + } + } } diff --git a/KoeBook/Views/MainPage.xaml b/KoeBook/Views/MainPage.xaml index d282124..15af6f8 100644 --- a/KoeBook/Views/MainPage.xaml +++ b/KoeBook/Views/MainPage.xaml @@ -54,11 +54,17 @@ --> - + + Margin="{StaticResource XSmallTopMargin}" + PlaceholderText="https://www.aozora.gr.jp/cards/000035/files/1567_14913.html"/> +