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

ToggleSwitch feature request #58

Open
davepruitt opened this issue Apr 27, 2023 · 0 comments
Open

ToggleSwitch feature request #58

davepruitt opened this issue Apr 27, 2023 · 0 comments

Comments

@davepruitt
Copy link
Contributor

I came to use your customized version of the ToggleSwitch because the standard .NET MAUI Switch was not meeting my needs. Your drawn-control version seemed easy enough to modify to fit my needs, and indeed it is.

In my project, any time the user attempts to toggle a Switch/ToggleSwitch control, I'd like to display an alert to the user to verify if they actually want to take this action. Ideally, it would be nice to intercept the user before actually toggling the IsOn value on the switch. So, a basic flow would look something like this:

  1. User initiates interaction with the ToggleSwitch by clicking it. This calls the OnToggleSwitchStartInteraction function in the ToggleSwitch class.
  2. Before doing anything else, the ToggleSwitch raises an event that can be handled in user code, indicating that the toggle switch has received an interaction.
  3. The user code sees this event and is allowed to respond to it. In my specific scenario, I display an alert to the user and ask the user if they actually want to toggle the switch.
  4. The user code returns a value to the OnToggleSwitchStartInteraction function indicating whether the interaction will be allowed to proceed or whether it should be cancelled.
  5. If allowed to proceed, then OnToggleSwitchStartInteraction proceeds as normal.

I have already implemented this in my own code, and I have it working. It looks like this:

  1. I first define some event arguments we will use:
public class StartInteractionEventArgs : EventArgs
{
    public bool AllowInteractionToContinue { get; set; } = true;

    public StartInteractionEventArgs()
    {
        //empty
    }
}
  1. In ToggleSwitch, I have defined the following delegate:
public delegate Task AsyncEventHandler(object sender, StartInteractionEventArgs e);
public AsyncEventHandler UserHandleStartInteraction;
  1. In ToggleSwitch, I change OnToggleSwitchStartInteraction to be async, and I redefine the code to look like this:
async void OnToggleSwitchStartInteraction(object sender, TouchEventArgs e)
{
    if (IsEnabled)
    {
        StartInteractionEventArgs should_allow_interaction_event_args = new StartInteractionEventArgs();
        if (UserHandleStartInteraction != null)
        {
            await UserHandleStartInteraction(this, should_allow_interaction_event_args);
        }

        if (should_allow_interaction_event_args.AllowInteractionToContinue)
        {
            IsOn = !IsOn;
        }
    }
}

Note: this code also includes my fixes from #57

This is all the code that would need to be added to ToggleSwitch. Everything else would be in the user's application.

In my specific application, I use it like this:

  1. I have a ToggleSwitch defined in the XAML of my page:
<ToggleSwitch x:Name="MySwitch" IsOn="{Binding Path=IsMySwitchToggled, Mode=TwoWay}" />
  1. In the code-behind, I subscribe to the UserHandleStartInteraction event:
public MainPage()
{
	InitializeComponent();
	BindingContext = new MainPageViewModel();
    MySwitch.UserHandleStartInteraction += async (s, e) =>
    {
        await MySwitch_UserHandleStartInteraction(s, e);
    };
}

private async Task MySwitch_UserHandleStartInteraction(object sender, SwitchTest.StartInteractionEventArgs e)
{
    var result = await DisplayAlert("Are you sure?", string.Empty, "Yes", "No");
    e.AllowInteractionToContinue = result;
}

And that's it.

While this may not be the best way to implement this feature, it gets the idea across. It allows the application to intercept a user's interaction and determine whether or not to allow it to proceed. I think this could be a valuable feature moving forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant