From aa0544cd3d011c26a57ce69757daa7c62f539eb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Bispo?= Date: Tue, 13 Dec 2022 21:53:04 +0000 Subject: [PATCH] [SG-872] Create generic exception handler for mobile (#2222) * [SG-872] Add method to handle exception in BaseViewModel * [SG-872] Code format --- src/App/Pages/Accounts/LoginPageViewModel.cs | 10 --------- .../Accounts/LoginPasswordlessViewModel.cs | 10 --------- src/App/Pages/BaseViewModel.cs | 21 ++++++++++++++++++- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/App/Pages/Accounts/LoginPageViewModel.cs b/src/App/Pages/Accounts/LoginPageViewModel.cs index 40d928c78..ba04b1e4b 100644 --- a/src/App/Pages/Accounts/LoginPageViewModel.cs +++ b/src/App/Pages/Accounts/LoginPageViewModel.cs @@ -305,15 +305,5 @@ namespace Bit.App.Pages _logger.Exception(e); } } - - private void HandleException(Exception ex) - { - Xamarin.Essentials.MainThread.InvokeOnMainThreadAsync(async () => - { - await _deviceActionService.HideLoadingAsync(); - await _platformUtilsService.ShowDialogAsync(AppResources.GenericErrorMessage); - }).FireAndForget(); - _logger.Exception(ex); - } } } diff --git a/src/App/Pages/Accounts/LoginPasswordlessViewModel.cs b/src/App/Pages/Accounts/LoginPasswordlessViewModel.cs index ced68d7b7..f795abe26 100644 --- a/src/App/Pages/Accounts/LoginPasswordlessViewModel.cs +++ b/src/App/Pages/Accounts/LoginPasswordlessViewModel.cs @@ -148,16 +148,6 @@ namespace Bit.App.Pages return string.Format(AppResources.XMinutesAgo, DateTime.UtcNow.Minute - requestDate.Value.ToUniversalTime().Minute); } - - private void HandleException(Exception ex) - { - Xamarin.Essentials.MainThread.InvokeOnMainThreadAsync(async () => - { - await _deviceActionService.HideLoadingAsync(); - await _platformUtilsService.ShowDialogAsync(AppResources.GenericErrorMessage); - }).FireAndForget(); - _logger.Exception(ex); - } } public class LoginPasswordlessDetails diff --git a/src/App/Pages/BaseViewModel.cs b/src/App/Pages/BaseViewModel.cs index 42afae11b..b8cc83c7a 100644 --- a/src/App/Pages/BaseViewModel.cs +++ b/src/App/Pages/BaseViewModel.cs @@ -1,4 +1,9 @@ -using Bit.App.Controls; +using System; +using Bit.App.Abstractions; +using Bit.App.Controls; +using Bit.App.Resources; +using Bit.Core.Abstractions; +using Bit.Core.Services; using Bit.Core.Utilities; using Xamarin.Forms; @@ -8,6 +13,9 @@ namespace Bit.App.Pages { private string _pageTitle = string.Empty; private AvatarImageSource _avatar; + private LazyResolve _deviceActionService = new LazyResolve(); + private LazyResolve _platformUtilsService = new LazyResolve(); + private LazyResolve _logger = new LazyResolve(); public string PageTitle { @@ -22,5 +30,16 @@ namespace Bit.App.Pages } public ContentPage Page { get; set; } + + protected void HandleException(Exception ex, string message = null) + { + Xamarin.Essentials.MainThread.InvokeOnMainThreadAsync(async () => + { + await _deviceActionService.Value.HideLoadingAsync(); + await _platformUtilsService.Value.ShowDialogAsync(message ?? AppResources.GenericErrorMessage); + }).FireAndForget(); + _logger.Value.Exception(ex); + } + } }