-
Notifications
You must be signed in to change notification settings - Fork 3
/
AccountHelper.js
55 lines (41 loc) · 1.72 KB
/
AccountHelper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { ACCOUNT_INDEXES_DELIMETER, ACCOUNT_INDEXES_KEY, ACTIVE_ACCOUNT_INDEX_KEY } from "./constants";
const { getItem, setItem } = require('./Storage');
getAccountIndexesArray = async () => {
const accountIndexesString = await getItem(ACCOUNT_INDEXES_KEY);
if (!accountIndexesString) {
return [];
}
return accountIndexesString.split(ACCOUNT_INDEXES_DELIMETER);
}
setAccountIndexesArray = async (nextAccountIndex) => {
let currentAccountIndexesArray = await getAccountIndexesArray();
currentAccountIndexesArray.push(nextAccountIndex);
setItem(ACCOUNT_INDEXES_KEY, currentAccountIndexesArray.join(ACCOUNT_INDEXES_DELIMETER));
}
getActiveAccountIndexFromStorage = async () => {
return (await getItem(ACTIVE_ACCOUNT_INDEX_KEY));
}
setActiveAccountIndexToStorage = (activeAccountIndex) => {
setItem(ACTIVE_ACCOUNT_INDEX_KEY, activeAccountIndex.toString());
}
getNextAccountIndex = async () => {
const accountIndexesArray = await getAccountIndexesArray();
const length = accountIndexesArray.length;
if (length == 0) {
return 0;
}
return (parseInt(accountIndexesArray[length -1]) + 1);
}
handleActiveAccountIndex = async (setActiveAccountIndex) => {
const activeAccountIndexFromStorage = await getActiveAccountIndexFromStorage();
setActiveAccountIndex(activeAccountIndexFromStorage ? activeAccountIndexFromStorage : 0)
}
handleAccountIndexesArray = async (setAccountIndexesArray) => {
setAccountIndexesArray(await getAccountIndexesArray());
}
module.exports = {
getAccountIndexesArray, setAccountIndexesArray,
getActiveAccountIndexFromStorage, setActiveAccountIndexToStorage,
getNextAccountIndex,
handleActiveAccountIndex, handleAccountIndexesArray
}