From 84e3f241029a4a0a63069a0a793b4f09408d3ca6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 13 Oct 2013 01:18:42 +0200 Subject: [PATCH] Simplify moving an album up and down in its parent. --- .../java/net/pterodactylus/sone/data/Album.java | 26 +++----------- .../pterodactylus/sone/data/impl/DefaultAlbum.java | 42 ++++++++-------------- .../net/pterodactylus/sone/web/EditAlbumPage.java | 4 +-- .../sone/web/ajax/EditAlbumAjaxPage.java | 8 ++--- 4 files changed, 24 insertions(+), 56 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/data/Album.java b/src/main/java/net/pterodactylus/sone/data/Album.java index 26da9b7..df514d4 100644 --- a/src/main/java/net/pterodactylus/sone/data/Album.java +++ b/src/main/java/net/pterodactylus/sone/data/Album.java @@ -121,28 +121,6 @@ public interface Album extends Identified, Fingerprintable { List getAlbums(); /** - * Moves the given album up in this album’s albums. If the album is already the - * first album, nothing happens. - * - * @param album - * The album to move up - * @return The album that the given album swapped the place with, or - * null if the album did not change its place - */ - Album moveAlbumUp(Album album); - - /** - * Moves the given album down in this album’s albums. If the album is already - * the last album, nothing happens. - * - * @param album - * The album to move down - * @return The album that the given album swapped the place with, or - * null if the album did not change its place - */ - Album moveAlbumDown(Album album); - - /** * Returns the images in this album. * * @return The images in this album @@ -229,6 +207,10 @@ public interface Album extends Identified, Fingerprintable { */ Modifier modify() throws IllegalStateException; + void moveUp(); + + void moveDown(); + void remove() throws IllegalStateException; /** diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java index cbef5da..161cdd9 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java @@ -92,34 +92,6 @@ public class DefaultAlbum extends AbstractAlbum { } @Override - public Album moveAlbumUp(Album album) { - checkNotNull(album, "album must not be null"); - checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album"); - checkArgument(equals(album.getParent()), "album must belong to this album"); - int oldIndex = albums.indexOf(album); - if (oldIndex <= 0) { - return null; - } - albums.remove(oldIndex); - albums.add(oldIndex - 1, album); - return albums.get(oldIndex); - } - - @Override - public Album moveAlbumDown(Album album) { - checkNotNull(album, "album must not be null"); - checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album"); - checkArgument(equals(album.getParent()), "album must belong to this album"); - int oldIndex = albums.indexOf(album); - if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) { - return null; - } - albums.remove(oldIndex); - albums.add(oldIndex + 1, album); - return albums.get(oldIndex); - } - - @Override public List getImages() { return new ArrayList(Collections2.filter(Collections2.transform(imageIds, new Function() { @@ -203,6 +175,20 @@ public class DefaultAlbum extends AbstractAlbum { } @Override + public void moveUp() { + int oldIndex = parent.albums.indexOf(this); + parent.albums.remove(this); + parent.albums.add(Math.max(0, oldIndex - 1), this); + } + + @Override + public void moveDown() { + int oldIndex = parent.albums.indexOf(this); + parent.albums.remove(this); + parent.albums.add(Math.min(parent.albums.size(), oldIndex + 1), this); + } + + @Override public void remove() throws IllegalStateException { checkState(!isRoot(), "can not remove root album"); removeAllAlbums(); diff --git a/src/main/java/net/pterodactylus/sone/web/EditAlbumPage.java b/src/main/java/net/pterodactylus/sone/web/EditAlbumPage.java index 3de23a3..e4fbdd5 100644 --- a/src/main/java/net/pterodactylus/sone/web/EditAlbumPage.java +++ b/src/main/java/net/pterodactylus/sone/web/EditAlbumPage.java @@ -61,11 +61,11 @@ public class EditAlbumPage extends SoneTemplatePage { throw new RedirectException("noPermission.html"); } if ("true".equals(request.getHttpRequest().getPartAsStringFailsafe("moveLeft", 4))) { - album.get().getParent().moveAlbumUp(album.get()); + album.get().moveUp(); webInterface.getCore().touchConfiguration(); throw new RedirectException("imageBrowser.html?album=" + album.get().getParent().getId()); } else if ("true".equals(request.getHttpRequest().getPartAsStringFailsafe("moveRight", 4))) { - album.get().getParent().moveAlbumDown(album.get()); + album.get().moveDown(); webInterface.getCore().touchConfiguration(); throw new RedirectException("imageBrowser.html?album=" + album.get().getParent().getId()); } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPage.java index 6652cd1..1bb419b 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPage.java @@ -59,14 +59,14 @@ public class EditAlbumAjaxPage extends JsonPage { return createErrorJsonObject("not-authorized"); } if ("true".equals(request.getHttpRequest().getParam("moveLeft"))) { - Album swappedAlbum = album.get().getParent().moveAlbumUp(album.get()); + album.get().moveUp(); webInterface.getCore().touchConfiguration(); - return createSuccessJsonObject().put("sourceAlbumId", album.get().getId()).put("destinationAlbumId", swappedAlbum.getId()); + return createSuccessJsonObject(); // TODO - fix javascript! } if ("true".equals(request.getHttpRequest().getParam("moveRight"))) { - Album swappedAlbum = album.get().getParent().moveAlbumDown(album.get()); + album.get().moveDown(); webInterface.getCore().touchConfiguration(); - return createSuccessJsonObject().put("sourceAlbumId", album.get().getId()).put("destinationAlbumId", swappedAlbum.getId()); + return createSuccessJsonObject(); // TODO - fix javascript! } String title = request.getHttpRequest().getParam("title").trim(); String description = request.getHttpRequest().getParam("description").trim(); -- 2.7.4