Browse Source

support check update

MaysWind 6 years ago
parent
commit
53dc167a93

+ 1 - 0
app/index.html

@@ -421,6 +421,7 @@
 <script src="scripts/services/ariaNgSettingService.js"></script>
 <script src="scripts/services/ariaNgMonitorService.js"></script>
 <script src="scripts/services/ariaNgTitleService.js"></script>
+<script src="scripts/services/ariaNgVersionService.js"></script>
 <script src="scripts/services/aria2HttpRpcService.js"></script>
 <script src="scripts/services/aria2WebSocketRpcService.js"></script>
 <script src="scripts/services/aria2RpcService.js"></script>

+ 4 - 0
app/langs/zh_Hans.txt

@@ -146,6 +146,10 @@ Failed to load file!=加载文件失败!
 Download Completed=下载完成
 BT Download Completed=BT 下载完成
 Download Error=下载出错
+Check Update=检查更新
+Latest Version=最新版本
+You have installed the latest version!=您已经安装了最新的版本!
+Failed to get latest version!=获取最新版本失败!
 Language=语言
 Debug Mode=调试模式
 Page Title=页面标题

+ 4 - 0
app/langs/zh_Hant.txt

@@ -146,6 +146,10 @@ Failed to load file!=載入檔案失敗!
 Download Completed=下載完成
 BT Download Completed=BT 下載完成
 Download Error=下載出錯
+Check Update=檢查更新
+Latest Version=最新版本
+You have installed the latest version!=您已經安裝了最新的版本!
+Failed to get latest version!=獲取最新版本失敗!
 Language=語言
 Debug Mode=偵錯模式
 Page Title=頁面標題

+ 4 - 0
app/scripts/config/defaultLanguage.js

@@ -150,6 +150,10 @@
             'Download Completed': 'Download Completed',
             'BT Download Completed': 'BT Download Completed',
             'Download Error': 'Download Error',
+            'Check Update': 'Check Update',
+            'Latest Version': 'Latest Version',
+            'You have installed the latest version!': 'You have installed the latest version!',
+            'Failed to get latest version!': 'Failed to get latest version!',
             'Language': 'Language',
             'Debug Mode': 'Debug Mode',
             'Page Title': 'Page Title',

+ 25 - 1
app/scripts/controllers/settings-ariang.js

