Skip to content

Commit

Permalink
Merge pull request #155 from MartinZikmund/feature/windowshell
Browse files Browse the repository at this point in the history
Start creating scoped WindowShell, port views
  • Loading branch information
MartinZikmund authored Nov 13, 2023
2 parents 9a423a9 + 58e5751 commit 36c2bc6
Show file tree
Hide file tree
Showing 39 changed files with 592 additions and 198 deletions.
3 changes: 2 additions & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
PRI257: Ignore default language (en) not being one of the included resources (eg en-us, en-uk)
-->
<NoWarn>$(NoWarn);NU1507;NETSDK1201;PRI257</NoWarn>

<WarningsNotAsErrors>Uno0001</WarningsNotAsErrors>

<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
<EnableWindowsTargeting>true</EnableWindowsTargeting>

Expand Down
5 changes: 3 additions & 2 deletions src/MZikmund.sln
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MZikmund.DataContracts", "a
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shared", "Shared", "{6AD11812-0C83-402E-83D6-6D46B2CB7AF5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MZikmund.Shared", "shared\MZikmund.Shared\MZikmund.Shared.csproj", "{AA373B1D-F17C-4F24-86E5-F6DB8688434A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MZikmund.Shared", "shared\MZikmund.Shared\MZikmund.Shared.csproj", "{AA373B1D-F17C-4F24-86E5-F6DB8688434A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MZikmund.Api", "shared\MZikmund.Api\MZikmund.Api.csproj", "{55640146-2DFC-4B9F-8A6E-F44C4D74950D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MZikmund.Api", "shared\MZikmund.Api\MZikmund.Api.csproj", "{55640146-2DFC-4B9F-8A6E-F44C4D74950D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -902,6 +902,7 @@ Global
{6A207857-8DAD-47D3-AAEB-CEF69C468841}.AppStore|x86.Build.0 = Debug|x86
{6A207857-8DAD-47D3-AAEB-CEF69C468841}.Debug|Any CPU.ActiveCfg = Debug|x64
{6A207857-8DAD-47D3-AAEB-CEF69C468841}.Debug|Any CPU.Build.0 = Debug|x64
{6A207857-8DAD-47D3-AAEB-CEF69C468841}.Debug|Any CPU.Deploy.0 = Debug|x64
{6A207857-8DAD-47D3-AAEB-CEF69C468841}.Debug|ARM.ActiveCfg = Debug|x64
{6A207857-8DAD-47D3-AAEB-CEF69C468841}.Debug|ARM.Build.0 = Debug|x64
{6A207857-8DAD-47D3-AAEB-CEF69C468841}.Debug|ARM64.ActiveCfg = Debug|arm64
Expand Down
74 changes: 59 additions & 15 deletions src/app/MZikmund/App.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
using MZikmund.ViewModels;
using MZikmund.Services.Preferences;
using MZikmund.Services.Navigation;
using MZikmund.Services.Dialogs;
using MZikmund.Services.Account;
using MZikmund.ViewModels.Admin;
using MZikmund.Api.Client;
using Refit;
using CommunityToolkit.Mvvm.DependencyInjection;
using MZikmund.Services.Loading;

namespace MZikmund;

public class App : Application
{
protected Window? MainWindow { get; private set; }

protected IHost? Host { get; private set; }

protected override void OnLaunched(LaunchActivatedEventArgs args)
Expand Down Expand Up @@ -62,11 +74,7 @@ protected override void OnLaunched(LaunchActivatedEventArgs args)
#endif
.AddSingleton<IWeatherCache, WeatherCache>()
.AddRefitClient<IApiClient>(context))
.ConfigureServices((context, services) =>
{
// TODO: Register your services
//services.AddSingleton<IMyService, MyService>();
})
.ConfigureServices((context, services) => ConfigureServices(services))
);
MainWindow = builder.Window;

Expand All @@ -78,23 +86,59 @@ protected override void OnLaunched(LaunchActivatedEventArgs args)

// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (MainWindow.Content is not Frame rootFrame)
if (MainWindow.Content is not WindowShell windowShell)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();
windowShell = new WindowShell(Host.Services);

// Place the frame in the current Window
MainWindow.Content = rootFrame;
MainWindow.Content = windowShell;
}

if (rootFrame.Content == null)
{
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
rootFrame.Navigate(typeof(MainPage), args.Arguments);
}
//if (windowShell.Content == null)
//{
// // When the navigation stack isn't restored navigate to the first page,
// // configuring the new page by passing required information as a navigation
// // parameter
// windowShell.Navigate(typeof(MainPage), args.Arguments);
//}
// Ensure the current window is active
MainWindow.Activate();
}

private static void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<SettingsViewModel>();
services.AddScoped<WindowShellViewModel>();
services.AddSingleton<BlogTagsManagerViewModel>();
services.AddSingleton<BlogCategoriesManagerViewModel>();
services.AddSingleton<AddOrUpdateBlogCategoryDialogViewModel>();
services.AddSingleton<AddOrUpdateBlogTagDialogViewModel>();

services.AddSingleton<IPreferencesService, PreferencesService>();
services.AddScoped<IDialogCoordinator, DialogCoordinator>();
services.AddScoped<IFrameProvider, FrameProvider>();
services.AddScoped<INavigationService, NavigationService>();
services.AddScoped<ILoadingIndicator, LoadingIndicator>();
services.AddScoped<IDialogService, DialogService>();
services.AddScoped<IWindowShellProvider, WindowShellProvider>();
services.AddSingleton(provider =>
{
return RestService.For<IMZikmundApi>("https://localhost:5001/api", new RefitSettings()
{
AuthorizationHeaderValueGetter = GetTokenAsync
});
});

async Task<string> GetTokenAsync(HttpRequestMessage message, CancellationToken cancellationToken)
{
//TODO: Move somewhere more appropriate and integrate refresh token support
var userService = Ioc.Default.GetRequiredService<IUserService>();
if (!userService.IsLoggedIn)
{
await userService.AuthenticateAsync();
}
return userService.AccessToken!;
};
}
}
13 changes: 7 additions & 6 deletions src/app/MZikmund/AppResources.xaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<ResourceDictionary.MergedDictionaries>
<!-- Load WinUI resources -->
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />

