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

feat: add a forced update feature #396

Merged
merged 2 commits into from
Apr 18, 2024
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)

Prefix your items with `(Template)` if the change is about the template and not the resulting application.

## 3.3.X
- Added a forced update feature to the app.

## 3.2.X
- Added support for mouse back button navigation.
- Set `fetchDepth` to 0 on canary merge CI to avoid `refusing to merge unrelated histories` errors.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Uno Platform Application Template
# Uno Platform Application Template

[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg?style=flat-square)](LICENSE) ![Version](https://img.shields.io/nuget/v/NV.Templates.Mobile?style=flat-square) ![Downloads](https://img.shields.io/nuget/dt/NV.Templates.Mobile?style=flat-square)

Expand Down
4 changes: 4 additions & 0 deletions doc/Architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ Business services are always declared using an interface and implemented in a se

This application uses [DynamicData](https://github.com/reactivemarbles/DynamicData) to expose observable lists from business services. These can then be used in the presentation layer to create ViewModels that are automatically disposed when their associated items are removed from the list.

### Forced Update

This application uses the [IUpdateRequiredService](/src/app/ApplicationTemplate.Business/ForcedUpdates/IUpdateRequiredService.cs) which exposes an event that allows you to know when you should redirect the user to a page that will lead him to the appstore where he can update the app.

## Presentation

### MVVM - ViewModels
Expand Down
8 changes: 8 additions & 0 deletions doc/ForcedUpdate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Forced Update

Marc-Antoine-Soucy marked this conversation as resolved.
Show resolved Hide resolved
The forced update feature is for when you want to force the user to update the app.
You could use this, for example, when the backend changes and you do not want the users to still use the old API.

To force an update, we suscribe to the `UpdateRequired` `event` from the `UpdateRequiredservice` in the [CoreStartup](../src/app/ApplicationTemplate.Presentation/CoreStartup.cs#L203).

This will direct the user to a page from which they cannot navigate back. The page will contain a button that leads them to the appropriate page for updating the app, with the links defined in `AppSettings`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;

namespace ApplicationTemplate.DataAccess;

/// <summary>
/// Gets the minimum version required to use the application.
/// </summary>
public interface IMinimumVersionReposiory
{
/// <summary>
/// Checks the minimum required version.
/// </summary>
void CheckMinimumVersion();

/// <summary>
/// An observable that emits the minimum version required to use the application.
/// </summary>
IObservable<Version> MinimumVersionObservable { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Reactive.Subjects;

namespace ApplicationTemplate.DataAccess;

/// <summary>
/// A mock implementation of the minimum version repository. Used for testing.
/// </summary>
public sealed class MinimumVersionRepositoryMock : IMinimumVersionReposiory, IDisposable
{
private readonly Subject<Version> _minimumVersionSubject = new();

/// <inheritdoc/>
public IObservable<Version> MinimumVersionObservable => _minimumVersionSubject;

/// <inheritdoc/>
public void CheckMinimumVersion()
{
_minimumVersionSubject.OnNext(new Version(1, 0, 0, 0));
}

/// <inheritdoc/>
public void Dispose()
{
_minimumVersionSubject.Dispose();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;

namespace ApplicationTemplate;

/// <summary>
/// Contains the configuration for the application store URLs.
/// </summary>
public sealed class ApplicationStoreUrisOptions
{
/// <summary>
/// The URL to the application store for Android.
/// </summary>
public Uri Android { get; set; }

/// <summary>
/// The URL to the application store for iOS.
/// </summary>
public Uri Ios { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

namespace ApplicationTemplate.Business;

/// <summary>
/// This service checks if the application is running the minimum required version.
/// </summary>
public interface IUpdateRequiredService
{
/// <summary>
/// Event that is raised when the application needs to be updated.
/// </summary>
/// <remarks>This is using a plain event with no arguments because this is an event that should only be raised once,
/// and requires the application to be relaunched after being updated.</remarks>
event EventHandler UpdateRequired;
Marc-Antoine-Soucy marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Reactive.Linq;
using ApplicationTemplate.DataAccess;

namespace ApplicationTemplate.Business;

/// <summary>
/// Implementation of the <see cref="IUpdateRequiredService"/>.
/// </summary>
public sealed class UpdateRequiredService : IUpdateRequiredService, IDisposable
{
private readonly IDisposable _subscription;

/// <summary>
/// Initializes a new instance of the <see cref="UpdateRequiredService"/> class.
/// </summary>
/// <param name="minimumVersionReposiory">A repository that contains an observable we can use to update the app.</param>
public UpdateRequiredService(IMinimumVersionReposiory minimumVersionReposiory)
{
_subscription = minimumVersionReposiory.MinimumVersionObservable
.Subscribe(_ => UpdateRequired?.Invoke(this, EventArgs.Empty));
}

/// <inheritdoc />
public event EventHandler UpdateRequired;

/// <inheritdoc />
public void Dispose()
{
_subscription.Dispose();
UpdateRequired = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public static IServiceCollection AddApi(this IServiceCollection services, IConfi
.AddAuthentication()
.AddPosts(configuration)
.AddUserProfile()
.AddMinimumVersion()
.AddDadJokes(configuration);

return services;
Expand All @@ -53,6 +54,12 @@ private static IServiceCollection AddUserProfile(this IServiceCollection service
return services.AddSingleton<IUserProfileRepository, UserProfileRepositoryMock>();
}

private static IServiceCollection AddMinimumVersion(this IServiceCollection services)
{
// This one doesn't have an actual remote API yet. It's always a mock implementation.
return services.AddSingleton<IMinimumVersionReposiory, MinimumVersionRepositoryMock>();
}

private static IServiceCollection AddAuthentication(this IServiceCollection services)
{
// This one doesn't have an actual remote API yet. It's always a mock implementation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static IServiceCollection AddAppServices(this IServiceCollection services
.AddSingleton<IDadJokesService, DadJokesService>()
.AddSingleton<IAuthenticationService, AuthenticationService>()
.AddSingleton<IUserProfileService, UserProfileService>()
.AddSingleton<IUpdateRequiredService, UpdateRequiredService>()
.AddSingleton<DiagnosticsCountersService>();
}
}
20 changes: 20 additions & 0 deletions src/app/ApplicationTemplate.Presentation/CoreStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ protected override async Task StartServices(IServiceProvider services, bool isFi
NotifyUserOnSessionExpired(services);
services.GetRequiredService<DiagnosticsCountersService>().Start();
await ExecuteInitialNavigation(CancellationToken.None, services);
SuscribeToRequiredUpdate(services);
}
}

Expand Down Expand Up @@ -199,6 +200,25 @@ private static void HandleUnhandledExceptions(IServiceProvider services)
};
}

private void SuscribeToRequiredUpdate(IServiceProvider services)
{
var updateRequiredService = services.GetRequiredService<IUpdateRequiredService>();

updateRequiredService.UpdateRequired += ForceUpdate;

void ForceUpdate(object sender, EventArgs e)
{
var navigationController = services.GetRequiredService<ISectionsNavigator>();

_ = Task.Run(async () =>
{
await navigationController.NavigateAndClear(CancellationToken.None, () => new ForcedUpdatePageViewModel());
});

updateRequiredService.UpdateRequired -= ForceUpdate;
}
}

/// <summary>
/// Starts deactivating and reactivating ViewModels based on navigation.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace ApplicationTemplate;

/// <summary>
/// Provides the Uri to the app store for the current platform.
/// </summary>
public interface IAppStoreUriProvider
{
/// <summary>
/// Gets the Uri to the app store for the current platform.
/// </summary>
/// <returns>The Uri to the app store for the current platform.</returns>
Uri GetAppStoreUri();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using ApplicationTemplate.DataAccess;
using Chinook.DynamicMvvm;
using Chinook.SectionsNavigation;
using Uno;
Expand Down Expand Up @@ -39,4 +40,12 @@ public NavigationDebuggerViewModel()
{
await CoreStartup.ExecuteInitialNavigation(ct, ServiceProvider);
});

/// <summary>
/// Gets a command that raises the event that triggers the navigation to the force update page.
/// </summary>
public IDynamicCommand NavigateToForceUpdatePage => this.GetCommand(() =>
{
this.GetService<IMinimumVersionReposiory>().CheckMinimumVersion();
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Chinook.DynamicMvvm;

namespace ApplicationTemplate.Presentation;

/// <summary>
/// The ViewModel for the forced update page.
/// </summary>
public sealed class ForcedUpdatePageViewModel : ViewModel
{
/// <summary>
/// Navigates to the App Store.
/// </summary>
public IDynamicCommand NavigateToStore => this.GetCommandFromTask(async ct =>
{
var uriProvider = this.GetService<IAppStoreUriProvider>();

var uri = uriProvider.GetAppStoreUri();

await this.GetService<ILauncherService>().Launch(uri);
});
}
5 changes: 5 additions & 0 deletions src/app/ApplicationTemplate.Presentation/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,10 @@
},
"Mock": {
"IsMockEnabled": false
},
"ApplicationStoreUrls": {
// TODO: Update the URLs with the actual URLs.
"IOS": "https://apps.apple.com/us/app/uno-calculator/id1464736591",
"Android": "https://play.google.com/store/apps/details?id=uno.platform.calculator",
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
<Compile Include="$(MSBuildThisFileDirectory)Content\Diagnostics\NavigationDebuggerView.xaml.cs">
<DependentUpon>NavigationDebuggerView.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Content\ForcedUpdate\ForcedUpdatePage.xaml.cs">
<DependentUpon>ForcedUpdatePage.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Content\Menu.xaml.cs">
<DependentUpon>Menu.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -98,6 +101,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Framework\Email\EmailService.Mobile.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Framework\Email\EmailService.Windows.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Framework\DeviceInformation\DeviceInformationProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Framework\Launcher\AppStoreUriProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Framework\Launcher\LauncherServiceExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Framework\Launcher\LauncherService.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Framework\Connectivity\ConnectivityProvider.cs" />
Expand Down Expand Up @@ -235,6 +239,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Content\ForcedUpdate\ForcedUpdatePage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Content\Menu.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public static IServiceCollection AddNavigation(this IServiceCollection services)
{ typeof(DadJokesFiltersPageViewModel), typeof(DadJokesFiltersPage) },
{ typeof(SentEmailConfirmationPageViewModel), typeof(SentEmailConfirmationPage) },
{ typeof(ResetPasswordPageViewModel), typeof(ResetPasswordPage) },
{ typeof(ForcedUpdatePageViewModel), typeof(ForcedUpdatePage) },
};

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static IServiceCollection AddViewServices(this IServiceCollection service
.AddSingleton<IDiagnosticsService, DiagnosticsService>()
.AddSingleton<ILauncherService>(s => new LauncherService(s.GetRequiredService<DispatcherQueue>()))
.AddSingleton<IVersionProvider, VersionProvider>()
.AddSingleton<IAppStoreUriProvider, AppStoreUriProvider>()
.AddSingleton<IDeviceInformationProvider, DeviceInformationProvider>()
.AddSingleton<IExtendedSplashscreenController, ExtendedSplashscreenController>(s => new ExtendedSplashscreenController(Shell.Instance.DispatcherQueue))
.AddSingleton<IConnectivityProvider, ConnectivityProvider>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@
<Button Content="Reinitialize navigation"
Style="{StaticResource NavigationDebuggerButtonStyle}"
Command="{Binding Reinitialize}" />

<Button Content="Force update"
Style="{StaticResource NavigationDebuggerButtonStyle}"
Command="{Binding NavigateToForceUpdatePage}" />
</StackPanel>
</StackPanel>
</UserControl>
Marc-Antoine-Soucy marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Page x:Class="ApplicationTemplate.Views.Content.ForcedUpdatePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">

<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock x:Uid="ForcedUpdateContent"
Margin="0,0,0,20"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="24"
Text="An update is required to continue using the application." />
<Button HorizontalAlignment="Center"
VerticalAlignment="Center"
Command="{Binding NavigateToStore}"
Content="Update"
Grid.Row="1" />
</Grid>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.UI.Xaml.Controls;

namespace ApplicationTemplate.Views.Content;

public sealed partial class ForcedUpdatePage : Page
{
public ForcedUpdatePage()
{
this.InitializeComponent();
}
}
Loading