Skip to content

Commit

Permalink
feat: To Snackbar UI instead Alert.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Jan 2, 2024
1 parent f38df80 commit fd05406
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 72 deletions.
2 changes: 2 additions & 0 deletions Maui.Android.InAppUpdates.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Snackbar/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ NuGet package that implementing Android In-App Updates for MAUI with debugging c
# Usage
- Add NuGet package to your project:
```xml
<PackageReference Include="Oscore.Maui.Android.InAppUpdates" Version="0.9.1" />
<PackageReference Include="Oscore.Maui.Android.InAppUpdates" Version="1.0.0" />
```
- Add the following to your `MauiProgram.cs` `CreateMauiApp` method:
```diff
Expand Down
1 change: 1 addition & 0 deletions sample/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<Button Command="{Binding InstallFailsCommand}" Text="InstallFails" />
<Label Text="Test UI:" />
<Button Command="{Binding CompleteUpdateCommand}" Text="CompleteUpdate" />
<Button Command="{Binding DownloadingCommand}" Text="Downloading" />
</VerticalStackLayout>
</ScrollView>

Expand Down
18 changes: 15 additions & 3 deletions sample/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,21 @@ private void InstallFails()
private void CompleteUpdate()
{
#if ANDROID
Internal.Handler.Options.CompleteUpdateAction(
Platform.CurrentActivity!,
Internal.Handler.AppUpdateManager!);
Internal.Handler.Options.CompleteUpdateAction();
#endif
}

[RelayCommand]
private async Task Downloading()
{
#if ANDROID
for (var i = 0; i < 100; i += 10)
{
Internal.Handler.Options.DownloadingAction(i);
await Task.Delay(TimeSpan.FromMilliseconds(250));
}
#else
await Task.Delay(TimeSpan.FromMilliseconds(250));
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion src/libs/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</ItemGroup>

<PropertyGroup Label="Nuget">
<Version>0.9.1</Version>
<Version>1.0.0</Version>
<GeneratePackageOnBuild Condition=" '$(Configuration)' == 'Release' ">true</GeneratePackageOnBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Authors>Oscore</Authors>
Expand Down
32 changes: 10 additions & 22 deletions src/libs/Maui.Android.InAppUpdates/AndroidInAppUpdatesOptions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
#if ANDROID
using Android.Content;
using Xamarin.Google.Android.Play.Core.AppUpdate;
#endif

namespace Maui.Android.InAppUpdates;

/// <summary>
Expand Down Expand Up @@ -32,59 +27,52 @@ public class AndroidInAppUpdatesOptions
/// This action will be triggered when the app is updated. <br/>
/// Default action will show the toast with english text. <br/>
/// </summary>
public Action<Context> AppUpdatedAction { get; set; } = static context =>
public Action AppUpdatedAction { get; set; } = static () =>
Internal.DefaultUserInterface.ShowShortToast(
context: context,
text: "App updated");

/// <summary>
/// This action will be triggered when the update is cancelled. <br/>
/// Default action will show the toast with english text. <br/>
/// </summary>
public Action<Context> UpdateCancelledAction { get; set; } = static context =>
public Action UpdateCancelledAction { get; set; } = static () =>
Internal.DefaultUserInterface.ShowShortToast(
context: context,
text: "In app update cancelled");

/// <summary>
/// This action will be triggered when the update is failed. <br/>
/// Default action will show the toast with english text. <br/>
/// </summary>
public Action<Context> UpdateFailedAction { get; set; } = static context =>
public Action UpdateFailedAction { get; set; } = static () =>
Internal.DefaultUserInterface.ShowShortToast(
context: context,
text: "In app update failed");

/// <summary>
/// This action will be triggered when the download is failed. <br/>
/// Default action will show the toast with english text. <br/>
/// </summary>
public Action<Context> DownloadFailedAction { get; set; } = static context =>
public Action DownloadFailedAction { get; set; } = static () =>
Internal.DefaultUserInterface.ShowShortToast(
context: context,
text: "Update download failed.");

/// <summary>
/// This action will be triggered when downloading the update. <br/>
/// Second value is the percentage of the download. <br/>
/// Default action will show the toast with english text. <br/>
/// </summary>
public Action<Context, double> DownloadingAction { get; set; } = static (context, percents) =>
public Action<double> DownloadingAction { get; set; } = static percents =>
Internal.DefaultUserInterface.ShowShortToast(
context: context,
text: $"Downloaded {percents}%");

/// <summary>
/// This action will be triggered when the update is completed. <br/>
/// Default action will show the alert dialog to complete the update. <br/>
/// </summary>
public Action<Context, IAppUpdateManager> CompleteUpdateAction { get; set; } = static (context, appUpdateManager) =>
Internal.DefaultUserInterface.ShowAlertToCompleteUpdate(
context: context,
appUpdateManager: appUpdateManager,
title: "Download completed",
message: "Update is ready to be installed.", // "Application successfully updated! You need to restart the app in order to use this new features"
positiveButton: "Perform update"); // "Restart"
public Action CompleteUpdateAction { get; set; } = static () =>
Internal.DefaultUserInterface.ShowSnackbar(
text: "An update has just been downloaded.",
actionText: "RESTART",
clickHandler: static _ => Internal.Handler.AppUpdateManager?.CompleteUpdate());
#endif

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ public void OnSuccess(Java.Lang.Object p0)
case UpdateAvailability.UpdateAvailable or UpdateAvailability.DeveloperTriggeredUpdateInProgress when
isFlexibleUpdatesAllowed:
{
InstallStateUpdatedListener ??= new InstallStateUpdatedListener(
context: activity,
appUpdateManager: appUpdateManager);
InstallStateUpdatedListener ??= new InstallStateUpdatedListener();
appUpdateManager.RegisterListener(InstallStateUpdatedListener);

_ = appUpdateManager.StartUpdateFlowForResult(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,47 @@
using Android.App;
using Android.Content;
using Android.Views;
using Android.Widget;
using Xamarin.Google.Android.Play.Core.AppUpdate;
using Google.Android.Material.Snackbar;

namespace Maui.Android.InAppUpdates.Internal;

public static class DefaultUserInterface
{
/// <summary>
/// This method will show the alert dialog to complete the update.
/// Displays a short duration toast message at the center of the screen.
/// </summary>
/// <param name="context"></param>
/// <param name="appUpdateManager"></param>
public static void ShowAlertToCompleteUpdate(
Context context,
IAppUpdateManager appUpdateManager,
string title,
string message,
string positiveButton)
/// <param name="text">The text to be displayed in the toast message.</param>
public static void ShowShortToast(
string text)
{
if (new AlertDialog.Builder(context).Create() is not {} alert)
Toast.MakeText(
context: Platform.AppContext,
text: text,
duration: ToastLength.Short)?.Show();
}

/// <summary>
/// Displays a snackbar with an action to complete the app update process.
/// </summary>
/// <param name="text">The text to display on the snackbar.</param>
/// <param name="actionText">The text for the action button.</param>
/// <param name="clickHandler">The handler for the action button.</param>
public static void ShowSnackbar(
string text,
string actionText,
Action<global::Android.Views.View> clickHandler)
{
if (Platform.CurrentActivity?.Window?.DecorView is not {} view ||
Snackbar.Make(
text: text,
duration: BaseTransientBottomBar.LengthIndefinite,
view: view) is not {} snackbar)
{
return;
}

alert.SetTitle(title);
alert.SetMessage(message);
alert.SetButton((int) DialogButtonType.Positive, positiveButton, (_, _) =>
{
appUpdateManager.CompleteUpdate();
// You can start your activityonresult method when update is not available when using immediate update when testing with fakeappupdate manager
//activity.StartActivityForResult(intent, 400);
});
alert.SetCancelable(false);
alert.Show();
}

public static void ShowShortToast(Context context, string text)
{
Toast.MakeText(
context: context,
text: text,
duration: ToastLength.Short)?.Show();
snackbar.SetAction(
text: actionText,
clickHandler: clickHandler);
snackbar.Show();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ public static void HandleActivityResult(
switch (resultCode)
{
case Result.Ok:
Options.AppUpdatedAction(activity);
Options.AppUpdatedAction();
break;

case Result.Canceled:
Options.UpdateCancelledAction(activity);
Options.UpdateCancelledAction();
break;

case (Result)ActivityResult.ResultInAppUpdateFailed:
Options.UpdateFailedAction(activity);
Options.UpdateFailedAction();
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Android.Content;
using Xamarin.Google.Android.Play.Core.AppUpdate;
using Xamarin.Google.Android.Play.Core.Install;
using Xamarin.Google.Android.Play.Core.Install.Model;
using Object = Java.Lang.Object;
Expand All @@ -9,9 +7,7 @@ namespace Maui.Android.InAppUpdates.Internal;
/// <summary>
/// Listener to track request state updates.
/// </summary>
public class InstallStateUpdatedListener(
Context context,
IAppUpdateManager appUpdateManager)
public class InstallStateUpdatedListener
: Object, IInstallStateUpdatedListener
{
/// <summary>
Expand Down Expand Up @@ -43,16 +39,16 @@ public void OnStateUpdate(InstallState state)
var percents = Math.Round(
100.0 * bytesDownloaded / totalBytesToDownload);

Handler.Options.DownloadingAction(context, percents);
Handler.Options.DownloadingAction(percents);
break;
}

case InstallStatus.Downloaded:
Handler.Options.CompleteUpdateAction(context, appUpdateManager);
Handler.Options.CompleteUpdateAction();
break;

case InstallStatus.Failed:
Handler.Options.DownloadFailedAction(context);
Handler.Options.DownloadFailedAction();
break;
}
}
Expand Down

0 comments on commit fd05406

Please sign in to comment.