Skip to content

Commit 0cb2df7

Browse files
authored
Merge pull request #13 from sylac/develop
Develop
2 parents e65924b + 8b147cb commit 0cb2df7

32 files changed

+188
-152
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace Sylac.Mvvm.Abstraction;
2+
3+
public interface IViewModel
4+
{
5+
/// <summary>
6+
/// Called when the navigation to the view model is initiated.
7+
/// </summary>
8+
void OnNavigatingTo();
9+
10+
/// <summary>
11+
/// Called when the navigation from the view model is initiated.
12+
/// </summary>
13+
void OnNavigatingFrom();
14+
15+
/// <summary>
16+
/// Called when the navigation to the view model is completed.
17+
/// </summary>
18+
void OnNavigatedTo();
19+
20+
/// <summary>
21+
/// Called when the navigation from the view model is completed.
22+
/// </summary>
23+
void OnNavigatedFrom();
24+
}
25+
26+
public interface IViewModel<out TParam> : IViewModel
27+
where TParam : IViewModelParameters
28+
{
29+
/// <summary>
30+
/// Initializes the view model with the provided parameters.
31+
/// </summary>
32+
/// <param name="parameter"> The parameters to initialize the view model with. </param>
33+
void Initialize(IViewModelParameters parameter);
34+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
namespace Sylac.Mvvm;
1+
namespace Sylac.Mvvm.Abstraction;
22

33
public interface IViewModelParameters;

Sylac.Mvvm.Core/IViewModel.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

Sylac.Mvvm.Core/Navigation/Abstractions/INavigationService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Reactive;
1+
using Sylac.Mvvm.Abstraction;
2+
using System.Reactive;
23

34
namespace Sylac.Mvvm.Navigation.Abstractions;
45

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
namespace Sylac.Mvvm.Navigation
2-
{
3-
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
4-
public sealed class ConnectWithViewModelAttribute<TViewModel, TParams>() : Attribute
1+
using Sylac.Mvvm.Abstraction;
2+
3+
namespace Sylac.Mvvm.Navigation;
4+
5+
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
6+
public sealed class ConnectWithViewModelAttribute<TViewModel, TParams>() : Attribute
57
where TViewModel : IViewModel<TParams>
68
where TParams : IViewModelParameters
7-
{
8-
}
9+
{
910
}

Sylac.Mvvm.Core/Navigation/NavigationService.cs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
using Sylac.Mvvm.Navigation.Abstractions;
1+
using Sylac.Mvvm.Abstraction;
2+
using Sylac.Mvvm.Navigation.Abstractions;
23
using System.Reactive;
34
using System.Reactive.Linq;
45

56
namespace Sylac.Mvvm.Navigation;
67

7-
public class NavigationService(IPlatformNavigation platformNavigation) : INavigationService
8+
public sealed class NavigationService(IPlatformNavigation platformNavigation) : INavigationService
89
{
910
private readonly IPlatformNavigation _platformNavigation = platformNavigation;
1011
internal static Dictionary<Type, (string Page, Type ParametersType)> ViewModelsRegistry { get; } = [];
@@ -41,33 +42,18 @@ public IObservable<Unit> NavigateTo<TViewModel, TParams>(TParams parameters)
4142
where TParams : IViewModelParameters
4243
{
4344
return Observable.Return(ViewModelsRegistry.GetValueOrDefault(typeof(TViewModel)))
44-
.Where(result =>
45-
result.Page != default &&
46-
result.ParametersType != default)
45+
.Where(result => result != default)
4746
.SelectMany(result =>
48-
{
4947
// is type check needed? Is it possible to pass a different type?
50-
if (parameters.GetType() != result.ParametersType)
51-
{
52-
return Observable.Throw<Unit>(new ArgumentException("Invalid parameters type"));
53-
}
54-
55-
return _platformNavigation.GoTo(result.Page, new Dictionary<string, object>() { { INavigationablePage.ParametersKey, parameters } });
56-
})
48+
parameters.GetType() != result.ParametersType
49+
? Observable.Throw<Unit>(new ArgumentException("Invalid parameters type"))
50+
: _platformNavigation.GoTo(result.Page, new Dictionary<string, object>() { { INavigationablePage.ParametersKey, parameters } }))
5751
.IsEmpty()
58-
.SelectMany(isEmpty =>
59-
{
60-
if (isEmpty)
61-
{
62-
return Observable.Throw<Unit>(new ArgumentException("Error navigating to page"));
63-
}
64-
return Observable.Return(Unit.Default);
65-
});
52+
.SelectMany(isEmpty => isEmpty
53+
? Observable.Throw<Unit>(new ArgumentException("Error navigating to page"))
54+
: Observable.Return(Unit.Default));
6655
}
6756

68-
public IObservable<Unit> NavigateBack()
69-
{
70-
return _platformNavigation.GoTo("..");
71-
}
57+
public IObservable<Unit> NavigateBack() => _platformNavigation.GoTo("..");
7258
}
7359

Sylac.Mvvm.Core/RegisterMvvmPageExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.Extensions.DependencyInjection;
2+
using Sylac.Mvvm.Abstraction;
23
using Sylac.Mvvm.Navigation.Abstractions;
34

45
namespace Sylac.Mvvm;
@@ -14,10 +15,9 @@ public static IServiceCollection RegisterMvvmPage<TPage, TViewModel>(
1415
{
1516
services.Add(new(typeof(TPage), null, typeof(TPage), pageLifetime));
1617
services.Add(new(typeof(TViewModel), null, typeof(TViewModel), viewModelLifetime));
17-
18-
using var serviceProvider = services.BuildServiceProvider();
19-
var navigetionService = serviceProvider.GetRequiredService<INavigationService>();
20-
navigetionService.RegisterNavigationView<TPage, TViewModel>();
18+
services.BuildServiceProvider()
19+
.GetRequiredService<INavigationService>()
20+
.RegisterNavigationView<TPage, TViewModel>();
2121

2222
return services;
2323
}

Sylac.Mvvm.Core/Sylac.Mvvm.Core.csproj

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFramework>net9.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
7+
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
78

89
<IsPackable>true</IsPackable>
910
<PackageId>Sylac.Mvvm.Core</PackageId>
1011
<RootNamespace>Sylac.Mvvm</RootNamespace>
11-
<Version>0.2.0</Version>
12+
<Version>0.3.0</Version>
1213
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
1314
<Authors>Sylwester Łach</Authors>
1415
<RepositoryUrl>https://github.com/sylac/Sylac.Mvvm</RepositoryUrl>
1516
<RepositoryType>git</RepositoryType>
17+
<PackageReadmeFile>README.md</PackageReadmeFile>
1618
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1719
<PackageTags>nuget, packaging, generator, example</PackageTags>
1820
</PropertyGroup>
1921

2022
<ItemGroup>
21-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
23+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
2224
<PackageReference Include="System.Reactive" Version="6.0.1" />
2325
</ItemGroup>
24-
26+
27+
<ItemGroup>
28+
<None Include="..\README.md" Pack="true" PackagePath="\"/>
29+
</ItemGroup>
30+
2531
<ItemGroup>
2632
<InternalsVisibleTo Include="Sylac.Mvvm.UnitTests" />
2733
</ItemGroup>

Sylac.Mvvm.Example/.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ insert_final_newline = false
1919

2020
# Organize usings
2121
dotnet_separate_import_directive_groups = false
22-
dotnet_sort_system_directives_first = false
22+
dotnet_sort_system_directives_first = true
2323
file_header_template = unset
2424

2525
# this. and Me. preferences

Sylac.Mvvm.Example/App.xaml.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ public partial class App : Application
55
public App()
66
{
77
InitializeComponent();
8+
}
89

9-
MainPage = new AppShell();
10+
protected override Window CreateWindow(IActivationState? activationState)
11+
{
12+
return new(new AppShell());
1013
}
1114
}
1215
}

0 commit comments

Comments
 (0)