file api working. Migration script missing

This commit is contained in:
Matias Alejo Garcia 2015-04-24 16:39:12 -03:00
parent a8f882472d
commit 1f6596a5ad
9 changed files with 180 additions and 50 deletions

View File

@ -126,6 +126,9 @@ if [ ! -d $PROJECT ]; then
cordova plugin add hu.dpal.phonegap.plugins.uniquedeviceid
checkOK
cordova plugin add org.apache.cordova.file
checkOK
fi
if $DBGJS

View File

@ -12,6 +12,9 @@
</author>
<content src="index.html" />
<access origin="*" />
<preference name="AndroidPersistentFileLocation" value="Internal" />
<preference name="iosPersistentFileLocation" value="Library" />
<preference name="DisallowOverscroll" value="true"/>
<preference name="HideKeyboardFormAccessoryBar" value="true"/>
<preference name="SplashScreen" value="copayscreen" />

View File

@ -426,6 +426,19 @@ angular.module('copayApp.controllers').controller('indexController', function($r
}
};
self.deviceError = function (err) {
if (isCordova) {
navigator.notification.confirm(
err,
function() {},
'Device Error', ['OK']
);
} else {
alert(err);
}
};
self.recreate = function(cb) {
var fc = profileService.focusedClient;
self.setOngoingProcess('recreating', true);
@ -563,6 +576,11 @@ angular.module('copayApp.controllers').controller('indexController', function($r
$rootScope.$apply();
});
$rootScope.$on('Local/DeviceError', function(event, err) {
self.deviceError(err);
$rootScope.$apply();
});
$rootScope.$on('Local/ClientError', function(event, err) {
if (err.code && err.code === 'NOTAUTHORIZED') {
// Show not error, just redirect to home (where the recreate option is shown)

View File

@ -434,18 +434,21 @@ angular
if (!profileService.profile && toState.needProfile) {
// Give us time to open / create the profile
event.preventDefault();
// Try to open local profile
profileService.loadAndBindProfile(function(err) {
if (err) {
if (err.message.match('NOPROFILE')) {
$log.debug('No profile... redirecting');
$state.transitionTo('splash');
event.preventDefault();
} else {
throw new Error(err); // TODO
}
} else {
// Profile was loaded
console.log('Profile loaded ... resuming transition');
$state.transitionTo('walletHome');
}
});
}

View File

@ -1,8 +1,10 @@
'use strict';
angular.module('copayApp.services').factory('configService', function(localStorageService, lodash, bwcService) {
angular.module('copayApp.services').factory('configService', function(localStorageService, fileStorageService, isCordova, lodash, bwcService) {
var root = {};
var storage = isCordova ? fileStorageService : localStorageService;
var defaultConfig = {
// wallet limits
limits: {
@ -66,8 +68,7 @@ angular.module('copayApp.services').factory('configService', function(localStora
};
root.get = function(cb) {
localStorageService.get('config', function(err, localConfig) {
storage.get('config', function(err, localConfig) {
if (localConfig) {
configCache = JSON.parse(localConfig);
@ -89,7 +90,7 @@ angular.module('copayApp.services').factory('configService', function(localStora
root.set = function(newOpts, cb) {
var config = defaultConfig;
localStorageService.get('config', function(err, oldOpts) {
storage.get('config', function(err, oldOpts) {
if (lodash.isString(oldOpts)) {
oldOpts = JSON.parse(oldOpts);
}
@ -102,12 +103,12 @@ angular.module('copayApp.services').factory('configService', function(localStora
lodash.merge(config, oldOpts, newOpts);
configCache = config;
localStorageService.set('config', JSON.stringify(config), cb);
storage.set('config', JSON.stringify(config), cb);
});
};
root.reset = function(cb) {
localStorageService.remove('config', cb);
storage.remove('config', cb);
};
root.getDefaults = function() {

View File

@ -0,0 +1,117 @@
'use strict';
angular.module('copayApp.services')
.factory('fileStorageService', function(lodash, $log) {
var root = {}, fs;
root.init = function(cb) {
if (fs) return cb(null, fs);
function onFileSystemSuccess(fileSystem) {
console.log('File system started: ', fileSystem.name, fileSystem.root.name);
fs = fileSystem;
return cb(null, fs);
}
function fail(evt) {
var msg = 'Could not init file system: ' + evt.target.error.code;
console.log(msg);
return cb(msg);
};
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);
};
root.get = function(k, cb) {
root.init(function(err, fs) {
if (err) return cb(err);
fs.root.getFile(k, {
create: false,
}, function(fileEntry) {
if (!fileEntry) return cb();
fileEntry.file(function(file) {
var reader = new FileReader();
reader.onloadend = function(e) {
console.log("Read: " + this.result);
return cb(null, this.result)
}
reader.readAsText(file);
});
}, function(err) {
// Not found
if (err.code==1) return cb();
else return cb(err);
});
})
};
root.set = function(k, v, cb) {
root.init(function(err, fs) {
if (err) return cb(err);
fs.root.getFile(k, {
create: true,
}, function(fileEntry) {
// Create a FileWriter object for our FileEntry (log.txt).
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) {
console.log('Write completed.');
return cb();
};
fileWriter.onerror = function(e) {
console.log('Write failed: ' + e.toString());
return cb('Fail to write:', e.toString());
};
if (lodash.isObject(v))
v = JSON.stringify(v);
var blob = new Blob([v], {
type: "application/json"
});
fileWriter.write(blob);
}, cb);
});
});
};
root.remove = function(k, cb) {
root.init(function(err, fs) {
if (err) return cb(err);
fs.root.getFile(k, {
create: false,
}, function(fileEntry) {
// Create a FileWriter object for our FileEntry (log.txt).
fileEntry.remove(function() {
console.log('File removed.');
return cb();
}, cb, cb);
});
});
};
/**
* Same as setItem, but fails if an item already exists
*/
root.create = function(name, value, callback) {
root.get(name,
function(err, data) {
if (data) {
return callback('EEXISTS');
} else {
return root.set(name, value, callback);
}
});
};
return root;
});

View File

@ -1,12 +1,9 @@
'use strict';
angular.module('copayApp.services')
.factory('localStorageService', function() {
var isChromeApp = typeof window !== "undefined" && window.chrome && chrome.runtime && chrome.runtime.id;
.factory('localStorageService', function(isChromeApp, $timeout) {
var root = {};
var ls = ((typeof localStorage !== "undefined") ? localStorage : null);
var ls = ((typeof window.localStorage !== "undefined") ? window.localStorage : null);
if (isChromeApp && !ls) {
ls = localStorage = chrome.storage.local;
@ -14,9 +11,7 @@ angular.module('copayApp.services')
}
if (!ls)
throw new Error('localstorage not available, cannot run plugin');
root.init = function() {};
throw new Error('localstorage not available');
root.get = function(k, cb) {
if (isChromeApp) {
@ -26,7 +21,13 @@ angular.module('copayApp.services')
return cb(null, data[k]);
});
} else {
console.log('[localStorage.js.24]',k); //TODO
console.log('[localStorage.js.25:TODO:]'); //TODO
console.log('[localStorage.js.26:TODO:]'); //TODO
console.log('[localStorage.js.27:TODO:]'); //TODO
$timeout(function() {
return cb(null, ls.getItem(k));
}, 1000);
}
};
@ -67,26 +68,5 @@ angular.module('copayApp.services')
};
root.clear = function(cb) {
// NOP
return cb();
};
root.list = function(cb) {
if (isChromeApp) {
chrome.storage.local.get(null, function(items) {
return cb(null, lodash.keys(items));
});
} else {
var ret = [];
var l = ls.length;
for (var i = 0; i < l; i++)
ret.push(ls.key(i));
return cb(null, ret);
}
};
return root;
});

View File

@ -111,6 +111,8 @@ angular.module('copayApp.services')
root.bindProfile = function(profile, cb) {
console.log('[profileService.js.114] Bind profile', profile); //TODO
root.profile = profile;
configService.get(function(err) {
@ -127,12 +129,14 @@ angular.module('copayApp.services')
root.loadAndBindProfile = function(cb) {
storageService.getProfile(function(err, profile) {
console.log('[profileService.js.129:err:]',err); //TODO
if (err) {
notification.error('CRITICAL ERROR: ' + err);
$rootScope.$emit('Local/DeviceError', err);
return cb(err);
}
if (!profile) return cb(new Error('NOPROFILE: No profile'));
console.log('[profileService.js.135] BIND'); //TODO
return root.bindProfile(profile, cb);
});
};

View File

@ -1,8 +1,9 @@
'use strict';
angular.module('copayApp.services')
.factory('storageService', function(localStorageService, sjcl, $log, lodash) {
.factory('storageService', function(fileStorageService, localStorageService, sjcl, $log, lodash, isCordova) {
var root = {};
var storage = isCordova ? fileStorageService : localStorageService;
var getUUID = function(cb) {
// TO SIMULATE MOBILE
@ -48,18 +49,18 @@ angular.module('copayApp.services')
root.storeNewProfile = function(profile, cb) {
encryptOnMobile(profile.toObj(), function(err, x) {
localStorageService.create('profile', x, cb);
storage.create('profile', x, cb);
});
};
root.storeProfile = function(profile, cb) {
encryptOnMobile(profile.toObj(), function(err, x) {
localStorageService.set('profile', x, cb);
storage.set('profile', x, cb);
});
};
root.getProfile = function(cb) {
localStorageService.get('profile', function(err, str) {
storage.get('profile', function(err, str) {
if (err || !str) return cb(err);
decryptOnMobile(str, function(err, str) {
@ -76,35 +77,35 @@ angular.module('copayApp.services')
};
root.deleteProfile = function(cb) {
localStorageService.remove('profile', cb);
storage.remove('profile', cb);
};
root.storeFocusedWalletId = function(id, cb) {
localStorageService.set('focusedWalletId', id, cb);
storage.set('focusedWalletId', id, cb);
};
root.getFocusedWalletId = function(cb) {
localStorageService.get('focusedWalletId', cb);
storage.get('focusedWalletId', cb);
};
root.getLastAddress = function(walletId, cb) {
localStorageService.get('lastAddress-' + walletId, cb);
storage.get('lastAddress-' + walletId, cb);
};
root.storeLastAddress = function(walletId, address, cb) {
localStorageService.set('lastAddress-' + walletId, address, cb);
storage.set('lastAddress-' + walletId, address, cb);
};
root.clearLastAddress = function(walletId, cb) {
localStorageService.remove('lastAddress-' + walletId, cb);
storage.remove('lastAddress-' + walletId, cb);
};
root.setBackupFlag = function(walletId, cb) {
localStorageService.set('backup-' + walletId, Date.now(), cb);
storage.set('backup-' + walletId, Date.now(), cb);
};
root.getBackupFlag = function(walletId, cb) {
localStorageService.get('backup-' + walletId, cb);
storage.get('backup-' + walletId, cb);
};
return root;