mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
save settings on read mode change
This commit is contained in:
@@ -4,6 +4,7 @@ import javax.persistence.Column;
|
|||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.EnumType;
|
import javax.persistence.EnumType;
|
||||||
import javax.persistence.Enumerated;
|
import javax.persistence.Enumerated;
|
||||||
|
import javax.persistence.OneToOne;
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@@ -12,10 +13,11 @@ import javax.persistence.Table;
|
|||||||
public class UserSettings extends AbstractModel {
|
public class UserSettings extends AbstractModel {
|
||||||
|
|
||||||
public enum ReadingMode {
|
public enum ReadingMode {
|
||||||
ALL, UNREAD
|
all, unread
|
||||||
}
|
}
|
||||||
|
|
||||||
@Column(name = "user_id")
|
@Column(name = "user_id")
|
||||||
|
@OneToOne
|
||||||
private User user;
|
private User user;
|
||||||
|
|
||||||
@Enumerated(EnumType.STRING)
|
@Enumerated(EnumType.STRING)
|
||||||
|
|||||||
@@ -23,6 +23,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="span10">
|
<div class="span10">
|
||||||
|
<toolbar></toolbar>
|
||||||
<ng:view></ng:view>
|
<ng:view></ng:view>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.commafeed.frontend.rest;
|
package com.commafeed.frontend.rest;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
@@ -8,11 +9,13 @@ import java.text.SimpleDateFormat;
|
|||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
|
import javax.ws.rs.Consumes;
|
||||||
import javax.ws.rs.Produces;
|
import javax.ws.rs.Produces;
|
||||||
import javax.ws.rs.WebApplicationException;
|
import javax.ws.rs.WebApplicationException;
|
||||||
import javax.ws.rs.core.HttpHeaders;
|
import javax.ws.rs.core.HttpHeaders;
|
||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
import javax.ws.rs.core.MultivaluedMap;
|
import javax.ws.rs.core.MultivaluedMap;
|
||||||
|
import javax.ws.rs.ext.MessageBodyReader;
|
||||||
import javax.ws.rs.ext.MessageBodyWriter;
|
import javax.ws.rs.ext.MessageBodyWriter;
|
||||||
import javax.ws.rs.ext.Provider;
|
import javax.ws.rs.ext.Provider;
|
||||||
|
|
||||||
@@ -28,7 +31,9 @@ import com.google.gson.JsonSerializer;
|
|||||||
|
|
||||||
@Provider
|
@Provider
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
public class JSONMessageBodyWriter implements MessageBodyWriter<Object> {
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
|
public class JSONMessageBodyReaderWriter implements MessageBodyWriter<Object>,
|
||||||
|
MessageBodyReader<Object> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isWriteable(Class<?> type, Type genericType,
|
public boolean isWriteable(Class<?> type, Type genericType,
|
||||||
@@ -36,6 +41,12 @@ public class JSONMessageBodyWriter implements MessageBodyWriter<Object> {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isReadable(Class<?> type, Type genericType,
|
||||||
|
Annotation[] annotations, MediaType mediaType) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getSize(Object t, Class<?> type, Type genericType,
|
public long getSize(Object t, Class<?> type, Type genericType,
|
||||||
Annotation[] annotations, MediaType mediaType) {
|
Annotation[] annotations, MediaType mediaType) {
|
||||||
@@ -50,11 +61,23 @@ public class JSONMessageBodyWriter implements MessageBodyWriter<Object> {
|
|||||||
WebApplicationException {
|
WebApplicationException {
|
||||||
httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, mediaType.toString()
|
httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, mediaType.toString()
|
||||||
+ ";charset=UTF-8");
|
+ ";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,
|
Gson gson = new GsonBuilder().registerTypeAdapter(Date.class,
|
||||||
new DateSerializer()).create();
|
new DateSerializer()).create();
|
||||||
IOUtils.write(gson.toJson(t), entityStream, "UTF-8");
|
return gson;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class DateSerializer implements JsonSerializer<Date> {
|
private static class DateSerializer implements JsonSerializer<Date> {
|
||||||
@@ -16,7 +16,7 @@ public class RESTApplication extends Application {
|
|||||||
@Override
|
@Override
|
||||||
public Set<Class<?>> getClasses() {
|
public Set<Class<?>> getClasses() {
|
||||||
Set<Class<?>> set = Sets.newHashSet();
|
Set<Class<?>> set = Sets.newHashSet();
|
||||||
set.add(JSONMessageBodyWriter.class);
|
set.add(JSONMessageBodyReaderWriter.class);
|
||||||
|
|
||||||
set.add(SubscriptionsREST.class);
|
set.add(SubscriptionsREST.class);
|
||||||
set.add(EntriesREST.class);
|
set.add(EntriesREST.class);
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import javax.annotation.PostConstruct;
|
|||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import javax.ws.rs.Consumes;
|
||||||
import javax.ws.rs.Produces;
|
import javax.ws.rs.Produces;
|
||||||
import javax.ws.rs.WebApplicationException;
|
import javax.ws.rs.WebApplicationException;
|
||||||
import javax.ws.rs.core.Context;
|
import javax.ws.rs.core.Context;
|
||||||
@@ -27,6 +28,7 @@ import com.commafeed.frontend.CommaFeedApplication;
|
|||||||
import com.commafeed.frontend.CommaFeedSession;
|
import com.commafeed.frontend.CommaFeedSession;
|
||||||
|
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
public abstract class AbstractREST {
|
public abstract class AbstractREST {
|
||||||
|
|
||||||
@Context
|
@Context
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package com.commafeed.frontend.rest.resources;
|
|||||||
import javax.ws.rs.GET;
|
import javax.ws.rs.GET;
|
||||||
import javax.ws.rs.POST;
|
import javax.ws.rs.POST;
|
||||||
import javax.ws.rs.Path;
|
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;
|
||||||
import com.commafeed.backend.model.UserSettings.ReadingMode;
|
import com.commafeed.backend.model.UserSettings.ReadingMode;
|
||||||
@@ -14,15 +16,19 @@ public class SettingsREST extends AbstractREST {
|
|||||||
@Path("get")
|
@Path("get")
|
||||||
@GET
|
@GET
|
||||||
public Settings get() {
|
public Settings get() {
|
||||||
UserSettings settings = userSettingsService.findByUser(getUser());
|
|
||||||
Settings s = new Settings();
|
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;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Path("save")
|
@Path("save")
|
||||||
@POST
|
@POST
|
||||||
public void save(Settings settings) {
|
public Response save(Settings settings) {
|
||||||
UserSettings s = userSettingsService.findByUser(getUser());
|
UserSettings s = userSettingsService.findByUser(getUser());
|
||||||
if (s == null) {
|
if (s == null) {
|
||||||
s = new UserSettings();
|
s = new UserSettings();
|
||||||
@@ -30,6 +36,7 @@ public class SettingsREST extends AbstractREST {
|
|||||||
}
|
}
|
||||||
s.setReadingMode(ReadingMode.valueOf(settings.getReadingMode()));
|
s.setReadingMode(ReadingMode.valueOf(settings.getReadingMode()));
|
||||||
userSettingsService.saveOrUpdate(s);
|
userSettingsService.saveOrUpdate(s);
|
||||||
|
return Response.ok(Status.OK).build();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
6
src/main/webapp/directives/toolbar.html
Normal file
6
src/main/webapp/directives/toolbar.html
Normal 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>
|
||||||
@@ -81,23 +81,26 @@ module.controller('CategoryTreeCtrl', function($scope, $routeParams, $location,
|
|||||||
});
|
});
|
||||||
|
|
||||||
module.controller('FeedListCtrl', function($scope, $routeParams, $http,
|
module.controller('FeedListCtrl', function($scope, $routeParams, $http,
|
||||||
EntryService) {
|
EntryService, SettingsService) {
|
||||||
|
|
||||||
$scope.selectedType = $routeParams._type;
|
$scope.selectedType = $routeParams._type;
|
||||||
$scope.selectedId = $routeParams._id;
|
$scope.selectedId = $routeParams._id;
|
||||||
|
|
||||||
$scope.readType = 'all';
|
$scope.settings = SettingsService.settings;
|
||||||
|
$scope.$watch('settings.readingMode', function() {
|
||||||
|
$scope.refreshList();
|
||||||
|
});
|
||||||
|
|
||||||
$scope.refreshList = function() {
|
$scope.refreshList = function() {
|
||||||
$scope.entryList = EntryService.get({
|
if ($scope.settings.readingMode) {
|
||||||
_type : $scope.selectedType,
|
$scope.entryList = EntryService.get({
|
||||||
_id : $scope.selectedId,
|
_type : $scope.selectedType,
|
||||||
_readtype : $scope.readType
|
_id : $scope.selectedId,
|
||||||
});
|
_readtype : $scope.settings.readingMode
|
||||||
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
$scope.refreshList();
|
|
||||||
|
|
||||||
$scope.mark = function(entry, read) {
|
$scope.mark = function(entry, read) {
|
||||||
if (entry.read != read) {
|
if (entry.read != read) {
|
||||||
entry.read = read;
|
entry.read = read;
|
||||||
|
|||||||
@@ -23,4 +23,21 @@ module.directive('category', function($compile) {
|
|||||||
$compile(ul.contents())(scope);
|
$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();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
});
|
});
|
||||||
@@ -2,13 +2,7 @@ var module = angular.module('commafeed.services', [ 'ngResource' ]);
|
|||||||
|
|
||||||
module.factory('CategoryService', [ '$resource', '$http',
|
module.factory('CategoryService', [ '$resource', '$http',
|
||||||
function($resource, $http) {
|
function($resource, $http) {
|
||||||
var actions = {
|
return $resource('rest/subscriptions');
|
||||||
'get' : {
|
|
||||||
method : 'GET'
|
|
||||||
}
|
|
||||||
};
|
|
||||||
res = $resource('rest/subscriptions', {}, actions);
|
|
||||||
return res;
|
|
||||||
} ]);
|
} ]);
|
||||||
|
|
||||||
module.factory('EntryService', [
|
module.factory('EntryService', [
|
||||||
@@ -16,7 +10,7 @@ module.factory('EntryService', [
|
|||||||
'$http',
|
'$http',
|
||||||
function($resource, $http) {
|
function($resource, $http) {
|
||||||
var actions = {
|
var actions = {
|
||||||
'get' : {
|
get : {
|
||||||
method : 'GET',
|
method : 'GET',
|
||||||
params : {
|
params : {
|
||||||
_method : 'get'
|
_method : 'get'
|
||||||
@@ -32,4 +26,13 @@ module.factory('EntryService', [
|
|||||||
res = $resource('rest/entries/:_method/:_type/:_id/:_readtype', {},
|
res = $resource('rest/entries/:_method/:_type/:_id/:_readtype', {},
|
||||||
actions);
|
actions);
|
||||||
return res;
|
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;
|
||||||
|
});
|
||||||
@@ -1,14 +1,5 @@
|
|||||||
<div ng-controller="FeedListCtrl">
|
<div ng-controller="FeedListCtrl">
|
||||||
|
{{readType}}
|
||||||
|
|
||||||
<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>
|
|
||||||
|
|
||||||
|
|
||||||
<span>{{entryList.name}}</span><span
|
<span>{{entryList.name}}</span><span
|
||||||
ng-show="selectedType == 'category'"> »</span>
|
ng-show="selectedType == 'category'"> »</span>
|
||||||
<div class="accordion" id="feed-accordion">
|
<div class="accordion" id="feed-accordion">
|
||||||
|
|||||||
Reference in New Issue
Block a user