Skip to content

Commit

Permalink
Implement IsToggled property in WinUI SwitchHandler (#719)
Browse files Browse the repository at this point in the history
* Implement IsToggled property in WinUI SwitchHandler

* - update to IsOn

* - fix naming

* - wire up switch toggle

* - add tests

* - ios skip

* - fix

Co-authored-by: Shane Neuville <[email protected]>
  • Loading branch information
jsuarezruiz and PureWeen authored Apr 15, 2021
1 parent 9c1bdcb commit d1823cd
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 33 deletions.
6 changes: 2 additions & 4 deletions src/Compatibility/Core/src/AppHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@ public static class AppHostBuilderExtensions
typeof(Label),
typeof(CheckBox),
typeof(Entry),
#if !WINDOWS
typeof(Switch),
typeof(Editor),
typeof(ActivityIndicator),
typeof(DatePicker),
typeof(Editor),
typeof(Picker),
typeof(ProgressBar),
typeof(SearchBar),
typeof(Slider),
typeof(Stepper),
typeof(Switch),
typeof(TimePicker),
#endif
};

public static IAppHostBuilder UseFormsCompatibility(this IAppHostBuilder builder, bool registerRenderers = true)
Expand Down
6 changes: 6 additions & 0 deletions src/Controls/src/Core/HandlerImpl/Switch.Impl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,11 @@ Color ISwitch.TrackColor
return null;
}
}

bool ISwitch.IsOn
{
get => IsToggled;
set => IsToggled = value;
}
}
}
2 changes: 1 addition & 1 deletion src/Core/src/Core/ISwitch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface ISwitch : IView
/// <summary>
/// Gets or sets a Boolean value that indicates whether this Switch is toggled.
/// </summary>
bool IsToggled { get; set; }
bool IsOn { get; set; }

/// <summary>
/// Gets the Switch Track Color.
Expand Down
8 changes: 4 additions & 4 deletions src/Core/src/Handlers/Switch/SwitchHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public override Size GetDesiredSize(double widthConstraint, double heightConstra
return size;
}

public static void MapIsToggled(SwitchHandler handler, ISwitch view)
public static void MapIsOn(SwitchHandler handler, ISwitch view)
{
handler.NativeView?.UpdateIsToggled(view);
handler.NativeView?.UpdateIsOn(view);
}

public static void MapTrackColor(SwitchHandler handler, ISwitch view)
Expand All @@ -68,12 +68,12 @@ public static void MapThumbColor(SwitchHandler handler, ISwitch view)
handler.NativeView?.UpdateThumbColor(view, DefaultThumbColorStateList);
}

void OnCheckedChanged(bool isToggled)
void OnCheckedChanged(bool isOn)
{
if (VirtualView == null)
return;

VirtualView.IsToggled = isToggled;
VirtualView.IsOn = isOn;
}

class CheckedChangeListener : Java.Lang.Object, CompoundButton.IOnCheckedChangeListener
Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/Handlers/Switch/SwitchHandler.Standard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public partial class SwitchHandler : ViewHandler<ISwitch, object>
{
protected override object CreateNativeView() => throw new NotImplementedException();

public static void MapIsToggled(SwitchHandler handler, ISwitch view) { }
public static void MapIsOn(SwitchHandler handler, ISwitch view) { }
public static void MapTrackColor(SwitchHandler handler, ISwitch view) { }
public static void MapThumbColor(SwitchHandler handler, ISwitch view) { }
}
Expand Down
27 changes: 24 additions & 3 deletions src/Core/src/Handlers/Switch/SwitchHandler.Windows.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using Microsoft.UI.Xaml.Controls;

