Simplify moving an album up and down in its parent.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 12 Oct 2013 23:18:42 +0000 (01:18 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:24 +0000 (22:25 +0100)
src/main/java/net/pterodactylus/sone/data/Album.java
src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java
src/main/java/net/pterodactylus/sone/web/EditAlbumPage.java
src/main/java/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPage.java

index 26da9b7..df514d4 100644 (file)
@@ -121,28 +121,6 @@ public interface Album extends Identified, Fingerprintable {
        List<Album> 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
-        *         <code>null</code> 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
-        *         <code>null</code> 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;
 
        /**
index cbef5da..161cdd9 100644 (file)
@@ -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<Image> getImages() {
                return new ArrayList<Image>(Collections2.filter(Collections2.transform(imageIds, new Function<String, Image>() {
 
@@ -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();
index 3de23a3..e4fbdd5 100644 (file)
@@ -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());
                        }
index 6652cd1..1bb419b 100644 (file)
@@ -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();