Skip to content

Commit

Permalink
Merge pull request #690 from Maximbndrnk/master
Browse files Browse the repository at this point in the history
add profiles feature (issue #678)
  • Loading branch information
dularion committed Nov 30, 2018
2 parents ab92ef0 + f33967b commit 78414f6
Show file tree
Hide file tree
Showing 46 changed files with 1,114 additions and 57 deletions.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 14 additions & 3 deletions grails-app/assets/javascripts/streama/controllers/dash-ctrl.js
Expand Up @@ -13,10 +13,8 @@ angular.module('streama').controller('dashCtrl',

$scope.$on('changedGenre', onChangedGenre);


init();


function init() {
if ($rootScope.currentUser.isAdmin) {
showInitialSettingsWarning();
Expand All @@ -26,6 +24,20 @@ angular.module('streama').controller('dashCtrl',
modalService.mediaDetailModal({mediaId: $stateParams.mediaModal, mediaType: $stateParams.mediaType, isApiMovie: false});
}

if(!localStorageService.get('currentProfile')){
apiService.profile.getUserProfiles().success(function(data) {
localStorageService.set('currentProfile', data[0]);
initMedia();
}
).error(function (data) {
alertify.error(data.message);
});
} else {
initMedia();
}
}

function initMedia() {
vm.movie = mediaListService.init(apiService.dash.listMovies, {sort: 'title', order: 'ASC'}, currentUser.data);
vm.tvShow = mediaListService.init(apiService.dash.listShows, {sort: 'name', order: 'ASC'}, currentUser.data);
vm.genericVideo = mediaListService.init(apiService.dash.listGenericVideos, {sort: 'title', order: 'ASC'}, currentUser.data);
Expand All @@ -37,7 +49,6 @@ angular.module('streama').controller('dashCtrl',
apiService.dash.listGenres().success(onGenreLoaded);
}


// HOISTED FUNCTIONS BELOW


Expand Down
Expand Up @@ -70,13 +70,31 @@ angular.module('streama').controller('modalUserCtrl', [

$scope.saveAndInviteUser = function (user) {
$scope.loading = true;

var dateObj = angular.copy(user);
var dateObj = angular.copy(user);
apiService.user.saveAndInviteUser(dateObj)

.success(function (data) {
$uibModalInstance.close(data);
$scope.loading = false;
if(user.id){
alertify.success('User Updated!');
$uibModalInstance.close(data);
$scope.loading = false;
return;
}
var basicProfile = {
profileName: data.username,
profileLanguage: data.language,
isChild: false,
user: data
};
apiService.profile.save(basicProfile)
.success(function () {
alertify.success('Profile Created!');
$uibModalInstance.close(data);
$scope.loading = false;
})
.error(function (data) {
alertify.error(data.message);
$scope.loading = false;
});
})
.error(function (response) {
$scope.loading = false;
Expand All @@ -92,13 +110,26 @@ angular.module('streama').controller('modalUserCtrl', [

$scope.saveAndCreateUser = function (user) {
$scope.loading = true;

var dateObj = angular.copy(user);
apiService.user.saveAndCreateUser(dateObj)

.success(function (data) {
$uibModalInstance.close(data);
$scope.loading = false;
var basicProfile = {
profileName: data.username,
profileLanguage: data.language,
isChild: false,
user: data
};
apiService.profile.save(basicProfile)
.success(function () {
alertify.success('Profile Created!');
$uibModalInstance.close(data);
$scope.loading = false;
})
.error(function (data) {
alertify.error(data.message);
$scope.loading = false;
});
})
.error(function () {
$scope.loading = false;
Expand Down
125 changes: 125 additions & 0 deletions grails-app/assets/javascripts/streama/controllers/subProfiles-ctrl.js
@@ -0,0 +1,125 @@
'use strict';

angular.module('streama').controller('subProfilesCtrl',
function ($scope, apiService, $rootScope, userService, localStorageService, $state, profileService) {

$scope.profile = {
profileName: '',
profileLanguage: 'en',
isChild: false,
avatarColor: '0b74b2'
};
$scope.standardColor = '0b74b2';
$scope.existingProfiles = [];
$scope.loading = true;
$scope.isManageProfiles = false;
$scope.isEditProfile = false;
$scope.isCreateProfile = false;
$scope.availableColors = [
'0b74b2','ba1c56','099166','d1b805','c03da7',
'488f16','d36e10','4b4b4b','3a328b','b81f1f'
];

profileService.getUserProfiles().success(
function(data) {
$scope.existingProfiles = data;
}
);
$scope.setCurrentProfile = profileService.setCurrentProfile;

$scope.setProfileColor = function(color){
$scope.profile.avatarColor = color;
};

$scope.toggleManageProfiles = function(){
$scope.isManageProfiles = !$scope.isManageProfiles;
};

$scope.goToEditProfile = function(profile){
$scope.isEditProfile = !$scope.isEditProfile;
$scope.profile = profile;
};

$scope.goToCreateProfile = function(){
$scope.isCreateProfile = !$scope.isCreateProfile;
$scope.profile = {
profileName: '',
profileLanguage: 'en',
isChild: false,
avatarColor: '0b74b2'
}
};

$scope.deleteProfile = function(){
if($scope.existingProfiles.length === 1){
alertify.error("You must have at least ONE profile!");
return;
}
if($scope.profile.id === profileService.getCurrentProfile().id){
alertify.error("You currently use this profile! Change profile first");
return;
}
if(!$scope.profile.id){
return;
}
apiService.profile.delete($scope.profile.id)
.success(function () {
alertify.success('Profile Deleted!');
$scope.getAllProfiles();
$scope.loading = false;
$scope.refreshStates();
})
.error(function (data) {
alertify.error(data.message);
$scope.loading = false;
});
};

$scope.refreshStates = function(){
$scope.isEditProfile = false;
$scope.isCreateProfile = false;
};

$scope.saveProfile = function(){
if (!$scope.profile.profileName) {
return;
}
var saveProfileEndpoint;
if ($scope.profile.id) {
saveProfileEndpoint = apiService.profile.update;
}else {
saveProfileEndpoint = apiService.profile.save;
}
saveProfileEndpoint($scope.profile)
.success(function () {
alertify.success($scope.profile.id ? 'Profile Updated!' : 'Profile Created!');
$scope.getAllProfiles();
$scope.loading = false;
$rootScope.$broadcast('streama.profiles.onChange');
})
.error(function (data) {
alertify.error(data.message);
$scope.loading = false;
});
};

$scope.getAllProfiles = function () {
apiService.profile.getUserProfiles()
.success(function (data) {
$scope.existingProfiles = data;
$scope.refreshStates();
})
.error(function (data) {
alertify.error(data.message);
$scope.loading = false;
});
};

$scope.showPreviewProfiles = function() {
return !$scope.isManageProfiles && !($scope.isEditProfile || $scope.isCreateProfile);
};

$scope.showEditProfiles = function() {
return $scope.isManageProfiles && !($scope.isEditProfile || $scope.isCreateProfile);
}
});
@@ -1,6 +1,6 @@
'use strict';

angular.module('streama').controller('profileCtrl', function ($scope, apiService, $rootScope, userService) {
angular.module('streama').controller('userSettingsCtrl', function ($scope, apiService, $rootScope, userService) {
$scope.user = angular.copy($rootScope.currentUser);
$scope.loading = true;
$scope.passwordData = {};
Expand All @@ -14,8 +14,8 @@ angular.module('streama').controller('profileCtrl', function ($scope, apiService
});

$scope.toggleSelectGenre = function (genre) {
$scope.user.favoriteGenres = _.xorBy($scope.user.favoriteGenres, [genre], 'apiId');
$scope.profileForm.$setDirty();
$scope.user.favoriteGenres = _.xorBy($scope.user.favoriteGenres, [genre], 'apiId');
$scope.profileForm.$setDirty();
};

$scope.isGenreSelected = function (genre) {
Expand Down
18 changes: 17 additions & 1 deletion grails-app/assets/javascripts/streama/services/api-service.js
Expand Up @@ -336,6 +336,22 @@ angular.module('streama').factory('apiService', function ($http, $rootScope, con
triggerPlayerAction: function (params) {
return $http.get('websocket/triggerPlayerAction.json', {params: params});
}
}
},

profile: {
save: function (params) {
return $http.post('profile/save', params)
},
update: function (params) {
return $http.put('profile/update.json', params)
},
delete: function (id) {
return $http.delete('profile/delete.json', {params: {id: id}})
},
getUserProfiles: function () {
return $http.get('profile/getUserProfiles.json')
}
}

};
});
26 changes: 26 additions & 0 deletions grails-app/assets/javascripts/streama/services/profile-service.js
@@ -0,0 +1,26 @@
'use strict';

angular.module('streama').factory('profileService', function (localStorageService, apiService, $state, $rootScope, $translate) {

function setCurrentProfile(profile) {
localStorageService.set('currentProfile', profile);
$rootScope.currentProfile = profile;
$state.go('dash', {}, {reload: true});
$translate.use(_.get($rootScope, 'currentProfile.profileLanguage') || _.get($rootScope, 'currentUser.language') || 'en');
}

function getCurrentProfile() {
return localStorageService.get('currentProfile') || null;
}

function getUserProfiles() {
return apiService.profile.getUserProfiles();
}

return {
setCurrentProfile: setCurrentProfile,
getCurrentProfile: getCurrentProfile,
getUserProfiles: getUserProfiles
};

});
Expand Up @@ -4,7 +4,6 @@ angular.module('streama').factory('userService', function ($rootScope, $translat
return {
setCurrentUser: function (data) {
$rootScope.currentUser = data;
$translate.use(_.get($rootScope, 'currentUser.language') || 'en');
}
};
});
7 changes: 5 additions & 2 deletions grails-app/assets/javascripts/streama/streama.interceptor.js
Expand Up @@ -6,13 +6,16 @@ angular.module('streama').config(function ($httpProvider) {
});


angular.module('streama').factory('httpInterceptor', function ($rootScope, $q) {
angular.module('streama').factory('httpInterceptor', function ($rootScope, $q, localStorageService) {
return {
request: function (config) {
config.params = config.params || {};
if(config.params.socketSessionId){
config.params.browserSocketUUID = $rootScope.browserSocketUUID;
}
if (localStorageService.get('currentProfile')){
config.headers.profileId = localStorageService.get('currentProfile').id || 0;
}
return config || $q.when(config);
},
response: function (response) {
Expand All @@ -35,4 +38,4 @@ angular.module('streama').factory('httpInterceptor', function ($rootScope, $q) {
return $q.reject(response);
}
};
});
});
13 changes: 9 additions & 4 deletions grails-app/assets/javascripts/streama/streama.routes.js
Expand Up @@ -24,11 +24,16 @@ angular.module('streama').config(function ($stateProvider) {
currentUser: resolveCurrentUser
}
})
.state('sub-profiles', {
url: '/sub-profiles',
templateUrl: '/streama/sub-profiles.htm',
controller: 'subProfilesCtrl'
})

.state('profile', {
url: '/profile',
templateUrl: '/streama/profile.htm',
controller: 'profileCtrl',
.state('userSettings', {
url: '/user-settings',
templateUrl: '/streama/user-settings.htm',
controller: 'userSettingsCtrl',
resolve: {
currentUser: resolveCurrentUser
}
Expand Down

0 comments on commit 78414f6

Please sign in to comment.