namespace Microsoft.Maui.Handlers
Expand All @@ -7,13 +6,35 @@ public partial class SwitchHandler : ViewHandler<ISwitch, ToggleSwitch>
{
protected override ToggleSwitch CreateNativeView() => new ToggleSwitch();

[MissingMapper]
public static void MapIsToggled(SwitchHandler handler, ISwitch view) { }
public static void MapIsOn(SwitchHandler handler, ISwitch view)
{
handler.NativeView?.UpdateIsToggled(view);
}

[MissingMapper]
public static void MapTrackColor(SwitchHandler handler, ISwitch view) { }

[MissingMapper]
public static void MapThumbColor(SwitchHandler handler, ISwitch view) { }

protected override void DisconnectHandler(ToggleSwitch nativeView)
{
base.DisconnectHandler(nativeView);
nativeView.Toggled -= OnToggled;
}

protected override void ConnectHandler(ToggleSwitch nativeView)
{
base.ConnectHandler(nativeView);
nativeView.Toggled += OnToggled;
}

void OnToggled(object sender, UI.Xaml.RoutedEventArgs e)
{
if (VirtualView == null || NativeView == null)
return;

VirtualView.IsOn = NativeView.IsOn;
}
}
}
2 changes: 1 addition & 1 deletion src/Core/src/Handlers/Switch/SwitchHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public partial class SwitchHandler
{
public static PropertyMapper<ISwitch, SwitchHandler> SwitchMapper = new PropertyMapper<ISwitch, SwitchHandler>(ViewHandler.ViewMapper)
{
[nameof(ISwitch.IsToggled)] = MapIsToggled,
[nameof(ISwitch.IsOn)] = MapIsOn,
[nameof(ISwitch.ThumbColor)] = MapThumbColor,
[nameof(ISwitch.TrackColor)] = MapTrackColor,
};
Expand Down
6 changes: 3 additions & 3 deletions src/Core/src/Handlers/Switch/SwitchHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ protected override void SetupDefaults(UISwitch nativeView)
DefaultThumbColor = UISwitch.Appearance.ThumbTintColor;
}

public static void MapIsToggled(SwitchHandler handler, ISwitch view)
public static void MapIsOn(SwitchHandler handler, ISwitch view)
{
handler.NativeView?.UpdateIsToggled(view);
handler.NativeView?.UpdateIsOn(view);
}

public static void MapTrackColor(SwitchHandler handler, ISwitch view)
Expand All @@ -53,7 +53,7 @@ void OnControlValueChanged(object? sender, EventArgs e)
return;