</ResourceDictionary.MergedDictionaries>
<!-- Add resources here -->
<ResourceDictionary.MergedDictionaries>
<!-- Load WinUI resources -->
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
<ResourceDictionary Source="ms-appx:///MZikmund/Resources/Colors.xaml" />
<ResourceDictionary Source="ms-appx:///MZikmund/Resources/Converters.xaml" />
<ResourceDictionary Source="ms-appx:///MZikmund/Resources/Fonts.xaml" />
</ResourceDictionary.MergedDictionaries>

</ResourceDictionary>
127 changes: 0 additions & 127 deletions src/app/MZikmund/AppShell.xaml.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/app/MZikmund/Dialogs/DialogBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MZikmund.ViewModels.Abstract;
using MZikmund.ViewModels;

namespace MZikmund.Dialogs;

Expand Down
11 changes: 10 additions & 1 deletion src/app/MZikmund/MZikmund.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows')) or '$(EnableWindowsTargeting)' == 'true'">$(TargetFrameworks);net8.0-windows10.0.19041</TargetFrameworks>
<TargetFrameworks>$(TargetFrameworks);net8.0;net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks>
Expand Down Expand Up @@ -77,4 +77,13 @@
<EmbeddedResource Include="appsettings.*.json" DependentUpon="appsettings.json" />
<UpToDateCheckInput Include="**\*.xaml" Exclude="bin\**\*.xaml;obj\**\*.xaml" />
</ItemGroup>

<ItemGroup>
<None Remove="Views\SettingsView.xaml" />
</ItemGroup>

<ItemGroup>
<UpToDateCheckInput Remove="Views\SettingsView.xaml" />
<UpToDateCheckInput Remove="WindowShell.xaml" />
</ItemGroup>
</Project>
13 changes: 0 additions & 13 deletions src/app/MZikmund/MainPage.xaml

This file was deleted.

9 changes: 0 additions & 9 deletions src/app/MZikmund/MainPage.xaml.cs

This file was deleted.

19 changes: 14 additions & 5 deletions src/app/MZikmund/Services/Loading/LoadingIndicator.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
namespace MZikmund.Services.Loading;
using MZikmund.Services.Navigation;

namespace MZikmund.Services.Loading;

public class LoadingIndicator : ILoadingIndicator
{
public IDisposable BeginLoading() => AppShell.GetForCurrentView().ViewModel.BeginLoading();
private readonly IWindowShellProvider _windowShellProvider;

public LoadingIndicator(IWindowShellProvider windowShellProvider)
{
_windowShellProvider = windowShellProvider;
}

public IDisposable BeginLoading() => _windowShellProvider.ViewModel.BeginLoading();

public bool IsLoading => AppShell.GetForCurrentView().ViewModel.IsLoading;
public bool IsLoading => _windowShellProvider.ViewModel.IsLoading;

public string StatusMessage
{
get => AppShell.GetForCurrentView().ViewModel.LoadingStatusMessage;
set => AppShell.GetForCurrentView().ViewModel.LoadingStatusMessage = value;
get => _windowShellProvider.ViewModel.LoadingStatusMessage;
set => _windowShellProvider.ViewModel.LoadingStatusMessage = value;
}
}
9 changes: 8 additions & 1 deletion src/app/MZikmund/Services/Navigation/FrameProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,12 @@

public class FrameProvider : IFrameProvider
{
public Frame GetForCurrentView() => AppShell.GetForCurrentView().RootFrame;
private readonly IWindowShellProvider _windowShellProvider;

public FrameProvider(IWindowShellProvider windowShellProvider)
{
_windowShellProvider = windowShellProvider;
}

public Frame GetForCurrentView() => _windowShellProvider.WindowShell.RootFrame;
}
14 changes: 14 additions & 0 deletions src/app/MZikmund/Services/Navigation/IWindowShellProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using MZikmund.ViewModels;

namespace MZikmund.Services.Navigation;

public interface IWindowShellProvider
{
WindowShellViewModel ViewModel { get; }

WindowShell WindowShell { get; }

XamlRoot XamlRoot { get; }

IServiceProvider ServiceProvider { get; }
}
1 change: 1 addition & 0 deletions src/app/MZikmund/Services/Navigation/NavigationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ private bool TryFindViewForViewModel(Type viewModelType, out Type? viewType)

public void RegisterViewsFromAssembly(Assembly sourceAssembly)
{
// TODO: Avoid reflection
var pageType = typeof(Page);
var pages = sourceAssembly.GetTypes().Where(t => pageType.IsAssignableFrom(t) && t.Name.EndsWith("View", StringComparison.OrdinalIgnoreCase)).ToArray();
foreach (var viewType in pages)
Expand Down
Loading

0 comments on commit 36c2bc6

Please sign in to comment.