using Microsoft.Maui.Controls.PlatformConfiguration; using System; using System.Diagnostics; using System.Linq; using Microsoft.Maui.ApplicationModel; using Xamarin.Forms.Clinical6.Core.Helpers; using Xamarin.Forms.Clinical6.Core.ViewModels; using Xamarin.Forms.Clinical6.UI.Services; using Xamarin.Forms.Clinical6.ViewModels; using Microsoft.Maui.Dispatching; namespace Xamarin.Forms.Clinical6.Views { public partial class DashboardTabPage : TabbedPage { public static readonly BindableProperty IsAndroidLeftNavBarItemVisibleProperty = BindableProperty.Create(nameof(IsAndroidLeftNavBarItemVisibleProperty), typeof(bool), typeof(DashboardTabPage), false); public bool IsAndroidLeftNavBarItemVisible { get { return (bool)GetValue(IsAndroidLeftNavBarItemVisibleProperty); } set { SetValue(IsAndroidLeftNavBarItemVisibleProperty, value); } } private bool menuIsOpen = false; private int lastSelectedItemIndex; private bool isHandlingMenuTab = false; private Page menuPage; private Page homePage; private bool isSubscribedToFlyout = false; public event EventHandler DashboardBadgeEvent; private NavigationPage _alertNavigationPage; private int _alertTabIndex = -1; public DashboardTabPage() { InitializeComponent(); Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific.TabbedPage.SetIsSwipePagingEnabled(this, false); var navigationService = new NavigationService(); var alertPage = NavigationService.GetNewPage(); var alertsVm = alertPage.BindingContext as AlertsViewModel; if (alertsVm != null) { alertsVm.BadgeEvent -= AlertsVm_BadgeEvent; alertsVm.BadgeEvent += AlertsVm_BadgeEvent; } _alertNavigationPage = new NavigationPage(alertPage); _alertNavigationPage.Title = "AlertsText".Localized(); _alertNavigationPage.IconImageSource = "gui_tab_alerts"; _alertNavigationPage.BarTextColor = (Color)AppHelpers.GetResource("DarkGrey"); var toolbarItem = new ToolbarItem(); toolbarItem.Text = "ArchiveText".Localized(); toolbarItem.IconImageSource = "gui_tool_archive_2"; toolbarItem.SetBinding(MenuItem.CommandProperty, new Binding("ArchiveListCommand")); alertPage.ToolbarItems.Add(toolbarItem); alertPage.Disappearing -= AlertPage_Disappearing; alertPage.Disappearing += AlertPage_Disappearing; alertPage.Appearing -= AlertPage_Appearing; alertPage.Appearing += AlertPage_Appearing; menuPage = NavigationService.GetNewPage(); homePage = NavigationService.GetNewPage(); if (DeviceInfo.Platform == DevicePlatform.Android) { if (menuPage != null) { menuPage.AutomationId = "MenuPageTab"; Children.Add(menuPage); } if (homePage != null) { homePage.IconImageSource = "gui_tab_home"; homePage.AutomationId = "HomePageTab"; Children.Add(homePage); } if (!MainService.Instance.HideAlertsSection) { alertPage.AutomationId = "AlertPageTab"; Children.Add(_alertNavigationPage); } } else if (DeviceInfo.Platform == DevicePlatform.iOS) { if (!MainService.Instance.HideAlertsSection) { alertPage.AutomationId = "AlertPageTab"; Children.Add(_alertNavigationPage); } if (homePage != null) { homePage.IconImageSource = "gui_tab_home"; homePage.AutomationId = "HomePageTab"; Children.Add(homePage); } if (menuPage != null) { menuPage.AutomationId = "MenuPageTab"; Children.Add(menuPage); } } _alertTabIndex = Children.IndexOf(_alertNavigationPage); RegisterPlatformBadgeHandler(); var indexPage = Children.IndexOf(homePage); CurrentPage = Children[indexPage]; lastSelectedItemIndex = indexPage; CurrentPageChanged += (object sender, EventArgs e) => { if (isHandlingMenuTab) return; if (Children.IndexOf(CurrentPage) == 0) { var alertsViewModel = alertPage.BindingContext as AlertsViewModel; if (alertsViewModel != null && !alertsViewModel.IsRefreshing) alertsViewModel.RefreshCommand.Execute(null); } HandleCurrentPageChanged(sender, e); }; NavigationPage.SetHasNavigationBar(this, false); } private void AlertsVm_BadgeEvent(object sender, int count) { // Keep title as "Alerts" only. Forward count to platform handler. MainThread.BeginInvokeOnMainThread(() => { try { DashboardBadgeEvent?.Invoke(this, count); } catch (Exception ex) { Debug.WriteLine($"AlertsVm_BadgeEvent exception: {ex}"); } }); } partial void RegisterPlatformBadgeHandler(); private void AlertPage_Appearing(object sender, EventArgs e) { IsAndroidLeftNavBarItemVisible = false; } private void AlertPage_Disappearing(object sender, EventArgs e) { IsAndroidLeftNavBarItemVisible = true; } public void DashboardTabPageBadgeEvent(object sender, int count) { DashboardBadgeEvent?.Invoke(null, count); } void HandleCurrentPageChanged(object sender, EventArgs e) { var i = Children.IndexOf(CurrentPage); Debug.WriteLine("Page No:" + i); var menuPageType = NavigationService.GetPageType(); if (CurrentPage.GetType() != menuPageType) { lastSelectedItemIndex = i; return; } isHandlingMenuTab = true; var masterpage = MainService.HomePage?.Value as DashboardMasterPage; if (masterpage != null) { if (!isSubscribedToFlyout) { masterpage.IsPresentedChanged -= OnFlyoutIsPresentedChanged; masterpage.IsPresentedChanged += OnFlyoutIsPresentedChanged; isSubscribedToFlyout = true; } menuIsOpen = masterpage.IsPresented; masterpage.IsPresented = !menuIsOpen; } Dispatcher.Dispatch(() => { try { if (Children != null && Children.Count > lastSelectedItemIndex && lastSelectedItemIndex >= 0) { var nextPage = Children[lastSelectedItemIndex]; if (nextPage != null) { CurrentPage = nextPage; } } } finally { isHandlingMenuTab = false; } }); } private void OnFlyoutIsPresentedChanged(object sender, EventArgs e) { var masterpage = sender as DashboardMasterPage; if (masterpage != null) { menuIsOpen = masterpage.IsPresented; var menuPageType = NavigationService.GetPageType(); var appMenuViewModel = Children.FirstOrDefault(p => p.GetType() == menuPageType)?.BindingContext as AppMenuViewModel; if (appMenuViewModel != null) { appMenuViewModel.IsMenuPresented = menuIsOpen; } } } public void MenuOpenClose(bool isPresented) { var masterpage = MainService.HomePage?.Value as DashboardMasterPage; if (masterpage == null) return; if (!isSubscribedToFlyout) { masterpage.IsPresentedChanged -= OnFlyoutIsPresentedChanged; masterpage.IsPresentedChanged += OnFlyoutIsPresentedChanged; isSubscribedToFlyout = true; } masterpage.IsPresented = isPresented; } void HomePageIsPresentedChanged(object sender, EventArgs e) { var masterpage = MainService.HomePage?.Value as DashboardMasterPage; if (masterpage == null) return; menuIsOpen = masterpage.IsPresented; var menuPageType = NavigationService.GetPageType(); var appMenuViewModel = Children.FirstOrDefault(p => p.GetType() == menuPageType)?.BindingContext as AppMenuViewModel; if (appMenuViewModel != null) { appMenuViewModel.IsMenuPresented = menuIsOpen; } } } }