| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- using Microsoft.AspNetCore.Components;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
-
- namespace cwebplusApp.Shared.Services {
- public class PageHistoryManager {
-
- public EventCallback OnBeforeNavigateBack;
-
- private readonly List<string> previousPages;
-
- private readonly NavigationManager navigationManager;
-
- public PageHistoryManager(NavigationManager _navigationManager) {
- previousPages = new List<string>();
- this.navigationManager = _navigationManager;
- }
-
- public void Reset() {
- previousPages.Clear();
- }
-
- public void AddPageToHistory(string pageName) {
- previousPages.Add(pageName);
- }
-
- public string GetPreviousPage() {
- if (CanGoBack()) {
- // You add a page on initialization, so you need to return the 2nd from the last
- string previousPage = previousPages.ElementAt(previousPages.Count - 1);
- previousPages.RemoveAt(previousPages.Count - 1);
- return previousPage;
- }
-
- // Can't go back because you didn't navigate enough
- return previousPages.FirstOrDefault();
- }
-
- public bool CanGoBack() {
- return previousPages.Count >= 1;
- }
-
- public async void NavigateBack() {
- await FireOnBeforeNavigateBackEvent();
- navigationManager.NavigateTo(GetPreviousPage());
- }
-
- protected async Task FireOnBeforeNavigateBackEvent() {
- await OnBeforeNavigateBack.InvokeAsync();
- }
- }
- }
|