Skip to content

Commit b731f05

Browse files
authored
Issue 11 - layout/design_layout_snackbar_include: Error inflating class android.widget.Button (#13)
* Update Microsoft.Maui.Controls to version 9.0.82 Updated the `Microsoft.Maui.Controls` package version from a variable reference `$(MauiVersion)` to a specific version `9.0.82` in both the `Maui.Android.InAppUpdates.SampleApp.csproj` and `Maui.Android.InAppUpdates.csproj` files. * Update package versions in project files Updated `Microsoft.Extensions.Logging.Debug` to version `9.0.7` in `Maui.Android.InAppUpdates.SampleApp.csproj`. In `Maui.Android.InAppUpdates.csproj`, incremented versions of several Xamarin.AndroidX and Google Play App Update packages to: - `Xamarin.AndroidX.Activity.Ktx` to `1.10.1.2` - `Xamarin.AndroidX.Collection.Ktx` to `1.5.0.2` - `Xamarin.AndroidX.Fragment.Ktx` to `1.8.8` - `Xamarin.Google.Android.Play.App.Update.Ktx` to `2.1.0.15` * Enhance error handling in toast and snackbar methods - Modified `ShowShortToast` to include null checks and exception handling. - Refactored `ShowSnackbar` to add fallback to toast and improved error handling for theme-related exceptions. - Introduced `FallbackToToastWithAction` and `HandleSnackbarFailure` for better organization and readability. - Added `IsThemeRelated` method to identify theme-related exceptions. * Update NuGet properties and package references - Added a new "NuGet" PropertyGroup in `Maui.Android.InAppUpdates.csproj` for package ID, description, and tags. - Replaced hardcoded `Microsoft.Maui.Controls` version with variable `$(MauiVersion)` for improved version management. - Updated ItemGroup for package references while retaining AndroidX library references. * Update ShowSnackbar documentation in DefaultUserInterface Modified the parameter documentation for the `ShowSnackbar` method. Removed the note about the `clickHandler` parameter being nullable and replaced it with a simpler description of its purpose. * Update Maui.Controls package version management Replaced hardcoded version number for the Microsoft.Maui.Controls package in the project file `Maui.Android.InAppUpdates.SampleApp.csproj` with a variable `$(MauiVersion)` for improved version management.
1 parent 939d524 commit b731f05

File tree

3 files changed

+105
-24
lines changed

3 files changed

+105
-24
lines changed

sample/Maui.Android.InAppUpdates.SampleApp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464

6565
<ItemGroup>
6666
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
67-
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="9.0.5" />
67+
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="9.0.7" />
6868
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
6969
</ItemGroup>
7070

src/libs/Maui.Android.InAppUpdates/Maui.Android.InAppUpdates.csproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,22 @@
1919
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
2020
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
2121
</PropertyGroup>
22-
22+
2323
<PropertyGroup Label="NuGet">
2424
<PackageId>Oscore.$(AssemblyName)</PackageId>
2525
<Description>NuGet package that implementing Android In-App Updates for MAUI with debugging capabilities.</Description>
2626
<PackageTags>maui;android;in-app-updates;updates;in-app;net8;dotnet;csharp</PackageTags>
2727
</PropertyGroup>
2828

2929
<ItemGroup>
30-
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
30+
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
3131
</ItemGroup>
3232

3333
<ItemGroup Condition="'$(TargetFramework)' == 'net9.0-android'" Label="Android In-app Updates">
34-
<PackageReference Include="Xamarin.AndroidX.Activity.Ktx" Version="1.10.1.1" />
35-
<PackageReference Include="Xamarin.AndroidX.Collection.Ktx" Version="1.5.0.1" />
36-
<PackageReference Include="Xamarin.AndroidX.Fragment.Ktx" Version="1.8.6.1" />
37-
<PackageReference Include="Xamarin.Google.Android.Play.App.Update.Ktx" Version="2.1.0.14" />
34+
<PackageReference Include="Xamarin.AndroidX.Activity.Ktx" Version="1.10.1.2" />
35+
<PackageReference Include="Xamarin.AndroidX.Collection.Ktx" Version="1.5.0.2" />
36+
<PackageReference Include="Xamarin.AndroidX.Fragment.Ktx" Version="1.8.8" />
37+
<PackageReference Include="Xamarin.Google.Android.Play.App.Update.Ktx" Version="2.1.0.15" />
3838
</ItemGroup>
3939

4040
<ItemGroup Condition="$(TargetFramework.StartsWith('Xamarin.iOS')) != true AND $(TargetFramework.StartsWith('net9.0-ios')) != true AND $(TargetFramework.StartsWith('net9.0-maccatalyst')) != true ">

src/libs/Maui.Android.InAppUpdates/Platforms/Android/DefaultUserInterface.cs

Lines changed: 98 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,30 @@ public static class DefaultUserInterface
1010
/// Displays a short duration toast message at the center of the screen.
1111
/// </summary>
1212
/// <param name="text">The text to be displayed in the toast message.</param>
13-
public static void ShowShortToast(
14-
string text)
13+
public static void ShowShortToast(string text)
1514
{
16-
Toast.MakeText(
17-
context: Platform.AppContext,
18-
text: text,
19-
duration: ToastLength.Short)?.Show();
15+
try
16+
{
17+
if (Platform.AppContext is null)
18+
{
19+
Handler.Options.DebugAction($"Cannot show toast - Platform.AppContext is null: {text}");
20+
return;
21+
}
22+
23+
Toast.MakeText(
24+
context: Platform.AppContext,
25+
text: text,
26+
duration: ToastLength.Short)?.Show();
27+
}
28+
catch (Exception ex)
29+
{
30+
Handler.Options.DebugAction($"Failed to show toast '{text}': {ex}");
31+
}
2032
}
2133

2234
/// <summary>
2335
/// Displays a snackbar with an action to complete the app update process.
36+
/// If snackbar fails due to theme incompatibility, falls back to toast message.
2437
/// </summary>
2538
/// <param name="text">The text to display on the snackbar.</param>
2639
/// <param name="actionText">The text for the action button.</param>
@@ -30,18 +43,86 @@ public static void ShowSnackbar(
3043
string actionText,
3144
Action<global::Android.Views.View> clickHandler)
3245
{
33-
if (Platform.CurrentActivity?.Window?.DecorView is not {} view ||
34-
Snackbar.Make(
35-
text: text,
36-
duration: BaseTransientBottomBar.LengthIndefinite,
37-
view: view) is not {} snackbar)
46+
var fallbackMessage = $"{text} - {actionText}";
47+
48+
try
49+
{
50+
var view = Platform.CurrentActivity?.Window?.DecorView;
51+
if (view is null)
52+
{
53+
FallbackToToastWithAction("Cannot show snackbar - no active view", fallbackMessage, clickHandler, null);
54+
return;
55+
}
56+
57+
var snackbar = Snackbar.Make(view, text, BaseTransientBottomBar.LengthIndefinite);
58+
if (snackbar is null)
59+
{
60+
FallbackToToastWithAction("Cannot create snackbar", fallbackMessage, clickHandler, view);
61+
return;
62+
}
63+
64+
snackbar.SetAction(text: actionText, clickHandler: clickHandler);
65+
snackbar.Show();
66+
}
67+
catch (Exception ex) when (IsThemeRelated(ex))
68+
{
69+
HandleSnackbarFailure(ex, "theme related", fallbackMessage, clickHandler, Platform.CurrentActivity?.Window?.DecorView);
70+
}
71+
catch (Exception ex)
3872
{
39-
return;
73+
HandleSnackbarFailure(ex, "general exception", fallbackMessage, null, null);
4074
}
41-
42-
snackbar.SetAction(
43-
text: actionText,
44-
clickHandler: clickHandler);
45-
snackbar.Show();
4675
}
76+
77+
private static void FallbackToToastWithAction(
78+
string debugMessage,
79+
string fallbackMessage,
80+
Action<global::Android.Views.View>? clickHandler,
81+
global::Android.Views.View? fallbackView)
82+
{
83+
Handler.Options.DebugAction($"{debugMessage}, falling back to toast and auto-triggering action");
84+
ShowShortToast(fallbackMessage);
85+
86+
// Auto-trigger the action since user can't click the snackbar
87+
if (clickHandler is not null && fallbackView is not null)
88+
{
89+
try
90+
{
91+
clickHandler(fallbackView);
92+
}
93+
catch (Exception actionEx)
94+
{
95+
Handler.Options.DebugAction($"Error auto-executing snackbar action: {actionEx}");
96+
}
97+
}
98+
}
99+
100+
private static void HandleSnackbarFailure(
101+
Exception ex,
102+
string errorType,
103+
string fallbackMessage,
104+
Action<global::Android.Views.View>? clickHandler,
105+
global::Android.Views.View? view)
106+
{
107+
Handler.Options.DebugAction($"Snackbar {errorType}: {ex}");
108+
ShowShortToast(fallbackMessage);
109+
110+
// Only auto-trigger for theme-related exceptions where user interaction was expected
111+
if (clickHandler is not null && view is not null)
112+
{
113+
try
114+
{
115+
clickHandler(view);
116+
}
117+
catch (Exception actionEx)
118+
{
119+
Handler.Options.DebugAction($"Error executing snackbar action: {actionEx}");
120+
}
121+
}
122+
}
123+
124+
private static bool IsThemeRelated(Exception ex) =>
125+
ex is global::Android.Views.InflateException ||
126+
(ex is Java.Lang.UnsupportedOperationException &&
127+
ex.Message?.Contains("Failed to resolve attribute", StringComparison.Ordinal) == true);
47128
}

0 commit comments

Comments
 (0)