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

Implement Alerts (Alert, Prompt and ActionSheet) #1328

Merged
merged 23 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/Compatibility/Core/src/WinUI/Forms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,6 @@ static void SetupInit(
{
MainWindow = mainWindow;

//TODO WINUI3
Platform.UWP.Platform.SubscribeAlertsAndActionSheets();

if (mainWindow is WindowsBasePage windowsPage)
{
windowsPage.LoadApplication(windowsPage.CreateApplication());
Expand Down
44 changes: 44 additions & 0 deletions src/Controls/samples/Controls.Sample/Pages/Core/AlertsPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8" ?>
<views:BasePage
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Pages.AlertsPage"
xmlns:views="clr-namespace:Maui.Controls.Sample.Pages.Base"
Title="Alerts">
<views:BasePage.Content>
<StackLayout
Margin="12">
<Label
Text="Display Alert"
Style="{StaticResource Headline}"/>
<Button
Text="Alert Simple"
Clicked="OnAlertSimpleClicked" />
<Button
Text="Alert Yes/No"
Clicked="OnAlertYesNoClicked" />
<Label
Text="Display ActionSheet"
Style="{StaticResource Headline}"/>
<Button
Text="ActionSheet Simple"
Clicked="OnActionSheetSimpleClicked" />
<Button
Text="ActionSheet Cancel/Delete"
Clicked="OnActionSheetCancelDeleteClicked" />
<Label
Text="Display Prompt"
Style="{StaticResource Headline}"/>
<Button
Text="Question 1"
Clicked="OnQuestion1ButtonClicked" />
<Label
x:Name="question1ResultLabel" />
<Button
Text="Question 2"
Clicked="OnQuestion2ButtonClicked" />
<Label
x:Name="question2ResultLabel" />
</StackLayout>
</views:BasePage.Content>
</views:BasePage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Diagnostics;
using Microsoft.Maui;

namespace Maui.Controls.Sample.Pages
{
public partial class AlertsPage
{
public AlertsPage()
{
InitializeComponent();
}

async void OnAlertSimpleClicked(object sender, EventArgs e)
{
await DisplayAlert("Alert", "You have been alerted", "OK");
}

async void OnAlertYesNoClicked(object sender, EventArgs e)
{
var answer = await DisplayAlert("Question?", "Would you like to play a game", "Yes", "No");
Debug.WriteLine("Answer: " + answer);
}

async void OnActionSheetSimpleClicked(object sender, EventArgs e)
{
var action = await DisplayActionSheet("ActionSheet: Send to?", "Cancel", null, "Email", "Twitter", "Facebook");
Debug.WriteLine("Action: " + action);
}

async void OnActionSheetCancelDeleteClicked(object sender, EventArgs e)
{
var action = await DisplayActionSheet("ActionSheet: SavePhoto?", "Cancel", "Delete", "Photo Roll", "Email");
Debug.WriteLine("Action: " + action);
}

async void OnQuestion1ButtonClicked(object sender, EventArgs e)
{
string result = await DisplayPromptAsync("Question 1", "What's your name?", initialValue: string.Empty);

if (!string.IsNullOrWhiteSpace(result))
{
question1ResultLabel.Text = $"Hello {result}.";
}
}

async void OnQuestion2ButtonClicked(object sender, EventArgs e)
{
string result = await DisplayPromptAsync("Question 2", "What's 5 + 5?", initialValue: "10", maxLength: 2, keyboard: Keyboard.Numeric);

if (!string.IsNullOrWhiteSpace(result))
{
int number = Convert.ToInt32(result);
question2ResultLabel.Text = number == 10 ? "Correct." : "Incorrect.";
}
}
}
}
6 changes: 4 additions & 2 deletions src/Controls/samples/Controls.Sample/Pages/TempPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ public TempPage()
InitializeComponent();
}

void OnButtonClicked(object sender, EventArgs e)
async void OnButtonClicked(object sender, EventArgs e)
{
_counter++;
CounterLabel.Text = $"You clicked {_counter} times!";
string message = $"You clicked {_counter} times!";
CounterLabel.Text = message;
await DisplayAlert("Alert", message, "Ok");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ namespace Maui.Controls.Sample.ViewModels
public class CoreViewModel : BaseGalleryViewModel
{
protected override IEnumerable<SectionModel> CreateItems() => new[]
{
{
new SectionModel(typeof(AlertsPage), "Alerts",
"Displaying an alert, asking a user to make a choice, or displaying a prompt."),

new SectionModel(typeof(BrushesPage), "Brushes",
"A brush enables you to paint an area, such as the background of a control, using different approaches."),

Expand Down
3 changes: 0 additions & 3 deletions src/Controls/src/Core/HandlerImpl/Window.Android.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#nullable enable

using System;
using System.Collections.Generic;
using System.Text;
using Android.App;
using AndroidX.AppCompat.App;

namespace Microsoft.Maui.Controls
Expand Down
19 changes: 17 additions & 2 deletions src/Controls/src/Core/HandlerImpl/Window.Impl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ public partial class Window : VisualElement, IWindow
internal IMauiContext MauiContext => Page?.Handler?.MauiContext
?? throw new InvalidOperationException("MauiContext is null");

internal AlertManager AlertManager { get; }

internal ModalNavigationService ModalNavigationService { get; }

public Window()
{
ModalNavigationService = new ModalNavigationService(this);
Navigation = new NavigationImpl(this);
AlertManager = new AlertManager(this);

InternalChildren.CollectionChanged += OnCollectionChanged;
}

Expand All @@ -38,7 +42,6 @@ public Window(Page page)
Page = page;
}


void SendWindowAppearing()
{
Page?.SendAppearing();
Expand Down Expand Up @@ -80,7 +83,10 @@ public Page? Page
InternalChildren.Remove(_page);

if (_page != null)
{
_page.AttachedHandler -= OnPageAttachedHandler;
_page.DetachedHandler -= OnPageDetachedHandler;
}
}

_page = value;
Expand All @@ -94,13 +100,23 @@ public Page? Page
ModalNavigationService.SettingNewPage();

if (value != null)
{
value.AttachedHandler += OnPageAttachedHandler;
value.DetachedHandler += OnPageDetachedHandler;
}
}
}

void OnPageAttachedHandler(object? sender, EventArgs e)
{
ModalNavigationService.PageAttachedHandler();

AlertManager.Subscribe();
jsuarezruiz marked this conversation as resolved.
Show resolved Hide resolved
}

void OnPageDetachedHandler(object? sender, EventArgs e)
{
AlertManager.Unsubscribe();
}

IView IWindow.View
Expand Down Expand Up @@ -149,7 +165,6 @@ void OnModalPushing(Page modalPage)
(Parent as Application)?.NotifyOfWindowModalEvent(args);
}


void OnPopCanceled()
{
PopCanceled?.Invoke(this, EventArgs.Empty);
Expand Down
4 changes: 0 additions & 4 deletions src/Controls/src/Core/HandlerImpl/Window.iOS.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#nullable enable

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Maui.Controls.Platform;
using UIKit;

namespace Microsoft.Maui.Controls
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#nullable disable
using System;
using System.Linq;
using Microsoft.Maui.Controls.Internals;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;

namespace Microsoft.Maui.Controls.Platform
{
public sealed class ActionSheetContent : UserControl
{
readonly ActionSheetArguments _options;

public event EventHandler OptionSelected;

public ActionSheetContent(ActionSheetArguments sheetOptions)
{
Initialize();

_options = sheetOptions;

TitleBlock.Text = _options.Title ?? string.Empty;
OptionsList.ItemsSource = _options.Buttons.ToList();

if (_options.FlowDirection == Controls.FlowDirection.RightToLeft)
{
TitleBlock.FlowDirection = UI.Xaml.FlowDirection.RightToLeft;
OptionsList.FlowDirection = UI.Xaml.FlowDirection.RightToLeft;
}
else if (_options.FlowDirection == Controls.FlowDirection.LeftToRight)
{
TitleBlock.FlowDirection = UI.Xaml.FlowDirection.LeftToRight;
OptionsList.FlowDirection = UI.Xaml.FlowDirection.LeftToRight;
}

if (_options.FlowDirection == Controls.FlowDirection.RightToLeft)
{
if (_options.Cancel != null)
{
LeftBtn.Content = _options.Cancel;
if (_options.Destruction != null)
RightBtn.Content = _options.Destruction;
}
else if (_options.Destruction != null)
LeftBtn.Content = _options.Destruction;
}
else
{
if (_options.Cancel != null)
{
RightBtn.Content = _options.Cancel;
if (_options.Destruction != null)
LeftBtn.Content = _options.Destruction;
}
else if (_options.Destruction != null)
RightBtn.Content = _options.Destruction;
}

LeftBtn.Visibility = LeftBtn.Content == null ? UI.Xaml.Visibility.Collapsed : UI.Xaml.Visibility.Visible;
RightBtn.Visibility = RightBtn.Content == null ? UI.Xaml.Visibility.Collapsed : UI.Xaml.Visibility.Visible;
}

internal TextBlock TitleBlock { get; private set; }

internal UI.Xaml.Controls.ListView OptionsList { get; private set; }

internal UI.Xaml.Controls.Button LeftBtn { get; private set; }

internal UI.Xaml.Controls.Button RightBtn { get; private set; }

void Initialize()
{
var mainLayout = new UI.Xaml.Controls.Grid
{
Padding = new UI.Xaml.Thickness(10),
RowDefinitions =
{
new UI.Xaml.Controls.RowDefinition { Height = new UI.Xaml.GridLength(0, UI.Xaml.GridUnitType.Star) },
new UI.Xaml.Controls.RowDefinition { Height = new UI.Xaml.GridLength(0, UI.Xaml.GridUnitType.Auto) }
}
};

var firstLayout = new UI.Xaml.Controls.Grid
{
RowDefinitions =
{
new UI.Xaml.Controls.RowDefinition { Height = new UI.Xaml.GridLength(0, UI.Xaml.GridUnitType.Auto) },
new UI.Xaml.Controls.RowDefinition { Height = new UI.Xaml.GridLength(0, UI.Xaml.GridUnitType.Star) }
}
};

TitleBlock = new TextBlock { FontSize = 18, MaxLines = 2 };
firstLayout.Children.Add(TitleBlock);
UI.Xaml.Controls.Grid.SetRow(TitleBlock, 0);

OptionsList = new UI.Xaml.Controls.ListView { IsItemClickEnabled = true, Margin = new UI.Xaml.Thickness(0, 10, 0, 10), SelectionMode = UI.Xaml.Controls.ListViewSelectionMode.None };
OptionsList.ItemClick += ListItemSelected;
firstLayout.Children.Add(OptionsList);
UI.Xaml.Controls.Grid.SetRow(OptionsList, 1);

var secondLayout = new UI.Xaml.Controls.Grid
{
ColumnDefinitions =
{
new UI.Xaml.Controls.ColumnDefinition { Width = new UI.Xaml.GridLength(0, UI.Xaml.GridUnitType.Star) },
new UI.Xaml.Controls.ColumnDefinition { Width = new UI.Xaml.GridLength(0, UI.Xaml.GridUnitType.Star) }
},
VerticalAlignment = VerticalAlignment.Bottom
};

LeftBtn = new UI.Xaml.Controls.Button { Height = 32, HorizontalAlignment = HorizontalAlignment.Stretch, Margin = new UI.Xaml.Thickness(0, 0, 5, 0) };
LeftBtn.Click += ActionButtonClicked;
secondLayout.Children.Add(LeftBtn);
UI.Xaml.Controls.Grid.SetColumn(LeftBtn, 0);

RightBtn = new UI.Xaml.Controls.Button { Height = 32, HorizontalAlignment = HorizontalAlignment.Stretch, Margin = new UI.Xaml.Thickness(5, 0, 0, 0) };
RightBtn.Click += ActionButtonClicked;
secondLayout.Children.Add(RightBtn);
UI.Xaml.Controls.Grid.SetColumn(RightBtn, 1);

mainLayout.Children.Add(firstLayout);
UI.Xaml.Controls.Grid.SetRow(firstLayout, 0);

mainLayout.Children.Add(secondLayout);
UI.Xaml.Controls.Grid.SetRow(secondLayout, 1);

Content = mainLayout;
}

void ListItemSelected(object sender, ItemClickEventArgs e)
{
var selection = (string)e.ClickedItem;
_options.SetResult(selection);

OptionSelected?.Invoke(this, null);
}

void ActionButtonClicked(object sender, RoutedEventArgs e)
{
var button = (UI.Xaml.Controls.Button)sender;
var selection = (string)button.Content;
_options.SetResult(selection);

OptionSelected?.Invoke(this, null);
}
}
}
Loading