From 404201d876ee2f50e0b7fdf8b6dd733ef9f55fe2 Mon Sep 17 00:00:00 2001 From: Andrew Hoefling Date: Fri, 17 Nov 2017 22:25:55 +0000 Subject: [PATCH] Added display alert to clean up code for DisplayAlerts from the Platform class --- Ooui.Forms/DisplayAlert.cs | 106 +++++++++++++++++++++++++++++++++++ Ooui.Forms/Ooui.Forms.csproj | 1 - Ooui.Forms/Platform.cs | 73 ++---------------------- 3 files changed, 110 insertions(+), 70 deletions(-) create mode 100644 Ooui.Forms/DisplayAlert.cs diff --git a/Ooui.Forms/DisplayAlert.cs b/Ooui.Forms/DisplayAlert.cs new file mode 100644 index 0000000..dc04911 --- /dev/null +++ b/Ooui.Forms/DisplayAlert.cs @@ -0,0 +1,106 @@ +using System.Web; +using Xamarin.Forms.Internals; + +namespace Ooui.Forms +{ + public class DisplayAlert + { + private readonly Button _closeButton; + private readonly Button _acceptButton; + private readonly Button _cancelButton; + + public DisplayAlert(AlertArguments arguments) + { + Element = new Div + { + ClassName = "modal-dialog" + }; + + var content = new Div + { + ClassName = "modal-content" + }; + + var header = new Div + { + ClassName = "modal-header" + }; + + _closeButton = new Button + { + ClassName = "close" + }; + + _closeButton.AppendChild(new Span(HttpUtility.HtmlDecode("×"))); + + var h4 = new Heading(4) + { + Text = arguments.Title + }; + + header.AppendChild(_closeButton); + header.AppendChild(h4); + + content.AppendChild(header); + content.AppendChild(new Div() + { + ClassName = "modal-body", + Text = arguments.Message + }); + + if (!string.IsNullOrEmpty(arguments.Cancel)) + { + var footer = new Div() + { + ClassName = "modal-footer" + }; + + _cancelButton = new Button(arguments.Cancel) + { + ClassName = "btn btn-default" + }; + + footer.AppendChild(_cancelButton); + + if (!string.IsNullOrEmpty(arguments.Accept)) + { + _acceptButton = new Button(arguments.Accept) + { + ClassName = "btn btn-default" + }; + + footer.AppendChild(_acceptButton); + } + + content.AppendChild(footer); + } + + Element.AppendChild(content); + } + + public event TargetEventHandler Clicked + { + add + { + _closeButton.Clicked += value; + + if(_cancelButton != null) + _cancelButton.Clicked += value; + + if(_acceptButton != null) + _acceptButton.Clicked += value; + } + remove + { + _closeButton.Clicked -= value; + + if (_cancelButton != null) + _cancelButton.Clicked -= value; + + if (_acceptButton != null) + _acceptButton.Clicked -= value; + } + } + public Element Element { get; private set; } + } +} diff --git a/Ooui.Forms/Ooui.Forms.csproj b/Ooui.Forms/Ooui.Forms.csproj index 5c0c488..e081c10 100644 --- a/Ooui.Forms/Ooui.Forms.csproj +++ b/Ooui.Forms/Ooui.Forms.csproj @@ -15,7 +15,6 @@ - diff --git a/Ooui.Forms/Platform.cs b/Ooui.Forms/Platform.cs index 9fd5be1..5295ac6 100644 --- a/Ooui.Forms/Platform.cs +++ b/Ooui.Forms/Platform.cs @@ -35,79 +35,14 @@ namespace Ooui.Forms MessagingCenter.Subscribe(this, Page.AlertSignalName, (Page sender, AlertArguments arguments) => { - var alert = new Div - { - ClassName = "modal-dialog" - }; + var alert = new DisplayAlert(arguments); + alert.Clicked += CloseAlert; - var content = new Div - { - ClassName = "modal-content" - }; - - var header = new Div - { - ClassName = "modal-header" - }; - - var closeButton = new Button - { - ClassName = "close" - }; - - closeButton.AppendChild(new Span(HttpUtility.HtmlDecode("×"))); - closeButton.Clicked += CloseAlert; - - var h4 = new Heading(4) - { - Text = arguments.Title - }; - - header.AppendChild(closeButton); - header.AppendChild(h4); - - content.AppendChild(header); - content.AppendChild(new Div() - { - ClassName = "modal-body", - Text = arguments.Message - }); - - if (!string.IsNullOrEmpty(arguments.Cancel)) - { - var footer = new Div() - { - ClassName = "modal-footer" - }; - - var cancel = new Button(arguments.Cancel) - { - ClassName = "btn btn-default" - }; - cancel.Clicked += CloseAlert; - - footer.AppendChild(cancel); - - if (!string.IsNullOrEmpty(arguments.Accept)) - { - var accept = new Button(arguments.Accept) - { - ClassName = "btn btn-default" - }; - accept.Clicked += CloseAlert; - - footer.AppendChild(accept); - } - - content.AppendChild(footer); - } - - alert.AppendChild(content); - _renderer.AppendChild(alert); + _renderer.AppendChild(alert.Element); void CloseAlert(object s, EventArgs e) { - _renderer.RemoveChild(alert); + _renderer.RemoveChild(alert.Element); } }); }