Added WP8 support

This commit is contained in:
EddyVerbruggen 2014-04-17 20:41:02 +02:00
parent b0a024f012
commit 495cab2b79
3 changed files with 128 additions and 37 deletions

View File

@ -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
<param name="android-package" value="nl.xservices.plugins.Toast" />
</feature>
```
```xml
<!-- for WP8 -->
<feature name="Toast">
<param name="wp-package" value="Toast"/>
</feature>
```
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
<script type="text/javascript" src="js/Toast.js"></script>
```
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/<ProjectName>/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:
<button onclick="window.plugins.toast.show('Hello there!', 'long', 'center', function(a){console.log('toast success: ' + a)}, function(b){alert('toast error: ' + b)})">Toast show long center</button>
```
## 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)

View File

@ -2,7 +2,7 @@
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="nl.x-services.plugins.toast"
version="1.0">
version="2.0">
<name>Toast</name>
@ -50,7 +50,7 @@
<source-file src="src/android/nl/xservices/plugins/Toast.java" target-dir="src/nl/xservices/plugins"/>
</platform>
<!-- wp8.. enable when we have a proper iOS-style Toast impl, not the native ShellToast impl which is more like a local push message
<!-- wp8 -->
<platform name="wp8">
<config-file target="config.xml" parent="/*">
<feature name="Toast">
@ -59,6 +59,6 @@
</config-file>
<source-file src="src/wp8/Toast.cs" />
</platform-->
</platform>
</plugin>

View File

@ -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<string[]>(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<string[]>(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;
}
}
}