if (NativeView != null)
VirtualView.IsToggled = NativeView.On;
VirtualView.IsOn = NativeView.On;
}
}
}
4 changes: 2 additions & 2 deletions src/Core/src/Platform/Android/SwitchExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace Microsoft.Maui
{
public static class SwitchExtensions
{
public static void UpdateIsToggled(this ASwitch aSwitch, ISwitch view) =>
aSwitch.Checked = view.IsToggled;
public static void UpdateIsOn(this ASwitch aSwitch, ISwitch view) =>
aSwitch.Checked = view.IsOn;

public static void UpdateTrackColor(this ASwitch aSwitch, ISwitch view, ColorStateList? defaultTrackColor)
{
Expand Down
12 changes: 12 additions & 0 deletions src/Core/src/Platform/Windows/SwitchExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.UI.Xaml.Controls;

namespace Microsoft.Maui
{
public static class SwitchExtensions
{
public static void UpdateIsToggled(this ToggleSwitch toggleSwitch, ISwitch view)
{
toggleSwitch.IsOn = view.IsOn;
}
}
}
4 changes: 2 additions & 2 deletions src/Core/src/Platform/iOS/SwitchExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ namespace Microsoft.Maui
{
public static class SwitchExtensions
{
public static void UpdateIsToggled(this UISwitch uiSwitch, ISwitch view)
public static void UpdateIsOn(this UISwitch uiSwitch, ISwitch view)
{
uiSwitch.SetState(view.IsToggled, true);
uiSwitch.SetState(view.IsOn, true);
}

public static void UpdateTrackColor(this UISwitch uiSwitch, ISwitch view, UIColor? defaultOnTrackColor, UIColor? defaultOffTrackColor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ namespace Microsoft.Maui.DeviceTests
{
public partial class SwitchHandlerTests
{
void SetIsOn(SwitchHandler switchHandler, bool value) =>
GetNativeSwitch(switchHandler).Checked = value;

ASwitch GetNativeSwitch(SwitchHandler switchHandler) =>
(ASwitch)switchHandler.NativeView;

bool GetNativeIsChecked(SwitchHandler switchHandler) =>
bool GetNativeIsOn(SwitchHandler switchHandler) =>
GetNativeSwitch(switchHandler).Checked;

Task ValidateTrackColor(ISwitch switchStub, Color color, Action action = null) =>
Expand Down
35 changes: 29 additions & 6 deletions src/Core/tests/DeviceTests/Handlers/Switch/SwitchHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public async Task IsToggledInitializesCorrectly()
{
var switchStub = new SwitchStub()
{
IsToggled = true
IsOn = true
};

await ValidatePropertyInitValue(switchStub, () => switchStub.IsToggled, GetNativeIsChecked, switchStub.IsToggled);
await ValidatePropertyInitValue(switchStub, () => switchStub.IsOn, GetNativeIsOn, switchStub.IsOn);
}

[Theory(DisplayName = "Track Color Initializes Correctly")]
Expand All @@ -28,7 +28,7 @@ public async Task TrackColorInitializesCorrectly(bool isToggled)
{
var switchStub = new SwitchStub()
{
IsToggled = isToggled,
IsOn = isToggled,
TrackColor = Colors.Red
};

Expand All @@ -40,7 +40,7 @@ public async Task TrackColorUpdatesCorrectly()
{
var switchStub = new SwitchStub()
{
IsToggled = true
IsOn = true
};

await ValidateTrackColor(switchStub, Colors.Red, () => switchStub.TrackColor = Colors.Red);
Expand All @@ -51,7 +51,7 @@ public async Task ThumbColorInitializesCorrectly()
{
var switchStub = new SwitchStub()
{
IsToggled = true,
IsOn = true,
ThumbColor = Colors.Blue
};

Expand All @@ -63,10 +63,33 @@ public async Task ThumbColorUpdatesCorrectly()
{
var switchStub = new SwitchStub()
{
IsToggled = true
IsOn = true
};

await ValidateThumbColor(switchStub, Colors.Red, () => switchStub.ThumbColor = Colors.Red);
}

[Fact(DisplayName = "Updating Native Is On property updates Virtual View"
#if __IOS__
,Skip = "iOS doesn't throw ValueChanged events when changing property via code."
#endif
)]
public async Task NativeIsOnPropagatesToVirtual()
{
var switchStub = new SwitchStub()
{
IsOn = false
};

bool isOn = false;
switchStub.IsOnDelegate += () =>
{
isOn = switchStub.IsOn;
};

await SetValueAsync(switchStub, true, SetIsOn);

Assert.True(isOn);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ public partial class SwitchHandlerTests
UISwitch GetNativeSwitch(SwitchHandler switchHandler) =>
(UISwitch)switchHandler.NativeView;

bool GetNativeIsChecked(SwitchHandler switchHandler) =>
// This will not fire a ValueChanged event on native
void SetIsOn(SwitchHandler switchHandler, bool value) =>
switchHandler.NativeView.SetState(value, true);

bool GetNativeIsOn(SwitchHandler switchHandler) =>
GetNativeSwitch(switchHandler).On;

async Task ValidateTrackColor(ISwitch switchStub, Color color, Action action = null)
Expand Down
16 changes: 12 additions & 4 deletions src/Core/tests/DeviceTests/Stubs/SwitchStub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@ namespace Microsoft.Maui.DeviceTests.Stubs
{
public partial class SwitchStub : StubBase, ISwitch
{
public Action ToggledDelegate;
public Action IsOnDelegate;
Color _thumbColor;
Color _trackColor;
bool _isOn;

public bool IsOn
{
get => _isOn;
set
{
SetProperty(ref _isOn, value);
IsOnDelegate?.Invoke();
}
}

public bool IsToggled { get; set; }
public Color TrackColor
{
get => _trackColor;
Expand All @@ -21,7 +31,5 @@ public Color ThumbColor
get => _thumbColor;
set => SetProperty(ref _thumbColor, value);
}

public void Toggled() => ToggledDelegate?.Invoke();
}
}

0 comments on commit d1823cd

Please sign in to comment.