Skip to content

Commit

Permalink
feature/settings — Persist settings accross restart and apply them wh…
Browse files Browse the repository at this point in the history
…en starting.
  • Loading branch information
jeandat committed Feb 28, 2016
1 parent 949d809 commit 221a07a
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 38 deletions.
3 changes: 1 addition & 2 deletions app/app.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@

// All keys in local storage will have the prefix 'lrnion'.
function localStorageConfig(localStorageServiceProvider) {
localStorageServiceProvider.setPrefix('lrnion'); // learning-ionic
localStorageServiceProvider.setPrefix('app');
}

// Ionic defaults
Expand All @@ -121,7 +121,6 @@
$ionicConfigProvider.backButton.previousTitleText(false);
$ionicConfigProvider.tabs.position('top');
$ionicConfigProvider.views.maxCache(3);
//$ionicConfigProvider.views.transition('none');
}

// ! NOT USED FOR THE MOMENT !
Expand Down
14 changes: 9 additions & 5 deletions app/app.run.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
.run(setCustomLogs)
.run(addGlobals)
.run(setHttpDefaultCache)
.run(restConfig)
.run(setRestConfig)
.run(handleAdjustPanKeyboardMode)
.run(checkRequirements);
.run(applySettings)
.run(boot);

//////////////////////

Expand Down Expand Up @@ -55,7 +56,7 @@
}

// Restangular configuration
function restConfig(Restangular, apiEndpoint, apiKey, privateApiKey, $q, $window) {
function setRestConfig(Restangular, apiEndpoint, apiKey, privateApiKey, $q, $window) {

// Temporary hack: Restangular is not compatible with Lodash v4.
_.contains = _.includes;
Expand Down Expand Up @@ -137,8 +138,11 @@
}
}

function checkRequirements($state, $cordovaSplashscreen, $timeout, ImgCache, $rootScope,
$log) {
function applySettings(settingService, $ionicConfig){
!settingService.settings.enableAnimations && $ionicConfig.views.transition('none');
}

function boot($state, $cordovaSplashscreen, $timeout, ImgCache, $rootScope, $log) {
initImgCache().then(goHome).then(hideSplash);

/////////////
Expand Down
42 changes: 11 additions & 31 deletions app/setting/setting-list.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
.module('app')
.controller('SettingListController', SettingListController);

function SettingListController($log, $scope, defaultCacheName, CacheFactory,
$ionicPopup, throwErr, ImgCache, $timeout, $ionicConfig) {
function SettingListController($scope, $ionicPopup, throwErr, $timeout, settingService) {

var vm = this;
vm.settings = {
enableAnimations: true
};
vm.settings = settingService.settings;
vm.clearing = false;
vm.clearCache = clearCache;

Expand All @@ -20,18 +17,7 @@
/////////////

function activate() {
$scope.$watch('vm.settings.enableAnimations', function (newValue, oldValue) {
if (newValue === oldValue) return;
newValue === true ? enableAnimations() : disableAnimations();
});
}

function enableAnimations(){
$ionicConfig.views.transition('platform');
}

function disableAnimations(){
$ionicConfig.views.transition('none');
$scope.$watchCollection('vm.settings', settingService.update);
}

function clearCache() {
Expand All @@ -47,20 +33,14 @@

function clear() {
vm.clearing = true;
CacheFactory.get(defaultCacheName).removeAll();
$log.info('HTTP cache is now empty');
ImgCache.clearCache(ok, throwErr);

/////////////

function ok() {
$scope.$apply(function () {
$log.info('Image cache is now empty');
// So we could read something at least
$timeout(function () {
vm.clearing = false;
}, 500);
});
return settingService.clearCache().finally(updateStatus);

////////////

function updateStatus(){
return $timeout(function(){
vm.clearing = false;
}, 500);
}
}

Expand Down
62 changes: 62 additions & 0 deletions app/setting/setting.factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
(function () {
'use strict';

angular
.module('app')
.factory('settingService', factory);

function factory($log, localStorageService, defaultCacheName, CacheFactory, ImgCache, $q, $ionicConfig) {

var defaults = {
enableAnimations: true
};

var service = {
settings: _.defaults(localStorageService.get('settings'), defaults),
update: update,
clearCache: clearCache
};

return service;

////////////

function update(){
var settings = service.settings;

// enableAnimations property
settings.enableAnimations ? enableAnimations() : disableAnimations();

// Persist in local storage
localStorageService.set('settings', settings);
$log.info('Settings persisted');
}

function clearCache() {
return $q(function (resolve, reject) {

CacheFactory.get(defaultCacheName).removeAll();
$log.info('HTTP cache is now empty');
ImgCache.clearCache(ok, reject);

/////////////

function ok() {
$log.info('Image cache is now empty');
resolve();
}
});
}

function enableAnimations(){
$ionicConfig.views.transition('platform');
}

function disableAnimations(){
$ionicConfig.views.transition('none');
}

}

})();

0 comments on commit 221a07a

Please sign in to comment.