Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Switch Off Track Color - Fixes #10099 #10758

Merged
merged 28 commits into from
Apr 3, 2023

Conversation

tj-devel709
Copy link
Contributor

@tj-devel709 tj-devel709 commented Oct 18, 2022

Fixes #10099

The customer points out that when a switch track color is set, the switch is turned on, then when the switch is turned off, the switch track color stays on which is not the expected behavior. See below:

Original Incorrect Behavior

Adjusted Behavior

After the fix from this PR, the switch works as expected:

iOS Android

Customer reported issues in both android and iOS when using the VisualStateManager, but android worked fine for me without any fixes as shown above

@tj-devel709 tj-devel709 changed the title Fix Switch Off Track Color - Fixes #10099 Fix Switch Off Track Color - Fixes https://github.com/dotnet/maui/issues/10099 Oct 18, 2022
@tj-devel709 tj-devel709 changed the title Fix Switch Off Track Color - Fixes https://github.com/dotnet/maui/issues/10099 Fix Switch Off Track Color - Fixes #10099 Oct 18, 2022
@jsuarezruiz jsuarezruiz added area-controls-switch Switch t/bug Something isn't working labels Oct 18, 2022
@ghost ghost added the legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor label Oct 18, 2022
@tj-devel709
Copy link
Contributor Author

Needs tests 😋

hartez
hartez previously requested changes Nov 1, 2022
Copy link
Contributor

@hartez hartez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's guard against NREs here.

src/Core/src/Platform/iOS/SwitchExtensions.cs Outdated Show resolved Hide resolved
@tj-devel709
Copy link
Contributor Author

I added the NRE checks fine, but the tests that I added started failing again locally for other reasons. I am looking into it some more, but it seems that the UIView that I get into my test is different (different handle at least) and the colors that were set are not set for the argument passed into my test. Visually, things work as expected though..

@tj-devel709
Copy link
Contributor Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 2 pipeline(s).

@github-actions
Copy link
Contributor

Thank you for your pull request. We are auto-formatting your source code to follow our code guidelines.

@tj-devel709 tj-devel709 marked this pull request as ready for review March 29, 2023 21:05
@tj-devel709
Copy link
Contributor Author

@PureWeen @mattleibow @mandel-macaque This one should be good now!

if (OperatingSystem.IsIOSVersionAtLeast(13) || OperatingSystem.IsTvOSVersionAtLeast(13))
uIView = uISwitch.Subviews[0].Subviews[0];
return uISwitch.Subviews?.FirstOrDefault()?.Subviews?.FirstOrDefault();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not use linq for this?
Just make it a long if statement or nested?

We typically just stay away from LINQ for performance reasons.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to agree, there is nothing nice we can do. Sometimes code has to be ugly and work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mandel-macaque and @PureWeen updated! Thanks for the idea!

}

internal static UIColor? GetTrackColor(this UISwitch uISwitch)
{
return uISwitch.GetTrackSubview()?.BackgroundColor;
}

internal static T? FirstOrDefaultNoLinq<T>(this T[] items)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you move this into ArrayExtensions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PureWeen When I move this to ArrayExtensions as is, I get this error: "Error CS0518: Predefined type 'System.Index' is not defined or imported(CS0518) (Core)". I'm not sure why as it appears that this is an error I could get prior to having C#8 but we are in the same project.

I changed the implementation to something that I don't believe is worse, but let me know if there is a better solution to this?

@@ -64,5 +64,7 @@ public static T[] RemoveAt<T>(this T[] self, int index)
/// <param name="self">The array to retrieve the last item from.</param>
/// <returns>An object of type <typeparamref name="T"/> that is the last item in the collection.</returns>
public static T Last<T>(this T[] self) => self[self.Length - 1];

internal static T? FirstOrDefaultNoLinq<T>(this T[] items) => items is null || items.Length == 0 ? default : items[0];
Copy link
Member

@PureWeen PureWeen Apr 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mandel-macaque this look alright to you?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but I am surprise the compiler is not printing a warning with nullability enabled saying something like 'items is not set as nullable'. I think we probably want to do something like:

Suggested change
internal static T? FirstOrDefaultNoLinq<T>(this T[] items) => items is null || items.Length == 0 ? default : items[0];
internal static T? FirstOrDefaultNoLinq<T>(this T[]? items) => items is null || items.Length == 0 ? default : items[0];

Right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at FirstOrDefaults behavior, this sounds like the correct approach :)

@PureWeen PureWeen requested a review from hartez April 3, 2023 15:19
@espenrl
Copy link
Contributor

espenrl commented Jun 7, 2023

@tj-devel709 would love a backport to .NET 7

@jfversluis jfversluis added the backport/suggested The PR author or issue review has suggested that the change should be backported. label Sep 8, 2023
@jfversluis
Copy link
Member

Some people requested a backport to net7.

This is functionality that works in Xamarin.Forms and the change seems safe enough. On the other hand, a workaround is available and we're close to .NET 8 where this is already fixed.

@github-actions github-actions bot locked and limited conversation to collaborators Dec 17, 2023
@Eilon Eilon removed the legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor label May 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-controls-switch Switch backport/suggested The PR author or issue review has suggested that the change should be backported. t/bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Toggle button OnColor for Off state not working in iOS, also VisualStateManager is also not working.
9 participants