Skip to content

Instantly share code, notes, and snippets.

@AppWerft
Created December 4, 2015 08:58
Show Gist options
  • Save AppWerft/51afbe66f2b3e2bf1320 to your computer and use it in GitHub Desktop.
Save AppWerft/51afbe66f2b3e2bf1320 to your computer and use it in GitHub Desktop.
const FOLDER = '3DModelsCache';
if (Ti.Filesystem.isExternalStoragePresent())
var DEPOT = Ti.Filesystem.externalStorageDirectory;
else
var DEPOT = Ti.Filesystem.applicationDataDirectory;
var folder = Ti.Filesystem.getFile(DEPOT, FOLDER);
if (!folder.exists()) {
folder.createDirectory();
}
/* Adapter object */
var Adapter = function(modelurls) {
this.eventhandlers = {};
var _this = this;
_this.models = [];
if (modelurls && Array.isArray(modelurls)) {
modelurls.forEach(function(url) {
var file = Ti.Filesystem.getFile(DEPOT, FOLDER, Ti.Utils.md5HexDigest(url) + '.wt3');
_this.models.push({
url : url,
file : file,
cached : file.exists() ? true : false,
});
});
}
return this;
};
Adapter.prototype = {
getModels : function() {
return this.models;
},
getModel : function(modelurl) {
this.models.filter(function(m, i) {
return _this.models[i].url == modelurl ? true : false;
});
},
areCached : function() {
var cachedmodels = this.models.filter(function(model) {
return Ti.Filesystem.getFile(DEPOT, FOLDER, Ti.Utils.md5HexDigest(model.url) + '.wt3').exists();
});
return cachedmodels.length == this.models.length ? true : false;
},
toggleAllModelsCache : function() {
if (this.areCached() == true) {
this.uncacheAllModels();
} else {
this.cacheAllModels();
}
},
cacheAllModels : function() {
var _this = this;
function loadIt(model, onloadFn) {
var $ = Ti.Network.createHTTPClient({
onload : function() {
console.log(this.status);
if (this.status == 200) {
onloadFn(this.responseData);
}
}
});
console.log('caching of '+ model.url);
$.open('GET', model.url, true);
$.send();
}
var ndx = 0;
function cacheModel(ndx) {
loadIt(_this.models[ndx], function(modelblob) {
_this.models[ndx].file.write(modelblob);
_this.models[ndx].cached = true;
ndx++;
if (ndx < _this.models.length) {
console.log('NEXT model');
_this.fireEvent('onprogress', {
progress : ndx / _this.models.lengths
});
cacheModel(ndx);
} else {
_this.fireEvent('onload');
}
});
}
cacheModel(ndx);
},
uncacheAllModels : function() {
this.models.forEach(function(model) {
if (model.file.exists()) {
model.file.deleteFile();
}
model.cached = false;
});
this.fireEvent('onload');
},
fireEvent : function(_event, _payload) {
if (this.eventhandlers[_event]) {
for (var i = 0; i < this.eventhandlers[_event].length; i++) {
this.eventhandlers[_event][i].call(this, _payload);
}
}
},
addEventListener : function(_event, _callback) {
if (!this.eventhandlers)
this.eventhandlers = {};
if (!this.eventhandlers[_event])
this.eventhandlers[_event] = [];
this.eventhandlers[_event].push(_callback);
},
removeEventListener : function(_event, _callback) {
if (!this.eventhandlers[_event])
return;
var newArray = this.eventhandlers[_event].filter(function(element) {
return element != _callback;
});
this.eventhandlers[_event] = newArray;
}
};
module.exports = Adapter;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment