Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added MVVM choice for F# MVVM template #149

Merged
merged 3 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions templates/csharp/app-mvvm/.template.config/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
"type": "computed",
"value": "(MVVMToolkit == \"ReactiveUI\")"
},
"CommunityToolkitChosen": {
"type": "computed",
"value": "(MVVMToolkit == \"CommunityToolkit\")"
},
"AvaloniaVersion": {
"type": "parameter",
"description": "The target version of Avalonia NuGet packages.",
Expand Down
4 changes: 2 additions & 2 deletions templates/csharp/app-mvvm/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
#if (!ReactiveUIToolkitChosen)
#if (CommunityToolkitChosen)
using Avalonia.Data.Core;
using Avalonia.Data.Core.Plugins;
#endif
Expand All @@ -21,7 +21,7 @@ public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
#if (!ReactiveUIToolkitChosen)
#if (CommunityToolkitChosen)
// Line below is needed to remove Avalonia data validation.
// Without this line you will get duplicate validations from both Avalonia and CT
ExpressionObserver.DataValidators.RemoveAll(x => x is DataAnnotationsValidationPlugin);
Expand Down
2 changes: 1 addition & 1 deletion templates/csharp/app-mvvm/AvaloniaAppTemplate.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="AvaloniaVersionTemplateParameter" />
<!--#if (ReactiveUIToolkitChosen) -->
<PackageReference Include="Avalonia.ReactiveUI" Version="AvaloniaVersionTemplateParameter" />
<!--#else-->
<!--#elif (CommunityToolkitChosen)-->
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.0.0" />
<!--#endif -->
<PackageReference Include="XamlNameReferenceGenerator" Version="1.5.1" />
Expand Down
6 changes: 3 additions & 3 deletions templates/csharp/app-mvvm/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public static void Main(string[] args) => BuildAvaloniaApp()
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
#if (!ReactiveUIToolkitChosen)
.LogToTrace();
#else
#if (ReactiveUIToolkitChosen)
.LogToTrace()
.UseReactiveUI();
#else
.LogToTrace();
#endif
}
4 changes: 2 additions & 2 deletions templates/csharp/app-mvvm/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#if (!ReactiveUIToolkitChosen)
#if (CommunityToolkitChosen)
using CommunityToolkit.Mvvm.ComponentModel;
#endif

namespace AvaloniaAppTemplate.ViewModels;

#if (!ReactiveUIToolkitChosen)
#if (CommunityToolkitChosen)
public class MainWindowViewModel : ObservableObject
#else
public class MainWindowViewModel : ViewModelBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
},
"AvaloniaVersion": {
"longName": "avalonia-version"
},
"MVVMToolkit": {
"longName": "mvvm"
}
},
"usageExamples": [
""
"--mvvm communitytoolkit"
]
}
7 changes: 7 additions & 0 deletions templates/fsharp/app-mvvm/.template.config/ide.host.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
"text": "Avalonia Version"
},
"isVisible": true
},
{
"id": "MVVMToolkit",
"name": {
"text": "MVVM Toolkit"
},
"isVisible": true
}
]
}
38 changes: 37 additions & 1 deletion templates/fsharp/app-mvvm/.template.config/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,30 @@
"replaces": "FrameworkParameter",
"defaultValue": "net6.0"
},
"MVVMToolkit": {
"type": "parameter",
"description": "MVVM toolkit to use in the template.",
"datatype": "choice",
"choices": [
{
"choice": "ReactiveUI",
"description": "Choose ReactiveUI as MVVM toolkit in the template."
},
{
"choice": "CommunityToolkit",
"description": "Choose CommunityToolkit as MVVM toolkit in the template."
}
],
"defaultValue": "ReactiveUI"
},
"ReactiveUIToolkitChosen": {
"type": "computed",
"value": "(MVVMToolkit == \"ReactiveUI\")"
},
"CommunityToolkitChosen": {
"type": "computed",
"value": "(MVVMToolkit == \"CommunityToolkit\")"
},
"AvaloniaVersion": {
"type": "parameter",
"description": "The target version of Avalonia NuGet packages.",
Expand All @@ -53,5 +77,17 @@
"type": "computed",
"value": "(AvaloniaVersion == \"0.10.18\")"
}
}
},
"sources": [
{
"modifiers": [
{
"condition": "(!ReactiveUIToolkitChosen)",
"exclude": [
"ViewModels/ViewModelBase.fs"
]
}
]
}
]
}
11 changes: 11 additions & 0 deletions templates/fsharp/app-mvvm/App.axaml.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ namespace AvaloniaAppTemplate

