From da63ee1bbf75d6b61315023cffb61cde8ac04ca4 Mon Sep 17 00:00:00 2001 From: "Andres G. Aragoneses" Date: Fri, 11 Aug 2023 14:49:05 +0800 Subject: [PATCH] XF.Android(Renderers): report warn, not crash Instead of crashing when we find an unexpected layout in this DatePicker hack, we report a Sentry warning; this way we mitigate this bug: https://gitlab.com/nodeffect/backlog/-/issues/47 --- src/GWallet.Backend/FSharpUtil.fs | 7 ++ .../Renderers/DatePickerYearFirstRenderer.fs | 65 ++++++++++++------- 2 files changed, 49 insertions(+), 23 deletions(-) diff --git a/src/GWallet.Backend/FSharpUtil.fs b/src/GWallet.Backend/FSharpUtil.fs index 1ce5fc616..49ae171ca 100644 --- a/src/GWallet.Backend/FSharpUtil.fs +++ b/src/GWallet.Backend/FSharpUtil.fs @@ -45,6 +45,8 @@ module FSharpUtil = let SPrintF5 (fmt: string) (a: Object) (b: Object) (c: Object) (d: Object) (e: Object) = String.Format(ToStringFormat fmt, a, b, c, d, e) + let SPrintF6 (fmt: string) (a: Object) (b: Object) (c: Object) (d: Object) (e: Object) (f: Object) = + String.Format(ToStringFormat fmt, a, b, c, d, e, f) module UwpHacks = #if STRICTER_COMPILATION_BUT_WITH_REFLECTION_AT_RUNTIME @@ -57,6 +59,8 @@ module FSharpUtil = let SPrintF4 fmt a b c d = sprintf fmt a b c d let SPrintF5 fmt a b c d e = sprintf fmt a b c d e + + let SPrintF6 fmt a b c d e f = sprintf fmt a b c d e f #else let SPrintF1 (fmt: string) (a: Object) = ReflectionlessPrint.SPrintF1 fmt a @@ -72,6 +76,9 @@ module FSharpUtil = let SPrintF5 (fmt: string) (a: Object) (b: Object) (c: Object) (d: Object) (e: Object) = ReflectionlessPrint.SPrintF5 fmt a b c d e + + let SPrintF6 (fmt: string) (a: Object) (b: Object) (c: Object) (d: Object) (e: Object) (f: Object) = + ReflectionlessPrint.SPrintF6 fmt a b c d e f #endif diff --git a/src/GWallet.Frontend.XF.Android/Renderers/DatePickerYearFirstRenderer.fs b/src/GWallet.Frontend.XF.Android/Renderers/DatePickerYearFirstRenderer.fs index 078859a24..61ff9dc78 100644 --- a/src/GWallet.Frontend.XF.Android/Renderers/DatePickerYearFirstRenderer.fs +++ b/src/GWallet.Frontend.XF.Android/Renderers/DatePickerYearFirstRenderer.fs @@ -2,9 +2,11 @@ namespace GWallet.Frontend.XF.Android open Android.Widget +open Xamarin.Essentials open Xamarin.Forms open Xamarin.Forms.Platform.Android +open GWallet.Backend open GWallet.Backend.FSharpUtil.UwpHacks // Custom renderer for Xamarin.Forms.DatePicker @@ -20,33 +22,50 @@ type DatePickerYearFirstRenderer (context) = // Android.Widget.DatePicker implementation // We navigate through multiple layers of child elements until we arrive // at the text element which displays the year and emit a click on it - let maybeLayoutA = dialog.DatePicker.GetChildAt 0 - match maybeLayoutA with - | :? LinearLayout as layoutA -> - let maybeLayoutB = layoutA.GetChildAt 0 - match maybeLayoutB with - | :? LinearLayout as layoutB -> - let maybeLayoutC = layoutB.GetChildAt 0 - match maybeLayoutC with - | :? LinearLayout as layoutC -> - let yearText = layoutC.GetChildAt 0 - yearText.PerformClick () |> ignore - dialog + let warningMsg = + let maybeLayoutA = dialog.DatePicker.GetChildAt 0 + match maybeLayoutA with + | :? LinearLayout as layoutA -> + let maybeLayoutB = layoutA.GetChildAt 0 + match maybeLayoutB with + | :? LinearLayout as layoutB -> + let maybeLayoutC = layoutB.GetChildAt 0 + match maybeLayoutC with + | :? LinearLayout as layoutC -> + let yearText = layoutC.GetChildAt 0 + yearText.PerformClick () |> ignore + None + | null -> + Some "Unexpected DatePicker layout when trying to find layoutC (got null)" + | _ -> + Some <| SPrintF1 "Unexpected DatePicker layout when trying to find layoutC (got %s)" + (maybeLayoutC.GetType().FullName) | null -> - failwith "Unexpected DatePicker layout when trying to find layoutC (got null)" + Some "Unexpected DatePicker layout when trying to find layoutB (got null)" | _ -> - failwith <| SPrintF1 "Unexpected DatePicker layout when trying to find layoutC (got %s)" - (maybeLayoutC.GetType().FullName) + Some <| SPrintF1 "Unexpected DatePicker layout when trying to find layoutB (got %s)" + (maybeLayoutB.GetType().FullName) | null -> - failwith "Unexpected DatePicker layout when trying to find layoutB (got null)" + Some <| "Unexpected DatePicker layout when trying to find layoutA (got null)" | _ -> - failwith <| SPrintF1 "Unexpected DatePicker layout when trying to find layoutB (got %s)" - (maybeLayoutB.GetType().FullName) - | null -> - failwith "Unexpected DatePicker layout when trying to find layoutA (got null)" - | _ -> - failwith <| SPrintF1 "Unexpected DatePicker layout when trying to find layoutA (got %s)" - (maybeLayoutA.GetType().FullName) + Some <| SPrintF1 "Unexpected DatePicker layout when trying to find layoutA (got %s)" + (maybeLayoutA.GetType().FullName) + + match warningMsg with + | Some msg -> + let devInfo = + SPrintF6 " [DevInfo: (Type=%s, Idiom=%s, Platform=%s, Version=%s, Manufacturer=%s, Model=%s)]" + (DeviceInfo.DeviceType.ToString()) + (DeviceInfo.Idiom.ToString()) + (DeviceInfo.Platform.ToString()) + DeviceInfo.VersionString + DeviceInfo.Manufacturer + DeviceInfo.Model + + Infrastructure.ReportWarningMessage (msg + devInfo) |> ignore + | _ -> () + + dialog [, typeof)>]