123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- namespace aipdev
- {
- static class ContentManager
- {
- private static ContentControl _contentControl;
- private static readonly Dictionary<string, IContentPage> PageInstances = new Dictionary<string, IContentPage>();
- private static readonly List<IContentPage> VisiblePages = new List<IContentPage>();
- private static WaitingSpinner _waitingSpinner;
- /// <summary>
- /// Gets if the back operation can be executed.
- /// </summary>
- public static bool CanBack { get; private set; }
- /// <summary>
- /// Raised when the visible page changed.
- /// </summary>
- public static event EventHandler VisiblePageChanged;
- /// <summary>
- /// Gets the current visible page.
- /// </summary>
- public static IContentPage CurrentVisiblePage => VisiblePages.LastOrDefault();
- /// <summary>
- /// Register main content control.
- /// </summary>
- /// <param name="contentControl"></param>
- public static void RegisterContentControl(ContentControl contentControl)
- {
- _contentControl = contentControl;
- }
- /// <summary>
- /// Register a loading icon into the system.
- /// </summary>
- /// <param name="waitingSpinner"></param>
- public static void RegisterLoadingControl(WaitingSpinner waitingSpinner)
- {
- _waitingSpinner = waitingSpinner;
- }
- /// <summary>
- /// Show loading icon and prevent operations.
- /// </summary>
- public static void ShowLoading()
- {
- if (_waitingSpinner?.Visibility == Visibility.Collapsed)
- {
- _waitingSpinner?.StartWaiting();
- }
- }
- /// <summary>
- /// Hide loading icon.
- /// </summary>
- public static void HideLoading()
- {
- if (_waitingSpinner?.Visibility == Visibility.Visible)
- {
- _waitingSpinner?.StopWaiting();
- }
- }
- /// <summary>
- /// Register a content page instance.
- /// </summary>
- /// <param name="page"></param>
- public static void RegisterContentPage(IContentPage page)
- {
- if (!PageInstances.ContainsKey(page.PageName))
- {
- PageInstances.Add(page.PageName, page);
- }
- }
- /// <summary>
- /// Show new page.
- /// </summary>
- /// <param name="pageName">The name of page to show</param>
- /// <param name="arg">The arg which pass to the page.</param>
- public static void Show(string pageName, object arg = null)
- {
- if (_contentControl != null)
- {
- var currentPage = CurrentVisiblePage;
- if (currentPage != null && currentPage.IsModalPage)
- {
- return;
- }
- if (PageInstances.ContainsKey(pageName))
- {
- var page = PageInstances[pageName];
- if (page.IsHomePage)
- {
- VisiblePages.Clear();
- VisiblePages.Add(page);
- _contentControl.Content = page;
- page.OnShow(arg);
- }
- else
- {
- var visiblePage = VisiblePages.FirstOrDefault(x => x.PageName == pageName);
- if (visiblePage == null)
- {
- VisiblePages.Add(page);
- _contentControl.Content = page;
- page.OnShow(arg);
- }
- else
- {
- VisiblePages.Remove(visiblePage);
- VisiblePages.Add(visiblePage);
- _contentControl.Content = visiblePage;
- visiblePage.OnShow(arg);
- }
- }
- }
- }
- if (VisiblePages.Count > 1)
- {
- CanBack = !CurrentVisiblePage.IsModalPage;
- }
- else
- {
- CanBack = false;
- }
- VisiblePageChanged?.Invoke(null, EventArgs.Empty);
- }
- /// <summary>
- /// Back to last page.
- /// </summary>
- public static void Back()
- {
- if (_contentControl != null)
- {
- var currentPage = CurrentVisiblePage;
- if (currentPage != null && currentPage.IsModalPage)
- {
- return;
- }
- if (VisiblePages.Count > 1)
- {
- var lastPage = VisiblePages[^1];
- lastPage.OnHide();
- VisiblePages.Remove(lastPage);
- lastPage = VisiblePages[^1];
- _contentControl.Content = lastPage;
- }
- }
- if (VisiblePages.Count > 1)
- {
- CanBack = !CurrentVisiblePage.IsModalPage;
- }
- else
- {
- CanBack = false;
- }
- VisiblePageChanged?.Invoke(null, EventArgs.Empty);
- }
- /// <summary>
- /// Force make the page to invisible.
- /// </summary>
- /// <param name="contentPage">The page to invisible.</param>
- public static void Hide(this IContentPage contentPage)
- {
- if (_contentControl != null)
- {
- if (VisiblePages.Count > 1)
- {
- VisiblePages.RemoveAt(VisiblePages.Count -1);
- var lastPage = VisiblePages[^1];
- _contentControl.Content = lastPage;
- }
- }
- if (VisiblePages.Count > 1)
- {
- CanBack = !CurrentVisiblePage.IsModalPage;
- }
- else
- {
- CanBack = false;
- }
- VisiblePageChanged?.Invoke(null, EventArgs.Empty);
- }
- }
- }
|