From f5d9cce2ea4e19700ab8df46848eaddcd4b83b8b Mon Sep 17 00:00:00 2001 From: "kwan.nguyen" Date: Wed, 30 Oct 2024 17:00:07 +0700 Subject: [PATCH] update version control --- PCUT/Http.Core/Contexts/UserContext.cs | 9 ++++++++ .../Exceptions/AppOutdatedException.cs | 13 +++++++++++ PCUT/Http.Core/Handlers/DeviceAuthHandler.cs | 16 +++++++++++--- PCUT/PCUT/App.xaml.cs | 22 ++++++++++++++++++- PCUT/PCUT/Package.appxmanifest | 2 +- PCUT/PCUT/Pages/LoginPage.xaml | 10 ++++++++- PCUT/PCUT/ViewModels/LoginViewModel.cs | 5 +++-- 7 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 PCUT/Http.Core/Exceptions/AppOutdatedException.cs diff --git a/PCUT/Http.Core/Contexts/UserContext.cs b/PCUT/Http.Core/Contexts/UserContext.cs index 781f52f..07d14d7 100644 --- a/PCUT/Http.Core/Contexts/UserContext.cs +++ b/PCUT/Http.Core/Contexts/UserContext.cs @@ -29,6 +29,7 @@ namespace Http.Core.Contexts public string AccessToken { get; private set; } public string RefreshToken { get; private set; } public string DeviceId { get; private set; } + public string AppVersion { get; private set; } public DateTime? ExpiredTime { get; private set; } public bool IsPermittedEdit => Profile.Role == "admin" || (Profile.IsPermittedEdit ?? false); @@ -41,6 +42,14 @@ namespace Http.Core.Contexts } } + public void SetAppVersion(string appVersion) + { + if (AppVersion == null) + { + AppVersion = appVersion; + } + } + private readonly object _updateLock = new object(); internal void SetValue(string accessToken, string refreshToken, string userName = null) { diff --git a/PCUT/Http.Core/Exceptions/AppOutdatedException.cs b/PCUT/Http.Core/Exceptions/AppOutdatedException.cs new file mode 100644 index 0000000..55d9ac4 --- /dev/null +++ b/PCUT/Http.Core/Exceptions/AppOutdatedException.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Http.Core.Exceptions +{ + public class AppOutdatedException : Exception + { + public AppOutdatedException() : base() { } + + public AppOutdatedException(string message) : base(message) { } + } +} diff --git a/PCUT/Http.Core/Handlers/DeviceAuthHandler.cs b/PCUT/Http.Core/Handlers/DeviceAuthHandler.cs index bee7eec..40c552a 100644 --- a/PCUT/Http.Core/Handlers/DeviceAuthHandler.cs +++ b/PCUT/Http.Core/Handlers/DeviceAuthHandler.cs @@ -1,4 +1,7 @@ using Http.Core.Contexts; +using Http.Core.Exceptions; +using Http.Core.Extensions; +using PCUT.Entities.ApiResponse; using System; using System.Collections.Generic; using System.Net.Http; @@ -10,10 +13,17 @@ namespace Http.Core.Handlers { public class DeviceAuthHandler : DelegatingHandler { - protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { - request.Headers.Add("x-device-id", $"{UserContext.Instance.DeviceId}"); - return base.SendAsync(request, cancellationToken); + request.Headers.Add("x-device-id", UserContext.Instance.DeviceId); + request.Headers.Add("x-version-id", UserContext.Instance.AppVersion); + var response = await base.SendAsync(request, cancellationToken); + if (response.StatusCode == System.Net.HttpStatusCode.Conflict && response.Content.Headers.ContentLength > 0) + { + var content = await response.DeserializeObjectAsync(ensureSuccess: false); + throw new AppOutdatedException(content.ErrorMessage); + } + return response; } } } diff --git a/PCUT/PCUT/App.xaml.cs b/PCUT/PCUT/App.xaml.cs index da8cf40..d96ec8e 100644 --- a/PCUT/PCUT/App.xaml.cs +++ b/PCUT/PCUT/App.xaml.cs @@ -1,4 +1,6 @@ using Http.Core; +using Http.Core.Contexts; +using Http.Core.Exceptions; using Http.Core.Handlers; using Microsoft.Extensions.DependencyInjection; using PCUT.Pages; @@ -12,6 +14,7 @@ using Windows.ApplicationModel.Activation; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.UI.Core; +using Windows.UI.Popups; using Windows.UI.ViewManagement; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; @@ -45,6 +48,7 @@ namespace PCUT { this.InitializeComponent(); this.Suspending += OnSuspending; + this.UnhandledException += HandleException; AppContainer = ConfigDI(); HttpClientFactory .AddHttpClient(ClientNames.AuthClient) @@ -65,8 +69,9 @@ namespace PCUT /// Details about the launch request and process. protected override void OnLaunched(LaunchActivatedEventArgs e) { - Frame rootFrame = Window.Current.Content as Frame; + UserContext.Instance.SetAppVersion(GetAppVersion()); ApplicationView.GetForCurrentView().IsScreenCaptureEnabled = false; + Frame rootFrame = Window.Current.Content as Frame; // Do not repeat app initialization when the Window already has content, // just ensure that the window is active if (rootFrame == null) @@ -124,10 +129,25 @@ namespace PCUT deferral.Complete(); } + private async void HandleException(object sender, Windows.UI.Xaml.UnhandledExceptionEventArgs e) + { + if (e.Exception is AppOutdatedException outdatedException) + { + e.Handled = true; + await new MessageDialog(outdatedException.Message, "App Outdated!").ShowAsync(); + } + } + IServiceProvider ConfigDI() { var serviceCollection = new ServiceCollection(); return serviceCollection.BuildServiceProvider(); } + + private string GetAppVersion() + { + var packageVersion = Package.Current.Id.Version; + return $"{packageVersion.Major}.{packageVersion.Minor}.{packageVersion.Build}.{packageVersion.Revision}"; + } } } diff --git a/PCUT/PCUT/Package.appxmanifest b/PCUT/PCUT/Package.appxmanifest index 742a2e1..9e500c1 100644 --- a/PCUT/PCUT/Package.appxmanifest +++ b/PCUT/PCUT/Package.appxmanifest @@ -9,7 +9,7 @@ + Version="1.3.4.6" /> diff --git a/PCUT/PCUT/Pages/LoginPage.xaml b/PCUT/PCUT/Pages/LoginPage.xaml index 008381e..b360014 100644 --- a/PCUT/PCUT/Pages/LoginPage.xaml +++ b/PCUT/PCUT/Pages/LoginPage.xaml @@ -27,7 +27,8 @@ - + + @@ -96,6 +97,13 @@ IsEnabled="{x:Bind Path=LoginModel.LoginEnabled, Mode=OneWay}" Command="{x:Bind Path=LoginModel.NavigateMainCommand}"/> + diff --git a/PCUT/PCUT/ViewModels/LoginViewModel.cs b/PCUT/PCUT/ViewModels/LoginViewModel.cs index 44944d5..2f672c0 100644 --- a/PCUT/PCUT/ViewModels/LoginViewModel.cs +++ b/PCUT/PCUT/ViewModels/LoginViewModel.cs @@ -71,9 +71,10 @@ namespace PCUT.ViewModels SetProperty(ref _loginEnabled, value); } } - #region Commands + + public string VersionDisplay = $"Version {UserContext.Instance.AppVersion}"; + public ICommand NavigateMainCommand { get; private set; } - #endregion private bool _success = false; private async Task LoginAsync()