2017-01-30 21:42:24 +01:00
|
|
|
const ObservableStore = require('obs-store')
|
2017-02-21 23:25:47 +01:00
|
|
|
const normalizeAddress = require('eth-sig-util').normalize
|
2017-02-23 00:12:56 +01:00
|
|
|
const extend = require('xtend')
|
2017-01-30 21:42:24 +01:00
|
|
|
|
|
|
|
class PreferencesController {
|
|
|
|
|
|
|
|
constructor (opts = {}) {
|
2017-03-09 22:07:38 +01:00
|
|
|
const initState = extend({
|
|
|
|
frequentRpcList: [],
|
2017-06-14 23:21:50 +02:00
|
|
|
currentAccountTab: 'history',
|
2017-06-16 01:22:53 +02:00
|
|
|
tokens: [],
|
2017-11-27 16:11:48 +01:00
|
|
|
useBlockie: false,
|
2017-11-14 17:04:55 +01:00
|
|
|
featureFlags: {},
|
2018-03-16 01:29:45 +01:00
|
|
|
currentLocale: 'ja',
|
2017-03-09 22:07:38 +01:00
|
|
|
}, opts.initState)
|
2017-01-30 21:42:24 +01:00
|
|
|
this.store = new ObservableStore(initState)
|
|
|
|
}
|
2017-06-16 01:22:53 +02:00
|
|
|
// PUBLIC METHODS
|
2017-01-30 21:42:24 +01:00
|
|
|
|
2017-11-24 18:35:17 +01:00
|
|
|
setUseBlockie (val) {
|
|
|
|
this.store.updateState({ useBlockie: val })
|
2017-11-24 02:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getUseBlockie () {
|
|
|
|
return this.store.getState().useBlockie
|
|
|
|
}
|
|
|
|
|
2018-03-16 01:29:45 +01:00
|
|
|
setCurrentLocale (key) {
|
|
|
|
this.store.updateState({ currentLocale: key })
|
|
|
|
}
|
|
|
|
|
2017-02-21 21:32:13 +01:00
|
|
|
setSelectedAddress (_address) {
|
2017-01-31 00:08:31 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const address = normalizeAddress(_address)
|
|
|
|
this.store.updateState({ selectedAddress: address })
|
|
|
|
resolve()
|
|
|
|
})
|
2017-01-30 21:42:24 +01:00
|
|
|
}
|
|
|
|
|
2017-09-27 23:43:23 +02:00
|
|
|
getSelectedAddress () {
|
2017-01-30 21:42:24 +01:00
|
|
|
return this.store.getState().selectedAddress
|
|
|
|
}
|
|
|
|
|
2017-12-19 00:57:04 +01:00
|
|
|
async addToken (rawAddress, symbol, decimals) {
|
2017-06-16 01:22:53 +02:00
|
|
|
const address = normalizeAddress(rawAddress)
|
|
|
|
const newEntry = { address, symbol, decimals }
|
|
|
|
|
|
|
|
const tokens = this.store.getState().tokens
|
2017-12-19 00:57:04 +01:00
|
|
|
const previousEntry = tokens.find((token, index) => {
|
2017-06-16 01:22:53 +02:00
|
|
|
return token.address === address
|
|
|
|
})
|
2017-12-19 00:57:04 +01:00
|
|
|
const previousIndex = tokens.indexOf(previousEntry)
|
2017-06-16 01:22:53 +02:00
|
|
|
|
2017-12-19 00:57:04 +01:00
|
|
|
if (previousEntry) {
|
2017-06-16 01:22:53 +02:00
|
|
|
tokens[previousIndex] = newEntry
|
|
|
|
} else {
|
|
|
|
tokens.push(newEntry)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.store.updateState({ tokens })
|
2017-12-20 23:46:12 +01:00
|
|
|
|
2017-10-03 19:25:52 +02:00
|
|
|
return Promise.resolve(tokens)
|
|
|
|
}
|
|
|
|
|
|
|
|
removeToken (rawAddress) {
|
|
|
|
const tokens = this.store.getState().tokens
|
|
|
|
|
|
|
|
const updatedTokens = tokens.filter(token => token.address !== rawAddress)
|
|
|
|
|
|
|
|
this.store.updateState({ tokens: updatedTokens })
|
|
|
|
return Promise.resolve(updatedTokens)
|
2017-06-16 01:22:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
getTokens () {
|
|
|
|
return this.store.getState().tokens
|
|
|
|
}
|
|
|
|
|
2017-02-23 22:56:58 +01:00
|
|
|
updateFrequentRpcList (_url) {
|
|
|
|
return this.addToFrequentRpcList(_url)
|
|
|
|
.then((rpcList) => {
|
|
|
|
this.store.updateState({ frequentRpcList: rpcList })
|
2017-03-08 01:01:51 +01:00
|
|
|
return Promise.resolve()
|
2017-02-23 22:56:58 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-14 23:21:50 +02:00
|
|
|
setCurrentAccountTab (currentAccountTab) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.store.updateState({ currentAccountTab })
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-02-21 21:51:46 +01:00
|
|
|
addToFrequentRpcList (_url) {
|
2017-04-27 06:05:45 +02:00
|
|
|
const rpcList = this.getFrequentRpcList()
|
|
|
|
const index = rpcList.findIndex((element) => { return element === _url })
|
2017-02-23 00:12:56 +01:00
|
|
|
if (index !== -1) {
|
|
|
|
rpcList.splice(index, 1)
|
|
|
|
}
|
|
|
|
if (_url !== 'http://localhost:8545') {
|
2017-02-21 21:32:13 +01:00
|
|
|
rpcList.push(_url)
|
2017-02-23 00:12:56 +01:00
|
|
|
}
|
|
|
|
if (rpcList.length > 2) {
|
|
|
|
rpcList.shift()
|
|
|
|
}
|
2017-02-23 22:56:58 +01:00
|
|
|
return Promise.resolve(rpcList)
|
2017-02-21 21:32:13 +01:00
|
|
|
}
|
|
|
|
|
2017-02-21 21:51:46 +01:00
|
|
|
getFrequentRpcList () {
|
2017-02-23 00:12:56 +01:00
|
|
|
return this.store.getState().frequentRpcList
|
2017-02-21 21:32:13 +01:00
|
|
|
}
|
2017-11-14 17:04:55 +01:00
|
|
|
|
|
|
|
setFeatureFlag (feature, activated) {
|
|
|
|
const currentFeatureFlags = this.store.getState().featureFlags
|
|
|
|
const updatedFeatureFlags = {
|
|
|
|
...currentFeatureFlags,
|
|
|
|
[feature]: activated,
|
|
|
|
}
|
|
|
|
|
|
|
|
this.store.updateState({ featureFlags: updatedFeatureFlags })
|
2017-11-16 20:28:59 +01:00
|
|
|
|
2017-11-14 17:04:55 +01:00
|
|
|
return Promise.resolve(updatedFeatureFlags)
|
|
|
|
}
|
|
|
|
|
|
|
|
getFeatureFlags () {
|
|
|
|
return this.store.getState().featureFlags
|
|
|
|
}
|
2017-01-30 21:42:24 +01:00
|
|
|
//
|
|
|
|
// PRIVATE METHODS
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = PreferencesController
|