Skip to content

Commit

Permalink
Added IsBusy functionality to CustomSwitch control (#16)
Browse files Browse the repository at this point in the history
Two new files, `IsBusyTestPage.xaml.cs` and `IsBusyTestPage.xaml`, have been added to `App.csproj`. The `CustomSwitch` control in `IosSwitch.xaml` and `CustomSwitch.xaml` now has a new `IsBusy` property, indicating whether the switch is busy, and an `IsBusyContent` property, which displays an `ActivityIndicator` when the switch is busy. A new button has been added to `MainPage.xaml` that navigates to the `IsBusyTestPage`. The `IsBusyTestPage.xaml` contains two `IosSwitch` controls bound to the `IsToggled` property of a `DynamicToggle` switch.
  • Loading branch information
IeuanWalker authored May 8, 2024
1 parent 26d465c commit 4baa20e
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 3 deletions.
6 changes: 6 additions & 0 deletions Demo/App/App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@
<Compile Update="Controls\BackButton.xaml.cs">
<DependentUpon>BackButton.xaml</DependentUpon>
</Compile>
<Compile Update="Pages\IsBusyTestPage.xaml.cs">
<DependentUpon>IsBusyTestPage.xaml</DependentUpon>
</Compile>
<Compile Update="Pages\IsEnabledTestPage.xaml.cs">
<DependentUpon>IsEnabledTestPage.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -119,6 +122,9 @@
<MauiXaml Update="Controls\SwitchViewExamples\ImageSwitch.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>
<MauiXaml Update="Pages\IsBusyTestPage.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>
<MauiXaml Update="Pages\IsEnabledTestPage.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>
Expand Down
7 changes: 7 additions & 0 deletions Demo/App/Controls/CustomSwitchExamples/IosSwitch.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
xmlns:switch="clr-namespace:IeuanWalker.Maui.Switch;assembly=IeuanWalker.Maui.Switch">
<switch:CustomSwitch HeightRequest="40"
HorizontalKnobMargin="1"
IsBusy="{Binding IsBusy, Source={RelativeSource AncestorType={x:Type ContentView}}}"
IsEnabled="{Binding IsEnabled, Source={RelativeSource AncestorType={x:Type ContentView}}}"
IsToggled="{Binding IsToggled, Source={RelativeSource AncestorType={x:Type ContentView}}}"
KnobBackgroundColor="White"
Expand Down Expand Up @@ -52,5 +53,11 @@
VerticalTextAlignment="Center" />
</Grid>
</switch:CustomSwitch.BackgroundContent>
<switch:CustomSwitch.IsBusyContent>
<ActivityIndicator HeightRequest="30"
IsRunning="True"
WidthRequest="30"
Color="Black" />
</switch:CustomSwitch.IsBusyContent>
</switch:CustomSwitch>
</ContentView>
7 changes: 7 additions & 0 deletions Demo/App/Controls/CustomSwitchExamples/IosSwitch.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ public string AccessibilityHint
set => SetValue(AccessibilityHintProperty, value);
}

public static readonly BindableProperty IsBusyProperty = BindableProperty.Create(nameof(IsBusy), typeof(bool), typeof(CustomSwitch), false);
public bool IsBusy
{
get => (bool)GetValue(IsBusyProperty);
set => SetValue(IsBusyProperty, value);
}

public static readonly BindableProperty ToggledCommandProperty = BindableProperty.Create(nameof(ToggledCommand), typeof(ICommand), typeof(IosSwitch));

public ICommand ToggledCommand
Expand Down
1 change: 1 addition & 0 deletions Demo/App/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<controls:CustomButton Clicked="SwitchViewBtn_Clicked" Text="SwitchView examples" />
<controls:CustomButton Clicked="AccessibilityBtn_Clicked" Text="Accessiblity tests" />
<controls:CustomButton Clicked="IsEnabledBtn_Clicked" Text="IsEnabled tests" />
<controls:CustomButton Clicked="IsBusyBtn_Clicked" Text="IsBusy tests" />
<controls:CustomButton Clicked="PanGestureBtn_Clicked" Text="PanGesture tests" />
</VerticalStackLayout>
</ContentPage.Content>
Expand Down
5 changes: 5 additions & 0 deletions Demo/App/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ async void IsEnabledBtn_Clicked(object sender, EventArgs e)
await Navigation.PushAsync(new IsEnabledTestPage());
}

