From fadf234798aa7e5ec409fa2640ff09b205b504fa Mon Sep 17 00:00:00 2001 From: finico Date: Wed, 11 Sep 2019 18:46:43 +0300 Subject: [PATCH 1/3] DEXW-1623: Add selecting account from Ledger by id --- src/modules/app/less/app.less | 2 +- src/modules/app/services/User.js | 4 + src/modules/ledger/controllers/LedgerCtrl.js | 171 ++++++++++++++---- src/modules/ledger/templates/ledger.html | 98 ++++++---- src/modules/ui/directives/input/input.less | 4 +- .../utils/modals/settings/SettingsCtrl.js | 1 + .../utils/modals/settings/settings.html | 12 +- 7 files changed, 225 insertions(+), 67 deletions(-) diff --git a/src/modules/app/less/app.less b/src/modules/app/less/app.less index 91b648ab7d..caf2f8d82a 100644 --- a/src/modules/app/less/app.less +++ b/src/modules/app/less/app.less @@ -344,7 +344,7 @@ fieldset { } .get-started-checkbox-save { - margin: 20px 0; + margin: 10px 0; } @media screen and (min-width: 480px) { diff --git a/src/modules/app/services/User.js b/src/modules/app/services/User.js index aa1406cb66..a70c257bdc 100644 --- a/src/modules/app/services/User.js +++ b/src/modules/app/services/User.js @@ -146,6 +146,10 @@ return this.currentUser ? this.currentUser.address : null; } + get id() { + return this.currentUser ? this.currentUser.id : null; + } + get name() { return this.currentUser ? this.currentUser.name : null; } diff --git a/src/modules/ledger/controllers/LedgerCtrl.js b/src/modules/ledger/controllers/LedgerCtrl.js index f7c426e82e..113e36cd10 100644 --- a/src/modules/ledger/controllers/LedgerCtrl.js +++ b/src/modules/ledger/controllers/LedgerCtrl.js @@ -5,6 +5,7 @@ const USERS_COUNT = 5; const PRELOAD_USERS_COUNT = 5; + const MAX_USER_COUNT = 2147483647; /** * @param {typeof Base} Base @@ -35,10 +36,6 @@ * @type {boolean} */ this.error = false; - /** - * @type {Array} - */ - this.users = []; /** * @type {Array} */ @@ -50,7 +47,7 @@ /** * @type {number} */ - this.currentStep = 0; + this.offset = 0; /** * @type {boolean} */ @@ -75,6 +72,10 @@ * @type {boolean} */ this.saveUserData = true; + /** + * @type {string} + */ + this.id = ''; /** * @type {string} */ @@ -84,6 +85,11 @@ * @private */ this._runLedgerCommand = ''; + /** + * @type {Object} + * @private + */ + this._users = {}; /** * @type {Array} * @private @@ -123,27 +129,31 @@ } /** - * @param count - * @return {void} + * @param {number} count + * @return {Promise} */ getUsers(count) { this._runLedgerCommand = 'getUsers'; this.loading = true; this.error = false; - const start = this.users.length; + const countUsers = (count || USERS_COUNT) - 1; - const promise = utils.timeoutPromise(this.adapter.getUserList(start, countUsers), 25000); + const promise = utils.timeoutPromise(this.adapter.getUserList(this.offset, countUsers), 25000); const modalPromise = this.isInit ? Promise.resolve() : modalManager.showLoginByDevice(promise, this.adapter.type); - Promise.all([promise, modalPromise]) + return Promise.all([promise, modalPromise]) .then(([users]) => { this.isInit = true; - this.users = [...this.users, ...users]; this.loading = false; this.error = false; + + (users || []).forEach(curUser => { + this._users[curUser.id] = curUser; + }); + this.showVisibleUsers(); this.selectUser(); $scope.$digest(); @@ -152,6 +162,12 @@ const error = { ...err, count }; this.loading = false; this.error = error; + + if (err instanceof RangeError) { + this.offset = 0; + this.id = ''; + } + $scope.$digest(); throw error; }); @@ -177,10 +193,8 @@ return null; } - if (!user && !this.selectedUser && this.users.length) { - this.selectedUser = this.users[0]; - // } else if (this.selectedUser === user) { - // this.selectedUser = null; + if (!user && !this.selectedUser && this._users[0]) { + this.selectedUser = this._users[0]; } else if (user) { this.selectedUser = user; } @@ -196,9 +210,19 @@ if (this.selectDefault || this.disabledLeft) { return; } - this.currentStep--; - this.currentStep = this.currentStep >= 0 ? this.currentStep : 0; - this.showVisibleUsers(); + + this.offset = this._normalizeOffset(this.offset - USERS_COUNT); + + if (this._hasUsersInCache(this.offset, this.offset + USERS_COUNT - 1)) { + this.showVisibleUsers(); + } else { + if (this.loading) { + return; + } + + this.getUsers(); + } + this._calculateDisabled(); } @@ -210,25 +234,32 @@ return; } - if (this.users.length <= this.currentStep + USERS_COUNT) { + this.offset = this._normalizeOffset(this.offset + USERS_COUNT); + + if (this._hasUsersInCache(this.offset, this.offset + USERS_COUNT - 1)) { + this.showVisibleUsers(); + } else { if (this.loading) { return; } - this.currentStep++; + this.getUsers(); - this._calculateDisabled(); - } else { - this.currentStep++; - this.showVisibleUsers(); - this._calculateDisabled(); } + + this._calculateDisabled(); } /** * {void} */ showVisibleUsers() { - this.visibleUsers = this.users.slice(this.currentStep, this.currentStep + USERS_COUNT); + const tmp = []; + + for (let i = this.offset; i < this.offset + USERS_COUNT; i++) { + tmp.push(this._users[i]); + } + + this.visibleUsers = tmp; } /** @@ -262,18 +293,62 @@ return index !== 0 && this.selectDefault; } + /** + * @public + */ + onChangeId() { + let id = parseInt(this.id, 10); + + if (isNaN(id) || id < 0) { + id = 0; + } + + if (id > MAX_USER_COUNT) { + id = MAX_USER_COUNT; + } + + this.id = String(id); + + this.offset = this._normalizeOffset(id - Math.floor(USERS_COUNT / 2)); + + if (this._hasUsersInCache(this.offset, this.offset + USERS_COUNT - 1)) { + this.showVisibleUsers(); + this.selectUser(this._users[id]); + } else { + this.getUsers().then(() => { + this.selectUser(this._users[id]); + }); + } + } + _calculateDisabled(disable) { - // const limitRight = this.users.length < this.currentStep + USERS_COUNT + 1; - this.disabledLogin = disable || this.loading || !this.selectedUser; - this.disabledRight = disable || this.selectDefault || this.loading; - this.disabledLeft = disable || this.selectDefault || this.loading || this.currentStep === 0; + this.disabledLogin = ( + disable || + this.loading || + !this.selectedUser + ); + + this.disabledRight = ( + disable || + this.selectDefault || + this.loading || + this.offset === MAX_USER_COUNT - (USERS_COUNT - 1) + ); + + this.disabledLeft = ( + disable || + this.selectDefault || + this.loading || + this.offset === 0 + ); } _onChangeSelectDefault() { if (this.selectDefault) { - this.currentStep = 0; - this.selectedUser = this.users[0]; + this.offset = 0; + this.selectedUser = this._users[0]; } + this._calculateDisabled(); this.showVisibleUsers(); } @@ -282,6 +357,7 @@ * @private */ _onSelectUser() { + this.id = this.selectedUser.id; this.userExisted = this._usersInStorage.find(user => user.address === this.selectedUser.address) || null; @@ -300,6 +376,37 @@ this.importForm.userName.$setValidity('isUnique', !isUnique); } + /** + * @private + * @param {number} from + * @param {number} to + */ + _hasUsersInCache(from, to) { + for (let i = from; i <= to; i++) { + if (!this._users[i]) { + return false; + } + } + + return true; + } + + /** + * @private + * @param {number} offset + */ + _normalizeOffset(offset) { + if (offset > MAX_USER_COUNT - (USERS_COUNT - 1)) { + return MAX_USER_COUNT - (USERS_COUNT - 1); + } + + if (offset < 0) { + return 0; + } + + return offset; + } + } return new LedgerCtrl(); diff --git a/src/modules/ledger/templates/ledger.html b/src/modules/ledger/templates/ledger.html index 16ebbe5860..da7d90bc3c 100644 --- a/src/modules/ledger/templates/ledger.html +++ b/src/modules/ledger/templates/ledger.html @@ -44,15 +44,20 @@

-
-
- + +
+
+ +
@@ -66,30 +71,61 @@

-
- - -
-
-
- - - - - - - +
+
+ + +
+
+
+ + + + + +
+ +
+ + + +
+ +
+
+ + + + + + + + +
diff --git a/src/modules/ui/directives/input/input.less b/src/modules/ui/directives/input/input.less index 3d4a0b8896..3f9894f258 100644 --- a/src/modules/ui/directives/input/input.less +++ b/src/modules/ui/directives/input/input.less @@ -276,13 +276,13 @@ md-dialog { input[type="number"], textarea { background: @color-basic-50; - + &:not(.no-validate).ng-touched.ng-valid:not(:focus):not(.focus):not(.ng-empty) { border-color: @color-basic-200; } } } - + .input-like { background: @color-basic-50; } diff --git a/src/modules/utils/modals/settings/SettingsCtrl.js b/src/modules/utils/modals/settings/SettingsCtrl.js index a690f9ba93..a538543728 100644 --- a/src/modules/utils/modals/settings/SettingsCtrl.js +++ b/src/modules/utils/modals/settings/SettingsCtrl.js @@ -59,6 +59,7 @@ tab = 'general'; address = user.address; publicKey = user.publicKey; + id = user.id; shownSeed = false; shownKey = false; node = ''; diff --git a/src/modules/utils/modals/settings/settings.html b/src/modules/utils/modals/settings/settings.html index 76c9cbc9a3..59a16bb663 100644 --- a/src/modules/utils/modals/settings/settings.html +++ b/src/modules/utils/modals/settings/settings.html @@ -159,11 +159,21 @@
{{::$ctrl.publicKey}}
-
+
{{::$ctrl.address}}
+
+
+
{{::$ctrl.id}}
+
+
From efe4f5cb842e02cbef29310c6a65b189f83a58b0 Mon Sep 17 00:00:00 2001 From: finico Date: Fri, 4 Oct 2019 15:14:31 +0300 Subject: [PATCH 2/3] DEXW-1623: Fix race id vs selectedUser.id --- src/modules/ledger/controllers/LedgerCtrl.js | 3 ++- src/modules/utils/modals/settings/settings.html | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/modules/ledger/controllers/LedgerCtrl.js b/src/modules/ledger/controllers/LedgerCtrl.js index 113e36cd10..4c0d325bc4 100644 --- a/src/modules/ledger/controllers/LedgerCtrl.js +++ b/src/modules/ledger/controllers/LedgerCtrl.js @@ -297,7 +297,7 @@ * @public */ onChangeId() { - let id = parseInt(this.id, 10); + let id = parseInt(this.selectDefault ? this.selectedUser.id : this.id, 10); if (isNaN(id) || id < 0) { id = 0; @@ -347,6 +347,7 @@ if (this.selectDefault) { this.offset = 0; this.selectedUser = this._users[0]; + this.id = this.selectedUser.id; } this._calculateDisabled(); diff --git a/src/modules/utils/modals/settings/settings.html b/src/modules/utils/modals/settings/settings.html index 59a16bb663..6eec304c08 100644 --- a/src/modules/utils/modals/settings/settings.html +++ b/src/modules/utils/modals/settings/settings.html @@ -168,7 +168,7 @@
{{::$ctrl.id}}
From 616d42fecbbc7a2d7de3d01bd44638a1abf94fbd Mon Sep 17 00:00:00 2001 From: finico Date: Fri, 4 Oct 2019 16:35:22 +0300 Subject: [PATCH 3/3] DEXW-1623: Fix avatar's className --- src/modules/ledger/templates/ledger.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/ledger/templates/ledger.html b/src/modules/ledger/templates/ledger.html index da7d90bc3c..f8c24ab760 100644 --- a/src/modules/ledger/templates/ledger.html +++ b/src/modules/ledger/templates/ledger.html @@ -46,7 +46,7 @@

-