Skip to content
This repository has been archived by the owner on Nov 4, 2022. It is now read-only.

Commit

Permalink
Merge pull request #14 from vniehues/develop
Browse files Browse the repository at this point in the history
SectionRouteAttribute
  • Loading branch information
vniehues authored May 15, 2022
2 parents efda468 + 8fe6c6f commit 174a9a4
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 78 deletions.
4 changes: 0 additions & 4 deletions samples/mavvmApp/AppShell.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@ xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
</mavvm:MavvmShellContent>
</ShellItem>
<TabBar x:Name="MainTabbar" Route="main">
<Tab>
<mavvm:MavvmShellContent ViewModel="{x:Type viewmodels:SecondPageViewModel}">
</mavvm:MavvmShellContent>
</Tab>
<Tab>
<mavvm:MavvmShellContent ViewModel="{x:Type viewmodels:SecondTabPageViewModel}">
</mavvm:MavvmShellContent>
</Tab>
</TabBar>

<!--<Tab Title="Library" Icon="library.png">
Expand Down
4 changes: 3 additions & 1 deletion samples/mavvmApp/ViewModels/MainPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
using System.ComponentModel;
using CommunityToolkit.Mvvm.ComponentModel;
using mavvm;
using mavvm.Attibutes;
using Microsoft.Maui.Controls;

