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 #15 from vniehues/develop
Browse files Browse the repository at this point in the history
New BaseMethods
  • Loading branch information
vniehues committed Jun 8, 2022
2 parents 8446039 + ded47a2 commit 4f59cca
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 8 deletions.
2 changes: 1 addition & 1 deletion samples/mavvmApp/ViewModels/MainPageViewModel.cs
Expand Up @@ -48,7 +48,7 @@ void CountUp()

async void Navigate()
{
await BaseMethods.GoToViewModel<SecondTabPageViewModel>(parameters: new NavigationParameters{ { "countParam", Count } });
await BaseMethods.GoToViewModel<LastPageViewModel>(intermediates: new []{typeof(SecondPageViewModel), typeof(ThirdPageViewModel)}, new NavigationParameters{ { "countParam", Count } });
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions samples/mavvmApp/ViewModels/SecondPageViewModel.cs
Expand Up @@ -4,9 +4,11 @@
using Microsoft.Maui.Hosting;
using mavvm;
using CommunityToolkit.Mvvm.Input;
using mavvm.Attibutes;

namespace mavvmApp.ViewModels
{
[SectionRoute("main")]
[QueryProperty(nameof(Count), "countParam")]
public partial class SecondPageViewModel : BindableBase, IPageAware, INavigationAware
{
Expand Down
2 changes: 1 addition & 1 deletion samples/mavvmApp/Views/LastPage.xaml
Expand Up @@ -4,7 +4,7 @@
x:Class="mavvmApp.Views.LastPage"
Title="{Binding Title}"
BackgroundColor="{DynamicResource SecondaryColor}"
Shell.PresentationMode="Animated">
Shell.PresentationMode="ModalAnimated">


<ScrollView>
Expand Down
4 changes: 2 additions & 2 deletions samples/mavvmApp/mavvmApp.csproj
Expand Up @@ -60,8 +60,8 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.0.0-preview3" />
<PackageReference Include="Microsoft.Maui.Dependencies" Version="6.0.300-rc.3.5667" />
<PackageReference Include="Microsoft.Maui.Extensions" Version="6.0.300-rc.3.5667" />
<PackageReference Include="Microsoft.Maui.Dependencies" Version="6.0.312" />
<PackageReference Include="Microsoft.Maui.Extensions" Version="6.0.312" />
</ItemGroup>
<ItemGroup>
<MauiXaml Condition=" '$(EnableDefaultXamlItems)' == 'true' " Update="Views\SecondPage %28Kopie%29.xaml">
Expand Down
101 changes: 100 additions & 1 deletion src/mavvm/Navigation/BaseMethods.cs
Expand Up @@ -30,7 +30,7 @@ public static async Task GoBack(bool replaceStack = false, NavigationParameters
}
}

public static async Task GoToViewModel<TViewModel>(bool replaceStack = false, NavigationParameters parameters = null)
public static async Task GoToViewModel<TViewModel>(bool replaceStack, NavigationParameters parameters = null)
{
var vmType = typeof(TViewModel);
var path = (replaceStack ? "//" : "") + vmType.Name;
Expand Down Expand Up @@ -58,6 +58,105 @@ public static async Task GoToViewModel<TViewModel>(bool replaceStack = false, Na
}
}

public static async Task GoToViewModel<TViewModel>(NavigationParameters parameters = null)
{
var vmType = typeof(TViewModel);
var path = vmType.Name;

SelectCorrectTab(vmType);

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

if (Shell.Current?.CurrentPage?.BindingContext?.GetType() != vmType)
{
if (parameters is null)
{
await Shell.Current.GoToAsync(path);
}
else
{
await Shell.Current.GoToAsync(path, parameters);
}
}

if (Shell.Current?.CurrentPage?.BindingContext is INavigateToAware vm)
{
vm.NavigatedTo(parameters);
}
}

public static async Task GoToViewModel<TViewModel>(Type[] intermediates, NavigationParameters parameters = null)
{
var vmType = typeof(TViewModel);

var path = "";

foreach (var vmStep in intermediates)
{
path += $"{vmStep.Name}/";
}

path += vmType.Name;

SelectCorrectTab(intermediates.Length > 0 ? intermediates[0] : vmType);

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

if (Shell.Current?.CurrentPage?.BindingContext?.GetType() != vmType)
{
if (parameters is null)
{
await Shell.Current.GoToAsync(path);
}
else
{
await Shell.Current.GoToAsync(path, parameters);
}
}

if (Shell.Current?.CurrentPage?.BindingContext is INavigateToAware vm)
{
vm.NavigatedTo(parameters);
}
}
public static async Task GoToViewModel<TViewModel>(Type intermediate, NavigationParameters parameters = null)
{
var vmType = typeof(TViewModel);

var path = "";

if (intermediate is not null)
{
path += $"{intermediate.Name}/";
}

path += vmType.Name;

SelectCorrectTab(intermediate ?? vmType);

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

if (Shell.Current?.CurrentPage?.BindingContext?.GetType() != vmType)
{
if (parameters is null)
{
await Shell.Current.GoToAsync(path);
}
else
{
await Shell.Current.GoToAsync(path, parameters);
}
}

if (Shell.Current?.CurrentPage?.BindingContext is INavigateToAware vm)
{
vm.NavigatedTo(parameters);
}
}

static void SelectCorrectTab(Type viewModelType)
{
if (Application.Current?.MainPage is IShellWithTabBar shell)
Expand Down
6 changes: 3 additions & 3 deletions src/mavvm/mavvm.csproj
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.4</PackageVersion>
<PackageVersion>1.0.5</PackageVersion>
<Authors>Vincent Niehues</Authors>
<Copyright>Vincent Niehues 2021</Copyright>
<Owners>Vincent Niehues</Owners>
Expand Down Expand Up @@ -41,7 +41,7 @@ It uses the Shell we all know from xamarin together with the all new .net maui w
<Folder Include="Attibutes\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Maui.Dependencies" Version="6.0.300-rc.3.5667" />
<PackageReference Include="Microsoft.Maui.Extensions" Version="6.0.300-rc.3.5667" />
<PackageReference Include="Microsoft.Maui.Dependencies" Version="6.0.312" />
<PackageReference Include="Microsoft.Maui.Extensions" Version="6.0.312" />
</ItemGroup>
</Project>

0 comments on commit 4f59cca

Please sign in to comment.