async void IsBusyBtn_Clicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new IsBusyTestPage());
}

async void PanGestureBtn_Clicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new PanGestureTestPage());
Expand Down
37 changes: 37 additions & 0 deletions Demo/App/Pages/IsBusyTestPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Class="App.Pages.IsBusyTestPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls="clr-namespace:App.Controls;assembly=App"
xmlns:examples="clr-namespace:App.Controls.CustomSwitchExamples;assembly=App"
Title="IsBusy Test Page">
<ScrollView Padding="{StaticResource PagePadding}">
<VerticalStackLayout BackgroundColor="Transparent" MinimumHeightRequest="1000">
<controls:BackButton />

<HorizontalStackLayout Margin="0,20">
<Label Style="{StaticResource sectionHeading}" Text="Toggle IsBusy: " />
<examples:AndroidSwitch x:Name="DynamicToggle" />
</HorizontalStackLayout>

<examples:IosSwitch IsBusy="{Binding Source={x:Reference DynamicToggle}, Path=IsToggled}" IsToggled="False">
<examples:IosSwitch.Triggers>
<DataTrigger Binding="{Binding Source={x:Reference DynamicToggle}, Path=IsToggled}"
TargetType="examples:IosSwitch"
Value="True">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</examples:IosSwitch.Triggers>
</examples:IosSwitch>
<examples:IosSwitch IsBusy="{Binding Source={x:Reference DynamicToggle}, Path=IsToggled}" IsToggled="True">
<examples:IosSwitch.Triggers>
<DataTrigger Binding="{Binding Source={x:Reference DynamicToggle}, Path=IsToggled}"
TargetType="examples:IosSwitch"
Value="True">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</examples:IosSwitch.Triggers>
</examples:IosSwitch>
</VerticalStackLayout>
</ScrollView>
</ContentPage>
9 changes: 9 additions & 0 deletions Demo/App/Pages/IsBusyTestPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace App.Pages;

public partial class IsBusyTestPage : ContentPage
{
public IsBusyTestPage()
{
InitializeComponent();
}
}
1 change: 1 addition & 0 deletions Scr/Switch/CustomSwitch.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
StrokeThickness="{Binding KnobStrokeThickness, Source={RelativeSource AncestorType={x:Type ContentView}}}"
VerticalOptions="Center"
WidthRequest="{Binding KnobWidth, Source={RelativeSource AncestorType={x:Type ContentView}}}" />
<ContentView Content="{Binding IsBusyContent, Source={RelativeSource AncestorType={x:Type ContentView}, AncestorLevel=1}}" IsVisible="{Binding IsBusy, Source={RelativeSource AncestorType={x:Type ContentView}, AncestorLevel=1}}" />
</Grid>
</view:SwitchView.Content>
</view:SwitchView>
18 changes: 15 additions & 3 deletions Scr/Switch/CustomSwitch.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,20 @@ public KnobLimitEnum KnobLimit
set => SetValue(KnobLimitProperty, value);
}

public static readonly BindableProperty IsBusyProperty = BindableProperty.Create(nameof(IsBusy), typeof(bool), typeof(CustomSwitch), false);
public bool IsBusy
{
get => (bool)GetValue(IsBusyProperty);
set => SetValue(IsBusyProperty, value);
}

public static readonly BindableProperty IsBusyContentProperty = BindableProperty.Create(nameof(IsBusyContent), typeof(View), typeof(CustomSwitch), null);
public View? IsBusyContent
{
get => (View?)GetValue(IsBusyContentProperty);
set => SetValue(IsBusyContentProperty, value);
}

#endregion Properties

#region Events
Expand Down Expand Up @@ -205,8 +219,6 @@ public CustomSwitch()

HasLoaded = true;
};


}

protected override void IsToggledChanged()
Expand All @@ -215,7 +227,7 @@ protected override void IsToggledChanged()
{
return;
}

if (IsToggled && CurrentState != SwitchStateEnum.Right)
{
GoToRight();
Expand Down

0 comments on commit 4baa20e

Please sign in to comment.