diff options
author | Dan Finlay <flyswatter@users.noreply.github.com> | 2017-03-17 01:21:21 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-17 01:21:21 +0800 |
commit | 00f1a2d78d01301f51157e9c8b8352dc0796d034 (patch) | |
tree | 91d9cbb8baa26efa462a86e9ecda460792f90996 /app/scripts/controllers | |
parent | 570cc891b5386dc04462737de4df6f2adf5059c9 (diff) | |
parent | a186e40d179d230fc1e81f7f507a5232d23ad462 (diff) | |
download | tangerine-wallet-browser-00f1a2d78d01301f51157e9c8b8352dc0796d034.tar tangerine-wallet-browser-00f1a2d78d01301f51157e9c8b8352dc0796d034.tar.gz tangerine-wallet-browser-00f1a2d78d01301f51157e9c8b8352dc0796d034.tar.bz2 tangerine-wallet-browser-00f1a2d78d01301f51157e9c8b8352dc0796d034.tar.lz tangerine-wallet-browser-00f1a2d78d01301f51157e9c8b8352dc0796d034.tar.xz tangerine-wallet-browser-00f1a2d78d01301f51157e9c8b8352dc0796d034.tar.zst tangerine-wallet-browser-00f1a2d78d01301f51157e9c8b8352dc0796d034.zip |
Merge pull request #1198 from MetaMask/i1165-predictive
Add predictive address functionality to recipient field in send
Diffstat (limited to 'app/scripts/controllers')
-rw-r--r-- | app/scripts/controllers/address-book.js | 79 | ||||
-rw-r--r-- | app/scripts/controllers/preferences.js | 4 |
2 files changed, 82 insertions, 1 deletions
diff --git a/app/scripts/controllers/address-book.js b/app/scripts/controllers/address-book.js new file mode 100644 index 000000000..c66eb2bd4 --- /dev/null +++ b/app/scripts/controllers/address-book.js @@ -0,0 +1,79 @@ +const ObservableStore = require('obs-store') +const extend = require('xtend') + +class AddressBookController { + + + // Controller in charge of managing the address book functionality from the + // recipients field on the send screen. Manages a history of all saved + // addresses and all currently owned addresses. + constructor (opts = {}, keyringController) { + const initState = extend({ + addressBook: [], + }, opts.initState) + this.store = new ObservableStore(initState) + this.keyringController = keyringController + } + + // + // PUBLIC METHODS + // + + // Sets a new address book in store by accepting a new address and nickname. + setAddressBook (address, name) { + return this._addToAddressBook(address, name) + .then((addressBook) => { + this.store.updateState({ + addressBook, + }) + return Promise.resolve() + }) + } + + // + // PRIVATE METHODS + // + + + // Performs the logic to add the address and name into the address book. The + // pushed object is an object of two fields. Current behavior does not set an + // upper limit to the number of addresses. + _addToAddressBook (address, name) { + let addressBook = this._getAddressBook() + let identities = this._getIdentities() + + let addressBookIndex = addressBook.findIndex((element) => { return element.address.toLowerCase() === address.toLowerCase() || element.name === name }) + let identitiesIndex = Object.keys(identities).findIndex((element) => { return element.toLowerCase() === address.toLowerCase() }) + // trigger this condition if we own this address--no need to overwrite. + if (identitiesIndex !== -1) { + return Promise.resolve(addressBook) + // trigger this condition if we've seen this address before--may need to update nickname. + } else if (addressBookIndex !== -1) { + addressBook.splice(addressBookIndex, 1) + } else if (addressBook.length > 15) { + addressBook.shift() + } + + + addressBook.push({ + address: address, + name, + }) + return Promise.resolve(addressBook) + } + + // Internal method to get the address book. Current persistence behavior + // should not require that this method be called from the UI directly. + _getAddressBook () { + return this.store.getState().addressBook + } + + // Retrieves identities from the keyring controller in order to avoid + // duplication + _getIdentities () { + return this.keyringController.memStore.getState().identities + } + +} + +module.exports = AddressBookController diff --git a/app/scripts/controllers/preferences.js b/app/scripts/controllers/preferences.js index 18fccf11b..c7f675a41 100644 --- a/app/scripts/controllers/preferences.js +++ b/app/scripts/controllers/preferences.js @@ -5,7 +5,9 @@ const extend = require('xtend') class PreferencesController { constructor (opts = {}) { - const initState = extend({ frequentRpcList: [] }, opts.initState) + const initState = extend({ + frequentRpcList: [], + }, opts.initState) this.store = new ObservableStore(initState) } |