7 changed files with 273 additions and 69 deletions
@ -0,0 +1,57 @@
|
||||
|
||||
/** |
||||
* Singleton class for notification dispatching. |
||||
*/ |
||||
Ext.define('Rambox.util.Notifier', { |
||||
|
||||
singleton: true, |
||||
|
||||
constructor: function(config) { |
||||
|
||||
config = config || {}; |
||||
|
||||
/** |
||||
* Returns the notification text depending on the service type. |
||||
* |
||||
* @param view |
||||
* @param count |
||||
* @return {*} |
||||
*/ |
||||
function getNotificationText(view, count) { |
||||
var text; |
||||
switch (Ext.getStore('ServicesList').getById(view.type).get('type')) { |
||||
case 'messaging': |
||||
text = 'You have ' + Ext.util.Format.plural(count, 'new message', 'new messages') + '.'; |
||||
break; |
||||
case 'email': |
||||
text = 'You have ' + Ext.util.Format.plural(count, 'new email', 'new emails') + '.'; |
||||
break; |
||||
default: |
||||
text = 'You have ' + Ext.util.Format.plural(count, 'new activity', 'new activities') + '.'; |
||||
break; |
||||
} |
||||
return text; |
||||
} |
||||
|
||||
/** |
||||
* Dispatches a notification for a specific service. |
||||
* |
||||
* @param view The view of the service |
||||
* @param {number} count The unread count |
||||
*/ |
||||
this.dispatchNotification = function(view, count) { |
||||
var text = getNotificationText(view, count); |
||||
|
||||
var notification = new Notification(view.record.get('name'), { |
||||
body: text, |
||||
icon: view.tab.icon, |
||||
silent: view.record.get('muted') |
||||
}); |
||||
|
||||
notification.onclick = function() { |
||||
require('electron').remote.getCurrentWindow().show(); |
||||
Ext.cq1('app-main').setActiveTab(view); |
||||
}; |
||||
} |
||||
} |
||||
}); |
@ -0,0 +1,75 @@
|
||||
/** |
||||
* Singleton class to handle the global unread counter. |
||||
*/ |
||||
Ext.define('Rambox.util.UnreadCounter', { |
||||
|
||||
singleton: true, |
||||
|
||||
constructor: function(config) { |
||||
|
||||
config = config || {}; |
||||
|
||||
/** |
||||
* Map for storing the global unread count. |
||||
* service id -> unread count |
||||
* |
||||
* @type {Map} |
||||
*/ |
||||
var unreadCountByService = new Map(); |
||||
|
||||
/** |
||||
* Holds the global unread count for internal usage. |
||||
* |
||||
* @type {number} |
||||
*/ |
||||
var totalUnreadCount = 0; |
||||
|
||||
/** |
||||
* Sets the application's unread count to tracked unread count. |
||||
*/ |
||||
function updateAppUnreadCounter() { |
||||
Rambox.app.setTotalNotifications(totalUnreadCount); |
||||
} |
||||
|
||||
/** |
||||
* Returns the global unread count. |
||||
* |
||||
* @return {number} |
||||
*/ |
||||
this.getTotalUnreadCount = function() { |
||||
return totalUnreadCount; |
||||
}; |
||||
|
||||
/** |
||||
* Sets the global unread count for a specific service. |
||||
* |
||||
* @param {*} id Id of the service to set the global unread count for. |
||||
* @param {number} unreadCount The global unread count for the service. |
||||
*/ |
||||
this.setUnreadCountForService = function(id, unreadCount) { |
||||
unreadCount = parseInt(unreadCount, 10); |
||||
|
||||
if (unreadCountByService.has(id)) { |
||||
totalUnreadCount -= unreadCountByService.get(id); |
||||
} |
||||
totalUnreadCount += unreadCount; |
||||
unreadCountByService.set(id, unreadCount); |
||||
|
||||
updateAppUnreadCounter(); |
||||
}; |
||||
|
||||
/** |
||||
* Clears the global unread count for a specific service. |
||||
* |
||||
* @param {*} id Id of the service to clear the global unread count for. |
||||
*/ |
||||
this.clearUnreadCountForService = function(id) { |
||||
if (unreadCountByService.has(id)) { |
||||
totalUnreadCount -= unreadCountByService.get(id); |
||||
} |
||||
unreadCountByService['delete'](id); |
||||
|
||||
updateAppUnreadCounter(); |
||||
} |
||||
} |
||||
}); |
Loading…
Reference in new issue