namespace mavvmApp.ViewModels
{
[SectionRoute("start")]
public class MainPageViewModel : ObservableObject
{
string _title;
Expand Down Expand Up @@ -46,7 +48,7 @@ void CountUp()

async void Navigate()
{
await BaseMethods.GoToSection<SecondTabPageViewModel>("main",parameters: new NavigationParameters{ { "countParam", Count } });
await BaseMethods.GoToViewModel<SecondTabPageViewModel>(parameters: new NavigationParameters{ { "countParam", Count } });
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions samples/mavvmApp/ViewModels/SecondPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public partial class SecondPageViewModel : BindableBase, IPageAware, INavigation
[ICommand]
async void GoBack()
{
await BaseMethods.GoBack();
await BaseMethods.GoToViewModel<MainPageViewModel>();
}

[ICommand]
Expand Down Expand Up @@ -70,12 +70,12 @@ async void LogCount()

public void Appearing()
{
_consoleService.Log("Appearing");
_consoleService.Log("SecondPage Appearing");
}

public void Disappearing()
{
_consoleService.Log("Disappearing");
_consoleService.Log("SecondPage Disappearing");
}

public void NavigatedBackTo(NavigationParameters parameters)
Expand Down
12 changes: 10 additions & 2 deletions samples/mavvmApp/ViewModels/SecondTabPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace mavvmApp.ViewModels
{
[TabRoute("secondTabPage")]
public class SecondTabPageViewModel : BindableBase
[SectionRoute("main")]
public class SecondTabPageViewModel : BindableBase, INavigationAware
{
string _title;
public string Title
Expand All @@ -24,6 +24,14 @@ public SecondTabPageViewModel()
{
Title = "Second Tab";
}

public void NavigatedTo(NavigationParameters parameters)
{
}

public void NavigatedBackTo(NavigationParameters parameters)
{
}
}
}

15 changes: 15 additions & 0 deletions src/mavvm/Attibutes/SectionRouteAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
namespace mavvm.Attibutes
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public class SectionRouteAttribute : Attribute
{
public string SectionRoute { get; }

public SectionRouteAttribute(string sectionRoute)
{
SectionRoute = sectionRoute;
}
}
}

12 changes: 5 additions & 7 deletions src/mavvm/Customs/MavvmShellContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@ protected override void OnParentSet()
{
base.OnParentSet();

var tab = this.Parent as Tab;
if (tab is null) return;

var attrib = ViewModel.GetCustomAttribute<TabRouteAttribute>();
if (attrib is null) return;

tab.Route = attrib.TabRoute;
if (this.Parent is ShellSection tab)
{
if (tab is null) return;
tab.Route = ViewModel.Name + "Tab";
}
}

public MavvmShellContent()
Expand Down
78 changes: 18 additions & 60 deletions src/mavvm/Navigation/BaseMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,67 +30,17 @@ public static async Task GoBack(bool replaceStack = false, NavigationParameters
}
}

public static async Task GoToSection(string sectionRoute, NavigationParameters parameters = null, bool animate = true)
{
var path = $"//{sectionRoute}";

if (parameters is null)
{
await Shell.Current.GoToAsync(path, animate);
}
else
{
await Shell.Current.GoToAsync(path, false, parameters);

//HACK: Shell doesn't immediately know about PresentedPage.
await Task.Delay(50);

if ((Shell.Current?.CurrentItem?.CurrentItem as IShellSectionController)?.PresentedPage?.BindingContext is INavigateToAware vmTo)
{
vmTo.NavigatedTo(parameters);
}
}
}

public static async Task GoToSection<TViewModel>(string sectionRoute, NavigationParameters parameters = null, bool animate = true)
{
var vmType = typeof(TViewModel);

var path = $"//{sectionRoute}";

if (parameters is null)
{
await Shell.Current.GoToAsync(path, animate);

//HACK: Shell doesn't immediately know about PresentedPage.
await Task.Delay(50);

SelectCorrectTab(vmType);
}
else
{
await Shell.Current.GoToAsync(path, false, parameters);

//HACK: Shell doesn't immediately know about PresentedPage.
await Task.Delay(50);

SelectCorrectTab(vmType);

if ((Shell.Current?.CurrentItem?.CurrentItem as IShellSectionController)?.PresentedPage?.BindingContext is INavigateToAware vmTo)
{
vmTo.NavigatedTo(parameters);
}
}
}

public static async Task GoToViewModel<TViewModel>(bool replaceStack = false, NavigationParameters parameters = null)
{
var vmType = typeof(TViewModel);
var path = (replaceStack ? "//" : "") + vmType.Name;

SelectCorrectTab(vmType);

if (Shell.Current.CurrentPage.BindingContext.GetType() != vmType)
//HACK: Shell doesn't immediately know about CurrentPage.
await Task.Delay(50);

if (Shell.Current?.CurrentPage?.BindingContext?.GetType() != vmType)
{
if (parameters is null)
{
Expand All @@ -102,7 +52,7 @@ public static async Task GoToViewModel<TViewModel>(bool replaceStack = false, Na
}
}

if ((Shell.Current?.CurrentItem?.CurrentItem as IShellSectionController)?.PresentedPage.BindingContext is INavigateToAware vm)
if (Shell.Current?.CurrentPage?.BindingContext is INavigateToAware vm)
{
vm.NavigatedTo(parameters);
}
Expand All @@ -111,20 +61,28 @@ public static async Task GoToViewModel<TViewModel>(bool replaceStack = false, Na
static void SelectCorrectTab(Type viewModelType)
{
if (Application.Current?.MainPage is IShellWithTabBar shell)
{
var attrib = viewModelType.GetCustomAttribute<TabRouteAttribute>();
if (attrib is null) return;

{
var tabItem = shell
.TabBar?
.Items
.FirstOrDefault(x => x.Route == attrib.TabRoute);
.FirstOrDefault(x => x.Route == viewModelType.Name + "Tab");

if (tabItem is object)
{
shell.TabBar.CurrentItem = tabItem;
}
}

var sectionAttribute = viewModelType.GetCustomAttribute<SectionRouteAttribute>();
if (sectionAttribute is object)
{
var sectionItem = Shell.Current.Items.FirstOrDefault(x => x.Route == sectionAttribute.SectionRoute);

if (sectionItem is object)
{
Shell.Current.CurrentItem = sectionItem;
}
}
}

public static Task ShowAlert(string title, string message, string cancel)
Expand Down
2 changes: 1 addition & 1 deletion src/mavvm/mavvm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'net6.0-android'">21.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$(TargetFramework.Contains('-windows'))">10.0.18362.0</SupportedOSPlatformVersion>
<PackageId>mavvm</PackageId>
<PackageVersion>1.0.3</PackageVersion>
<PackageVersion>1.0.4</PackageVersion>
<Authors>Vincent Niehues</Authors>
<Copyright>Vincent Niehues 2021</Copyright>
<Owners>Vincent Niehues</Owners>
Expand Down

0 comments on commit 174a9a4

Please sign in to comment.