Commit 0d6b4926 by Sebastián Katzer

Changed internal code how to merge defaults

parent c2a88a5e
...@@ -25,7 +25,6 @@ import android.app.PendingIntent; ...@@ -25,7 +25,6 @@ import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.media.session.PlaybackState;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
...@@ -331,7 +330,7 @@ public final class Builder { ...@@ -331,7 +330,7 @@ public final class Builder {
* @param builder Local notification builder instance. * @param builder Local notification builder instance.
*/ */
private void applyContentReceiver(NotificationCompat.Builder builder) { private void applyContentReceiver(NotificationCompat.Builder builder) {
new MediaSessionCompat(context, "").getSessionToken();
if (clickActivity == null) if (clickActivity == null)
return; return;
......
...@@ -64,16 +64,14 @@ exports.schedule = function (msgs, callback, scope, args) { ...@@ -64,16 +64,14 @@ exports.schedule = function (msgs, callback, scope, args) {
if (!granted) return; if (!granted) return;
var notifications = Array.isArray(msgs) ? msgs : [msgs]; var toasts = this.toArray(msgs);
for (var i = 0; i < notifications.length; i++) { for (var toast of toasts) {
var notification = notifications[i]; this.mergeWithDefaults(toast);
this.convertProperties(toast);
this.mergeWithDefaults(notification);
this.convertProperties(notification);
} }
this.exec('schedule', notifications, callback, scope); this.exec('schedule', toasts, callback, scope);
}; };
if (args && args.skipPermission) { if (args && args.skipPermission) {
...@@ -98,15 +96,12 @@ exports.update = function (msgs, callback, scope, args) { ...@@ -98,15 +96,12 @@ exports.update = function (msgs, callback, scope, args) {
if (!granted) return; if (!granted) return;
var notifications = Array.isArray(msgs) ? msgs : [msgs]; var toasts = this.toArray(msgs);
for (var i = 0; i < notifications.length; i++) {
var notification = notifications[i];
this.convertProperties(notification); for (var toast of toasts) {
} this.convertProperties(toast); }
this.exec('update', notifications, callback, scope); this.exec('update', toasts, callback, scope);
}; };
if (args && args.skipPermission) { if (args && args.skipPermission) {
...@@ -126,7 +121,7 @@ exports.update = function (msgs, callback, scope, args) { ...@@ -126,7 +121,7 @@ exports.update = function (msgs, callback, scope, args) {
* @return [ Void ] * @return [ Void ]
*/ */
exports.clear = function (ids, callback, scope) { exports.clear = function (ids, callback, scope) {
ids = Array.isArray(ids) ? ids : [ids]; ids = this.toArray(ids);
ids = this.convertIds(ids); ids = this.convertIds(ids);
this.exec('clear', ids, callback, scope); this.exec('clear', ids, callback, scope);
...@@ -154,7 +149,7 @@ exports.clearAll = function (callback, scope) { ...@@ -154,7 +149,7 @@ exports.clearAll = function (callback, scope) {
* @return [ Void ] * @return [ Void ]
*/ */
exports.cancel = function (ids, callback, scope) { exports.cancel = function (ids, callback, scope) {
ids = Array.isArray(ids) ? ids : [ids]; ids = this.toArray(ids);
ids = this.convertIds(ids); ids = this.convertIds(ids);
this.exec('cancel', ids, callback, scope); this.exec('cancel', ids, callback, scope);
...@@ -340,7 +335,15 @@ exports.addActionGroup = function (id, actions, callback, scope) { ...@@ -340,7 +335,15 @@ exports.addActionGroup = function (id, actions, callback, scope) {
* @return [ Object ] * @return [ Object ]
*/ */
exports.getDefaults = function () { exports.getDefaults = function () {
return this._defaults; var map = Object.create(this._defaults);
for (var key in map) {
if (Object.prototype.isPrototypeOf(map[key])) {
map[key] = Object.create(map[key]);
}
}
return map;
}; };
/** /**
...@@ -351,13 +354,7 @@ exports.getDefaults = function () { ...@@ -351,13 +354,7 @@ exports.getDefaults = function () {
* @return [ Void ] * @return [ Void ]
*/ */
exports.setDefaults = function (newDefaults) { exports.setDefaults = function (newDefaults) {
var defaults = this.getDefaults(); Object.assign(this._defaults, newDefaults);
for (var key in defaults) {
if (newDefaults.hasOwnProperty(key)) {
defaults[key] = newDefaults[key];
}
}
}; };
/** /**
......
...@@ -28,13 +28,13 @@ exports._defaults = { ...@@ -28,13 +28,13 @@ exports._defaults = {
text: '', text: '',
title: '', title: '',
sound: true, sound: true,
badge: undefined, badge: null,
data: undefined, data: null,
icon: undefined, icon: null,
silent: false, silent: false,
trigger: { type: 'calendar' }, trigger: { type: 'calendar' },
actions: [], actions: [],
actionGroupId: undefined, actionGroupId: null,
attachments: [], attachments: [],
progressBar: false progressBar: false
}; };
...@@ -52,24 +52,24 @@ exports.applyPlatformSpecificOptions = function () { ...@@ -52,24 +52,24 @@ exports.applyPlatformSpecificOptions = function () {
switch (device.platform) { switch (device.platform) {
case 'Android': case 'Android':
defaults.group = undefined; defaults.group = null;
defaults.groupSummary = false; defaults.groupSummary = false;
defaults.summary = undefined; defaults.summary = null;
defaults.icon = 'res://icon'; defaults.icon = 'res://icon';
defaults.smallIcon = undefined; defaults.smallIcon = null;
defaults.sticky = false; defaults.sticky = false;
defaults.autoClear = true; defaults.autoClear = true;
defaults.led = true; defaults.led = true;
defaults.color = undefined; defaults.color = null;
defaults.vibrate = false; defaults.vibrate = false;
defaults.lockscreen = true; defaults.lockscreen = true;
defaults.showWhen = true; defaults.showWhen = true;
defaults.defaults = 0; defaults.defaults = 0;
defaults.priority = 0; defaults.priority = 0;
defaults.number = 0; defaults.number = 0;
defaults.channel = undefined; defaults.channel = null;
defaults.launch = true; defaults.launch = true;
defaults.mediaSession = undefined; defaults.mediaSession = null;
break; break;
} }
}; };
...@@ -82,37 +82,26 @@ exports.applyPlatformSpecificOptions = function () { ...@@ -82,37 +82,26 @@ exports.applyPlatformSpecificOptions = function () {
* @retrun [ Object ] * @retrun [ Object ]
*/ */
exports.mergeWithDefaults = function (options) { exports.mergeWithDefaults = function (options) {
var defaults = this.getDefaults(); var values = this.getDefaults();
options.text = this.getValueFor(options, 'text', 'message'); if (values.hasOwnProperty('sticky')) {
options.data = this.getValueFor(options, 'data', 'json'); options.sticky = this.getValueFor(options, 'sticky', 'ongoing');
if (defaults.hasOwnProperty('autoClear')) {
options.autoClear = this.getValueFor(options, 'autoClear', 'autoCancel');
} }
if (options.autoClear !== true && options.ongoing) { if (options.sticky && options.autoClear !== true) {
options.autoClear = false; options.autoClear = false;
} }
if (defaults.hasOwnProperty('sticky')) { Object.assign(values, options);
options.sticky = this.getValueFor(options, 'sticky', 'ongoing');
}
for (var key in defaults) { for (var key in values) {
if (options[key] === null || options[key] === undefined) { if (values[key] !== null) {
if (options.hasOwnProperty(key) && ['data','sound'].indexOf(key) > -1) { options[key] = values[key];
options[key] = undefined;
} else { } else {
var obj = defaults[key]; delete options[key];
options[key] = typeof obj === 'object' ? Object.assign({}, obj) : obj;
}
}
} }
for (key in options) { if (!this._defaults.hasOwnProperty(key)) {
if (!defaults.hasOwnProperty(key)) {
// delete options[key];
console.warn('Unknown property: ' + key); console.warn('Unknown property: ' + key);
} }
} }
...@@ -157,9 +146,7 @@ exports.convertProperties = function (options) { ...@@ -157,9 +146,7 @@ exports.convertProperties = function (options) {
options.defaults = parseToInt('defaults', options); options.defaults = parseToInt('defaults', options);
} }
if (typeof options.data == 'object') {
options.data = JSON.stringify(options.data); options.data = JSON.stringify(options.data);
}
this.convertTrigger(options); this.convertTrigger(options);
this.convertActions(options); this.convertActions(options);
...@@ -183,12 +170,11 @@ exports.convertActions = function (options) { ...@@ -183,12 +170,11 @@ exports.convertActions = function (options) {
var actions = []; var actions = [];
for (var i = 0, action; i < options.actions.length; i++) { for (var action of options.actions) {
action = options.actions[i];
if (!action.id) { if (!action.id) {
console.warn( console.warn('Action with title ' + action.title + ' ' +
'Action with title ' + action.title + ' has no id and will not be added.'); 'has no id and will not be added.');
continue; continue;
} }
...@@ -197,7 +183,7 @@ exports.convertActions = function (options) { ...@@ -197,7 +183,7 @@ exports.convertActions = function (options) {
actions.push(action); actions.push(action);
} }
options.category = (options.category || 'DEFAULT_GROUP').toString(); options.actionGroupId = (options.actionGroupId || 'DEFAULT_GROUP').toString();
options.actions = actions; options.actions = actions;
return options; return options;
...@@ -281,14 +267,14 @@ exports.convertProgressBar = function (options) { ...@@ -281,14 +267,14 @@ exports.convertProgressBar = function (options) {
} }
if (typeof cfg.enabled !== 'boolean') { if (typeof cfg.enabled !== 'boolean') {
cfg.enabled = !!(cfg.value || cfg.maxValue || cfg.indeterminate !== undefined); cfg.enabled = !!(cfg.value || cfg.maxValue || cfg.indeterminate !== null);
} }
cfg.value = cfg.value || 0; cfg.value = cfg.value || 0;
if (isAndroid) { if (isAndroid) {
cfg.maxValue = cfg.maxValue || 100; cfg.maxValue = cfg.maxValue || 100;
cfg.indeterminate = cfg.indeterminate !== undefined ? cfg.indeterminate : false; cfg.indeterminate = !!cfg.indeterminate;
} }
cfg.enabled = !!cfg.enabled; cfg.enabled = !!cfg.enabled;
...@@ -324,8 +310,8 @@ exports.createCallbackFn = function (fn, scope) { ...@@ -324,8 +310,8 @@ exports.createCallbackFn = function (fn, scope) {
exports.convertIds = function (ids) { exports.convertIds = function (ids) {
var convertedIds = []; var convertedIds = [];
for (var i = 0; i < ids.length; i++) { for (var id of ids) {
convertedIds.push(Number(ids[i])); convertedIds.push(Number(id));
} }
return convertedIds; return convertedIds;
...@@ -342,13 +328,24 @@ exports.convertIds = function (ids) { ...@@ -342,13 +328,24 @@ exports.convertIds = function (ids) {
exports.getValueFor = function (options) { exports.getValueFor = function (options) {
var keys = Array.apply(null, arguments).slice(1); var keys = Array.apply(null, arguments).slice(1);
for (var i = 0; i < keys.length; i++) { for (var key of keys) {
var key = keys[i];
if (options.hasOwnProperty(key)) { if (options.hasOwnProperty(key)) {
return options[key]; return options[key];
} }
} }
return null;
};
/**
* Convert a value to an array.
*
* @param [ Object ] obj Any kind of object.
*
* @return [ Array ] An array with the object as first item.
*/
exports.toArray = function (obj) {
return Array.isArray(obj) ? Array.from(obj) : [obj];
}; };
/** /**
...@@ -366,6 +363,10 @@ exports.fireEvent = function (event) { ...@@ -366,6 +363,10 @@ exports.fireEvent = function (event) {
if (!listener) if (!listener)
return; return;
if (args[0] && typeof args[0].data === 'string') {
args[0].data = JSON.parse(args[0].data);
}
for (var i = 0; i < listener.length; i++) { for (var i = 0; i < listener.length; i++) {
var fn = listener[i][0], var fn = listener[i][0],
scope = listener[i][1]; scope = listener[i][1];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment