diff --git a/Resources/iphone/alloy/widgets/ds.slideMenu/controllers/widget.js b/Resources/iphone/alloy/widgets/ds.slideMenu/controllers/widget.js index f5c71f8..9f4462f 100644 --- a/Resources/iphone/alloy/widgets/ds.slideMenu/controllers/widget.js +++ b/Resources/iphone/alloy/widgets/ds.slideMenu/controllers/widget.js @@ -352,25 +352,14 @@ function Controller() { text: "All Events", id: "__alloyId28" }); -<<<<<<< HEAD - $.__views.__alloyId7.add($.__views.__alloyId8); - $.__views.settingsBtn = Ti.UI.createButton({ -======= $.__views.__alloyId26.add($.__views.__alloyId28); $.__views.__alloyId29 = Ti.UI.createView({ layout: "absolute", ->>>>>>> FETCH_HEAD height: "7%", right: 0, width: "95%", backgroundColor: "#424242", top: "1%", -<<<<<<< HEAD - id: "settingsBtn" - }); - $.__views.menuView.add($.__views.settingsBtn); - $.__views.__alloyId9 = Ti.UI.createLabel({ -======= id: "__alloyId29" }); $.__views.menuView.add($.__views.__alloyId29); @@ -383,7 +372,6 @@ function Controller() { }); $.__views.__alloyId29.add($.__views.__alloyId30); $.__views.__alloyId31 = Ti.UI.createLabel({ ->>>>>>> FETCH_HEAD width: Ti.UI.SIZE, height: Ti.UI.SIZE, color: "white", @@ -396,11 +384,7 @@ function Controller() { text: "Settings", id: "__alloyId31" }); -<<<<<<< HEAD - $.__views.settingsBtn.add($.__views.__alloyId9); -======= $.__views.__alloyId29.add($.__views.__alloyId31); ->>>>>>> FETCH_HEAD $.__views.movableview = Ti.UI.createView({ top: 0, left: 0, diff --git a/app/controllers/homeView.js b/app/controllers/homeView.js index d4186e0..2ad7619 100644 --- a/app/controllers/homeView.js +++ b/app/controllers/homeView.js @@ -1,6 +1,14 @@ var args = arguments[0] || {}; +var api = require('api'); +api.user.login('test2@test.com', 'testtest', function(data){ + console.log('got success', data); +}, function(){ + console.log('got error', arguments); +}); + + function openMenu() { Alloy.createController("menu").getView().open(); } diff --git a/app/lib/api.js b/app/lib/api.js new file mode 100644 index 0000000..111137c --- /dev/null +++ b/app/lib/api.js @@ -0,0 +1,43 @@ +//IIFE + +console.log('loading'); + +//Include the XHR wrapper which makes requests painless. +var XHR = require('xhr'); + +//Create a new XHR object. +var xhr = new XHR(); + +var endpoint = 'http://ourchive.elasticbeanstalk.com/api/v1/'; + +//User API Methods +var user = { + login: function(email, password, successCB, errorCB){ + request('post', 'user/login', {username: email, password: password}, successCB, errorCB); + } +}; + +//Universal request function which +function request(method, url, data, successCB, errorCB){ + if(method.toLowerCase() === 'post'){ + xhr.post(endpoint + url, createApiData(data), successCB, errorCB); + } + if(method.toLowerCase() === 'get'){ + xhr.get(endpoint + url, successCB, errorCB); + } +}; + +//Used to inject information into the API requests: +function createApiData(data){ + data._apiRequest = true; + data._token = 'this+would+be+the+token'; + return data; +}; + +//Create the exported object: +module.exports = { + user: user, + //Give the API raw access to the API request: + request: request +}; + diff --git a/app/lib/xhr.js b/app/lib/xhr.js new file mode 100644 index 0000000..c0603ca --- /dev/null +++ b/app/lib/xhr.js @@ -0,0 +1,410 @@ +// Create the cache manager (a shared object) +var cacheManager = Titanium.App.Properties.getObject("cachedXHRDocuments", {}); + +XHR = function(){}; + +// Public functions +// ================ + +// GET +// @url (string) URL to fetch +// @onSuccess (function) success callback +// @onError (function) error callback +// @extraParams (object) +XHR.prototype.get = function(url, onSuccess, onError, extraParams) { + // Debug + Titanium.API.info(url); + + // Create some default params + var onSuccess = onSuccess || function(){}; + var onError = onError || function(){}; + var extraParams = extraParams || {}; + extraParams.async = (extraParams.hasOwnProperty('async')) ? extraParams.async : true; + extraParams.ttl = extraParams.ttl || false; + extraParams.shouldAuthenticate = extraParams.shouldAuthenticate || false; // if you set this to true, pass "username" and "password" as well + extraParams.contentType = extraParams.contentType || "application/json"; + + var cache = readCache(url); + // If there is nothing cached, send the request + if (!extraParams.ttl || cache == 0) { + + // Create the HTTP connection + var xhr = Titanium.Network.createHTTPClient({ + enableKeepAlive: false + }); + // Create the result object + var result = {}; + + // Open the HTTP connection + xhr.open("GET", url, extraParams.async); + xhr.setRequestHeader('Content-Type', extraParams.contentType); + + // If we need to authenticate + if (extraParams.shouldAuthenticate) { + var authstr = 'Basic ' + Titanium.Utils.base64encode(extraParams.username + ':' + extraParams.password); + xhr.setRequestHeader('Authorization', authstr); + } + + // When the connection was successful + xhr.onload = function() { + // Check the status of this + result.status = xhr.status == 200 ? "ok" : xhr.status; + + // Check the type of content we should serve back to the user + if (extraParams.contentType.indexOf("application/json") != -1) { + result.data = xhr.responseText; + } else if (extraParams.contentType.indexOf("text/xml") != -1) { + result.data = xhr.responseXML; + } else { + result.data = xhr.responseData; + } + + onSuccess(result); + + // Cache this response + writeCache(result.data, url, extraParams.ttl); + }; + + // When there was an error + xhr.onerror = function(e) { + // Check the status of this + result.status = "error"; + result.data = e; + result.code = xhr.status; + onError(result); + }; + + xhr.send(); + } else { + var result = {}; + + result.status = "cache"; + result.data = cache; + + onSuccess(result); + } +}; + +// POST requests +// @url (string) URL to fetch +// @data (object) +// @onSuccess (function) success callback +// @onError (function) error callback +// @extraParams (object) +XHR.prototype.post = function(url, data, onSuccess, onError, extraParams) { + + // Debug + Titanium.API.info(url + " " + JSON.stringify(data)); + + // Create some default params + var onSuccess = onSuccess || function(){}; + var onError = onError || function(){}; + var extraParams = extraParams || {}; + extraParams.async = (extraParams.hasOwnProperty('async')) ? extraParams.async : true; + extraParams.shouldAuthenticate = extraParams.shouldAuthenticate || false; // if you set this to true, pass "username" and "password" as well + extraParams.contentType = extraParams.contentType || "application/json"; + + // Create the HTTP connection + var xhr = Titanium.Network.createHTTPClient({ + enableKeepAlive: false + }); + // Create the result object + var result = {}; + + // Open the HTTP connection + xhr.open("POST", url, extraParams.async); + xhr.setRequestHeader('Content-Type', extraParams.contentType); + + // If we need to authenticate + if (extraParams.shouldAuthenticate) { + var authstr = 'Basic ' + Titanium.Utils.base64encode(extraParams.username + ':' + extraParams.password); + xhr.setRequestHeader('Authorization', authstr); + } + + // When the connection was successful + xhr.onload = function() { + // Check the status of this + result.status = xhr.status == 200 ? "ok" : xhr.status; + result.data = xhr.responseText; + + onSuccess(result); + }; + + // When there was an error + xhr.onerror = function(e) { + // Check the status of this + result.status = "error"; + result.data = e.error; + result.code = xhr.status; + onError(result); + }; + + xhr.send(data); +}; + +// PUT requests +// @url (string) URL to fetch +// @data (object) +// @onSuccess (function) success callback +// @onError (function) error callback +// @extraParams (object) +XHR.prototype.put = function(url, data, onSuccess, onError, extraParams) { + // Create some default params + var onSuccess = onSuccess || function(){}; + var onError = onError || function(){}; + var extraParams = extraParams || {}; + extraParams.async = (extraParams.hasOwnProperty('async')) ? extraParams.async : true; + extraParams.shouldAuthenticate = extraParams.shouldAuthenticate || false; // if you set this to true, pass "username" and "password" as well + extraParams.contentType = extraParams.contentType || "application/json"; + + // Create the HTTP connection + var xhr = Titanium.Network.createHTTPClient({ + enableKeepAlive: false + }); + // Create the result object + var result = {}; + + // Open the HTTP connection + xhr.open("PUT", url, extraParams.async); + xhr.setRequestHeader('Content-Type', extraParams.contentType); + + // If we need to authenticate + if (extraParams.shouldAuthenticate) { + var authstr = 'Basic ' + Titanium.Utils.base64encode(extraParams.username + ':' + extraParams.password); + xhr.setRequestHeader('Authorization', authstr); + } + + // When the connection was successful + xhr.onload = function() { + // Check the status of this + result.status = xhr.status == 200 ? "ok" : xhr.status; + result.data = xhr.responseText; + + onSuccess(result); + }; + + // When there was an error + xhr.onerror = function(e) { + // Check the status of this + result.status = "error"; + result.data = e.error; + result.code = xhr.status; + onError(result); + }; + + xhr.send(data); +}; + +// DELETE requests +// @url (string) URL to fetch +// @onSuccess (function) success callback +// @onError (function) error callback +// @extraParams (object) +XHR.prototype.destroy = function(url, onSuccess, onError, extraParams) { + // Debug + Titanium.API.info(url); + + // Create some default params + var onSuccess = onSuccess || function(){}; + var onError = onError || function(){}; + var extraParams = extraParams || {}; + extraParams.async = (extraParams.hasOwnProperty('async')) ? extraParams.async : true; + extraParams.shouldAuthenticate = extraParams.shouldAuthenticate || false; // if you set this to true, pass "username" and "password" as well + extraParams.contentType = extraParams.contentType || "application/json"; + + // Create the HTTP connection + var xhr = Titanium.Network.createHTTPClient({ + enableKeepAlive: false + }); + // Create the result object + var result = {}; + + // Open the HTTP connection + xhr.open("DELETE", url, extraParams.async); + xhr.setRequestHeader('Content-Type', extraParams.contentType); + + // If we need to authenticate + if (extraParams.shouldAuthenticate) { + var authstr = 'Basic ' + Titanium.Utils.base64encode(extraParams.username + ':' + extraParams.password); + xhr.setRequestHeader('Authorization', authstr); + } + + // When the connection was successful + xhr.onload = function() { + // Check the status of this + result.status = xhr.status == 200 ? "ok" : xhr.status; + result.data = xhr.responseText; + + onSuccess(result); + }; + + // When there was an error + xhr.onerror = function(e) { + // Check the status of this + result.status = "error"; + result.data = e.error; + result.code = xhr.status; + onError(result); + }; + + xhr.send(); +}; + +// Helper functions +// ================= + +// Removes the cached content of a given URL (this is useful if you are not satisfied with the data returned that time) +XHR.prototype.clear = function(url) { + + if (url) { + // Hash the URL + var hashedURL = Titanium.Utils.md5HexDigest(url); + // Check if the file exists in the manager + var cache = cacheManager[hashedURL]; + + // If the file was found + if (cache) { + // Delete references and file + var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, hashedURL); + // Delete the record and file + delete cacheManager[hashedURL]; + file.deleteFile(); + + // Update the cache manager + updateCacheManager(); + + //Titanium.API.info("REMOVED CACHE FILE " + hashedURL); + } + } + +}; + +// Removes all the expired documents from the manager and the file system +XHR.prototype.clean = function() { + + var nowInMilliseconds = new Date().getTime(); + var expiredDocuments = 0; + + for (var key in cacheManager) { + var cache = cacheManager[key]; + + if(cache.timestamp <= nowInMilliseconds){ + // Delete references and file + var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, key); + // Delete the record and file + delete cacheManager[key]; + file.deleteFile(); + + // Update the cache manager + updateCacheManager(); + + // Update the deleted documents count + expiredDocuments = expiredDocuments + 1; + + //Titanium.API.info("REMOVED CACHE FILE " + cachedDocuments[i].file); + } + + } + + // Return the number of files deleted + return expiredDocuments; +}; + +// Removes all documents from the manager and the file system +XHR.prototype.purge = function() { + + var purgedDocuments = 0; + + for (var key in cacheManager) { + var cache = cacheManager[key]; + // Delete references and file + var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, key); + // Delete the record and file + delete cacheManager[key]; + file.deleteFile(); + + // Update the cache manager + updateCacheManager(); + + // Update the deleted documents count + purgedDocuments = purgedDocuments + 1; + + //Titanium.API.info("REMOVED CACHE FILE " + cachedDocuments[i].file); + + } + + // Return the number of files deleted + return purgedDocuments; +}; + +// Private functions +// ================= + +readCache = function(url) { + // Hash the URL + var hashedURL = Titanium.Utils.md5HexDigest(url); + + // Check if the file exists in the manager + var cache = cacheManager[hashedURL]; + // Default the return value to false + var result = false; + + //Titanium.API.info("CHECKING CACHE"); + + // If the file was found + if (cache) { + // Fetch a reference to the cache file + var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, hashedURL); + + // Check that the TTL is further than the current date + if (cache.timestamp >= new Date().getTime()) { + //Titanium.API.info("CACHE FOUND"); + + // Return the content of the file + result = file.read(); + + } else { + //Titanium.API.info("OLD CACHE"); + + // Delete the record and file + delete cacheManager[hashedURL]; + file.deleteFile(); + + // Update the cache manager + updateCacheManager(); + } + } else { + //Titanium.API.info("CACHE " + hashedURL + " NOT FOUND"); + } + + return result; +}; + +updateCacheManager = function(){ + Titanium.App.Properties.setObject("cachedXHRDocuments", cacheManager); +}; + +writeCache = function(data, url, ttl) { + + //Titanium.API.info("WRITING CACHE"); + + // hash the url + var hashedURL = Titanium.Utils.md5HexDigest(url); + + // Write the file to the disk + var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, hashedURL); + + // Write the file to the disk + // TODO: There appears to be a bug in Titanium and makes the method + // below always return false when dealing with binary files + file.write(data); + + // Insert the cached object in the cache manager + cacheManager[hashedURL] = { "timestamp": (new Date().getTime()) + (ttl*60*1000) }; + updateCacheManager(); + + //Titanium.API.info("WROTE CACHE"); +}; + +// Return everything +module.exports = XHR; \ No newline at end of file diff --git a/app/widgets/ds.slideMenu/views/widget.xml b/app/widgets/ds.slideMenu/views/widget.xml index 86db6a8..5d389b2 100755 --- a/app/widgets/ds.slideMenu/views/widget.xml +++ b/app/widgets/ds.slideMenu/views/widget.xml @@ -5,17 +5,6 @@ -<<<<<<< HEAD - - - - - - - - - -======= @@ -57,7 +46,6 @@ ->>>>>>> FETCH_HEAD