open Avalonia
open Avalonia.Controls.ApplicationLifetimes
#if (CommunityToolkitChosen)
open Avalonia.Data.Core
open Avalonia.Data.Core.Plugins
#endif
open Avalonia.Markup.Xaml
open AvaloniaAppTemplate.ViewModels
open AvaloniaAppTemplate.Views
Expand All @@ -13,6 +17,13 @@ type App() =
AvaloniaXamlLoader.Load(this)

override this.OnFrameworkInitializationCompleted() =

#if (CommunityToolkitChosen)
// Line below is needed to remove Avalonia data validation.
// Without this line you will get duplicate validations from both Avalonia and CT
ExpressionObserver.DataValidators.RemoveAll(fun x -> x :? DataAnnotationsValidationPlugin) |> ignore
#endif

match this.ApplicationLifetime with
| :? IClassicDesktopStyleApplicationLifetime as desktop ->
desktop.MainWindow <- MainWindow(DataContext = MainWindowViewModel())
Expand Down
8 changes: 7 additions & 1 deletion templates/fsharp/app-mvvm/AvaloniaAppTemplate.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
</ItemGroup>

<ItemGroup>
<!--#if (ReactiveUIToolkitChosen) -->
<Compile Include="ViewModels\ViewModelBase.fs" />
<!--#endif -->
<Compile Include="ViewModels\MainWindowViewModel.fs" />
<Compile Include="Views\MainWindow.axaml.fs" />
<Compile Include="ViewLocator.fs" />
Expand All @@ -33,7 +35,11 @@
<!--#endif -->
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="AvaloniaVersionTemplateParameter" />
<!--#if (ReactiveUIToolkitChosen) -->
<PackageReference Include="Avalonia.ReactiveUI" Version="AvaloniaVersionTemplateParameter" />
<PackageReference Include="XamlNameReferenceGenerator" Version="1.3.4" />
<!--#elif (CommunityToolkitChosen)-->
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.0.0" />
<!--#endif -->
<PackageReference Include="XamlNameReferenceGenerator" Version="1.5.1" />
</ItemGroup>
</Project>
4 changes: 4 additions & 0 deletions templates/fsharp/app-mvvm/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

open System
open Avalonia
#if (ReactiveUIToolkitChosen)
open Avalonia.ReactiveUI
#endif
open AvaloniaAppTemplate

module Program =
Expand All @@ -13,7 +15,9 @@ module Program =
.Configure<App>()
.UsePlatformDetect()
.LogToTrace(areas = Array.empty)
#if (ReactiveUIToolkitChosen)
.UseReactiveUI()
#endif

[<EntryPoint; STAThread>]
let main argv =
Expand Down
11 changes: 10 additions & 1 deletion templates/fsharp/app-mvvm/ViewLocator.fs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
namespace AvaloniaAppTemplate

open System
#if (!ReactiveUIToolkitChosen)
open System.ComponentModel
#endif
open Avalonia.Controls
open Avalonia.Controls.Templates
#if (ReactiveUIToolkitChosen)
open AvaloniaAppTemplate.ViewModels
#endif

type ViewLocator() =
interface IDataTemplate with
Expand All @@ -15,5 +20,9 @@ type ViewLocator() =
upcast TextBlock(Text = sprintf "Not Found: %s" name)
else
downcast Activator.CreateInstance(typ)


#if (ReactiveUIToolkitChosen)
member this.Match(data) = data :? ViewModelBase
#else
member this.Match(data) = data :? INotifyPropertyChanged
#endif
8 changes: 8 additions & 0 deletions templates/fsharp/app-mvvm/ViewModels/MainWindowViewModel.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
namespace AvaloniaAppTemplate.ViewModels

#if (CommunityToolkitChosen)
open CommunityToolkit.Mvvm.ComponentModel;
#endif

type MainWindowViewModel() =
#if (CommunityToolkitChosen)
inherit ObservableObject()
#else
inherit ViewModelBase()
#endif

member this.Greeting = "Welcome to Avalonia!"
2 changes: 2 additions & 0 deletions tests/build-test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,7 @@ Test-Template "avalonia.mvvm" "AvaloniaMvvm" "F#" "f" "net6.0" $binlog
Test-Template "avalonia.mvvm" "AvaloniaMvvm" "F#" "f" "net7.0" $binlog
Create-And-Build "avalonia.mvvm" "AvaloniaMvvm" "F#" "av" "0.10.18" $binlog
Create-And-Build "avalonia.mvvm" "AvaloniaMvvm" "F#" "av" "11.0.0-preview4" $binlog
Create-And-Build "avalonia.mvvm" "AvaloniaMvvm" "F#" "m" "ReactiveUI" $binlog
Create-And-Build "avalonia.mvvm" "AvaloniaMvvm" "F#" "m" "CommunityToolkit" $binlog

Create-And-Build "avalonia.xplat" "AvaloniaXplat" "F#" "f" "net7.0" $binlog