From b2f5aee4e74716c775f14752bff89151619a236c Mon Sep 17 00:00:00 2001 From: Selim Mustafaev Date: Sun, 5 May 2024 21:42:38 +0300 Subject: [PATCH] Adding dialog for checking new number --- AutoCat/App.axaml.cs | 1 + AutoCat/Utils/Alerts.cs | 21 ++++++ AutoCat/ViewModels/AuthWindowViewModel.cs | 15 +---- AutoCat/ViewModels/CheckNumberViewModel.cs | 65 +++++++++++++++++++ AutoCat/Views/PlateView.axaml.cs | 1 + AutoCat/Windows/CheckNumberDialog.axaml | 38 +++++++++++ AutoCat/Windows/CheckNumberDialog.axaml.cs | 15 +++++ AutoCat/Windows/MainWindow.axaml | 4 +- AutoCat/Windows/MainWindow.axaml.cs | 51 ++++++++++++++- .../Model}/PlateNumber.cs | 7 +- AutoCatCore/Model/User.cs | 13 +++- AutoCatCore/Services/Api/ApiService.cs | 21 ++++-- AutoCatCore/Services/Api/IApiService.cs | 1 + 13 files changed, 220 insertions(+), 33 deletions(-) create mode 100644 AutoCat/Utils/Alerts.cs create mode 100644 AutoCat/ViewModels/CheckNumberViewModel.cs create mode 100644 AutoCat/Windows/CheckNumberDialog.axaml create mode 100644 AutoCat/Windows/CheckNumberDialog.axaml.cs rename {AutoCat/Views => AutoCatCore/Model}/PlateNumber.cs (87%) diff --git a/AutoCat/App.axaml.cs b/AutoCat/App.axaml.cs index db9617b..1c0996d 100644 --- a/AutoCat/App.axaml.cs +++ b/AutoCat/App.axaml.cs @@ -25,6 +25,7 @@ public partial class App : Application services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); // Windows services.AddWindowFactory(); diff --git a/AutoCat/Utils/Alerts.cs b/AutoCat/Utils/Alerts.cs new file mode 100644 index 0000000..4bea53a --- /dev/null +++ b/AutoCat/Utils/Alerts.cs @@ -0,0 +1,21 @@ +using System; +using MsBox.Avalonia; +using MsBox.Avalonia.Dto; +using MsBox.Avalonia.Enums; + +namespace AutoCat.Utils; + +public class Alerts +{ + public static async void ShowError(Exception error) + { + var msgBox = MessageBoxManager.GetMessageBoxStandard(new MessageBoxStandardParams + { + ButtonDefinitions = ButtonEnum.Ok, + ContentTitle = "Error", + ContentMessage = error.Message + }); + + await msgBox.ShowAsync(); + } +} \ No newline at end of file diff --git a/AutoCat/ViewModels/AuthWindowViewModel.cs b/AutoCat/ViewModels/AuthWindowViewModel.cs index 3da2ae0..f18edd5 100644 --- a/AutoCat/ViewModels/AuthWindowViewModel.cs +++ b/AutoCat/ViewModels/AuthWindowViewModel.cs @@ -1,6 +1,7 @@ using System; using System.Reactive; using System.Threading.Tasks; +using AutoCat.Utils; using AutoCatCore.Services.Api; using AutoCatCore.Services.Storage; using ReactiveUI; @@ -65,7 +66,7 @@ public class AuthWindowViewModel: ViewModelBase (login, password) => login.Length > 3 && password.Length > 3); LoginCommand = ReactiveCommand.CreateFromTask(Login, loginEnabled); - LoginCommand.ThrownExceptions.Subscribe(ShowError); + LoginCommand.ThrownExceptions.Subscribe(Alerts.ShowError); } private async Task Login() @@ -74,16 +75,4 @@ public class AuthWindowViewModel: ViewModelBase await _storageService.SetUser(user); ReplaceWithMainWindowEvent?.Invoke(this, EventArgs.Empty); } - - private async void ShowError(Exception error) - { - var msgBox = MessageBoxManager.GetMessageBoxStandard(new MessageBoxStandardParams - { - ButtonDefinitions = ButtonEnum.Ok, - ContentTitle = "Error", - ContentMessage = error.Message - }); - - await msgBox.ShowAsync(); - } } \ No newline at end of file diff --git a/AutoCat/ViewModels/CheckNumberViewModel.cs b/AutoCat/ViewModels/CheckNumberViewModel.cs new file mode 100644 index 0000000..78a4a6d --- /dev/null +++ b/AutoCat/ViewModels/CheckNumberViewModel.cs @@ -0,0 +1,65 @@ +using System; +using System.Reactive; +using System.Reactive.Linq; +using System.Threading.Tasks; +using AutoCat.Utils; +using AutoCatCore.Model; +using AutoCatCore.Services.Api; +using DynamicData.Binding; +using ReactiveUI; + +namespace AutoCat.ViewModels; + +public class CheckNumberViewModel : ViewModelBase +{ + #region Fields + + private string _plateNumber = ""; + + #endregion + + #region Dependencies + + private readonly IApiService _apiService; + + #endregion + + #region Commands + + public ReactiveCommand CheckCommand { get; } + + #endregion + + #region Properties + + public string PlateNumber + { + get => _plateNumber; + set => this.RaiseAndSetIfChanged(ref _plateNumber, value); + } + + public Vehicle? Vehicle { get; set; } + + #endregion + + #region Events + + public event EventHandler? AutoCloseEvent; + + #endregion + + public CheckNumberViewModel(IApiService apiService) + { + _apiService = apiService; + + var checkEnabled = this.WhenAnyValue(vm => vm.PlateNumber, n => n.Length >= 8); + CheckCommand = ReactiveCommand.CreateFromTask(Check, checkEnabled); + CheckCommand.ThrownExceptions.Subscribe(Alerts.ShowError); + } + + private async Task Check(string number) + { + Vehicle = await _apiService.CheckNumber(number); + AutoCloseEvent?.Invoke(this, EventArgs.Empty); + } +} \ No newline at end of file diff --git a/AutoCat/Views/PlateView.axaml.cs b/AutoCat/Views/PlateView.axaml.cs index 084552d..a1cb28e 100644 --- a/AutoCat/Views/PlateView.axaml.cs +++ b/AutoCat/Views/PlateView.axaml.cs @@ -1,4 +1,5 @@ using System; +using AutoCatCore.Model; using Avalonia; using Avalonia.Controls; using Avalonia.Data; diff --git a/AutoCat/Windows/CheckNumberDialog.axaml b/AutoCat/Windows/CheckNumberDialog.axaml new file mode 100644 index 0000000..7987af0 --- /dev/null +++ b/AutoCat/Windows/CheckNumberDialog.axaml @@ -0,0 +1,38 @@ + + + + + + + +