Skip to content

Commit

Permalink
[Feat] <Services> 添加NavigationViewService.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhh135 committed Jul 20, 2024
1 parent aa8384f commit e97cb78
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 21 deletions.
4 changes: 4 additions & 0 deletions src/HyPlayer.App/App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<None Remove="Views\Pages\HomePage.xaml" />
<None Remove="Views\Pages\PlaylistPage.xaml" />
<None Remove="Views\Pages\SearchPage.xaml" />
<None Remove="Views\Pages\SettingsPage.xaml" />
<None Remove="Views\Pages\ShellPage.xaml" />
<None Remove="Views\Pages\UserPage.xaml" />
<None Remove="Views\Window\CompactWindow.xaml" />
Expand Down Expand Up @@ -110,6 +111,9 @@
<None Update="Controls\App\SelectableTextBox.xaml">
<Generator>MSBuild:Compile</Generator>
</None>
<Page Update="Views\Pages\SettingsPage.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Update="Resources\Styles\FontSizes.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
Expand Down
2 changes: 2 additions & 0 deletions src/HyPlayer.App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public App()
Depository?.AddMvvm();
Depository?.AddSingleton<INavigationService, NavigationService>();
Depository?.AddSingleton<IPageService, PageService>();
Depository?.AddSingleton<IAppNotificationService, AppNotificationService>();
Depository?.AddSingleton<INavigationViewService, NavigationViewService>();
}

/// <summary>
Expand Down
24 changes: 24 additions & 0 deletions src/HyPlayer.App/Interfaces/Services/INavigationViewService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.Generic;

namespace HyPlayer.Interfaces.Services;

public interface INavigationViewService
{
IList<object>? MenuItems
{
get;
}

object? SettingsItem
{
get;
}

void Initialize(NavigationView navigationView);

void UnregisterEvents();

NavigationViewItem? GetSelectedItem(Type pageType);
}
107 changes: 107 additions & 0 deletions src/HyPlayer.App/Services/NavigationViewService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System.Diagnostics.CodeAnalysis;

using HyPlayer.Interfaces.Services;
using HyPlayer.Extensions.Helpers;
using HyPlayer.ViewModels;

using Microsoft.UI.Xaml.Controls;
using System.Collections.Generic;
using System;
using System.Linq;

namespace HyPlayer.Services;

public class NavigationViewService : INavigationViewService
{
private readonly INavigationService _navigationService;

private readonly IPageService _pageService;

private NavigationView? _navigationView;

public IList<object>? MenuItems => _navigationView?.MenuItems;

public object? SettingsItem => _navigationView?.SettingsItem;

public NavigationViewService(INavigationService navigationService, IPageService pageService)
{
_navigationService = navigationService;
_pageService = pageService;
}

[MemberNotNull(nameof(_navigationView))]
public void Initialize(NavigationView navigationView)
{
_navigationView = navigationView;
_navigationView.BackRequested += OnBackRequested;
_navigationView.ItemInvoked += OnItemInvoked;
}

public void UnregisterEvents()
{
if (_navigationView != null)
{
_navigationView.BackRequested -= OnBackRequested;
_navigationView.ItemInvoked -= OnItemInvoked;
}
}

public NavigationViewItem? GetSelectedItem(Type pageType)
{
if (_navigationView != null)
{
return GetSelectedItem(_navigationView.MenuItems, pageType) ?? GetSelectedItem(_navigationView.FooterMenuItems, pageType);
}

return null;
}

private void OnBackRequested(NavigationView sender, NavigationViewBackRequestedEventArgs args) => _navigationService.GoBack();

private void OnItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
{
if (args.IsSettingsInvoked)
{
_navigationService.NavigateTo("SettingsPage");
}
else
{
var selectedItem = args.InvokedItemContainer as NavigationViewItem;

if (selectedItem?.GetValue(NavigationHelper.NavigateToProperty) is string pageKey)
{
_navigationService.NavigateTo(pageKey);
}
}
}

private NavigationViewItem? GetSelectedItem(IEnumerable<object> menuItems, Type pageType)
{
foreach (var item in menuItems.OfType<NavigationViewItem>())
{
if (IsMenuItemForPageType(item, pageType))
{
return item;
}

var selectedChild = GetSelectedItem(item.MenuItems, pageType);
if (selectedChild != null)
{
return selectedChild;
}
}

return null;
}

private bool IsMenuItemForPageType(NavigationViewItem menuItem, Type sourcePageType)
{
if (menuItem.GetValue(NavigationHelper.NavigateToProperty) is string pageKey)
{
return _pageService.GetPageType(pageKey) == sourcePageType;
}

return false;
}

}
1 change: 1 addition & 0 deletions src/HyPlayer.App/Services/PageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public PageService()
Configure<SearchPage>();
Configure<UserPage>();
Configure<ErrorPage>();
Configure<SettingsPage>();
}

