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();
}
// Check Profile
this.profile.get().then((profile: any) => {
this.profile.loadAndBindProfile().then((profile: any) => {
if (profile) {
this.logger.info('Profile read. Go to HomePage.');
this.openLockModal();
@ -58,6 +58,8 @@ export class CopayApp {
this.logger.warn('Profile does not exist. Go to Onboarding.');
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

@ -17,19 +17,20 @@ export class HomePage {
private release: ReleaseProvider,
) {
this.release.getLatestAppVersion()
.catch((err) => {
console.log('Error:', err)})
.then((version) => {
console.log('Current app version:',version);
var result = this.release.checkForUpdates(version);
console.log('Update available:', result.updateAvailable);
});
.catch((err) => {
console.log('Error:', err)
})
.then((version) => {
console.log('Current app version:', version);
var result = this.release.checkForUpdates(version);
console.log('Update available:', result.updateAvailable);
});
}
ionViewDidLoad() {
console.log('ionViewDidLoad HomePage');
this.wallets = this.profile.bind();
this.wallets = this.profile.getWallets();
console.log('[home.ts:20]', this.wallets); //TODO
}

View File

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

View File

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

File diff suppressed because it is too large Load Diff