From 068c80d87767d1e887132a468c3dd07913ae1c55 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sat, 16 Jul 2016 23:16:07 +0800 Subject: [PATCH] support show success message after connect to aria2 first --- bower.json | 1 + src/index.html | 2 + src/langs/zh_CN.json | 1 + src/scripts/config/configuration.js | 6 ++- src/scripts/config/constants.js | 3 +- src/scripts/config/defaultLanguage.js | 1 + src/scripts/core/app.js | 1 + src/scripts/core/root.js | 6 +++ src/scripts/services/aria2RpcService.js | 23 ++++++++++- src/scripts/services/aria2TaskService.js | 17 ++++++-- .../services/ariaNgNotificationService.js | 39 ++++++++++++++++--- 11 files changed, 88 insertions(+), 12 deletions(-) diff --git a/bower.json b/bower.json index 1cd6689..ca86664 100644 --- a/bower.json +++ b/bower.json @@ -45,6 +45,7 @@ "angular-utf8-base64": "^0.0.5", "angular-local-storage": "^0.2.7", "angular-notification": "775ee861c1737b284588bcb878ba1f4e43c70c97", + "angular-ui-notification": "^0.2.0", "angular-bittorrent-peerid": "^1.0.2", "angular-busy": "^4.1.3", "angular-promise-buttons": "^0.1.14", diff --git a/src/index.html b/src/index.html index 3e3e9ef..d19425a 100644 --- a/src/index.html +++ b/src/index.html @@ -21,6 +21,7 @@ + @@ -276,6 +277,7 @@ + diff --git a/src/langs/zh_CN.json b/src/langs/zh_CN.json index 89c70b9..489eda3 100644 --- a/src/langs/zh_CN.json +++ b/src/langs/zh_CN.json @@ -1,6 +1,7 @@ { "Simplified Chinese": "简体中文", "Operation Succeeded": "操作成功", + "Connection Succeeded": "连接成功", "Error": "错误", "OK": "确定", "Confirm": "确认", diff --git a/src/scripts/config/configuration.js b/src/scripts/config/configuration.js index 75da414..6dd7a36 100644 --- a/src/scripts/config/configuration.js +++ b/src/scripts/config/configuration.js @@ -1,7 +1,7 @@ (function () { 'use strict'; - angular.module('ariaNg').config(['$translateProvider', 'localStorageServiceProvider', 'ariaNgConstants', function ($translateProvider, localStorageServiceProvider, ariaNgConstants) { + angular.module('ariaNg').config(['$translateProvider', 'localStorageServiceProvider', 'NotificationProvider', 'ariaNgConstants', function ($translateProvider, localStorageServiceProvider, NotificationProvider, ariaNgConstants) { localStorageServiceProvider .setPrefix(ariaNgConstants.appPrefix) .setStorageType('localStorage') @@ -14,5 +14,9 @@ .preferredLanguage('en') .fallbackLanguage('en') .useSanitizeValueStrategy('escapeParameters'); + + NotificationProvider.setOptions({ + delay: ariaNgConstants.notificationInPageTimeout + }); }]); })(); diff --git a/src/scripts/config/constants.js b/src/scripts/config/constants.js index 2b50b9e..c6aeada 100644 --- a/src/scripts/config/constants.js +++ b/src/scripts/config/constants.js @@ -8,7 +8,8 @@ globalStatStorageCapacity: 120, taskStatStorageCapacity: 300, lazySaveTimeout: 500, - errorTooltipDelay: 200 + errorTooltipDelay: 200, + notificationInPageTimeout: 2000 }).constant('ariaNgFileTypes', { video: [ '.3gp', diff --git a/src/scripts/config/defaultLanguage.js b/src/scripts/config/defaultLanguage.js index b16cefc..b7a921e 100644 --- a/src/scripts/config/defaultLanguage.js +++ b/src/scripts/config/defaultLanguage.js @@ -5,6 +5,7 @@ $translateProvider.translations('en', { 'English': 'English', 'Operation Succeeded': 'Operation Succeeded', + 'Connection Succeeded': 'Connection Succeeded', 'Error': 'Error', 'OK': 'OK', 'Confirm': 'Confirm', diff --git a/src/scripts/core/app.js b/src/scripts/core/app.js index 783ac26..170f247 100644 --- a/src/scripts/core/app.js +++ b/src/scripts/core/app.js @@ -14,6 +14,7 @@ 'ab-base64', 'LocalStorageModule', 'notification', + 'ui-notification', 'angularBittorrentPeerid', 'cgBusy', 'angularPromiseButtons', diff --git a/src/scripts/core/root.js b/src/scripts/core/root.js index 30af4a6..b5cd0a6 100644 --- a/src/scripts/core/root.js +++ b/src/scripts/core/root.js @@ -154,6 +154,12 @@ } }; + aria2TaskService.onFirstSuccess(function () { + ariaNgNotificationService.notifyInPage('', 'Connection Succeeded', { + type: 'success' + }); + }); + aria2TaskService.onTaskCompleted(function (event) { ariaNgNotificationService.notifyTaskComplete(event.task); }); diff --git a/src/scripts/services/aria2RpcService.js b/src/scripts/services/aria2RpcService.js index 4da1738..9d3edfa 100644 --- a/src/scripts/services/aria2RpcService.js +++ b/src/scripts/services/aria2RpcService.js @@ -3,8 +3,10 @@ angular.module('ariaNg').factory('aria2RpcService', ['$q', 'aria2RpcConstants', 'aria2RpcErrors', 'ariaNgCommonService', 'ariaNgSettingService', 'aria2HttpRpcService', 'aria2WebSocketRpcService', function ($q, aria2RpcConstants, aria2RpcErrors, ariaNgCommonService, ariaNgSettingService, aria2HttpRpcService, aria2WebSocketRpcService) { var rpcImplementService = ariaNgSettingService.isUseWebSocket() ? aria2WebSocketRpcService : aria2HttpRpcService; + var isConnected = false; var secret = ariaNgSettingService.getSecret(); + var onFirstSuccessCallbacks = []; var onDownloadStartCallbacks = []; var onDownloadPauseCallbacks = []; var onDownloadStopCallbacks = []; @@ -28,7 +30,7 @@ if (returnContextOnly) { return requestContext; } - + var uniqueId = ariaNgCommonService.generateUniqueId(); var requestBody = { @@ -63,6 +65,17 @@ }); }; + var fireFirstSuccessEvent = function () { + if (!angular.isArray(onFirstSuccessCallbacks) || onFirstSuccessCallbacks.length < 1) { + return; + } + + for (var i = 0; i < onFirstSuccessCallbacks.length; i++) { + var callback = onFirstSuccessCallbacks[i]; + callback(); + } + }; + var invokeMulti = function (methodFunc, contexts, callback) { var promises = []; @@ -123,6 +136,11 @@ var innerContext = arguments[1]; context.successCallback = function (id, result) { + if (!isConnected) { + isConnected = true; + fireFirstSuccessEvent(); + } + if (innerContext.callback) { innerContext.callback({ id: id, @@ -397,6 +415,9 @@ listMethods: function (context) { return invoke(buildRequestContext('system.listMethods', context)); }, + onFirstSuccess: function (context) { + onFirstSuccessCallbacks.push(context.callback); + }, onDownloadStart: function (context) { onDownloadStartCallbacks.push(context.callback); }, diff --git a/src/scripts/services/aria2TaskService.js b/src/scripts/services/aria2TaskService.js index 4b29307..a8776c6 100644 --- a/src/scripts/services/aria2TaskService.js +++ b/src/scripts/services/aria2TaskService.js @@ -204,7 +204,7 @@ return peers; }; - var createEventCallback = function (getTaskStatusFunc, callback, type) { + var createTaskEventCallback = function (getTaskStatusFunc, callback, type) { return function (event) { var context = { type: type, @@ -472,13 +472,22 @@ callback: callback }); }, + onFirstSuccess: function (callback) { + if (!callback) { + return; + } + + aria2RpcService.onFirstSuccess({ + callback: callback + }); + }, onTaskCompleted: function (callback) { if (!callback) { return; } aria2RpcService.onDownloadComplete({ - callback: createEventCallback(this.getTaskStatus, callback, 'completed') + callback: createTaskEventCallback(this.getTaskStatus, callback, 'completed') }); }, onBtTaskCompleted: function (callback) { @@ -487,7 +496,7 @@ } aria2RpcService.onBtDownloadComplete({ - callback: createEventCallback(this.getTaskStatus, callback, 'btcompleted') + callback: createTaskEventCallback(this.getTaskStatus, callback, 'btcompleted') }); }, onTaskErrorOccur: function (callback) { @@ -496,7 +505,7 @@ } aria2RpcService.onDownloadError({ - callback: createEventCallback(this.getTaskStatus, callback, 'error') + callback: createTaskEventCallback(this.getTaskStatus, callback, 'error') }); }, processDownloadTasks: function (tasks) { diff --git a/src/scripts/services/ariaNgNotificationService.js b/src/scripts/services/ariaNgNotificationService.js index 7e463e0..ede48c5 100644 --- a/src/scripts/services/ariaNgNotificationService.js +++ b/src/scripts/services/ariaNgNotificationService.js @@ -1,7 +1,7 @@ (function () { 'use strict'; - angular.module('ariaNg').factory('ariaNgNotificationService', ['$notification', '$translate', 'ariaNgSettingService', function ($notification, $translate, ariaNgSettingService) { + angular.module('ariaNg').factory('ariaNgNotificationService', ['$notification', '$translate', 'Notification', 'ariaNgSettingService', function ($notification, $translate, Notification, ariaNgSettingService) { var isSupportBrowserNotification = $notification.isSupported; var isPermissionGranted = function (permission) { @@ -37,21 +37,50 @@ } }); }, - notify: function (title, content) { + notifyViaBrowser: function (title, content) { if (isSupportBrowserNotification && ariaNgSettingService.getBrowserNotification()) { $notification($translate.instant(title), { body: $translate.instant(content) }); } }, + notifyInPage: function (title, content, options) { + if (!options) { + options = {}; + } + + if (title) { + title = $translate.instant(title); + } + + if (content) { + content = $translate.instant(content); + } + + if (!content) { + options.message = title; + } else { + options.title = title; + options.message = content; + } + + if (!options.type || !Notification[options.type]) { + options.type = 'primary'; + } + + Notification[options.type](options); + }, notifyTaskComplete: function (task) { - this.notify('Download Completed', (task && task.taskName ? task.taskName : '')); + this.notifyViaBrowser('Download Completed', (task && task.taskName ? task.taskName : '')); }, notifyBtTaskComplete: function (task) { - this.notify('BT Download Completed', (task && task.taskName ? task.taskName : '')); + this.notifyViaBrowser('BT Download Completed', (task && task.taskName ? task.taskName : '')); }, notifyTaskError: function (task) { - this.notify('Download Error', (task && task.taskName ? task.taskName : '')); + this.notifyViaBrowser('Download Error', (task && task.taskName ? task.taskName : '')); + }, + clearNotificationInPage: function () { + Notification.clearAll(); } } }]);