mirror of
https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin.git
synced 2026-07-31 00:02:14 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
55856bcb44 | ||
|
|
28cc48357c | ||
|
|
4399bed9e1 | ||
|
|
09d536b9ec | ||
|
|
653aed37a3 | ||
|
|
82f72d2da7 | ||
|
|
440ea5bded | ||
|
|
8869b81749 | ||
|
|
e4e18f143a | ||
|
|
9757e2c04c | ||
|
|
791beb0bc4 | ||
|
|
fde8da464f | ||
|
|
dbf27e63a4 | ||
|
|
3235b9c8e3 | ||
|
|
ed2398455a | ||
|
|
2a0ecbc2dd | ||
|
|
6351512cf1 | ||
|
|
206b7e014e | ||
|
|
a429424107 | ||
|
|
4324168d2a | ||
|
|
75287f6517 | ||
|
|
04b4cf7894 | ||
|
|
d5356d7562 | ||
|
|
8d4a3d646c | ||
|
|
d81936a08a | ||
|
|
f3f608ee1c | ||
|
|
e3fb477789 | ||
|
|
ce1dfa79cc | ||
|
|
7b903b7f72 | ||
|
|
093b523dc6 | ||
|
|
37a73baa1f | ||
|
|
0b76cb8811 | ||
|
|
b3e3d8b858 | ||
|
|
b5adb0da91 | ||
|
|
52e4072ce6 | ||
|
|
11bcf9b14d | ||
|
|
55f9b41d87 | ||
|
|
0aeb2c4f95 | ||
|
|
dc6c537125 | ||
|
|
9ed9f018a7 | ||
|
|
fe1aed17e3 | ||
|
|
566f6f7392 | ||
|
|
9b9365d9da | ||
|
|
4fb9932c9b | ||
|
|
4c65555aca | ||
|
|
aa1a771f7b | ||
|
|
35ace3dd9d | ||
|
|
c0daed7ce4 | ||
|
|
8dbcc43e68 | ||
|
|
4f6463e3f2 | ||
|
|
41a568c549 | ||
|
|
811e0a82ed | ||
|
|
a2d253bdde | ||
|
|
ffe056db75 | ||
|
|
5e5251bbd2 | ||
|
|
37c14a6d2d | ||
|
|
369ab363b9 | ||
|
|
6ba778dd22 | ||
|
|
e8ade312d9 | ||
|
|
f6479c8846 | ||
|
|
658ee711db | ||
|
|
5f4a0f5636 | ||
|
|
c7a329a7ac | ||
|
|
577b1704d9 | ||
|
|
ac0746b9b9 | ||
|
|
7a08e6afd6 | ||
|
|
23d35404b5 | ||
|
|
a5cdda3ea2 | ||
|
|
c5b6060973 | ||
|
|
cabeea18ed | ||
|
|
335f731da4 | ||
|
|
ecb47ab110 | ||
|
|
f8887dc45e | ||
|
|
273334664a | ||
|
|
6ee16e9e7d | ||
|
|
6e13de8982 | ||
|
|
03320113b4 | ||
|
|
5634e1d84c |
@@ -0,0 +1,4 @@
|
|||||||
|
.idea/
|
||||||
|
screenshots/
|
||||||
|
test/
|
||||||
|
issue_template.md
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
[The MIT License (MIT)](http://www.opensource.org/licenses/mit-license.html)
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
@@ -1,6 +1,9 @@
|
|||||||
# PhoneGap Toast plugin
|
# PhoneGap Toast plugin
|
||||||
|
|
||||||
for Android, iOS and WP8, by [Eddy Verbruggen](http://www.x-services.nl/phonegap-toast-plugin/796)
|
for Android, iOS, WP8, Windows and BlackBerry by [Eddy Verbruggen](http://www.x-services.nl/phonegap-toast-plugin/796)
|
||||||
|
|
||||||
|
[](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=eddyverbruggen%40gmail%2ecom&lc=US&item_name=cordova%2dplugin%2dtoast¤cy_code=USD&bn=PP%2dDonationsBF%3abtn_donate_SM%2egif%3aNonHosted)
|
||||||
|
If you like this plugin and want to say thanks please send a PR or donation. Both are equally appreciated!
|
||||||
|
|
||||||
|
|
||||||
<table width="100%">
|
<table width="100%">
|
||||||
@@ -15,13 +18,13 @@ for Android, iOS and WP8, by [Eddy Verbruggen](http://www.x-services.nl/phonegap
|
|||||||
1. [Description](#1-description)
|
1. [Description](#1-description)
|
||||||
2. [Screenshots](#2-screenshots)
|
2. [Screenshots](#2-screenshots)
|
||||||
3. [Installation](#3-installation)
|
3. [Installation](#3-installation)
|
||||||
3. [Automatically (CLI / Plugman)](#automatically-cli--plugman)
|
3. [Automatically (CLI / Plugman)](#automatically-cli--plugman)
|
||||||
3. [Manually](#manually)
|
3. [Manually](#manually)
|
||||||
3. [PhoneGap Build](#phonegap-build)
|
3. [PhoneGap Build](#phonegap-build)
|
||||||
4. [Usage](#4-usage)
|
4. [Usage](#4-usage)
|
||||||
|
4. [Styling](#styling)
|
||||||
5. [Credits](#5-credits)
|
5. [Credits](#5-credits)
|
||||||
6. [Changelog](#6-changelog)
|
6. [Changelog](#6-changelog)
|
||||||
7. [License](#7-license)
|
|
||||||
|
|
||||||
## 1. Description
|
## 1. Description
|
||||||
|
|
||||||
@@ -29,18 +32,15 @@ This plugin allows you to show a native Toast (a little text popup) on iOS, Andr
|
|||||||
It's great for showing a non intrusive native notification which is guaranteed always in the viewport of the browser.
|
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 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.
|
* You can choose two durations: short (approx. 2 seconds), or long (approx. 5 seconds), after which the Toast automatically disappears.
|
||||||
* Compatible with [Cordova Plugman](https://github.com/apache/cordova-plugman).
|
|
||||||
* Officially supported by [PhoneGap Build](https://build.phonegap.com/plugins).
|
|
||||||
* Minimum iOS version is [6](https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin/issues/7).
|
|
||||||
|
|
||||||
Example usages:
|
Example usages:
|
||||||
* "There were validation errors"
|
* "There were validation errors"
|
||||||
* "Account created successfully"
|
* "Account created successfully"
|
||||||
* "The record was deleted"
|
* "The record was deleted"
|
||||||
* "Login successful"
|
* "Login successful"
|
||||||
* "The battery is almost dead"
|
|
||||||
* "You are now logged out"
|
* "You are now logged out"
|
||||||
* "Connection failure, please try again later"
|
* "Connection failure, please try again later"
|
||||||
|
* "Created Order #00287
|
||||||
|
|
||||||
## 2. Screenshots
|
## 2. Screenshots
|
||||||
|
|
||||||
@@ -48,10 +48,18 @@ iOS
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
A few styling options
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
Android
|
Android
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
|
||||||
Windows Phone 8
|
Windows Phone 8
|
||||||
|
|
||||||

|

|
||||||
@@ -129,7 +137,7 @@ Toast.js is brought in automatically. There is no need to change or add anything
|
|||||||
### Showing a Toast
|
### Showing a Toast
|
||||||
You have two choices to make when showing a Toast: where to show it and for how long.
|
You have two choices to make when showing a Toast: where to show it and for how long.
|
||||||
* show(message, duration, position)
|
* show(message, duration, position)
|
||||||
* duration: 'short', 'long'
|
* duration: 'short', 'long', '3000', 900 (the latter are milliseconds)
|
||||||
* position: 'top', 'center', 'bottom'
|
* position: 'top', 'center', 'bottom'
|
||||||
|
|
||||||
You can also use any of these convenience methods:
|
You can also use any of these convenience methods:
|
||||||
@@ -151,11 +159,11 @@ You can copy-paste these lines of code for a quick test:
|
|||||||
Since 2.1.0 you can add pixels to move the toast up or down.
|
Since 2.1.0 you can add pixels to move the toast up or down.
|
||||||
Note that `showWithOptions` can be used instead of the functions above, but it's not useful unless you want to pass `addPixelsY`.
|
Note that `showWithOptions` can be used instead of the functions above, but it's not useful unless you want to pass `addPixelsY`.
|
||||||
```js
|
```js
|
||||||
function showTop() {
|
function showBottom() {
|
||||||
window.plugins.toast.showWithOptions(
|
window.plugins.toast.showWithOptions(
|
||||||
{
|
{
|
||||||
message: "hey there",
|
message: "hey there",
|
||||||
duration: "short",
|
duration: "short", // which is 2000 ms. "long" is 4000. Or specify the nr of ms yourself.
|
||||||
position: "bottom",
|
position: "bottom",
|
||||||
addPixelsY: -40 // added a negative value to move it up a bit (default 0)
|
addPixelsY: -40 // added a negative value to move it up a bit (default 0)
|
||||||
},
|
},
|
||||||
@@ -174,7 +182,84 @@ function hide() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### WP8 quircks
|
When the toast gets hidden, your success callback will be called (in case you have defined one) with the `event` property equals to `hide` (more details about the callback in the next section).
|
||||||
|
```js
|
||||||
|
|
||||||
|
window.plugins.toast.showWithOptions({
|
||||||
|
message: 'My message',
|
||||||
|
// More config here...
|
||||||
|
},
|
||||||
|
//Success callback
|
||||||
|
function(args) {
|
||||||
|
console.log(args.event);
|
||||||
|
//This will print 'hide'
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
console.error('toast error: ', error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Receiving a callback when a Toast is tapped
|
||||||
|
On iOS and Android the success handler of your `show` function will be notified (again) when the toast was tapped.
|
||||||
|
|
||||||
|
So the first time the success handler fires is when the toast is shown, and in case the user taps the toast it will be
|
||||||
|
called again. You can distinguish between those events of course:
|
||||||
|
|
||||||
|
```js
|
||||||
|
window.plugins.toast.showWithOptions(
|
||||||
|
{
|
||||||
|
message: "hey there",
|
||||||
|
duration: 1500, // ms
|
||||||
|
position: "bottom",
|
||||||
|
addPixelsY: -40, // (optional) added a negative value to move it up a bit (default 0)
|
||||||
|
data: {'foo':'bar'} // (optional) pass in a JSON object here (it will be sent back in the success callback below)
|
||||||
|
},
|
||||||
|
// implement the success callback
|
||||||
|
function(result) {
|
||||||
|
if (result && result.event) {
|
||||||
|
console.log("The toast was tapped or got hidden, see the value of result.event");
|
||||||
|
console.log("Event: " + result.event); // "touch" when the toast was touched by the user or "hide" when the toast geot hidden
|
||||||
|
console.log("Message: " + result.message); // will be equal to the message you passed in
|
||||||
|
console.log("data.foo: " + result.data.foo); // .. retrieve passed in data here
|
||||||
|
|
||||||
|
if (result.event === 'hide') {
|
||||||
|
console.log("The toast has been shown");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
The success callback is useful when your toast is bound to a notification id in your backend and you have to mark it as `read` when the toast is done, or to update the notifications counter for iOS. The usage of this will be defined by your application logic. Use the `result.data` object to support your specific logic.
|
||||||
|
|
||||||
|
### Styling
|
||||||
|
Since version 2.4.0 you can pass an optional `styling` object to the plugin.
|
||||||
|
The defaults make sure the Toast looks the same as when you would not pass in the `styling` object at all.
|
||||||
|
|
||||||
|
Note that on WP this object is currently ignored.
|
||||||
|
|
||||||
|
```js
|
||||||
|
window.plugins.toast.showWithOptions({
|
||||||
|
message: "hey there",
|
||||||
|
duration: "short", // 2000 ms
|
||||||
|
position: "bottom",
|
||||||
|
styling: {
|
||||||
|
opacity: 0.75, // 0.0 (transparent) to 1.0 (opaque). Default 0.8
|
||||||
|
backgroundColor: '#FF0000', // make sure you use #RRGGBB. Default #333333
|
||||||
|
textColor: '#FFFF00', // Ditto. Default #FFFFFF
|
||||||
|
textSize: 20.5, // Default is approx. 13.
|
||||||
|
cornerRadius: 16, // minimum is 0 (square). iOS default 20, Android default 100
|
||||||
|
horizontalPadding: 20, // iOS default 16, Android default 50
|
||||||
|
verticalPadding: 16 // iOS default 12, Android default 30
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
Tip: if you need to pass different values for iOS and Android you can use fi. the device plugin
|
||||||
|
to determine the platform and pass `opacity: isAndroid() ? 0.7 : 0.9`.
|
||||||
|
|
||||||
|
### WP8 quirks
|
||||||
The WP8 implementation needs a little more work, but it's perfectly useable when you keep this in mind:
|
The WP8 implementation needs a little more work, but it's perfectly useable when you keep this in mind:
|
||||||
* You can't show two Toasts simultaneously.
|
* You can't show two Toasts simultaneously.
|
||||||
* Wait until the first Toast is hidden before the second is shown, otherwise the second one will be hidden quickly.
|
* Wait until the first Toast is hidden before the second is shown, otherwise the second one will be hidden quickly.
|
||||||
@@ -188,30 +273,18 @@ 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).
|
For iOS most credits go to this excellent [Toast for iOS project by Charles Scalesse] (https://github.com/scalessec/Toast).
|
||||||
|
|
||||||
## 6. CHANGELOG
|
## 6. CHANGELOG
|
||||||
2.0.1: iOS messages are hidden when another one is shown. [Thanks Richie Min!](https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin/pull/13)
|
- 2.7.2: Even better Android compatibility, but you're limited to short and long durations now, on Android.
|
||||||
|
- 2.7.0: [Android P compatibility.](https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin/issues/116)
|
||||||
2.0: WP8 support
|
- 2.6.2: [iOS view hierarchy fix.](https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin/pull/112)
|
||||||
|
- 2.6.0: Windows support!
|
||||||
1.0: initial version supporting Android and iOS
|
- 2.5.2: Multi-line wrapping Toasts are now center aligned.
|
||||||
|
- 2.5.1: You can now specify the `textSize` used in the font for iOS and Android.
|
||||||
## 7. License
|
- 2.5.0: By popular demand: Specify the duration of the Toast on iOS and Android. Pass in `short` (2000ms), `long` (4000ms), or any nr of milliseconds: `900`.
|
||||||
|
- 2.4.2: You can now also set the Toast `opacity` for iOS.
|
||||||
[The MIT License (MIT)](http://www.opensource.org/licenses/mit-license.html)
|
- 2.4.1: As an addition to 2.4.0, [Sino](https://github.com/SinoBoeckmann) added the option to change the text color!
|
||||||
|
- 2.4.0: You can now style the Toast with a number of properties. See
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
- 2.3.2: The click event introduced with 2.3.0 did not work with Android 5+.
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
- 2.3.0: The plugin will now report back to JS if Toasts were tapped by the user.
|
||||||
in the Software without restriction, including without limitation the rights
|
- 2.0.1: iOS messages are hidden when another one is shown. [Thanks Richie Min!](https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin/pull/13)
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
- 2.0: WP8 support
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
- 1.0: initial version supporting Android and iOS
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in
|
|
||||||
all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
THE SOFTWARE.
|
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
### Expected behaviour
|
||||||
|
Tell us what should happen.
|
||||||
|
|
||||||
|
### Actual behaviour
|
||||||
|
Tell us what happens instead. Provide a log message if relevant.
|
||||||
|
|
||||||
|
### I'm seeing this behaviour on
|
||||||
|
_Remove this hint: these checkboxes can be checked like this: [x]_
|
||||||
|
|
||||||
|
- [ ] iOS device
|
||||||
|
- [ ] iOS sim
|
||||||
|
- [ ] Android device
|
||||||
|
- [ ] Android sim
|
||||||
|
|
||||||
|
#### Hardware models
|
||||||
|
Example: Samsung Galaxy S6, iPhone 6s
|
||||||
|
|
||||||
|
#### OS versions
|
||||||
|
Example: Android 4.4.2, iOS 9.2
|
||||||
|
|
||||||
|
### I'm not a dummy, so I've checked these
|
||||||
|
- [ ] It happens on a fresh Cordova CLI project as well.
|
||||||
|
- [ ] I'm waiting for `deviceready` to fire.
|
||||||
|
- [ ] My JavaScript has no errors (`window.onerror` catches nothing).
|
||||||
|
|
||||||
|
### So how can we reproduce this?
|
||||||
|
_Remove this hint: Pick one of these - use the Preview feature of this editor to get a sense which option we like best_
|
||||||
|
|
||||||
|
#### Awesome :star::star::star::star::star:
|
||||||
|
Provide a (link to a) minimal demo app showing the faulty behaviour.
|
||||||
|
|
||||||
|
#### Sweet :star::star::star::star:
|
||||||
|
Provide a concise code sample which can be dropped into a freshly created Cordova CLI app.
|
||||||
|
|
||||||
|
#### Good :star::star::star:
|
||||||
|
Provide your own app and instructions how to reproduce the issue.
|
||||||
|
|
||||||
|
#### Meh :star::star:
|
||||||
|
Provide a code sample with a bunch of magic parameters which I need to interpolate by guessing to reconstruct the actual runtime code.
|
||||||
|
|
||||||
|
#### Worst :poop:
|
||||||
|
Say the source code can't be disclosed and refuse to provide any of the above. Expect this issue to be closed by a bunch of angry aliens :alien::alien::alien::alien::alien: that will hunt you down and :fire: your :computer:. You've been warned. :fire_engine:
|
||||||
+3
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "cordova-plugin-x-toast",
|
"name": "cordova-plugin-x-toast",
|
||||||
"version": "2.2.0",
|
"version": "2.7.3",
|
||||||
"description": "This plugin allows you to show a Toast. A Toast is a little non intrusive buttonless popup which automatically disappears.",
|
"description": "This plugin allows you to show a Toast. A Toast is a little non intrusive buttonless popup which automatically disappears.",
|
||||||
"cordova": {
|
"cordova": {
|
||||||
"id": "cordova-plugin-x-toast",
|
"id": "cordova-plugin-x-toast",
|
||||||
@@ -8,6 +8,7 @@
|
|||||||
"ios",
|
"ios",
|
||||||
"android",
|
"android",
|
||||||
"wp8",
|
"wp8",
|
||||||
|
"windows",
|
||||||
"blackberry10"
|
"blackberry10"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -24,6 +25,7 @@
|
|||||||
"cordova-ios",
|
"cordova-ios",
|
||||||
"cordova-android",
|
"cordova-android",
|
||||||
"cordova-wp8",
|
"cordova-wp8",
|
||||||
|
"cordova-windows",
|
||||||
"cordova-blackberry10"
|
"cordova-blackberry10"
|
||||||
],
|
],
|
||||||
"engines": [
|
"engines": [
|
||||||
|
|||||||
+20
-14
@@ -1,8 +1,7 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
|
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
id="cordova-plugin-x-toast"
|
id="cordova-plugin-x-toast"
|
||||||
version="2.2.0">
|
version="2.7.3">
|
||||||
|
|
||||||
<name>Toast</name>
|
<name>Toast</name>
|
||||||
|
|
||||||
@@ -29,9 +28,6 @@
|
|||||||
<clobbers target="window.plugins.toast" />
|
<clobbers target="window.plugins.toast" />
|
||||||
</js-module>
|
</js-module>
|
||||||
|
|
||||||
<js-module src="test/tests.js" name="tests">
|
|
||||||
</js-module>
|
|
||||||
|
|
||||||
<!-- ios -->
|
<!-- ios -->
|
||||||
<platform name="ios">
|
<platform name="ios">
|
||||||
|
|
||||||
@@ -72,13 +68,23 @@
|
|||||||
<source-file src="src/wp8/Toast.cs" />
|
<source-file src="src/wp8/Toast.cs" />
|
||||||
</platform>
|
</platform>
|
||||||
|
|
||||||
<platform name="blackberry10">
|
<platform name="blackberry10">
|
||||||
<source-file src="src/blackberry10/index.js" target-dir="Toast"/>
|
<source-file src="src/blackberry10/index.js" target-dir="Toast"/>
|
||||||
<lib-file src="src/blackberry10/native/device/libToast.so" arch="device"/>
|
<lib-file src="src/blackberry10/native/device/libToast.so" arch="device"/>
|
||||||
<lib-file src="src/blackberry10/native/simulator/libToast.so" arch="simulator"/>
|
<lib-file src="src/blackberry10/native/simulator/libToast.so" arch="simulator"/>
|
||||||
<config-file target="www/config.xml" parent="/widget">
|
<config-file target="www/config.xml" parent="/widget">
|
||||||
<feature name="Toast" value="Toast" />
|
<feature name="Toast" value="Toast" />
|
||||||
</config-file>
|
</config-file>
|
||||||
</platform>
|
</platform>
|
||||||
|
|
||||||
|
<!-- windows -->
|
||||||
|
<platform name="windows">
|
||||||
|
<js-module src="src/windows/toastProxy.js" name="ToastProxy">
|
||||||
|
<merges target="" />
|
||||||
|
</js-module>
|
||||||
|
<config-file target="config.xml" parent="/*">
|
||||||
|
<feature name="Toast" value="Toast" />
|
||||||
|
</config-file>
|
||||||
|
</platform>
|
||||||
|
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 16 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 22 KiB |
@@ -1,75 +1,224 @@
|
|||||||
package nl.xservices.plugins;
|
package nl.xservices.plugins;
|
||||||
|
|
||||||
|
import android.graphics.Color;
|
||||||
|
import android.graphics.drawable.GradientDrawable;
|
||||||
|
import android.os.Build;
|
||||||
|
import android.os.CountDownTimer;
|
||||||
|
import android.text.Layout;
|
||||||
|
import android.text.Spannable;
|
||||||
|
import android.text.SpannableString;
|
||||||
|
import android.text.style.AlignmentSpan;
|
||||||
import android.view.Gravity;
|
import android.view.Gravity;
|
||||||
|
import android.view.MotionEvent;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
import android.widget.TextView;
|
||||||
|
|
||||||
import org.apache.cordova.CallbackContext;
|
import org.apache.cordova.CallbackContext;
|
||||||
import org.apache.cordova.CordovaPlugin;
|
import org.apache.cordova.CordovaPlugin;
|
||||||
|
import org.apache.cordova.PluginResult;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
/*
|
|
||||||
// TODO nice way for the Toast plugin to offer a longer delay than the default short and long options
|
|
||||||
// TODO also look at https://github.com/JohnPersano/Supertoasts
|
|
||||||
new CountDownTimer(6000, 1000) {
|
|
||||||
public void onTick(long millisUntilFinished) {toast.show();}
|
|
||||||
public void onFinish() {toast.show();}
|
|
||||||
}.start();
|
|
||||||
|
|
||||||
Also, check https://github.com/JohnPersano/SuperToasts
|
|
||||||
*/
|
|
||||||
public class Toast extends CordovaPlugin {
|
public class Toast extends CordovaPlugin {
|
||||||
|
|
||||||
private static final String ACTION_SHOW_EVENT = "show";
|
private static final String ACTION_SHOW_EVENT = "show";
|
||||||
private static final String ACTION_HIDE_EVENT = "hide";
|
private static final String ACTION_HIDE_EVENT = "hide";
|
||||||
|
|
||||||
|
private static final int GRAVITY_TOP = Gravity.TOP|Gravity.CENTER_HORIZONTAL;
|
||||||
|
private static final int GRAVITY_CENTER = Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL;
|
||||||
|
private static final int GRAVITY_BOTTOM = Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL;
|
||||||
|
|
||||||
|
private static final int BASE_TOP_BOTTOM_OFFSET = 20;
|
||||||
|
|
||||||
private android.widget.Toast mostRecentToast;
|
private android.widget.Toast mostRecentToast;
|
||||||
|
private ViewGroup viewGroup;
|
||||||
|
|
||||||
|
private static final boolean IS_AT_LEAST_JELLY_BEAN = Build.VERSION.SDK_INT >= 16;
|
||||||
|
private static final boolean IS_AT_LEAST_LOLLIPOP = Build.VERSION.SDK_INT >= 21;
|
||||||
|
private static final boolean IS_AT_LEAST_PIE = Build.VERSION.SDK_INT >= 28;
|
||||||
|
private static final boolean IS_AT_LEAST_R = Build.VERSION.SDK_INT >= 30;
|
||||||
|
|
||||||
// note that webView.isPaused() is not Xwalk compatible, so tracking it poor-man style
|
// note that webView.isPaused() is not Xwalk compatible, so tracking it poor-man style
|
||||||
private boolean isPaused;
|
private boolean isPaused;
|
||||||
|
|
||||||
|
private String currentMessage;
|
||||||
|
private JSONObject currentData;
|
||||||
|
private static CountDownTimer _timer;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
|
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
|
||||||
if (ACTION_HIDE_EVENT.equals(action)) {
|
if (ACTION_HIDE_EVENT.equals(action)) {
|
||||||
if (mostRecentToast != null) {
|
returnTapEvent("hide", currentMessage, currentData, callbackContext);
|
||||||
mostRecentToast.cancel();
|
hide();
|
||||||
}
|
|
||||||
callbackContext.success();
|
callbackContext.success();
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
} else if (ACTION_SHOW_EVENT.equals(action)) {
|
} else if (ACTION_SHOW_EVENT.equals(action)) {
|
||||||
|
|
||||||
if (this.isPaused) {
|
if (this.isPaused) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
final JSONObject options = args.getJSONObject(0);
|
final JSONObject options = args.getJSONObject(0);
|
||||||
|
final String msg = options.getString("message");
|
||||||
|
final Spannable message = new SpannableString(msg);
|
||||||
|
message.setSpan(
|
||||||
|
new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER),
|
||||||
|
0,
|
||||||
|
msg.length() - 1,
|
||||||
|
Spannable.SPAN_INCLUSIVE_INCLUSIVE);
|
||||||
|
|
||||||
final String message = options.getString("message");
|
|
||||||
final String duration = options.getString("duration");
|
final String duration = options.getString("duration");
|
||||||
final String position = options.getString("position");
|
final String position = options.getString("position");
|
||||||
final int addPixelsY = options.has("addPixelsY") ? options.getInt("addPixelsY") : 0;
|
final int addPixelsY = options.has("addPixelsY") ? options.getInt("addPixelsY") : 0;
|
||||||
|
final JSONObject data = options.has("data") ? options.getJSONObject("data") : null;
|
||||||
|
final JSONObject styling = options.optJSONObject("styling");
|
||||||
|
|
||||||
|
currentMessage = msg;
|
||||||
|
currentData = data;
|
||||||
|
|
||||||
cordova.getActivity().runOnUiThread(new Runnable() {
|
cordova.getActivity().runOnUiThread(new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
android.widget.Toast toast = android.widget.Toast.makeText(
|
int hideAfterMs;
|
||||||
cordova.getActivity().getApplicationContext(),
|
if ("short".equalsIgnoreCase(duration)) {
|
||||||
|
hideAfterMs = 2000;
|
||||||
|
} else if ("long".equalsIgnoreCase(duration)) {
|
||||||
|
hideAfterMs = 4000;
|
||||||
|
} else {
|
||||||
|
// assuming a number of ms
|
||||||
|
hideAfterMs = Integer.parseInt(duration);
|
||||||
|
}
|
||||||
|
final android.widget.Toast toast = android.widget.Toast.makeText(
|
||||||
|
IS_AT_LEAST_LOLLIPOP ? cordova.getActivity().getWindow().getContext() : cordova.getActivity().getApplicationContext(),
|
||||||
message,
|
message,
|
||||||
"short".equals(duration) ? android.widget.Toast.LENGTH_SHORT : android.widget.Toast.LENGTH_LONG);
|
"short".equalsIgnoreCase(duration) ? android.widget.Toast.LENGTH_SHORT : android.widget.Toast.LENGTH_LONG
|
||||||
|
);
|
||||||
|
|
||||||
if ("top".equals(position)) {
|
if ("top".equals(position)) {
|
||||||
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 20 + addPixelsY);
|
toast.setGravity(GRAVITY_TOP, 0, BASE_TOP_BOTTOM_OFFSET + addPixelsY);
|
||||||
} else if ("bottom".equals(position)) {
|
} else if ("bottom".equals(position)) {
|
||||||
toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 20 - addPixelsY);
|
toast.setGravity(GRAVITY_BOTTOM, 0, BASE_TOP_BOTTOM_OFFSET - addPixelsY);
|
||||||
} else if ("center".equals(position)) {
|
} else if ("center".equals(position)) {
|
||||||
toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL, 0, addPixelsY);
|
toast.setGravity(GRAVITY_CENTER, 0, addPixelsY);
|
||||||
} else {
|
} else {
|
||||||
callbackContext.error("invalid position. valid options are 'top', 'center' and 'bottom'");
|
callbackContext.error("invalid position. valid options are 'top', 'center' and 'bottom'");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
toast.show();
|
// if one of the custom layout options have been passed in, draw our own shape
|
||||||
|
// (but disabled on Android >= 11 since custom toast views are deprecated)
|
||||||
|
if (styling != null && IS_AT_LEAST_JELLY_BEAN && !IS_AT_LEAST_R) {
|
||||||
|
|
||||||
|
// the defaults mimic the default toast as close as possible
|
||||||
|
final String backgroundColor = styling.optString("backgroundColor", "#333333");
|
||||||
|
final String textColor = styling.optString("textColor", "#ffffff");
|
||||||
|
final Double textSize = styling.optDouble("textSize", -1);
|
||||||
|
final double opacity = styling.optDouble("opacity", 0.8);
|
||||||
|
final int cornerRadius = styling.optInt("cornerRadius", 100);
|
||||||
|
final int horizontalPadding = styling.optInt("horizontalPadding", 50);
|
||||||
|
final int verticalPadding = styling.optInt("verticalPadding", 30);
|
||||||
|
|
||||||
|
GradientDrawable shape = new GradientDrawable();
|
||||||
|
shape.setCornerRadius(cornerRadius);
|
||||||
|
shape.setAlpha((int)(opacity * 255)); // 0-255, where 0 is an invisible background
|
||||||
|
shape.setColor(Color.parseColor(backgroundColor));
|
||||||
|
toast.getView().setBackground(shape);
|
||||||
|
|
||||||
|
final TextView toastTextView;
|
||||||
|
toastTextView = (TextView) toast.getView().findViewById(android.R.id.message);
|
||||||
|
toastTextView.setTextColor(Color.parseColor(textColor));
|
||||||
|
if (textSize > -1) {
|
||||||
|
toastTextView.setTextSize(textSize.floatValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
toast.getView().setPadding(horizontalPadding, verticalPadding, horizontalPadding, verticalPadding);
|
||||||
|
|
||||||
|
// this gives the toast a very subtle shadow on newer devices
|
||||||
|
if (IS_AT_LEAST_LOLLIPOP) {
|
||||||
|
toast.getView().setElevation(6);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IS_AT_LEAST_R) {
|
||||||
|
// On Android >= 11 the 'toast.getView()' will always return null
|
||||||
|
// so no touchListener can be used or mocked
|
||||||
|
// DO NOTHING
|
||||||
|
} else if (IS_AT_LEAST_LOLLIPOP) {
|
||||||
|
// On Android >= 5 you can no longer rely on the 'toast.getView().setOnTouchListener',
|
||||||
|
// so created something funky that compares the Toast position to the tap coordinates.
|
||||||
|
getViewGroup().setOnTouchListener(new View.OnTouchListener() {
|
||||||
|
@Override
|
||||||
|
public boolean onTouch(View view, MotionEvent motionEvent) {
|
||||||
|
if (motionEvent.getAction() != MotionEvent.ACTION_DOWN) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (mostRecentToast == null || !mostRecentToast.getView().isShown()) {
|
||||||
|
getViewGroup().setOnTouchListener(null);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
float w = mostRecentToast.getView().getWidth();
|
||||||
|
float startX = (view.getWidth() / 2) - (w / 2);
|
||||||
|
float endX = (view.getWidth() / 2) + (w / 2);
|
||||||
|
|
||||||
|
float startY;
|
||||||
|
float endY;
|
||||||
|
|
||||||
|
float g = mostRecentToast.getGravity();
|
||||||
|
float y = mostRecentToast.getYOffset();
|
||||||
|
float h = mostRecentToast.getView().getHeight();
|
||||||
|
|
||||||
|
if (g == GRAVITY_BOTTOM) {
|
||||||
|
startY = view.getHeight() - y - h;
|
||||||
|
endY = view.getHeight() - y;
|
||||||
|
} else if (g == GRAVITY_CENTER) {
|
||||||
|
startY = (view.getHeight() / 2) + y - (h / 2);
|
||||||
|
endY = (view.getHeight() / 2) + y + (h / 2);
|
||||||
|
} else {
|
||||||
|
// top
|
||||||
|
startY = y;
|
||||||
|
endY = y + h;
|
||||||
|
}
|
||||||
|
|
||||||
|
float tapX = motionEvent.getX();
|
||||||
|
float tapY = motionEvent.getY();
|
||||||
|
|
||||||
|
final boolean tapped = tapX >= startX && tapX <= endX &&
|
||||||
|
tapY >= startY && tapY <= endY;
|
||||||
|
|
||||||
|
return tapped && returnTapEvent("touch", msg, data, callbackContext);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
toast.getView().setOnTouchListener(new View.OnTouchListener() {
|
||||||
|
@Override
|
||||||
|
public boolean onTouch(View view, MotionEvent motionEvent) {
|
||||||
|
return motionEvent.getAction() == MotionEvent.ACTION_DOWN && returnTapEvent("touch", msg, data, callbackContext);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// trigger show every 2500 ms for as long as the requested duration
|
||||||
|
_timer = new CountDownTimer(hideAfterMs, 2500) {
|
||||||
|
public void onTick(long millisUntilFinished) {
|
||||||
|
// see https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin/issues/116
|
||||||
|
// and https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin/issues/120
|
||||||
|
// if (!IS_AT_LEAST_PIE) {
|
||||||
|
// toast.show();
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
public void onFinish() {
|
||||||
|
returnTapEvent("hide", msg, data, callbackContext);
|
||||||
|
toast.cancel();
|
||||||
|
}
|
||||||
|
}.start();
|
||||||
|
|
||||||
mostRecentToast = toast;
|
mostRecentToast = toast;
|
||||||
callbackContext.success();
|
toast.show();
|
||||||
|
|
||||||
|
PluginResult pr = new PluginResult(PluginResult.Status.OK);
|
||||||
|
pr.setKeepCallback(true);
|
||||||
|
callbackContext.sendPluginResult(pr);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -80,11 +229,41 @@ public class Toast extends CordovaPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPause(boolean multitasking) {
|
private void hide() {
|
||||||
if (mostRecentToast != null) {
|
if (mostRecentToast != null) {
|
||||||
mostRecentToast.cancel();
|
mostRecentToast.cancel();
|
||||||
|
getViewGroup().setOnTouchListener(null);
|
||||||
}
|
}
|
||||||
|
if (_timer != null) {
|
||||||
|
_timer.cancel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean returnTapEvent(String eventName, String message, JSONObject data, CallbackContext callbackContext) {
|
||||||
|
final JSONObject json = new JSONObject();
|
||||||
|
try {
|
||||||
|
json.put("event", eventName);
|
||||||
|
json.put("message", message);
|
||||||
|
json.put("data", data);
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
callbackContext.success(json);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// lazy init and caching
|
||||||
|
private ViewGroup getViewGroup() {
|
||||||
|
if (viewGroup == null) {
|
||||||
|
viewGroup = (ViewGroup) ((ViewGroup) cordova.getActivity().findViewById(android.R.id.content)).getChildAt(0);
|
||||||
|
}
|
||||||
|
return viewGroup;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPause(boolean multitasking) {
|
||||||
|
hide();
|
||||||
this.isPaused = true;
|
this.isPaused = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
#import <Foundation/Foundation.h>
|
#import <Foundation/Foundation.h>
|
||||||
|
#import <Cordova/CDV.h>
|
||||||
|
|
||||||
@interface UIView (Toast)
|
@interface UIView (Toast)
|
||||||
|
|
||||||
// each makeToast method creates a view and displays it as toast
|
// each makeToast method creates a view and displays it as toast
|
||||||
- (void)makeToast:(NSString *)message;
|
- (void)makeToast:(NSString *)message;
|
||||||
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)interval position:(id)position;
|
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)interval position:(id)position;
|
||||||
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)duration position:(id)position addPixelsY:(int)addPixelsY;
|
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)duration position:(id)position addPixelsY:(int)addPixelsY data:(NSDictionary*)data styling:(NSDictionary*)styling commandDelegate:(id <CDVCommandDelegate>)commandDelegate callbackId:(NSString *)callbackId;
|
||||||
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)interval position:(id)position image:(UIImage *)image;
|
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)interval position:(id)position image:(UIImage *)image;
|
||||||
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)interval position:(id)position title:(NSString *)title;
|
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)interval position:(id)position title:(NSString *)title;
|
||||||
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)interval position:(id)position title:(NSString *)title image:(UIImage *)image;
|
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)interval position:(id)position title:(NSString *)title image:(UIImage *)image;
|
||||||
|
|||||||
+118
-41
@@ -48,6 +48,13 @@ static const NSString * CSToastActivityViewKey = @"CSToastActivityViewKey";
|
|||||||
|
|
||||||
static UIView *prevToast = NULL;
|
static UIView *prevToast = NULL;
|
||||||
|
|
||||||
|
// doesn't matter these are static
|
||||||
|
static id commandDelegate;
|
||||||
|
static id callbackId;
|
||||||
|
static id msg;
|
||||||
|
static id data;
|
||||||
|
static id styling;
|
||||||
|
|
||||||
@interface UIView (ToastPrivate)
|
@interface UIView (ToastPrivate)
|
||||||
|
|
||||||
- (void)hideToast:(UIView *)toast;
|
- (void)hideToast:(UIView *)toast;
|
||||||
@@ -56,12 +63,12 @@ static UIView *prevToast = NULL;
|
|||||||
- (CGPoint)centerPointForPosition:(id)position withToast:(UIView *)toast withAddedPixelsY:(int) addPixelsY;
|
- (CGPoint)centerPointForPosition:(id)position withToast:(UIView *)toast withAddedPixelsY:(int) addPixelsY;
|
||||||
- (UIView *)viewForMessage:(NSString *)message title:(NSString *)title image:(UIImage *)image;
|
- (UIView *)viewForMessage:(NSString *)message title:(NSString *)title image:(UIImage *)image;
|
||||||
- (CGSize)sizeForString:(NSString *)string font:(UIFont *)font constrainedToSize:(CGSize)constrainedSize lineBreakMode:(NSLineBreakMode)lineBreakMode;
|
- (CGSize)sizeForString:(NSString *)string font:(UIFont *)font constrainedToSize:(CGSize)constrainedSize lineBreakMode:(NSLineBreakMode)lineBreakMode;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
|
||||||
@implementation UIView (Toast)
|
@implementation UIView (Toast)
|
||||||
|
|
||||||
|
|
||||||
#pragma mark - Toast Methods
|
#pragma mark - Toast Methods
|
||||||
|
|
||||||
- (void)makeToast:(NSString *)message {
|
- (void)makeToast:(NSString *)message {
|
||||||
@@ -73,24 +80,37 @@ static UIView *prevToast = NULL;
|
|||||||
[self showToast:toast duration:duration position:position];
|
[self showToast:toast duration:duration position:position];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)duration position:(id)position addPixelsY:(int)addPixelsY {
|
- (void)makeToast:(NSString *)message
|
||||||
UIView *toast = [self viewForMessage:message title:nil image:nil];
|
duration:(NSTimeInterval)duration
|
||||||
[self showToast:toast duration:duration position:position addedPixelsY:addPixelsY];
|
position:(id)position addPixelsY:(int)addPixelsY
|
||||||
|
data:(NSDictionary*)_data
|
||||||
|
styling:(NSDictionary*)_styling
|
||||||
|
commandDelegate:(id <CDVCommandDelegate>)_commandDelegate
|
||||||
|
callbackId:(NSString *)_callbackId {
|
||||||
|
|
||||||
|
commandDelegate = _commandDelegate;
|
||||||
|
callbackId = _callbackId;
|
||||||
|
msg = message;
|
||||||
|
data = _data;
|
||||||
|
styling = _styling;
|
||||||
|
|
||||||
|
UIView *toast = [self viewForMessage:message title:nil image:nil];
|
||||||
|
[self showToast:toast duration:duration position:position addedPixelsY:addPixelsY];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)duration position:(id)position title:(NSString *)title {
|
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)duration position:(id)position title:(NSString *)title {
|
||||||
UIView *toast = [self viewForMessage:message title:title image:nil];
|
UIView *toast = [self viewForMessage:message title:title image:nil];
|
||||||
[self showToast:toast duration:duration position:position];
|
[self showToast:toast duration:duration position:position];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)duration position:(id)position image:(UIImage *)image {
|
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)duration position:(id)position image:(UIImage *)image {
|
||||||
UIView *toast = [self viewForMessage:message title:nil image:image];
|
UIView *toast = [self viewForMessage:message title:nil image:image];
|
||||||
[self showToast:toast duration:duration position:position];
|
[self showToast:toast duration:duration position:position];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)duration position:(id)position title:(NSString *)title image:(UIImage *)image {
|
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)duration position:(id)position title:(NSString *)title image:(UIImage *)image {
|
||||||
UIView *toast = [self viewForMessage:message title:title image:image];
|
UIView *toast = [self viewForMessage:message title:title image:image];
|
||||||
[self showToast:toast duration:duration position:position];
|
[self showToast:toast duration:duration position:position];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)showToast:(UIView *)toast {
|
- (void)showToast:(UIView *)toast {
|
||||||
@@ -101,38 +121,47 @@ static UIView *prevToast = NULL;
|
|||||||
[self showToast:toast duration:CSToastDefaultDuration position:CSToastDefaultPosition addedPixelsY:0];
|
[self showToast:toast duration:CSToastDefaultDuration position:CSToastDefaultPosition addedPixelsY:0];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)showToast:(UIView *)toast duration:(NSTimeInterval)duration position:(id)point addedPixelsY:(int) addPixelsY {
|
- (void)showToast:(UIView *)toast duration:(NSTimeInterval)duration position:(id)point addedPixelsY:(int) addPixelsY {
|
||||||
[self hideToast];
|
[self hideToast];
|
||||||
prevToast = toast;
|
prevToast = toast;
|
||||||
toast.center = [self centerPointForPosition:point withToast:toast withAddedPixelsY:addPixelsY];
|
toast.center = [self centerPointForPosition:point withToast:toast withAddedPixelsY:addPixelsY];
|
||||||
toast.alpha = 0.0;
|
toast.alpha = 0.0;
|
||||||
|
|
||||||
|
// note that we changed this to be always true
|
||||||
if (CSToastHidesOnTap) {
|
if (CSToastHidesOnTap) {
|
||||||
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:toast action:@selector(handleToastTapped:)];
|
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:toast action:@selector(handleToastTapped:)];
|
||||||
[toast addGestureRecognizer:recognizer];
|
[toast addGestureRecognizer:recognizer];
|
||||||
toast.userInteractionEnabled = YES;
|
toast.userInteractionEnabled = YES;
|
||||||
toast.exclusiveTouch = YES;
|
toast.exclusiveTouch = YES;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ([[[UIDevice currentDevice] systemVersion] compare:@"8.0" options:NSNumericSearch] != NSOrderedAscending) {
|
// make sure that if InAppBrowser is active, we're still showing Toasts on top of it
|
||||||
// on iOS8 when InAppBrowser is active, the Toast is below it,
|
UIViewController *vc = [self getTopMostViewController];
|
||||||
[self.superview.superview addSubview:toast];
|
UIView *v = [vc view];
|
||||||
} else {
|
[v addSubview:toast];
|
||||||
// ..on iOS7 however with this fix on landscape the Toast isn't rotated automatically
|
|
||||||
[self.superview addSubview:toast];
|
NSNumber * opacity = styling[@"opacity"];
|
||||||
}
|
CGFloat theOpacity = opacity == nil ? CSToastOpacity : [opacity floatValue];
|
||||||
|
|
||||||
[UIView animateWithDuration:CSToastFadeDuration
|
[UIView animateWithDuration:CSToastFadeDuration
|
||||||
delay:0.0
|
delay:0.0
|
||||||
options:(UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAllowUserInteraction)
|
options:(UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAllowUserInteraction)
|
||||||
animations:^{
|
animations:^{
|
||||||
toast.alpha = CSToastOpacity;
|
toast.alpha = theOpacity;
|
||||||
} completion:^(BOOL finished) {
|
} completion:^(BOOL finished) {
|
||||||
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:duration target:self selector:@selector(toastTimerDidFinish:) userInfo:toast repeats:NO];
|
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:duration target:self selector:@selector(toastTimerDidFinish:) userInfo:toast repeats:NO];
|
||||||
// associate the timer with the toast view
|
// associate the timer with the toast view
|
||||||
objc_setAssociatedObject (toast, &CSToastTimerKey, timer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
objc_setAssociatedObject (toast, &CSToastTimerKey, timer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
||||||
}];
|
}];
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
- (UIViewController*) getTopMostViewController {
|
||||||
|
UIViewController *presentingViewController = [[UIApplication sharedApplication] keyWindow].rootViewController;
|
||||||
|
while (presentingViewController.presentedViewController != nil) {
|
||||||
|
presentingViewController = presentingViewController.presentedViewController;
|
||||||
|
}
|
||||||
|
return presentingViewController;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)hideToast {
|
- (void)hideToast {
|
||||||
@@ -156,6 +185,15 @@ static UIView *prevToast = NULL;
|
|||||||
|
|
||||||
- (void)toastTimerDidFinish:(NSTimer *)timer {
|
- (void)toastTimerDidFinish:(NSTimer *)timer {
|
||||||
[self hideToast:(UIView *)timer.userInfo];
|
[self hideToast:(UIView *)timer.userInfo];
|
||||||
|
|
||||||
|
// also send an event back to JS
|
||||||
|
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:msg, @"message", @"hide", @"event", nil];
|
||||||
|
if (data != nil) {
|
||||||
|
[dict setObject:data forKey:@"data"];
|
||||||
|
}
|
||||||
|
|
||||||
|
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dict];
|
||||||
|
[commandDelegate sendPluginResult:pluginResult callbackId:callbackId];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)handleToastTapped:(UITapGestureRecognizer *)recognizer {
|
- (void)handleToastTapped:(UITapGestureRecognizer *)recognizer {
|
||||||
@@ -163,6 +201,15 @@ static UIView *prevToast = NULL;
|
|||||||
[timer invalidate];
|
[timer invalidate];
|
||||||
|
|
||||||
[self hideToast:recognizer.view];
|
[self hideToast:recognizer.view];
|
||||||
|
|
||||||
|
// also send an event back to JS
|
||||||
|
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:msg, @"message", @"touch", @"event", nil];
|
||||||
|
if (data != nil) {
|
||||||
|
[dict setObject:data forKey:@"data"];
|
||||||
|
}
|
||||||
|
|
||||||
|
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dict];
|
||||||
|
[commandDelegate sendPluginResult:pluginResult callbackId:callbackId];
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark - Toast Activity Methods
|
#pragma mark - Toast Activity Methods
|
||||||
@@ -175,31 +222,31 @@ static UIView *prevToast = NULL;
|
|||||||
// sanity
|
// sanity
|
||||||
UIView *existingActivityView = (UIView *)objc_getAssociatedObject(self, &CSToastActivityViewKey);
|
UIView *existingActivityView = (UIView *)objc_getAssociatedObject(self, &CSToastActivityViewKey);
|
||||||
if (existingActivityView != nil) return;
|
if (existingActivityView != nil) return;
|
||||||
|
|
||||||
UIView *activityView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CSToastActivityWidth, CSToastActivityHeight)];
|
UIView *activityView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CSToastActivityWidth, CSToastActivityHeight)];
|
||||||
activityView.center = [self centerPointForPosition:position withToast:activityView withAddedPixelsY:0];
|
activityView.center = [self centerPointForPosition:position withToast:activityView withAddedPixelsY:0];
|
||||||
activityView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:CSToastOpacity];
|
activityView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:CSToastOpacity];
|
||||||
activityView.alpha = 0.0;
|
activityView.alpha = 0.0;
|
||||||
activityView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);
|
activityView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);
|
||||||
activityView.layer.cornerRadius = CSToastCornerRadius;
|
activityView.layer.cornerRadius = CSToastCornerRadius;
|
||||||
|
|
||||||
if (CSToastDisplayShadow) {
|
if (CSToastDisplayShadow) {
|
||||||
activityView.layer.shadowColor = [UIColor blackColor].CGColor;
|
activityView.layer.shadowColor = [UIColor blackColor].CGColor;
|
||||||
activityView.layer.shadowOpacity = CSToastShadowOpacity;
|
activityView.layer.shadowOpacity = CSToastShadowOpacity;
|
||||||
activityView.layer.shadowRadius = CSToastShadowRadius;
|
activityView.layer.shadowRadius = CSToastShadowRadius;
|
||||||
activityView.layer.shadowOffset = CSToastShadowOffset;
|
activityView.layer.shadowOffset = CSToastShadowOffset;
|
||||||
}
|
}
|
||||||
|
|
||||||
UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
|
UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
|
||||||
activityIndicatorView.center = CGPointMake(activityView.bounds.size.width / 2, activityView.bounds.size.height / 2);
|
activityIndicatorView.center = CGPointMake(activityView.bounds.size.width / 2, activityView.bounds.size.height / 2);
|
||||||
[activityView addSubview:activityIndicatorView];
|
[activityView addSubview:activityIndicatorView];
|
||||||
[activityIndicatorView startAnimating];
|
[activityIndicatorView startAnimating];
|
||||||
|
|
||||||
// associate the activity view with self
|
// associate the activity view with self
|
||||||
objc_setAssociatedObject (self, &CSToastActivityViewKey, activityView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
objc_setAssociatedObject (self, &CSToastActivityViewKey, activityView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
||||||
|
|
||||||
[self addSubview:activityView];
|
[self addSubview:activityView];
|
||||||
|
|
||||||
[UIView animateWithDuration:CSToastFadeDuration
|
[UIView animateWithDuration:CSToastFadeDuration
|
||||||
delay:0.0
|
delay:0.0
|
||||||
options:UIViewAnimationOptionCurveEaseOut
|
options:UIViewAnimationOptionCurveEaseOut
|
||||||
@@ -238,7 +285,7 @@ static UIView *prevToast = NULL;
|
|||||||
} else if ([point isKindOfClass:[NSValue class]]) {
|
} else if ([point isKindOfClass:[NSValue class]]) {
|
||||||
return [point CGPointValue];
|
return [point CGPointValue];
|
||||||
}
|
}
|
||||||
|
|
||||||
NSLog(@"Warning: Invalid position for toast.");
|
NSLog(@"Warning: Invalid position for toast.");
|
||||||
return [self centerPointForPosition:CSToastDefaultPosition withToast:toast withAddedPixelsY:addPixelsY];
|
return [self centerPointForPosition:CSToastDefaultPosition withToast:toast withAddedPixelsY:addPixelsY];
|
||||||
}
|
}
|
||||||
@@ -270,7 +317,9 @@ static UIView *prevToast = NULL;
|
|||||||
// create the parent view
|
// create the parent view
|
||||||
UIView *wrapperView = [[UIView alloc] init];
|
UIView *wrapperView = [[UIView alloc] init];
|
||||||
wrapperView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);
|
wrapperView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);
|
||||||
wrapperView.layer.cornerRadius = CSToastCornerRadius;
|
|
||||||
|
NSNumber * cornerRadius = styling[@"cornerRadius"];
|
||||||
|
wrapperView.layer.cornerRadius = cornerRadius == nil ? CSToastCornerRadius : [cornerRadius floatValue];
|
||||||
|
|
||||||
if (CSToastDisplayShadow) {
|
if (CSToastDisplayShadow) {
|
||||||
wrapperView.layer.shadowColor = [UIColor blackColor].CGColor;
|
wrapperView.layer.shadowColor = [UIColor blackColor].CGColor;
|
||||||
@@ -279,12 +328,24 @@ static UIView *prevToast = NULL;
|
|||||||
wrapperView.layer.shadowOffset = CSToastShadowOffset;
|
wrapperView.layer.shadowOffset = CSToastShadowOffset;
|
||||||
}
|
}
|
||||||
|
|
||||||
wrapperView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:CSToastOpacity];
|
NSString * backgroundColor = styling[@"backgroundColor"];
|
||||||
|
UIColor *theColor = backgroundColor == nil ? [UIColor blackColor] : [self colorFromHexString:backgroundColor];
|
||||||
|
|
||||||
|
NSNumber * horizontalPadding = styling[@"horizontalPadding"];
|
||||||
|
CGFloat theHorizontalPadding = horizontalPadding == nil ? CSToastHorizontalPadding : [horizontalPadding floatValue];
|
||||||
|
|
||||||
|
NSNumber * verticalPadding = styling[@"verticalPadding"];
|
||||||
|
CGFloat theVerticalPadding = verticalPadding == nil ? CSToastVerticalPadding : [verticalPadding floatValue];
|
||||||
|
|
||||||
|
NSNumber * textSize = styling[@"textSize"];
|
||||||
|
CGFloat theTextSize = textSize == nil ? CSToastFontSize : [textSize floatValue];
|
||||||
|
|
||||||
|
wrapperView.backgroundColor = theColor;
|
||||||
|
|
||||||
if(image != nil) {
|
if(image != nil) {
|
||||||
imageView = [[UIImageView alloc] initWithImage:image];
|
imageView = [[UIImageView alloc] initWithImage:image];
|
||||||
imageView.contentMode = UIViewContentModeScaleAspectFit;
|
imageView.contentMode = UIViewContentModeScaleAspectFit;
|
||||||
imageView.frame = CGRectMake(CSToastHorizontalPadding, CSToastVerticalPadding, CSToastImageViewWidth, CSToastImageViewHeight);
|
imageView.frame = CGRectMake(theHorizontalPadding, theVerticalPadding, CSToastImageViewWidth, CSToastImageViewHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
CGFloat imageWidth, imageHeight, imageLeft;
|
CGFloat imageWidth, imageHeight, imageLeft;
|
||||||
@@ -293,18 +354,21 @@ static UIView *prevToast = NULL;
|
|||||||
if(imageView != nil) {
|
if(imageView != nil) {
|
||||||
imageWidth = imageView.bounds.size.width;
|
imageWidth = imageView.bounds.size.width;
|
||||||
imageHeight = imageView.bounds.size.height;
|
imageHeight = imageView.bounds.size.height;
|
||||||
imageLeft = CSToastHorizontalPadding;
|
imageLeft = theHorizontalPadding;
|
||||||
} else {
|
} else {
|
||||||
imageWidth = imageHeight = imageLeft = 0.0;
|
imageWidth = imageHeight = imageLeft = 0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (title != nil) {
|
if (title != nil) {
|
||||||
|
NSString * titleLabelTextColor = styling[@"textColor"];
|
||||||
|
UIColor *theTitleLabelTextColor = titleLabelTextColor == nil ? [UIColor whiteColor] : [self colorFromHexString:titleLabelTextColor];
|
||||||
|
|
||||||
titleLabel = [[UILabel alloc] init];
|
titleLabel = [[UILabel alloc] init];
|
||||||
titleLabel.numberOfLines = CSToastMaxTitleLines;
|
titleLabel.numberOfLines = CSToastMaxTitleLines;
|
||||||
titleLabel.font = [UIFont boldSystemFontOfSize:CSToastFontSize];
|
titleLabel.font = [UIFont boldSystemFontOfSize:theTextSize];
|
||||||
titleLabel.textAlignment = NSTextAlignmentLeft;
|
titleLabel.textAlignment = NSTextAlignmentCenter;
|
||||||
titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
|
titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
|
||||||
titleLabel.textColor = [UIColor whiteColor];
|
titleLabel.textColor = theTitleLabelTextColor;
|
||||||
titleLabel.backgroundColor = [UIColor clearColor];
|
titleLabel.backgroundColor = [UIColor clearColor];
|
||||||
titleLabel.alpha = 1.0;
|
titleLabel.alpha = 1.0;
|
||||||
titleLabel.text = title;
|
titleLabel.text = title;
|
||||||
@@ -316,11 +380,15 @@ static UIView *prevToast = NULL;
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (message != nil) {
|
if (message != nil) {
|
||||||
|
NSString * messageLabelTextColor = styling[@"textColor"];
|
||||||
|
UIColor *theMessageLabelTextColor = messageLabelTextColor == nil ? [UIColor whiteColor] : [self colorFromHexString:messageLabelTextColor];
|
||||||
|
|
||||||
messageLabel = [[UILabel alloc] init];
|
messageLabel = [[UILabel alloc] init];
|
||||||
messageLabel.numberOfLines = CSToastMaxMessageLines;
|
messageLabel.numberOfLines = CSToastMaxMessageLines;
|
||||||
messageLabel.font = [UIFont systemFontOfSize:CSToastFontSize];
|
messageLabel.font = [UIFont systemFontOfSize:theTextSize];
|
||||||
messageLabel.lineBreakMode = NSLineBreakByWordWrapping;
|
messageLabel.lineBreakMode = NSLineBreakByWordWrapping;
|
||||||
messageLabel.textColor = [UIColor whiteColor];
|
messageLabel.textAlignment = NSTextAlignmentCenter;
|
||||||
|
messageLabel.textColor = theMessageLabelTextColor;
|
||||||
messageLabel.backgroundColor = [UIColor clearColor];
|
messageLabel.backgroundColor = [UIColor clearColor];
|
||||||
messageLabel.alpha = 1.0;
|
messageLabel.alpha = 1.0;
|
||||||
messageLabel.text = message;
|
messageLabel.text = message;
|
||||||
@@ -337,8 +405,8 @@ static UIView *prevToast = NULL;
|
|||||||
if(titleLabel != nil) {
|
if(titleLabel != nil) {
|
||||||
titleWidth = titleLabel.bounds.size.width;
|
titleWidth = titleLabel.bounds.size.width;
|
||||||
titleHeight = titleLabel.bounds.size.height;
|
titleHeight = titleLabel.bounds.size.height;
|
||||||
titleTop = CSToastVerticalPadding;
|
titleTop = theVerticalPadding;
|
||||||
titleLeft = imageLeft + imageWidth + CSToastHorizontalPadding;
|
titleLeft = imageLeft + imageWidth + theHorizontalPadding;
|
||||||
} else {
|
} else {
|
||||||
titleWidth = titleHeight = titleTop = titleLeft = 0.0;
|
titleWidth = titleHeight = titleTop = titleLeft = 0.0;
|
||||||
}
|
}
|
||||||
@@ -349,8 +417,8 @@ static UIView *prevToast = NULL;
|
|||||||
if(messageLabel != nil) {
|
if(messageLabel != nil) {
|
||||||
messageWidth = messageLabel.bounds.size.width;
|
messageWidth = messageLabel.bounds.size.width;
|
||||||
messageHeight = messageLabel.bounds.size.height;
|
messageHeight = messageLabel.bounds.size.height;
|
||||||
messageLeft = imageLeft + imageWidth + CSToastHorizontalPadding;
|
messageLeft = imageLeft + imageWidth + theHorizontalPadding;
|
||||||
messageTop = titleTop + titleHeight + CSToastVerticalPadding;
|
messageTop = titleTop + titleHeight + theVerticalPadding;
|
||||||
} else {
|
} else {
|
||||||
messageWidth = messageHeight = messageLeft = messageTop = 0.0;
|
messageWidth = messageHeight = messageLeft = messageTop = 0.0;
|
||||||
}
|
}
|
||||||
@@ -359,8 +427,8 @@ static UIView *prevToast = NULL;
|
|||||||
CGFloat longerLeft = MAX(titleLeft, messageLeft);
|
CGFloat longerLeft = MAX(titleLeft, messageLeft);
|
||||||
|
|
||||||
// wrapper width uses the longerWidth or the image width, whatever is larger. same logic applies to the wrapper height
|
// wrapper width uses the longerWidth or the image width, whatever is larger. same logic applies to the wrapper height
|
||||||
CGFloat wrapperWidth = MAX((imageWidth + (CSToastHorizontalPadding * 2)), (longerLeft + longerWidth + CSToastHorizontalPadding));
|
CGFloat wrapperWidth = MAX((imageWidth + (theHorizontalPadding * 2)), (longerLeft + longerWidth + theHorizontalPadding));
|
||||||
CGFloat wrapperHeight = MAX((messageTop + messageHeight + CSToastVerticalPadding), (imageHeight + (CSToastVerticalPadding * 2)));
|
CGFloat wrapperHeight = MAX((messageTop + messageHeight + theVerticalPadding), (imageHeight + (theVerticalPadding * 2)));
|
||||||
|
|
||||||
wrapperView.frame = CGRectMake(0.0, 0.0, wrapperWidth, wrapperHeight);
|
wrapperView.frame = CGRectMake(0.0, 0.0, wrapperWidth, wrapperHeight);
|
||||||
|
|
||||||
@@ -381,4 +449,13 @@ static UIView *prevToast = NULL;
|
|||||||
return wrapperView;
|
return wrapperView;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Assumes input like "#00FF00" (#RRGGBB)
|
||||||
|
- (UIColor*) colorFromHexString:(NSString*) hexString {
|
||||||
|
unsigned rgbValue = 0;
|
||||||
|
NSScanner *scanner = [NSScanner scannerWithString:hexString];
|
||||||
|
[scanner setScanLocation:1]; // bypass '#' character
|
||||||
|
[scanner scanHexInt:&rgbValue];
|
||||||
|
return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16) / 255.0 green:((rgbValue & 0xFF00) >> 8) / 255.0 blue:(rgbValue & 0xFF) / 255.0 alpha:1.0];
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
+27
-21
@@ -5,40 +5,46 @@
|
|||||||
@implementation Toast
|
@implementation Toast
|
||||||
|
|
||||||
- (void)show:(CDVInvokedUrlCommand*)command {
|
- (void)show:(CDVInvokedUrlCommand*)command {
|
||||||
|
NSDictionary* options = [command argumentAtIndex:0];
|
||||||
NSDictionary* options = [command.arguments objectAtIndex:0];
|
NSString *message = options[@"message"];
|
||||||
|
NSString *duration = options[@"duration"];
|
||||||
NSString *message = [options objectForKey:@"message"];
|
NSString *position = options[@"position"];
|
||||||
NSString *duration = [options objectForKey:@"duration"];
|
NSDictionary *data = options[@"data"];
|
||||||
NSString *position = [options objectForKey:@"position"];
|
NSNumber *addPixelsY = options[@"addPixelsY"];
|
||||||
NSNumber *addPixelsY = [options objectForKey:@"addPixelsY"];
|
NSDictionary *styling = options[@"styling"];
|
||||||
|
|
||||||
if (![position isEqual: @"top"] && ![position isEqual: @"center"] && ![position isEqual: @"bottom"]) {
|
if (![position isEqual: @"top"] && ![position isEqual: @"center"] && ![position isEqual: @"bottom"]) {
|
||||||
CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"invalid position. valid options are 'top', 'center' and 'bottom'"];
|
CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"invalid position. valid options are 'top', 'center' and 'bottom'"];
|
||||||
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
NSInteger durationInt;
|
NSTimeInterval durationMS;
|
||||||
if ([duration isEqual: @"short"]) {
|
if ([duration.lowercaseString isEqualToString: @"short"]) {
|
||||||
durationInt = 2;
|
durationMS = 2000;
|
||||||
} else if ([duration isEqual: @"long"]) {
|
} else if ([duration.lowercaseString isEqualToString: @"long"]) {
|
||||||
durationInt = 5;
|
durationMS = 4000;
|
||||||
} else {
|
} else {
|
||||||
CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"invalid duration. valid options are 'short' and 'long'"];
|
durationMS = [duration intValue];
|
||||||
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[self.webView makeToast:message duration:durationInt position:position addPixelsY:addPixelsY == nil ? 0 : [addPixelsY intValue]];
|
[self.webView makeToast:message
|
||||||
|
duration:durationMS / 1000
|
||||||
|
position:position
|
||||||
|
addPixelsY:addPixelsY == nil ? 0 : [addPixelsY intValue]
|
||||||
|
data:data
|
||||||
|
styling:styling
|
||||||
|
commandDelegate:self.commandDelegate
|
||||||
|
callbackId:command.callbackId];
|
||||||
|
|
||||||
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
|
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
|
||||||
|
pluginResult.keepCallback = [NSNumber numberWithBool:YES];
|
||||||
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)hide:(CDVInvokedUrlCommand*)command {
|
- (void)hide:(CDVInvokedUrlCommand*)command {
|
||||||
[self.webView hideToast];
|
[self.webView hideToast];
|
||||||
|
|
||||||
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
|
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
|
||||||
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
/**
|
||||||
|
* @author Piotr Smolarski <ph0ndragxdev@gmail.com>
|
||||||
|
*/
|
||||||
|
var toastProxy = {
|
||||||
|
|
||||||
|
lastDisplayedNotification: null,
|
||||||
|
|
||||||
|
show: function(successCallback, errorCallback, options) {
|
||||||
|
var notifications = Windows.UI.Notifications;
|
||||||
|
|
||||||
|
var template = notifications.ToastTemplateType.ToastText01;
|
||||||
|
var toastXml = notifications.ToastNotificationManager.getTemplateContent(template);
|
||||||
|
|
||||||
|
var toastTextElements = toastXml.getElementsByTagName("text");
|
||||||
|
toastTextElements[0].appendChild(toastXml.createTextNode(options[0].message));
|
||||||
|
|
||||||
|
var toastNode = toastXml.selectSingleNode("/toast");
|
||||||
|
toastNode.setAttribute("duration", options[0].duration);
|
||||||
|
|
||||||
|
var toast = new notifications.ToastNotification(toastXml);
|
||||||
|
|
||||||
|
toast.onactivated = function (event) {
|
||||||
|
toastProxy.lastDisplayedNotification = null;
|
||||||
|
successCallback({
|
||||||
|
event: "touch",
|
||||||
|
message: options[0].message,
|
||||||
|
data: options[0].data
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
toast.ondismissed = function (event) {
|
||||||
|
toastProxy.lastDisplayedNotification = null;
|
||||||
|
successCallback({
|
||||||
|
event: "hide",
|
||||||
|
message: options[0].message,
|
||||||
|
data: options[0].data
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
toast.onfailed = function(err) {
|
||||||
|
toastProxy.lastDisplayedNotification = null;
|
||||||
|
errorCallback(err);
|
||||||
|
};
|
||||||
|
|
||||||
|
notifications.ToastNotificationManager.createToastNotifier().show(toast);
|
||||||
|
},
|
||||||
|
|
||||||
|
hide: function() {
|
||||||
|
if (this.lastDisplayedNotification !== null) {
|
||||||
|
notifications.ToastNotificationManager.createToastNotifier().hide(toast);
|
||||||
|
this.lastDisplayedNotification = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
cordova.commandProxy.add("Toast", toastProxy);
|
||||||
+2
-2
@@ -126,10 +126,10 @@ namespace WPCordovaClassLib.Cordova.Commands
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int hideDelay = 2800;
|
int hideDelay = 2500;
|
||||||
if ("long".Equals(duration))
|
if ("long".Equals(duration))
|
||||||
{
|
{
|
||||||
hideDelay = 5500;
|
hideDelay = 5000;
|
||||||
}
|
}
|
||||||
else if (!"short".Equals(duration))
|
else if (!"short".Equals(duration))
|
||||||
{
|
{
|
||||||
|
|||||||
+6
-4
@@ -11,12 +11,12 @@ Toast.prototype.optionsBuilder = function () {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
withMessage: function(m) {
|
withMessage: function(m) {
|
||||||
message = m;
|
message = m.toString();
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
withDuration: function(d) {
|
withDuration: function(d) {
|
||||||
duration = d;
|
duration = d.toString();
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -36,13 +36,15 @@ Toast.prototype.optionsBuilder = function () {
|
|||||||
duration: duration,
|
duration: duration,
|
||||||
position: position,
|
position: position,
|
||||||
addPixelsY: addPixelsY
|
addPixelsY: addPixelsY
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
Toast.prototype.showWithOptions = function (options, successCallback, errorCallback) {
|
Toast.prototype.showWithOptions = function (options, successCallback, errorCallback) {
|
||||||
|
options.duration = (options.duration === undefined ? 'long' : options.duration.toString());
|
||||||
|
options.message = options.message.toString();
|
||||||
cordova.exec(successCallback, errorCallback, "Toast", "show", [options]);
|
cordova.exec(successCallback, errorCallback, "Toast", "show", [options]);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user