FreakyAli / Maui.FreakyControls

FreakyControls is a free OSS UI Kit for .NET MAUI which provides a set of controls and utilities to build modern mobile apps.
MIT License
294 stars 36 forks source link

FreakyTextInputLayout: AppThemeBinding causing app crashes #144

Open sk1llsh0t opened 2 days ago

sk1llsh0t commented 2 days ago

Description

1) When i use AppThemeBinding to apply light and dark mode to the FreakyTextInputLayout, it throws errors when switching between modes.

2) There is also an issue in OnOutlineTitleBackgroundColorProperty Changed if the styles are loaded from a global styles file

System.InvalidOperationException: PlatformView cannot be null here at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IEntry, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[AndroidX.AppCompat.Widget.AppCompatEditText, Xamarin.AndroidX.AppCompat, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].getPlatformView() in //src/Core/src/Handlers/View/ViewHandlerOfT.cs:line 36 at Maui.FreakyControls.FreakyEntryHandler.MapFreakyEntry(IEntryHandler entryHandler, IEntry entry)

Code

<freakyControls:FreakyTextInputLayout Title="Full Name" BorderCornerRadius="10"
    BorderStrokeThickness="2"
    BorderType="Outlined"
    TextColor="{AppThemeBinding Light={StaticResource LightPrimaryText}, Dark={StaticResource DarkUncheckedBorder}}"
    TitleColor="{AppThemeBinding Light={StaticResource LightPrimaryText}, Dark={StaticResource DarkUncheckedBorder}}"
    UnderlineColor="{AppThemeBinding Light={StaticResource LightPrimaryText}, Dark={StaticResource DarkUncheckedBorder}}"
    OutlineTitleBackgroundColor="{AppThemeBinding Light={StaticResource LightBackground01}, Dark={StaticResource DarkBackground01}}"
    BorderStroke="{AppThemeBinding Light={StaticResource LightPrimaryText}, Dark={StaticResource DarkUncheckedBorder}}"
    FontSize="20"
    TitleFontSize="14"
/>

Expected Behavior

I can change between light and dark mode without crashing

Actual Behavior

App crashes

Basic Information

Latest version of Freaky running on Android 12 (net 8.0.8)

sk1llsh0t commented 2 days ago

workaround (or maybe possible solution, just not sure what the implication is of doing this), is in the FeakyEntryHandler class, surround the MapFreakyEntry method in a try...catch block to handle the invalidoperationexception...

private void MapFreakyEntry(IEntryHandler entryHandler, IEntry entry)
    {
        try
        {
            if (entry is FreakyEntry freakyEntry && entryHandler is FreakyEntryHandler freakyEntryHandler)
            {
                if (PlatformView is not null && VirtualView is not null)
                {
                    if (freakyEntry.ImageSource != default(ImageSource))
                    {
                        freakyEntryHandler.HandleAndAlignImageSourceAsync(freakyEntry).RunConcurrently();
                    }

                    HandleAllowCopyPaste(freakyEntry);
                }
            }
        }
        catch (InvalidOperationException ex) { }
    }
}

Doing this fixes the issue. When you have time, please add to the code base and release hotfix. Thanks for all your work!!!

FreakyAli commented 2 days ago

Gonna be a while buddy unfortunately I am a bit busy for the last couple of months and its gonna stay this way for a while

sk1llsh0t commented 2 days ago

The solution above fixes issue #1 listed in the original post. Issue #2 is a different solution when loading the style from a global styles file.

In the FreakyTextInputLayout class, modify OnOutlineTitleBackgroundColorProperty change event to make sure til.LabelTitle is not null. I guess the global styles are applying before all the controls are instantiated which is causing an issue in this case.

Fix:

private static void OnOutlineTitleBackgroundColorProperty(BindableObject bindable, object oldValue, object newValue)
    {
        if (bindable is FreakyTextInputLayout til && newValue is Color color && til.LabelTitle is not null)
        {
            til.LabelTitle.BackgroundColor = til.BorderType ==
                BorderType.Outlined ? color : Colors.Transparent;
        }
    }

With these 2 solutions, everything now works.

sk1llsh0t commented 2 days ago

If i make the change in my fork and create a merge request, would you be able to do a new build? If not, no worries. I'll just grab the 0.4.12 tag and build a 0.4.13 nuget package locally.

FreakyAli commented 2 days ago

@sk1llsh0t I am going to release a new version in October if you can add a quick fix for this it can be added