save settings on read mode change

This commit is contained in:
Athou
2013-03-23 23:14:14 +01:00
parent f0ac0d1722
commit 0b1241ebf9
11 changed files with 91 additions and 36 deletions

View File

@@ -4,6 +4,7 @@ import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@@ -12,10 +13,11 @@ import javax.persistence.Table;
public class UserSettings extends AbstractModel {
public enum ReadingMode {
ALL, UNREAD
all, unread
}
@Column(name = "user_id")
@OneToOne
private User user;
@Enumerated(EnumType.STRING)

View File

@@ -23,6 +23,7 @@
</div>
</div>
<div class="span10">
<toolbar></toolbar>
<ng:view></ng:view>
</div>
</div>

View File

@@ -1,6 +1,7 @@
package com.commafeed.frontend.rest;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
@@ -8,11 +9,13 @@ import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
@@ -28,7 +31,9 @@ import com.google.gson.JsonSerializer;
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JSONMessageBodyWriter implements MessageBodyWriter<Object> {
@Consumes(MediaType.APPLICATION_JSON)
public class JSONMessageBodyReaderWriter implements MessageBodyWriter<Object>,
MessageBodyReader<Object> {
@Override
public boolean isWriteable(Class<?> type, Type genericType,
@@ -36,6 +41,12 @@ public class JSONMessageBodyWriter implements MessageBodyWriter<Object> {
return true;
}
@Override
public boolean isReadable(Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType) {
return true;
}
@Override
public long getSize(Object t, Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType) {
@@ -50,11 +61,23 @@ public class JSONMessageBodyWriter implements MessageBodyWriter<Object> {
WebApplicationException {
httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, mediaType.toString()
+ ";charset=UTF-8");
IOUtils.write(getGson().toJson(t), entityStream, "UTF-8");
}
@Override
public Object readFrom(Class<Object> type, Type genericType,
Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
throws IOException, WebApplicationException {
String json = IOUtils.toString(entityStream, "UTF-8");
return getGson().fromJson(json, type);
}
private Gson getGson() {
Gson gson = new GsonBuilder().registerTypeAdapter(Date.class,
new DateSerializer()).create();
IOUtils.write(gson.toJson(t), entityStream, "UTF-8");
return gson;
}
private static class DateSerializer implements JsonSerializer<Date> {

View File

@@ -16,7 +16,7 @@ public class RESTApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> set = Sets.newHashSet();
set.add(JSONMessageBodyWriter.class);
set.add(JSONMessageBodyReaderWriter.class);
set.add(SubscriptionsREST.class);
set.add(EntriesREST.class);

View File

@@ -4,6 +4,7 @@ import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
@@ -27,6 +28,7 @@ import com.commafeed.frontend.CommaFeedApplication;
import com.commafeed.frontend.CommaFeedSession;
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public abstract class AbstractREST {
@Context

View File

@@ -3,6 +3,8 @@ package com.commafeed.frontend.rest.resources;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import com.commafeed.backend.model.UserSettings;
import com.commafeed.backend.model.UserSettings.ReadingMode;
@@ -14,15 +16,19 @@ public class SettingsREST extends AbstractREST {
@Path("get")
@GET
public Settings get() {
UserSettings settings = userSettingsService.findByUser(getUser());
Settings s = new Settings();
s.setReadingMode(settings.getReadingMode().name().toLowerCase());
UserSettings settings = userSettingsService.findByUser(getUser());
if (settings != null) {
s.setReadingMode(settings.getReadingMode().name());
} else {
s.setReadingMode(ReadingMode.unread.name());
}
return s;
}
@Path("save")
@POST
public void save(Settings settings) {
public Response save(Settings settings) {
UserSettings s = userSettingsService.findByUser(getUser());
if (s == null) {
s = new UserSettings();
@@ -30,6 +36,7 @@ public class SettingsREST extends AbstractREST {
}
s.setReadingMode(ReadingMode.valueOf(settings.getReadingMode()));
userSettingsService.saveOrUpdate(s);
return Response.ok(Status.OK).build();
}
}

View File

@@ -0,0 +1,6 @@
<div class="toolbar">
<div class="btn-group" data-toggle="buttons-radio">
<button type="button" class="btn" ng-model="settings.readingMode" btn-radio="'unread'">Unread</button>
<button type="button" class="btn" ng-model="settings.readingMode" btn-radio="'all'">All</button>
</div>
</div>

View File

@@ -81,23 +81,26 @@ module.controller('CategoryTreeCtrl', function($scope, $routeParams, $location,
});
module.controller('FeedListCtrl', function($scope, $routeParams, $http,
EntryService) {
EntryService, SettingsService) {
$scope.selectedType = $routeParams._type;
$scope.selectedId = $routeParams._id;
$scope.readType = 'all';
$scope.settings = SettingsService.settings;
$scope.$watch('settings.readingMode', function() {
$scope.refreshList();
});
$scope.refreshList = function() {
$scope.entryList = EntryService.get({
_type : $scope.selectedType,
_id : $scope.selectedId,
_readtype : $scope.readType
});
if ($scope.settings.readingMode) {
$scope.entryList = EntryService.get({
_type : $scope.selectedType,
_id : $scope.selectedId,
_readtype : $scope.settings.readingMode
});
}
};
$scope.refreshList();
$scope.mark = function(entry, read) {
if (entry.read != read) {
entry.read = read;

View File

@@ -23,4 +23,21 @@ module.directive('category', function($compile) {
$compile(ul.contents())(scope);
}
};
});
module.directive('toolbar', function(SettingsService) {
return {
scope : {},
restrict : 'E',
replace : true,
templateUrl : 'directives/toolbar.html',
controller : function($scope, SettingsService) {
$scope.settings = SettingsService.settings;
},
link : function($scope, element) {
element.find('button').bind('click', function() {
SettingsService.save();
});
}
};
});

View File

@@ -2,13 +2,7 @@ var module = angular.module('commafeed.services', [ 'ngResource' ]);
module.factory('CategoryService', [ '$resource', '$http',
function($resource, $http) {
var actions = {
'get' : {
method : 'GET'
}
};
res = $resource('rest/subscriptions', {}, actions);
return res;
return $resource('rest/subscriptions');
} ]);
module.factory('EntryService', [
@@ -16,7 +10,7 @@ module.factory('EntryService', [
'$http',
function($resource, $http) {
var actions = {
'get' : {
get : {
method : 'GET',
params : {
_method : 'get'
@@ -32,4 +26,13 @@ module.factory('EntryService', [
res = $resource('rest/entries/:_method/:_type/:_id/:_readtype', {},
actions);
return res;
} ]);
} ]);
module.service('SettingsService', function($resource) {
var s = {}
s.settings = $resource('rest/settings/get').get();
s.save = function() {
$resource('rest/settings/save').save(s.settings);
};
return s;
});

View File

@@ -1,14 +1,5 @@
<div ng-controller="FeedListCtrl">
<div class="toolbar">
<div class="btn-group" data-toggle="buttons-radio">
<button type="button" class="btn" ng-model="readType" btn-radio="'all'" ng-click="refreshList()">All</button>
<button type="button" class="btn" ng-model="readType" btn-radio="'unread'" ng-click="refreshList()">Unread only</button>
</div>
</div>
{{readType}}
<span>{{entryList.name}}</span><span
ng-show="selectedType == 'category'"> &#187;</span>
<div class="accordion" id="feed-accordion">