Created
May 1, 2016 19:30
-
-
Save lnaia/4e42a2ead5dffaa6c6faaa24181773c8 to your computer and use it in GitHub Desktop.
Offline stuff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
'use strict'; | |
angular | |
.module('app.offline') | |
.service('offlineStorageService', offlineStorageService); | |
offlineStorageService.$inject = ['store', 'imagePreloadService']; | |
function offlineStorageService(store, imagePreloadService) { | |
return { | |
get: get, | |
set: set, | |
findImage: findImage | |
}; | |
function findImage(data) { | |
eachRecursive(data); | |
function eachRecursive(obj) { | |
for (var key in obj) { | |
if (obj.hasOwnProperty(key)) { | |
if (typeof obj[key] == "object" && obj[key] !== null) { | |
eachRecursive(obj[key]); | |
} else { | |
if (String(obj[key]).match(/(https?:\/\/.*\.(?:png|jpg|svg))/i)) { | |
imagePreloadService.preloadImages([obj[key]]); | |
} | |
} | |
} | |
} | |
} | |
} | |
function get(key) { | |
return store.get(_wrapUserKey(key)); | |
} | |
function set(key, value) { | |
return store.set(_wrapUserKey(key), value); | |
} | |
function _wrapUserKey(key) { | |
return '' + key + '.' + store.get('profile').user_id; | |
} | |
} | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
'use strict'; | |
angular | |
.module('app.offline') | |
.factory('offlineInterceptor', offlineInterceptor); | |
offlineInterceptor.$inject = ['API_ENDPOINT', 'offlineStorageService', 'store']; | |
function offlineInterceptor(API_ENDPOINT, offlineStorageService, store) { | |
var api = API_ENDPOINT.replace(/\//g, "\\/"); | |
return { | |
response: response, | |
responseError: responseError | |
}; | |
function response(response) { | |
var url; | |
if (_validIntercept(response)) { | |
url = response.config.url; | |
if (_matchApi(url)) { | |
offlineStorageService.set(_getUrlKey(url), response.data); | |
offlineStorageService.findImage(response.data); | |
} | |
} | |
return response; | |
} | |
function responseError(response) { | |
var url; | |
if (_validIntercept(response)) { | |
url = response.config.url; | |
if (_matchApi(url)) { | |
response.data = offlineStorageService.get(_getUrlKey(url)); | |
} | |
} | |
return response; | |
} | |
function _validIntercept(response) { | |
return _verifyStore() && response.config.method === 'GET'; | |
} | |
function _verifyStore() { | |
return store.get('token') && store.get('profileComplete'); | |
} | |
function _getUrlKey(url) { | |
return url.replace(API_ENDPOINT, '').replace(/\//g, '.'); | |
} | |
function _matchApi(url) { | |
return url.match(api); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment