Skip to content

Commit

Permalink
More good practice setup
Browse files Browse the repository at this point in the history
  • Loading branch information
aritchie committed Dec 16, 2024
1 parent 00dcec2 commit dc0ff20
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 29 deletions.
5 changes: 3 additions & 2 deletions ProjectTemplates/ShinyApp/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
global using Shiny;
global using ShinyApp.Services;
global using Shiny;
#if (shinymediator)
global using Shiny.Mediator;
#endif
Expand All @@ -18,7 +19,7 @@
#endif
#if reactiveui
global using ReactiveUI;
global using ReactiveUI.Fody.Helpers;
global using ReactiveUI.SourceGenerators;
#endif
#if prism
global using Prism.AppModel;
Expand Down
10 changes: 4 additions & 6 deletions ProjectTemplates/ShinyApp/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
namespace ShinyApp;

#if reactiveui
public class MainViewModel : ViewModel
public class MainViewModel(BaseServices services) : ViewModel(services)
{
public MainViewModel(BaseServices services) : base(services) {}


[Reactive] string property;
}
#elif ctmvvm
public partial class MainViewModel : ObservableObject
public partial class MainViewModel(BaseServices services) : ObservableObject
{
[ObservableProperty] string property;
[ObservableProperty]
public partial string Property { get; set; }

[RelayCommand]
async Task DoSomething()
Expand Down
8 changes: 5 additions & 3 deletions ProjectTemplates/ShinyApp/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static MauiApp CreateMauiApp()
var builder = MauiApp
.CreateBuilder()
.UseMauiApp<App>()
.uSeShiny()
.UseShiny()
#if userdialogs
.UseUserDialogs()
#endif
Expand Down Expand Up @@ -219,11 +219,11 @@ public static MauiApp CreateMauiApp()
builder.Configuration.AddJsonPlatformBundle();
#endif
#if (remoteconfig)
builder.AddRemoteConfigurationMaui();
builder.AddRemoteConfigurationMaui("https://todo");
// builder.Services.AddOptions<MyConfig>().BindConfiguration("");
#endif
builder.Logging.EnableEnrichment();
builder.Services.AddSingleton<ILogEnricher, AppLogEnricher>();
builder.Services.AddSingleton<ILogEnricher, ShinyApp.Infrastructure.AppLogEnricher>();
#if sqlitelogging
builder.Logging.AddSqlite(Path.Combine(FileSystem.AppDataDirectory, "logging.db"));
#endif
Expand Down Expand Up @@ -397,8 +397,10 @@ public static MauiApp CreateMauiApp()
#endif

#if (prism)
builder.Services.AddScoped<BaseServices>();
builder.Services.RegisterForNavigation<MainPage, MainViewModel>();
#else
builder.Services.AddSingleton<BaseServices>();
builder.Services.AddTransient<MainPage>();
builder.Services.AddTransient<MainViewModel>();
#endif
Expand Down
14 changes: 14 additions & 0 deletions ProjectTemplates/ShinyApp/Services/BaseServices.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public record BaseServices(
IConfiguration Configuration,
#if prism
INavigationService Navigator,
IDialogService Dialogs,
#endif
#if settings
AppSettings Settings,
#endif
#if authservice
IAuthenticationService Authentication,
#endif
ILoggerFactory LoggerFactory
);
36 changes: 18 additions & 18 deletions ProjectTemplates/ShinyApp/ShinyApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<PropertyGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">
<DefineConstants>$(DefineConstants);PLATFORM</DefineConstants>
<SupportedOSPlatformVersion>26.0</SupportedOSPlatformVersion>
<TargetPlatformVersion>34</TargetPlatformVersion>
<TargetPlatformVersion>35</TargetPlatformVersion>
<!--
<EmbedAssembliesIntoApk Condition="'$(Configuration)' == 'Debug'">true</EmbedAssembliesIntoApk>
<AndroidPackageFormats Condition="'$(Configuration)' == 'Release'">aab</AndroidPackageFormats>
Expand Down Expand Up @@ -201,7 +201,7 @@
<!--#endif-->
<!--#if (remoteconfig)-->
<PackageReference Include="Shiny.Extensions.Configuration.Remote.Maui" Version="1.0.0-pre-0008" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="9.0.0" />
<!--#endif-->
<!--#if (bindablegenerator)-->
<PackageReference Include="M.BindableProperty.Generator" Version="0.11.1" />
Expand All @@ -210,7 +210,7 @@
<PackageReference Include="Microsoft.Extensions.Localization" Version="9.0.0" />
<!--#endif-->
<!--#if (usemsal)-->
<PackageReference Include="Microsoft.Identity.Client" Version="4.60.3" />
<PackageReference Include="Microsoft.Identity.Client" Version="4.66.2" />
<!--#endif-->
<!--#if (useconfig)-->
<PackageReference Include="Shiny.Extensions.Configuration" Version="$(ShinyVersion)" />
Expand Down Expand Up @@ -255,13 +255,13 @@
<PackageReference Include="Redth.Maui.VirtualListView" Version="0.4.0" />
<!--#endif-->
<!--#if (barcodes)-->
<PackageReference Include="BarcodeScanning.Native.Maui" Version="1.5.9" />
<PackageReference Include="BarcodeScanning.Native.Maui" Version="2.1.3" />
<!--#endif-->
<!--#if (storereview)-->
<PackageReference Include="Plugin.StoreReview" Version="6.2.0" />
<!--#endif-->
<!--#if (inappbilling)-->
<PackageReference Include="Plugin.InAppBilling" Version="8.0.4" />
<PackageReference Include="Plugin.InAppBilling" Version="8.0.5" />
<!--#endif-->
<!--#if (calendar)-->
<PackageReference Include="Plugin.Maui.CalendarStore" Version="2.0.0" />
Expand All @@ -270,25 +270,25 @@
<PackageReference Include="Plugin.Maui.Audio" Version="3.0.1" />
<!--#endif-->
<!--#if (ocr)-->
<PackageReference Include="Plugin.Maui.OCR" Version="1.0.12" />
<PackageReference Include="Plugin.Maui.OCR" Version="1.0.15" />
<!--#endif-->
<!--#if (fingerprint)-->
<PackageReference Include="Oscore.Maui.Biometric" Version="1.0.1" />
<PackageReference Include="Oscore.Maui.Biometric" Version="2.1.0" />
<!--#endif-->
<!--#if usehttp-->
<PackageReference Include="Refit" Version="7.2.1" />
<PackageReference Include="Refit" Version="8.0.0" />
<!--#endif-->
<!--#if (sqlite)-->
<PackageReference Include="sqlite-net-pcl" Version="1.9.172" />
<!--#endif-->
<!--#if (sharpnadotabs)-->
<PackageReference Include="Sharpnado.Tabs.Maui" Version="3.0.1" />
<PackageReference Include="Sharpnado.Tabs.Maui" Version="3.2.1" />
<!--#endif-->
<!--#if (sharpnadocv)-->
<PackageReference Include="Sharpnado.CollectionView.Maui" Version="3.1.2" />
<!--#endif-->
<!--#if (settingsview)-->
<PackageReference Include="AiForms.Maui.SettingsView" Version="1.0.15" />
<PackageReference Include="AiForms.Maui.SettingsView" Version="1.0.16" />
<!--#endif-->
<!--#if (livecharts)-->
<PackageReference Include="LiveChartsCore.SkiaSharpView.Maui" Version="2.0.0-rc2" />
Expand All @@ -300,7 +300,7 @@
<PackageReference Include="AlohaKit.Animations" Version="1.1.0" />
<!--#endif-->
<!--#if (cards)-->
<PackageReference Include="CardsView.Maui" Version="1.0.2" />
<PackageReference Include="CardsView.Maui" Version="1.1.0" />
<!--#endif-->
<!--#if (uraniumui)-->
<PackageReference Include="UraniumUI.Material" Version="2.11.0" />
Expand All @@ -323,7 +323,7 @@
<PackageReference Include="Plugin.Maui.ScreenBrightness" Version="1.0.0-preview3" />
<!--#endif-->
<!--#if (skia)-->
<PackageReference Include="SkiaSharp.Views.Maui.Controls" Version="2.88.8" />
<PackageReference Include="SkiaSharp.Views.Maui.Controls" Version="3.116.1" />
<!--#endif-->
<!--#if (skiaextended)-->
<PackageReference Include="SkiaSharp.Extended.UI.Maui" Version="2.0.0" />
Expand All @@ -341,21 +341,21 @@
<PackageReference Include="EmbedIO" Version="3.5.2" />
<!--#endif-->
<!--#if (mudblazor)-->
<PackageReference Include="MudBlazor" Version="7.8.0" />
<PackageReference Include="MudBlazor" Version="7.15.0" />
<!--#endif-->
<!--#if (radzen)-->
<PackageReference Include="Radzen.Blazor" Version="5.2.9" />
<PackageReference Include="Radzen.Blazor" Version="5.6.15" />
<!--#endif-->
<!--#if (fluentui)-->
<PackageReference Include="Microsoft.FluentUI.AspNetCore.Components" Version="4.10.1" />
<PackageReference Include="Microsoft.FluentUI.AspNetCore.Components.Icons" Version="4.10.1" />
<PackageReference Include="Microsoft.FluentUI.AspNetCore.Components" Version="4.10.4" />
<PackageReference Include="Microsoft.FluentUI.AspNetCore.Components.Icons" Version="4.10.4" />
<PackageReference Include="Microsoft.FluentUI.AspNetCore.Components.Emoji" Version="4.6.0" />
<!--#endif-->
<!--#if (syslinqasync)-->
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
<!--#endif-->
<!--#if (unitsnet)-->
<PackageReference Include="UnitsNet" Version="5.59.0" />
<PackageReference Include="UnitsNet" Version="5.61.0" />
<!--#endif-->
<!--#if (humanizer)-->
<PackageReference Include="Humanizer" Version="2.14.1" />
Expand All @@ -365,7 +365,7 @@
<!--#if (androidauto || storereview)-->
<ItemGroup Condition="$(TargetFramework.Contains('android'))">
<!--#if (androidauto)-->
<PackageReference Include="Xamarin.AndroidX.Car.App.App" Version="1.2.0.9" />
<PackageReference Include="Xamarin.AndroidX.Car.App.App" Version="1.4.0.5" />
<!--#endif-->
<!--#if (storereview)-->
<ProguardConfiguration Include="Platforms/Android/proguard.cfg" />
Expand Down
88 changes: 88 additions & 0 deletions ProjectTemplates/ShinyApp/ViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#if ctmvvm
public abstract partial class ViewModel(BaseServices services) : ObservableValidator
#elif reactiveui
public abstract partial class ViewModel(BaseServices services) : ReactiveObject
#else
public abstract class ViewModel(BaseServices services) : Shiny.NotifyPropertyChanged
#endif
#if prism
, IInitialize,
IPageLifecycleAware,
IApplicationLifecycleAware,
INavigationAware,
IConfirmNavigationAsync,
IDestructible
#endif
{
protected BaseServices Services => services;

#if prism
public virtual void Initialize(INavigationParameters parameters) {}
public virtual void OnAppearing() {}
public virtual void OnDisappearing() => this.Deactivate();

public virtual void OnResume() => this.OnAppearing();
public virtual void OnSleep() => this.OnDisappearing();
public virtual void OnNavigatedFrom(INavigationParameters parameters) {}
public virtual void OnNavigatedTo(INavigationParameters parameters) {}

public virtual Task<bool> CanNavigateAsync(INavigationParameters parameters)
=> Task.FromResult(true);

public virtual void Destroy()
{
this.destroyWith?.Dispose();
this.destroyToken?.Cancel();
this.destroyToken?.Dispose();
this.Deactivate();
}

CompositeDisposable? deactivateWith;
protected CompositeDisposable DeactivateWith => this.deactivateWith ??= new CompositeDisposable();

CompositeDisposable? destroyWith;
protected CompositeDisposable DestroyWith => this.destroyWith ??= new CompositeDisposable();


CancellationTokenSource? deactiveToken;
/// <summary>
/// The destroy cancellation token - called when your model is deactivated
/// </summary>
protected CancellationToken DeactivateToken
{
get
{
this.deactiveToken ??= new CancellationTokenSource();
return this.deactiveToken.Token;
}
}


CancellationTokenSource? destroyToken;
/// <summary>
/// The destroy cancellation token - called when your model is destroyed
/// </summary>
protected CancellationToken DestroyToken
{
get
{
this.destroyToken ??= new CancellationTokenSource();
return this.destroyToken.Token;
}
}


/// <summary>
/// This can be called manually, generally used when your viewmodel is going to the background in the nav stack
/// </summary>
protected virtual void Deactivate()
{
this.deactivateWith?.Dispose();
this.deactivateWith = null;

this.deactiveToken?.Cancel();
this.deactiveToken?.Dispose();
this.deactiveToken = null;
}
#endif
}

0 comments on commit dc0ff20

Please sign in to comment.