From 30c64fc4336000805cc11c6824ac79a8567d45e9 Mon Sep 17 00:00:00 2001 From: Stuart Ferguson Date: Thu, 10 Feb 2022 22:33:39 +0000 Subject: [PATCH] Added a navigation service --- .../Extensions/MauiAppBuilderExtensions.cs | 10 ++-- TransactionMobile.Maui/MauiProgram.cs | 2 +- .../UIServices/INavigationService.cs | 21 +++++++++ .../UIServices/ShellNavigationService.cs | 47 +++++++++++++++++++ .../ViewModels/LoginViewModel.cs | 8 +++- .../MobileTopupFailedPageViewModel.cs | 8 +++- .../MobileTopupPerformTopupPageViewModel.cs | 11 +++-- .../MobileTopupSelectOperatorPageViewModel.cs | 9 +++- .../MobileTopupSelectProductPageViewModel.cs | 9 ++-- .../MobileTopupSuccessPageViewModel.cs | 8 +++- .../Transactions/TransactionsPageViewModel.cs | 8 +++- 11 files changed, 119 insertions(+), 22 deletions(-) create mode 100644 TransactionMobile.Maui/UIServices/INavigationService.cs create mode 100644 TransactionMobile.Maui/UIServices/ShellNavigationService.cs diff --git a/TransactionMobile.Maui/Extensions/MauiAppBuilderExtensions.cs b/TransactionMobile.Maui/Extensions/MauiAppBuilderExtensions.cs index dcce8b61..c1876bfa 100644 --- a/TransactionMobile.Maui/Extensions/MauiAppBuilderExtensions.cs +++ b/TransactionMobile.Maui/Extensions/MauiAppBuilderExtensions.cs @@ -22,12 +22,12 @@ public static MauiAppBuilder ConfigureAppServices(this MauiAppBuilder builder) return builder; } - //public static MauiAppBuilder ConfigureUIServices(this MauiAppBuilder builder) - //{ + public static MauiAppBuilder ConfigureUIServices(this MauiAppBuilder builder) + { // builder.Services.AddSingleton(); - - // return builder; - //} + builder.Services.AddSingleton(); + return builder; + } public static MauiAppBuilder ConfigureRequestHandlers(this MauiAppBuilder builder) { diff --git a/TransactionMobile.Maui/MauiProgram.cs b/TransactionMobile.Maui/MauiProgram.cs index c299b00a..cfd3f057 100644 --- a/TransactionMobile.Maui/MauiProgram.cs +++ b/TransactionMobile.Maui/MauiProgram.cs @@ -15,7 +15,7 @@ public static MauiApp CreateMauiApp() .ConfigureRequestHandlers() .ConfigureViewModels() .ConfigureAppServices() - //.ConfigureUIServices() + .ConfigureUIServices() .UseMauiCommunityToolkit() .ConfigureFonts(fonts => { diff --git a/TransactionMobile.Maui/UIServices/INavigationService.cs b/TransactionMobile.Maui/UIServices/INavigationService.cs new file mode 100644 index 00000000..2a97f8c1 --- /dev/null +++ b/TransactionMobile.Maui/UIServices/INavigationService.cs @@ -0,0 +1,21 @@ +namespace TransactionMobile.Maui.UIServices; + +public interface INavigationService +{ + Task GoToHome(); + + Task GoToMobileTopupSuccessPage(); + + Task GoToMobileTopupFailedPage(); + + Task GoToMobileTopupSelectProductPage(String operatorIdentifier); + + Task GoToMobileTopupSelectOperatorPage(); + + Task GoToMobileTopupPerformTopupPage(String operatorIdentifier, + Guid contractId, + Guid productId, + Decimal topupAmount); + + Task PopToRoot(); +} \ No newline at end of file diff --git a/TransactionMobile.Maui/UIServices/ShellNavigationService.cs b/TransactionMobile.Maui/UIServices/ShellNavigationService.cs new file mode 100644 index 00000000..4f59c539 --- /dev/null +++ b/TransactionMobile.Maui/UIServices/ShellNavigationService.cs @@ -0,0 +1,47 @@ +namespace TransactionMobile.Maui.UIServices; + +public class ShellNavigationService : INavigationService +{ + #region Methods + + public async Task GoToHome() + { + await Shell.Current.GoToAsync("//home"); + } + + public async Task GoToMobileTopupFailedPage() + { + await Shell.Current.GoToAsync($"{nameof(MobileTopupFailedPage)}"); + } + + public async Task GoToMobileTopupPerformTopupPage(String operatorIdentifier, + Guid contractId, + Guid productId, + Decimal topupAmount) + { + await + Shell.Current.GoToAsync($"{nameof(MobileTopupPerformTopupPage)}?OperatorIdentifier={operatorIdentifier}&ContractId={contractId}&ProductId={productId}&TopupAmount={topupAmount}"); + } + + public async Task GoToMobileTopupSelectOperatorPage() + { + await Shell.Current.GoToAsync(nameof(MobileTopupSelectOperatorPage)); + } + + public async Task GoToMobileTopupSelectProductPage(String operatorIdentifier) + { + await Shell.Current.GoToAsync($"{nameof(MobileTopupSelectProductPage)}?OperatorIdentifier={operatorIdentifier}"); + } + + public async Task GoToMobileTopupSuccessPage() + { + await Shell.Current.GoToAsync($"{nameof(MobileTopupSuccessPage)}"); + } + + public async Task PopToRoot() + { + await Shell.Current.Navigation.PopToRootAsync(); + } + + #endregion +} \ No newline at end of file diff --git a/TransactionMobile.Maui/ViewModels/LoginViewModel.cs b/TransactionMobile.Maui/ViewModels/LoginViewModel.cs index 9ecc60fd..fb87c6c9 100644 --- a/TransactionMobile.Maui/ViewModels/LoginViewModel.cs +++ b/TransactionMobile.Maui/ViewModels/LoginViewModel.cs @@ -6,15 +6,19 @@ using MediatR; using MvvmHelpers; using MvvmHelpers.Commands; + using UIServices; public class LoginPageViewModel : BaseViewModel { + private readonly INavigationService NavigationService; + #region Constructors //public String Username { get; set; } //public String Password { get; set; } - public LoginPageViewModel(IMediator mediator) + public LoginPageViewModel(IMediator mediator, INavigationService navigationService) { + this.NavigationService = navigationService; this.LoginCommand = new AsyncCommand(this.LoginCommandExecute); this.Mediator = mediator; } @@ -63,7 +67,7 @@ private async Task LoginCommandExecute() var merchantBalance = await this.Mediator.Send(getMerchantBalanceRequest); // TODO: Cache the token as will be needed later - await Shell.Current.GoToAsync("//home"); + await this.NavigationService.GoToHome(); } #endregion diff --git a/TransactionMobile.Maui/ViewModels/Transactions/MobileTopupFailedPageViewModel.cs b/TransactionMobile.Maui/ViewModels/Transactions/MobileTopupFailedPageViewModel.cs index 4e7ba984..2b923d25 100644 --- a/TransactionMobile.Maui/ViewModels/Transactions/MobileTopupFailedPageViewModel.cs +++ b/TransactionMobile.Maui/ViewModels/Transactions/MobileTopupFailedPageViewModel.cs @@ -3,13 +3,17 @@ using System.Windows.Input; using MvvmHelpers; using MvvmHelpers.Commands; +using UIServices; public class MobileTopupFailedPageViewModel : BaseViewModel { + private readonly INavigationService NavigationService; + #region Constructors - public MobileTopupFailedPageViewModel() + public MobileTopupFailedPageViewModel(INavigationService navigationService) { + this.NavigationService = navigationService; this.CancelledCommand = new AsyncCommand(this.CancelledCommandExecute); } @@ -25,7 +29,7 @@ public MobileTopupFailedPageViewModel() private async Task CancelledCommandExecute() { - await Shell.Current.Navigation.PopToRootAsync(); + await this.NavigationService.PopToRoot(); } #endregion diff --git a/TransactionMobile.Maui/ViewModels/Transactions/MobileTopupPerformTopupPageViewModel.cs b/TransactionMobile.Maui/ViewModels/Transactions/MobileTopupPerformTopupPageViewModel.cs index aafd03dd..75eaf9a2 100644 --- a/TransactionMobile.Maui/ViewModels/Transactions/MobileTopupPerformTopupPageViewModel.cs +++ b/TransactionMobile.Maui/ViewModels/Transactions/MobileTopupPerformTopupPageViewModel.cs @@ -5,6 +5,7 @@ using MediatR; using MvvmHelpers; using MvvmHelpers.Commands; +using UIServices; [QueryProperty(nameof(MobileTopupPerformTopupPageViewModel.ContractId), nameof(MobileTopupPerformTopupPageViewModel.ContractId))] [QueryProperty(nameof(MobileTopupPerformTopupPageViewModel.ProductId), nameof(MobileTopupPerformTopupPageViewModel.ProductId))] @@ -20,15 +21,18 @@ public class MobileTopupPerformTopupPageViewModel : BaseViewModel private readonly IMediator Mediator; + private readonly INavigationService NavigationService; + private Decimal topupAmount; #endregion #region Constructors - public MobileTopupPerformTopupPageViewModel(IMediator mediator) + public MobileTopupPerformTopupPageViewModel(IMediator mediator, INavigationService navigationService) { this.Mediator = mediator; + this.NavigationService = navigationService; this.PerformTopupCommand = new AsyncCommand(this.PerformTopupCommandExecute); this.CustomerMobileNumberEntryCompletedCommand = new Command(this.CustomerMobileNumberEntryCompletedCommandExecute); this.TopupAmountEntryCompletedCommand = new Command(this.TopupAmountEntryCompletedCommandExecute); @@ -110,11 +114,12 @@ private async Task PerformTopupCommandExecute() if (response) { - await Shell.Current.GoToAsync($"{nameof(MobileTopupSuccessPage)}"); + await this.NavigationService.GoToMobileTopupSuccessPage(); + } else { - await Shell.Current.GoToAsync($"{nameof(MobileTopupFailedPage)}"); + await this.NavigationService.GoToMobileTopupFailedPage(); } } diff --git a/TransactionMobile.Maui/ViewModels/Transactions/MobileTopupSelectOperatorPageViewModel.cs b/TransactionMobile.Maui/ViewModels/Transactions/MobileTopupSelectOperatorPageViewModel.cs index e0991f91..b482f247 100644 --- a/TransactionMobile.Maui/ViewModels/Transactions/MobileTopupSelectOperatorPageViewModel.cs +++ b/TransactionMobile.Maui/ViewModels/Transactions/MobileTopupSelectOperatorPageViewModel.cs @@ -6,6 +6,7 @@ using MediatR; using MvvmHelpers; using MvvmHelpers.Commands; +using UIServices; public class MobileTopupSelectOperatorPageViewModel : BaseViewModel { @@ -13,13 +14,16 @@ public class MobileTopupSelectOperatorPageViewModel : BaseViewModel private readonly IMediator Mediator; + private readonly INavigationService NavigationService; + #endregion #region Constructors - public MobileTopupSelectOperatorPageViewModel(IMediator mediator) + public MobileTopupSelectOperatorPageViewModel(IMediator mediator, INavigationService navigationService) { this.Mediator = mediator; + this.NavigationService = navigationService; this.OperatorSelectedCommand = new AsyncCommand(this.OperatorSelectedCommandExecute); this.Title = "Select an Operator"; } @@ -61,7 +65,8 @@ public async Task Initialise(CancellationToken cancellationToken) private async Task OperatorSelectedCommandExecute(SelectedItemChangedEventArgs e) { ContractOperatorModel operatorModel = e.SelectedItem as ContractOperatorModel; - await Shell.Current.GoToAsync($"{nameof(MobileTopupSelectProductPage)}?OperatorIdentifier={operatorModel.OperatorIdentfier}"); + await this.NavigationService.GoToMobileTopupSelectProductPage(operatorModel.OperatorIdentfier); + } #endregion diff --git a/TransactionMobile.Maui/ViewModels/Transactions/MobileTopupSelectProductPageViewModel.cs b/TransactionMobile.Maui/ViewModels/Transactions/MobileTopupSelectProductPageViewModel.cs index 0ea22b40..40a41d8f 100644 --- a/TransactionMobile.Maui/ViewModels/Transactions/MobileTopupSelectProductPageViewModel.cs +++ b/TransactionMobile.Maui/ViewModels/Transactions/MobileTopupSelectProductPageViewModel.cs @@ -6,6 +6,7 @@ using MediatR; using MvvmHelpers; using MvvmHelpers.Commands; +using UIServices; [QueryProperty(nameof(MobileTopupSelectProductPageViewModel.OperatorIdentifier), nameof(MobileTopupSelectProductPageViewModel.OperatorIdentifier))] public class MobileTopupSelectProductPageViewModel : BaseViewModel @@ -14,13 +15,16 @@ public class MobileTopupSelectProductPageViewModel : BaseViewModel private readonly IMediator Mediator; + private readonly INavigationService NavigationService; + #endregion #region Constructors - public MobileTopupSelectProductPageViewModel(IMediator mediator) + public MobileTopupSelectProductPageViewModel(IMediator mediator, INavigationService navigationService) { this.Mediator = mediator; + this.NavigationService = navigationService; this.ProductSelectedCommand = new AsyncCommand(this.ProductSelectedCommandExecute); this.Title = "Select a Product"; } @@ -53,8 +57,7 @@ public async Task Initialise(CancellationToken cancellationToken) private async Task ProductSelectedCommandExecute(SelectedItemChangedEventArgs e) { ContractProductModel productModel = e.SelectedItem as ContractProductModel; - await - Shell.Current.GoToAsync($"{nameof(MobileTopupPerformTopupPage)}?OperatorIdentifier={productModel.OperatorIdentfier}&ContractId={productModel.ContractId}&ProductId={productModel.ProductId}&TopupAmount={productModel.Value}"); + await this.NavigationService.GoToMobileTopupPerformTopupPage(productModel.OperatorIdentfier, productModel.ContractId, productModel.ProductId, productModel.Value); } #endregion diff --git a/TransactionMobile.Maui/ViewModels/Transactions/MobileTopupSuccessPageViewModel.cs b/TransactionMobile.Maui/ViewModels/Transactions/MobileTopupSuccessPageViewModel.cs index 45085610..1b11fee9 100644 --- a/TransactionMobile.Maui/ViewModels/Transactions/MobileTopupSuccessPageViewModel.cs +++ b/TransactionMobile.Maui/ViewModels/Transactions/MobileTopupSuccessPageViewModel.cs @@ -3,13 +3,17 @@ using System.Windows.Input; using MvvmHelpers; using MvvmHelpers.Commands; + using UIServices; public class MobileTopupSuccessPageViewModel : BaseViewModel { + private readonly INavigationService NavigationService; + #region Constructors - public MobileTopupSuccessPageViewModel() + public MobileTopupSuccessPageViewModel(INavigationService navigationService) { + this.NavigationService = navigationService; this.CompletedCommand = new AsyncCommand(this.CompletedCommandExecute); this.Title = "Mobile Topup Successful"; } @@ -26,7 +30,7 @@ public MobileTopupSuccessPageViewModel() private async Task CompletedCommandExecute() { - await Shell.Current.Navigation.PopToRootAsync(); + await this.NavigationService.PopToRoot(); } #endregion diff --git a/TransactionMobile.Maui/ViewModels/Transactions/TransactionsPageViewModel.cs b/TransactionMobile.Maui/ViewModels/Transactions/TransactionsPageViewModel.cs index 86f09aaa..3bff5ad2 100644 --- a/TransactionMobile.Maui/ViewModels/Transactions/TransactionsPageViewModel.cs +++ b/TransactionMobile.Maui/ViewModels/Transactions/TransactionsPageViewModel.cs @@ -3,13 +3,17 @@ using System.Windows.Input; using MvvmHelpers; using MvvmHelpers.Commands; + using UIServices; public class TransactionsPageViewModel : BaseViewModel { + private readonly INavigationService NavigationService; + #region Constructors - public TransactionsPageViewModel() + public TransactionsPageViewModel(INavigationService navigationService) { + this.NavigationService = navigationService; this.MobileTopupCommand = new AsyncCommand(this.MobileTopupCommandExecute); this.MobileWalletCommand = new AsyncCommand(this.MobileWalletCommandExecute); this.BillPaymentCommand = new AsyncCommand(this.BillPaymentCommandExecute); @@ -46,7 +50,7 @@ private async Task BillPaymentCommandExecute() private async Task MobileTopupCommandExecute() { - await Shell.Current.GoToAsync(nameof(MobileTopupSelectOperatorPage)); + await this.NavigationService.GoToMobileTopupSelectOperatorPage(); } private async Task MobileWalletCommandExecute()