From 495cab2b79bc3e41fa61a7c4cbb24835994b5729 Mon Sep 17 00:00:00 2001 From: EddyVerbruggen Date: Thu, 17 Apr 2014 20:41:02 +0200 Subject: [PATCH] Added WP8 support --- README.md | 27 +++++++--- plugin.xml | 6 +-- src/wp8/Toast.cs | 132 +++++++++++++++++++++++++++++++++++++---------- 3 files changed, 128 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 8ba5382..6cb3ee9 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # PhoneGap Toast plugin -for Android and iOS, by [Eddy Verbruggen](http://www.x-services.nl/phonegap-toast-plugin/796) +for Android, iOS and WP8, by [Eddy Verbruggen](http://www.x-services.nl/phonegap-toast-plugin/796) ## 0. Index @@ -12,11 +12,12 @@ for Android and iOS, by [Eddy Verbruggen](http://www.x-services.nl/phonegap-toas 3. [PhoneGap Build](#phonegap-build) 4. [Usage](#4-usage) 5. [Credits](#5-credits) -6. [License](#6-license) +6. [Changelog](#6-changelog) +7. [License](#7-license) ## 1. Description -This plugin allows you to show a native Toast (a little text popup) on iOS and Android. +This plugin allows you to show a native Toast (a little text popup) on iOS, Android and WP8. It's great for showing a non intrusive native notification which is guaranteed always in the viewport of the browser. * You can choose where to show the Toast: at the top, center or bottom of the screen. * You can choose two durations: short (approx. 2 seconds), or long (approx. 5 seconds), after which the Toast automatically disappears. @@ -42,6 +43,8 @@ Android ![ScreenShot](screenshot-android-toast.png) +WP8 (will be added soon) + ## 3. Installation ### Automatically (CLI / Plugman) @@ -73,6 +76,12 @@ Toast.js is brought in automatically. There is no need to change or add anything ``` +```xml + + + + +``` For iOS, you'll need to add the `QuartzCore.framework` to your project (it's for automatically removing the Toast after a few seconds). Click your project, Build Phases, Link Binary With Libraries, search for and add `QuartzCore.framework`. @@ -82,12 +91,14 @@ Click your project, Build Phases, Link Binary With Libraries, search for and add ``` -3\. Download the source files for iOS and/or Android and copy them to your project. +3\. Download the source files and copy them to your project. iOS: Copy the two `.h` and two `.m` files to `platforms/ios//Plugins` Android: Copy `Toast.java` to `platforms/android/src/nl/xservices/plugins` (create the folders) +WP8: Copy `Toast.cs` to `platforms/wp8/Plugins/nl.x-services.plugins.toast` (create the folders) + ### PhoneGap Build Toast works with PhoneGap build too, but only with PhoneGap 3.0 and up. @@ -124,13 +135,17 @@ You can copy-paste these lines of code for a quick test: ``` -## 5. CREDITS ## +## 5. CREDITS This plugin was enhanced for Plugman / PhoneGap Build by [Eddy Verbruggen](http://www.x-services.nl). The Android code was entirely created by me. For iOS most credits go to this excellent [Toast for iOS project by Charles Scalesse] (https://github.com/scalessec/Toast). -## 6. License +## 6. CHANGELOG +2.0: WP8 support +1.0: initial version supporting Android and iOS + +## 7. License [The MIT License (MIT)](http://www.opensource.org/licenses/mit-license.html) diff --git a/plugin.xml b/plugin.xml index 5acc9f8..94211f2 100755 --- a/plugin.xml +++ b/plugin.xml @@ -2,7 +2,7 @@ + version="2.0"> Toast @@ -50,7 +50,7 @@ - @@ -59,6 +59,6 @@ - + diff --git a/src/wp8/Toast.cs b/src/wp8/Toast.cs index 334281b..f0d887a 100644 --- a/src/wp8/Toast.cs +++ b/src/wp8/Toast.cs @@ -1,35 +1,111 @@ -using WPCordovaClassLib.Cordova; -using WPCordovaClassLib.Cordova.Commands; -using WPCordovaClassLib.Cordova.JSON; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using Microsoft.Phone.Controls; +using System.Windows.Controls.Primitives; +using System.Windows.Media; -// TODO create a custom overlay similar to the iOS implementation because the ShellToast on WP is -// very different from the native Android Toast the iOS impl is inspired on. -// Differences: -// - Only WP8 update 3 will show a Toast when in the foreground -// - A ShellToast can't be positioned -// - A ShellToast has a fixed duration -// DOCS: http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj662938(v=vs.105).aspx -// --> Conclusion: ShellToast is more like a localnotification/pushmessage, so it's a nice WP8 impl of the LocalNotification plugin, -// So we'll only add WP8 Toast plugin support if we can create an similar impl as the iOS version of this Toast plugin. -// Hence, leaving out the WP8 config in plugin.xml for now. -// --> Future work based on the conclusion: investigate these options http://stackoverflow.com/questions/20346219/how-to-show-toast-after-performing-some-functionality-in-windows-phone-8 -namespace Cordova.Extension.Commands { - public class Toast : BaseCommand { +namespace WPCordovaClassLib.Cordova.Commands +{ + public class Toast : BaseCommand + { - public void show(string jsonArgs) { + Popup popup; - var options = JsonHelper.Deserialize(jsonArgs); + private PhoneApplicationPage Page + { + get + { + PhoneApplicationPage page = null; + PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame; + if (frame != null) + { + page = frame.Content as PhoneApplicationPage; + } + return page; + } + } - var message = options[0]; -// var duration = options[1]; -// var position = options[2]; + public void show(string options) + { + string[] args = JSON.JsonHelper.Deserialize(options); + var message = args[0]; + var duration = args[1]; + var position = args[2]; + string aliasCurrentCommandCallbackId = args[3]; - ShellToast toast = new ShellToast(); - toast.Title = "Test"; - toast.Content = message; - toast.Show(); + Deployment.Current.Dispatcher.BeginInvoke(() => + { + PhoneApplicationPage page = Page; + if (page != null) + { + Grid grid = page.FindName("LayoutRoot") as Grid; + if (grid != null) + { + TextBlock tb = new TextBlock(); + tb.TextWrapping = TextWrapping.Wrap; + tb.TextAlignment = TextAlignment.Center; + tb.Text = message; + tb.Foreground = new SolidColorBrush(Color.FromArgb(255,255,255,255)); // white - DispatchCommandResult(new PluginResult(PluginResult.Status.OK)); - } - } + Border b = new Border(); + b.CornerRadius = new CornerRadius(12); + b.Background = new SolidColorBrush(Color.FromArgb(180, 55, 55, 55)); + b.HorizontalAlignment = HorizontalAlignment.Center; + + Grid pgrid = new Grid(); + pgrid.HorizontalAlignment = HorizontalAlignment.Stretch; + pgrid.VerticalAlignment = VerticalAlignment.Stretch; + pgrid.Margin = new Thickness(20); + pgrid.Children.Add(tb); + pgrid.Width = Application.Current.Host.Content.ActualWidth - 80; + + b.Child = pgrid; + if (popup != null && popup.IsOpen) + { + popup.IsOpen = false; + } + popup = new Popup(); + popup.Child = b; + + popup.HorizontalOffset = 20; + popup.Width = Application.Current.Host.Content.ActualWidth; + popup.HorizontalAlignment = HorizontalAlignment.Center; + + if ("top".Equals(position)) + { + popup.VerticalAlignment = VerticalAlignment.Top; + popup.VerticalOffset = 20; + } + else if ("bottom".Equals(position)) + { + popup.VerticalAlignment = VerticalAlignment.Bottom; + popup.VerticalOffset = -100; // TODO can do better + } + else // center + { + popup.VerticalAlignment = VerticalAlignment.Center; + popup.VerticalOffset = -50; // TODO can do way better + } + + grid.Children.Add(popup); + popup.IsOpen = true; + + int hideDelay = "long".Equals(duration) ? 5500 : 2500; + this.hidePopup(hideDelay); + } + } + else + { + DispatchCommandResult(new PluginResult(PluginResult.Status.INSTANTIATION_EXCEPTION)); + } + }); + } + + private async void hidePopup(int delay) + { + await Task.Delay(delay); + popup.IsOpen = false; + } + } } \ No newline at end of file