@@ -1,7 +1,7 @@
 (function () {
     'use strict';
 
-    angular.module('ariaNg').controller('AriaNgSettingsController', ['$rootScope', '$scope', '$routeParams', '$window', '$interval', '$timeout', '$filter', 'clipboard', 'ariaNgLanguages', 'ariaNgCommonService', 'ariaNgNotificationService', 'ariaNgLocalizationService', 'ariaNgLogService', 'ariaNgFileService', 'ariaNgSettingService', 'ariaNgMonitorService', 'ariaNgTitleService', 'aria2SettingService', 'ariaNgNativeElectronService', function ($rootScope, $scope, $routeParams, $window, $interval, $timeout, $filter, clipboard, ariaNgLanguages, ariaNgCommonService, ariaNgNotificationService, ariaNgLocalizationService, ariaNgLogService, ariaNgFileService, ariaNgSettingService, ariaNgMonitorService, ariaNgTitleService, aria2SettingService, ariaNgNativeElectronService) {
+    angular.module('ariaNg').controller('AriaNgSettingsController', ['$rootScope', '$scope', '$routeParams', '$window', '$interval', '$timeout', '$filter', 'clipboard', 'ariaNgLanguages', 'ariaNgCommonService', 'ariaNgNotificationService', 'ariaNgLocalizationService', 'ariaNgLogService', 'ariaNgFileService', 'ariaNgSettingService', 'ariaNgMonitorService', 'ariaNgTitleService', 'aria2SettingService', 'ariaNgVersionService', 'ariaNgNativeElectronService', function ($rootScope, $scope, $routeParams, $window, $interval, $timeout, $filter, clipboard, ariaNgLanguages, ariaNgCommonService, ariaNgNotificationService, ariaNgLocalizationService, ariaNgLogService, ariaNgFileService, ariaNgSettingService, ariaNgMonitorService, ariaNgTitleService, aria2SettingService, ariaNgVersionService, ariaNgNativeElectronService) {
         var extendType = $routeParams.extendType;
         var lastRefreshPageNotification = null;
 
@@ -53,6 +53,7 @@
             currentTab: 'global',
             ariaNgNativeVersion: ariaNgNativeElectronService.getVersion(),
             ariaNgVersion: ariaNgNativeElectronService.getAriaNgVersion(),
+            isCurrentLatestVersion: false,
             runtimeEnvironment: ariaNgNativeElectronService.getRuntimeEnvironment(),
             runtimeEnvironmentCollapsed: true,
             languages: ariaNgLanguages,
@@ -97,6 +98,29 @@
             return parseInt($scope.context.currentTab.substring(3));
         };
 
+        $scope.checkUpdate = function () {
+            return ariaNgVersionService.getTheLatestVersion()
+                .then(function onSuccess(response) {
+                    if (!response || !response.data || !response.data.tag_name) {
+                        ariaNgLogService.warn('[AriaNgSettingsController.checkUpdate] data format of latest version is invalid', response);
+                        ariaNgLocalizationService.showError('Failed to get latest version!');
+                        return;
+                    }
+
+                    var latestVersion = response.data.tag_name;
+
+                    if (ariaNgVersionService.compareVersion($scope.context.ariaNgNativeVersion, latestVersion) >= 0) {
+                        ariaNgLocalizationService.showInfo('Check Update', 'You have installed the latest version!');
+                        $scope.context.isCurrentLatestVersion = true;
+                    } else {
+                        ariaNgNativeElectronService.openProjectReleaseLink();
+                    }
+                }).catch(function onError(response) {
+                    ariaNgLogService.error('[AriaNgSettingsController.checkUpdate] failed to get latest version', response);
+                    ariaNgLocalizationService.showError('Failed to get latest version!');
+                });
+        };
+
         $scope.updateTitlePreview = function () {
             $scope.context.titlePreview = getFinalTitle();
         };

+ 3 - 0
app/scripts/services/ariaNgNativeElectronService.js

@@ -88,6 +88,9 @@
             openProjectLink: function () {
                 return shell.openExternal && shell.openExternal('http://github.com/mayswind/AriaNg-Native');
             },
+            openProjectReleaseLink: function () {
+                return shell.openExternal && shell.openExternal('https://github.com/mayswind/AriaNg-Native/releases');
+            },
             openFileInDirectory: function (dir, filename) {
                 var fullpath = localfs.getFullPath(dir, filename);
                 return shell.showItemInFolder && shell.showItemInFolder(fullpath);

+ 66 - 0
app/scripts/services/ariaNgVersionService.js

@@ -0,0 +1,66 @@
+(function () {
+    'use strict';
+
+    angular.module('ariaNg').factory('ariaNgVersionService', ['$http', function ($http) {
+        var latestApi = 'https://api.github.com/repos/mayswind/AriaNg-Native/releases/latest';
+
+        var getTheLatestVersion = function () {
+            return $http({
+                url: latestApi,
+                method: 'GET',
+                headers: {
+                    'Accept': 'application/vnd.github.v3+json'
+                }
+            })
+        };
+
+        var compareVersion = function (version1, version2) {
+            var parts1 = version1.split('.');
+            var parts2 = version2.split('.');
+
+            for (var i = 0; i < Math.max(parts1.length, parts2.length); i++) {
+                if (parts1[i] && angular.isUndefined(parts2[i])) { // 1.1.1 > 1.1, 1.1.0 == 1.1
+                    return 1; // version1 > version2
+                }
+
+                if (parts2[i] && angular.isUndefined(parts1[i])) {
+                    return -1; // version1 < version2
+                }
+
+                if (parts2[i].indexOf('-') > 0 && parts1[i].indexOf('-') < 0) { // 1.1.0 > 1.1.0-beta1
+                    return 1; // version1 > version2
+                } else if (parts1[i].indexOf('-') > 0 && parts2[i].indexOf('-') < 0) {
+                    return -1; // version1 < version2
+                }
+
+                var subParts1 = parts1[i].split('-');
+                var subParts2 = parts2[i].split('-');
+
+                var subPart0CompareResult = String.naturalCompare(subParts1[0], subParts2[0]);
+
+                if (subPart0CompareResult > 0) { // 1.1.1 > 1.1.0
+                    return 1; // version1 > version2
+                } else if (subPart0CompareResult < 0) {
+                    return -1; // version1 < version2
+                }
+
+                if (!angular.isUndefined(subParts1[1]) && !angular.isUndefined(subParts2[1])) {
+                    var subPart1CompareResult = String.naturalCompare(subParts1[1], subParts2[1]);
+
+                    if (subPart1CompareResult > 0) { // 1.1.0-beta2 > 1.1.0-beta1
+                        return 1; // version1 > version2
+                    } else if (subPart1CompareResult < 0) {
+                        return -1; // version1 < version2
+                    }
+                }
+            }
+
+            return 0;
+        };
+
+        return {
+            getTheLatestVersion: getTheLatestVersion,
+            compareVersion: compareVersion
+        };
+    }]);
+}());

+ 6 - 1
app/views/settings-ariang.html

@@ -25,7 +25,12 @@
                         <div class="setting-key col-sm-4">
                             <span translate>AriaNg Version</span>
                         </div>
-                        <div class="setting-value col-sm-8" ng-bind="'AriaNg Native ' + context.ariaNgNativeVersion + ' (AriaNg ' + context.ariaNgVersion + ')'"></div>
+                        <div class="setting-value col-sm-8">
+                            <span ng-bind="'AriaNg Native ' + context.ariaNgNativeVersion + ' (AriaNg ' + context.ariaNgVersion + ')'"></span>
+                            <button class="btn btn-xs btn-default" ng-click="checkUpdate()" ng-disabled="context.isCurrentLatestVersion" promise-btn>
+                                <span ng-bind="(context.isCurrentLatestVersion ? 'Latest Version' : 'Check Update') | translate" translate>Check Update</span>
+                            </button>
+                        </div>
                     </div>
                     <div class="row" ng-if="context.runtimeEnvironment">
                         <div class="setting-key col-sm-4">