1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-11-05 09:01:11 +01:00

Fix by workaround crash on LabelRenderer and when changing themes #1689 (#1690)

This commit is contained in:
Federico Maccaroni 2021-12-16 11:36:06 -03:00 committed by GitHub
parent 04c7409418
commit adb8bb4f1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 105 additions and 68 deletions

View File

@ -4,6 +4,9 @@ using Bit.App.Services;
using Bit.App.Styles; using Bit.App.Styles;
using Bit.Core; using Bit.Core;
using Xamarin.Forms; using Xamarin.Forms;
#if !FDROID
using Microsoft.AppCenter.Crashes;
#endif
namespace Bit.App.Utilities namespace Bit.App.Utilities
{ {
@ -14,61 +17,79 @@ namespace Bit.App.Utilities
public static void SetThemeStyle(string name, ResourceDictionary resources) public static void SetThemeStyle(string name, ResourceDictionary resources)
{ {
Resources = () => resources; try
{
Resources = () => resources;
// Reset styles // Reset styles
resources.Clear(); resources.Clear();
resources.MergedDictionaries.Clear(); resources.MergedDictionaries.Clear();
// Variables // Variables
resources.MergedDictionaries.Add(new Variables()); resources.MergedDictionaries.Add(new Variables());
// Themed variables // Themed variables
if (name == "dark") if (name == "dark")
{
resources.MergedDictionaries.Add(new Dark());
UsingLightTheme = false;
}
else if (name == "black")
{
resources.MergedDictionaries.Add(new Black());
UsingLightTheme = false;
}
else if (name == "nord")
{
resources.MergedDictionaries.Add(new Nord());
UsingLightTheme = false;
}
else if (name == "light")
{
resources.MergedDictionaries.Add(new Light());
UsingLightTheme = true;
}
else
{
if (OsDarkModeEnabled())
{ {
resources.MergedDictionaries.Add(new Dark()); resources.MergedDictionaries.Add(new Dark());
UsingLightTheme = false; UsingLightTheme = false;
} }
else else if (name == "black")
{
resources.MergedDictionaries.Add(new Black());
UsingLightTheme = false;
}
else if (name == "nord")
{
resources.MergedDictionaries.Add(new Nord());
UsingLightTheme = false;
}
else if (name == "light")
{ {
resources.MergedDictionaries.Add(new Light()); resources.MergedDictionaries.Add(new Light());
UsingLightTheme = true; UsingLightTheme = true;
} }
} else
{
if (OsDarkModeEnabled())
{
resources.MergedDictionaries.Add(new Dark());
UsingLightTheme = false;
}
else
{
resources.MergedDictionaries.Add(new Light());
UsingLightTheme = true;
}
}
// Base styles // Base styles
resources.MergedDictionaries.Add(new Base()); resources.MergedDictionaries.Add(new Base());
// Platform styles // Platform styles
if (Device.RuntimePlatform == Device.Android) if (Device.RuntimePlatform == Device.Android)
{ {
resources.MergedDictionaries.Add(new Android()); resources.MergedDictionaries.Add(new Android());
}
else if (Device.RuntimePlatform == Device.iOS)
{
resources.MergedDictionaries.Add(new iOS());
}
} }
else if (Device.RuntimePlatform == Device.iOS) catch (InvalidOperationException ioex) when (ioex.Message != null && ioex.Message.StartsWith("Collection was modified"))
{ {
resources.MergedDictionaries.Add(new iOS()); // https://github.com/bitwarden/mobile/issues/1689 There are certain scenarios where this might cause "collection was modified; enumeration operation may not execute"
// the way I found to prevent this for now was to catch the exception here and move on.
// Because on the screens that I found it to happen, the screen is being closed while trying to apply the resources
// so we shouldn't be introducing any issues.
// TODO: Maybe something like this https://github.com/matteobortolazzo/HtmlLabelPlugin/pull/113 can be implemented to avoid this
// on html labels.
}
catch (Exception ex)
{
#if !FDROID
Crashes.TrackError(ex);
#endif
} }
} }

View File

@ -1,4 +1,5 @@
using System.ComponentModel; using System;
using System.ComponentModel;
using Bit.iOS.Core.Renderers; using Bit.iOS.Core.Renderers;
using Bit.iOS.Core.Utilities; using Bit.iOS.Core.Utilities;
using UIKit; using UIKit;
@ -9,34 +10,49 @@ using Xamarin.Forms.Platform.iOS;
namespace Bit.iOS.Core.Renderers namespace Bit.iOS.Core.Renderers
{ {
public class CustomLabelRenderer : LabelRenderer public class CustomLabelRenderer : LabelRenderer
{ {
protected override void OnElementChanged(ElementChangedEventArgs<Label> e) protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{ {
base.OnElementChanged(e); base.OnElementChanged(e);
if (Control != null && e.NewElement is Label) if (Control != null && e.NewElement is Label)
{ {
UpdateFont(); UpdateFont();
} }
} }
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{ {
base.OnElementPropertyChanged(sender, e); try
if (e.PropertyName == Label.FontProperty.PropertyName ||
e.PropertyName == Label.TextProperty.PropertyName ||
e.PropertyName == Label.FormattedTextProperty.PropertyName)
{ {
UpdateFont(); base.OnElementPropertyChanged(sender, e);
} }
} catch (NullReferenceException)
{
// Do nothing...
// There is an issue on Xamarin Forms which throws a null reference
// https://appcenter.ms/users/kspearrin/apps/bitwarden/crashes/errors/534094859u/overview
// TODO: Maybe something like this https://github.com/matteobortolazzo/HtmlLabelPlugin/pull/113 can be implemented to avoid this
// on html labels.
}
private void UpdateFont() if (e.PropertyName == Label.FontProperty.PropertyName ||
{ e.PropertyName == Label.TextProperty.PropertyName ||
var pointSize = iOSHelpers.GetAccessibleFont<Label>(Element.FontSize); e.PropertyName == Label.FormattedTextProperty.PropertyName)
if (pointSize != null) {
{ UpdateFont();
Control.Font = UIFont.FromDescriptor(Element.Font.ToUIFont().FontDescriptor, pointSize.Value); }
} }
}
} private void UpdateFont()
{
if (Element is null || Control is null)
return;
var pointSize = iOSHelpers.GetAccessibleFont<Label>(Element.FontSize);
if (pointSize != null)
{
Control.Font = UIFont.FromDescriptor(Element.Font.ToUIFont().FontDescriptor, pointSize.Value);
}
}
}
} }