public Type GetPageType(string key)
Expand Down
14 changes: 14 additions & 0 deletions src/HyPlayer.App/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using CommunityToolkit.Mvvm.ComponentModel;
using HyPlayer.Interfaces.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HyPlayer.ViewModels
{
public partial class SettingsViewModel : ObservableRecipient, IScopedViewModel
{
}
}
15 changes: 15 additions & 0 deletions src/HyPlayer.App/Views/Pages/SettingsPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<local:SettingsPageBase
x:Class="HyPlayer.Views.Pages.SettingsPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:HyPlayer.Views.Pages"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid>

</Grid>
</local:SettingsPageBase>
35 changes: 35 additions & 0 deletions src/HyPlayer.App/Views/Pages/SettingsPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using HyPlayer.Interfaces.Views;
using HyPlayer.ViewModels;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;


namespace HyPlayer.Views.Pages
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class SettingsPage : SettingsPageBase
{
public SettingsPage()
{
this.InitializeComponent();
}
}

public class SettingsPageBase : AppPageWithScopedViewModelBase<SettingsViewModel>
{
}
}
6 changes: 3 additions & 3 deletions src/HyPlayer.App/Views/Pages/ShellPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
xmlns:local="using:HyPlayer.Views.Pages"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:search="using:HyPlayer.Views.Controls.Search"
xmlns:helpers="using:HyPlayer.Extensions.Helpers"
mc:Ignorable="d">
<Grid>

Expand All @@ -20,8 +21,7 @@
IsBackButtonVisible="Collapsed"
IsBackEnabled="True"
IsPaneToggleButtonVisible="False"
ItemInvoked="{x:Bind NavView_ItemInvoked}"
Loaded="NavView_Loaded"
IsSettingsVisible="True"
Style="{StaticResource LeftNavViewDefaultStyle}">

<Frame x:Name="contentFrame" />
Expand All @@ -31,7 +31,7 @@
<NavigationViewItem
x:Name="Home"
x:Uid="MainNav_Item_Home"
Tag="HomePage">
helpers:NavigationHelper.NavigateTo="HomePage">
<NavigationViewItem.Icon>
<FontIcon FontFamily="Segoe Fluent Icons" Glyph="&#xE80F;" />
</NavigationViewItem.Icon>
Expand Down
17 changes: 1 addition & 16 deletions src/HyPlayer.App/Views/Pages/ShellPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,8 @@ public ShellPage()
InitializeComponent();

App.GetService<INavigationService>().RegisterFrameEvents(contentFrame);
App.GetService<INavigationViewService>().Initialize(NavView);
}

private void NavView_ItemInvoked(Microsoft.UI.Xaml.Controls.NavigationView sender, Microsoft.UI.Xaml.Controls.NavigationViewItemInvokedEventArgs args)
{
var invokedItem = (args.InvokedItemContainer as NavigationViewItem)?.Tag?.ToString();

HyPlayer.App.GetService<INavigationService>().NavigateTo(invokedItem);
// NavView.Header = invokedItemName;
}

private void NavView_Loaded(object sender, RoutedEventArgs e)
{
NavView.SelectedItem = Home;
}



}

public class ShellPageBase : AppPageWithScopedViewModelBase<ShellViewModel>
Expand Down
2 changes: 1 addition & 1 deletion src/HyPlayer.App/Views/Window/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
VerticalAlignment="Top"
Subtitle="Preview"
IsBackButtonVisible="True"
IsBackEnabled="{x:Bind CanGoBack, Mode=OneWay}"
IsBackEnabled="{x:Bind _ShellViewModel.CanBack, Mode=OneWay}"
BackRequested="AppTitleBar_BackRequested">
<TitleBar.Content>
<search:SearchBox
Expand Down
1 change: 0 additions & 1 deletion src/HyPlayer.App/Views/Window/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public sealed partial class MainWindow : WindowEx
private readonly IDepositoryResolveScope _scope;
private readonly ShellViewModel _ShellViewModel;

private bool CanGoBack => App.GetService<INavigationService>().CanGoBack;

public MainWindow()
{
Expand Down

0 comments on commit e97cb78

Please sign in to comment.