1
0
mirror of https://github.com/bitwarden/mobile synced 2025-12-24 04:04:34 +00: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
2 changed files with 105 additions and 68 deletions

View File

@@ -1,4 +1,5 @@
using System.ComponentModel;
using System;
using System.ComponentModel;
using Bit.iOS.Core.Renderers;
using Bit.iOS.Core.Utilities;
using UIKit;
@@ -9,34 +10,49 @@ using Xamarin.Forms.Platform.iOS;
namespace Bit.iOS.Core.Renderers
{
public class CustomLabelRenderer : LabelRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
if (Control != null && e.NewElement is Label)
{
UpdateFont();
}
}
{
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
if (Control != null && e.NewElement is Label)
{
UpdateFont();
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == Label.FontProperty.PropertyName ||
e.PropertyName == Label.TextProperty.PropertyName ||
e.PropertyName == Label.FormattedTextProperty.PropertyName)
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
try
{
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()
{
var pointSize = iOSHelpers.GetAccessibleFont<Label>(Element.FontSize);
if (pointSize != null)
{
Control.Font = UIFont.FromDescriptor(Element.Font.ToUIFont().FontDescriptor, pointSize.Value);
}
}
}
if (e.PropertyName == Label.FontProperty.PropertyName ||
e.PropertyName == Label.TextProperty.PropertyName ||
e.PropertyName == Label.FormattedTextProperty.PropertyName)
{
UpdateFont();
}
}
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);
}
}
}
}