Feat: Profile provider v4

This commit is contained in:
Gabriel Masclef 2017-10-18 17:08:13 -03:00
parent 7a051dc663
commit 51c0c36357
No known key found for this signature in database
GPG Key ID: DD6D7EAADE12280D
6 changed files with 1223 additions and 70 deletions

View File

@ -48,7 +48,7 @@ export class CopayApp {
this.splashScreen.hide(); this.splashScreen.hide();
} }
// Check Profile // Check Profile
this.profile.get().then((profile: any) => { this.profile.loadAndBindProfile().then((profile: any) => {
if (profile) { if (profile) {
this.logger.info('Profile read. Go to HomePage.'); this.logger.info('Profile read. Go to HomePage.');
this.openLockModal(); this.openLockModal();
@ -58,6 +58,8 @@ export class CopayApp {
this.logger.warn('Profile does not exist. Go to Onboarding.'); this.logger.warn('Profile does not exist. Go to Onboarding.');
this.rootPage = OnboardingPage; this.rootPage = OnboardingPage;
} }
}).catch((err: any) => {
console.log(err);
}); });
}); });
} }

View File

@ -0,0 +1,122 @@
import * as moment from 'moment';
export class Profile {
public version: string;
public createdOn: Number;
public credentials: Array<any>;
public disclaimerAccepted: boolean;
public checked: Object;
public checkedUA?: any;
public dirty: boolean;
constructor() {
this.version = '1.0.0';
}
public create(opts?: any): Profile {
opts = opts ? opts : {};
let x = new Profile();
x.createdOn = Date.now();
x.credentials = opts.credentials || [];
x.disclaimerAccepted = false;
x.checked = {};
return x;
};
public fromObj(obj: any): Profile {
let x = new Profile();
x.createdOn = obj.createdOn;
x.credentials = obj.credentials;
x.disclaimerAccepted = obj.disclaimerAccepted;
x.checked = obj.checked || {};
x.checkedUA = obj.checkedUA || {};
if (x.credentials[0] && typeof x.credentials[0] != 'object')
throw ("credentials should be an object");
return x;
};
public fromString(str: string): Profile {
return this.fromObj(JSON.parse(str));
};
public toObj(): string {
delete this.dirty;
return JSON.stringify(this);
};
public hasWallet(walletId: string): boolean {
for (let i in this.credentials) {
let c = this.credentials[i];
if (c.walletId == walletId) return true;
};
return false;
};
public isChecked(ua: any, walletId: string): boolean {
return !!(this.checkedUA == ua && this.checked[walletId]);
};
public isDeviceChecked(ua: any): boolean {
return this.checkedUA == ua;
};
public setChecked(ua: any, walletId: string): void {
if (this.checkedUA != ua) {
this.checkedUA = ua;
this.checked = {};
}
this.checked[walletId] = true;
this.dirty = true;
};
public addWallet(credentials: any): boolean {
if (!credentials.walletId)
throw 'credentials must have .walletId';
if (this.hasWallet(credentials.walletId))
return false;
this.credentials.push(credentials);
this.dirty = true;
return true;
};
public updateWallet(credentials: any): boolean {
if (!credentials.walletId)
throw 'credentials must have .walletId';
if (!this.hasWallet(credentials.walletId))
return false;
this.credentials = this.credentials.map((c: any) => {
if (c.walletId != credentials.walletId) {
return c;
} else {
return credentials
}
});
this.dirty = true;
return true;
};
public deleteWallet(walletId: string): boolean {
if (!this.hasWallet(walletId))
return false;
this.credentials = this.credentials.filter(function (c) {
return c.walletId != walletId;
});
this.dirty = true;
return true;
};
}

View File

@ -18,7 +18,8 @@ export class HomePage {
) { ) {
this.release.getLatestAppVersion() this.release.getLatestAppVersion()
.catch((err) => { .catch((err) => {
console.log('Error:', err)}) console.log('Error:', err)
})
.then((version) => { .then((version) => {
console.log('Current app version:', version); console.log('Current app version:', version);
var result = this.release.checkForUpdates(version); var result = this.release.checkForUpdates(version);
@ -29,7 +30,7 @@ export class HomePage {
ionViewDidLoad() { ionViewDidLoad() {
console.log('ionViewDidLoad HomePage'); console.log('ionViewDidLoad HomePage');
this.wallets = this.profile.bind(); this.wallets = this.profile.getWallets();
console.log('[home.ts:20]', this.wallets); //TODO console.log('[home.ts:20]', this.wallets); //TODO
} }

View File

@ -26,7 +26,7 @@ export class OnboardingPage {
createProfile() { createProfile() {
// TODO: create a new profile // TODO: create a new profile
this.profile.create(); this.profile.createProfile();
} }
getStarted() { getStarted() {

View File

@ -4,7 +4,7 @@ import { Events } from 'ionic-angular';
import { PersistenceProvider } from '../persistence/persistence'; import { PersistenceProvider } from '../persistence/persistence';
import { PlatformProvider } from '../platform/platform'; import { PlatformProvider } from '../platform/platform';
import * as _ from "lodash"; import * as lodash from "lodash";
@Injectable() @Injectable()
export class ConfigProvider { export class ConfigProvider {
@ -110,30 +110,34 @@ export class ConfigProvider {
public load() { public load() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.persistence.getConfig().then((config: object) => { this.persistence.getConfig().then((config: object) => {
if (!_.isEmpty(config)) this.configCache = _.clone(config); if (!lodash.isEmpty(config)) this.configCache = lodash.clone(config);
else this.configCache = _.clone(this.configDefault); else this.configCache = lodash.clone(this.configDefault);
resolve(this.configCache); resolve(this.configCache);
}); });
}); });
} }
set(newOpts: object) { public set(newOpts: object) {
let config = _.cloneDeep(this.configDefault); let config = lodash.cloneDeep(this.configDefault);
if (_.isString(newOpts)) { if (lodash.isString(newOpts)) {
newOpts = JSON.parse(newOpts); newOpts = JSON.parse(newOpts);
} }
_.merge(config, this.configCache, newOpts); lodash.merge(config, this.configCache, newOpts);
this.configCache = config; this.configCache = config;
this.events.publish('config:updated', this.configCache);
this.persistence.storeConfig(this.configCache).then(() => { this.persistence.storeConfig(this.configCache).then(() => {
this.logger.info('Config saved'); this.logger.info('Config saved');
}); });
this.events.publish('config:updated', this.configCache);
} }
get() { public get(): Object {
return this.configCache; return this.configCache;
} }
public getDefaults(): Object {
return this.configDefault;
}
} }

File diff suppressed because it is too large Load Diff