From 7b3e84ab47fa776860dc79428210b2438ca62a88 Mon Sep 17 00:00:00 2001 From: Lazy_Boy Date: Mon, 5 Feb 2024 12:40:18 +0000 Subject: [PATCH 1/2] playlist creation naming - added logic for shortening playlist name. - added logic for appending a duplicate number to playlist name --- .../controllers/MainController.java | 52 ++++++++++++++++--- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/src/main/java/app/musicplayer/controllers/MainController.java b/src/main/java/app/musicplayer/controllers/MainController.java index 91613aa..3bc6508 100644 --- a/src/main/java/app/musicplayer/controllers/MainController.java +++ b/src/main/java/app/musicplayer/controllers/MainController.java @@ -259,19 +259,55 @@ private void onClickAddNewPlaylist() { String text = textBox.getText(); if (!text.isEmpty()) { + + //ensure playlist name is correct length + if (text.length() > MAX_PLAYLIST_TITLE_LENGTH) { + + text=text.substring(0, MAX_PLAYLIST_TITLE_LENGTH); + } + //is the playlist a duplicate if (library.findPlaylistByTitle(text).isPresent()) { - System.out.println("TODO: Playlist already exists"); - } else { + //get user added duplicate number, if it exists + String previousDuplicateNumber=""; + int DuplicateLeng=1; + while (true){ + //loop through characters back to front to acquire user entered number + char digit =text.charAt(text.length()-DuplicateLeng); + if(Character.isDigit(digit)){ + //append number to front of string + previousDuplicateNumber=digit+previousDuplicateNumber; + DuplicateLeng++; + }else { + //reached end of number, exit loop + DuplicateLeng--; + break; + } + } + //add number to end to create unique name + int DuplicateNumber=2; - if (text.length() > MAX_PLAYLIST_TITLE_LENGTH) { - System.out.println("TODO: Playlist title is too long"); - } else { - // all good - var playlist = library.addPlaylist(text); + if(!previousDuplicateNumber.isEmpty()){ + //set duplicate number to the number from text + text=text.substring(0,text.length()-DuplicateLeng); + DuplicateNumber=Integer.parseInt(previousDuplicateNumber); + } + //increment duplicate number until a unique value is found + while (library.findPlaylistByTitle(text+ DuplicateNumber).isPresent()){ - addNewPlaylistToUI(playlist); + DuplicateNumber++; + } + text+=DuplicateNumber; + + //make sure duplicate number does not make the name too long + if (text.length() > MAX_PLAYLIST_TITLE_LENGTH) { + int overLeng=-text.length()-MAX_PLAYLIST_TITLE_LENGTH; + //remove front of string to signify to user this was a duplicate, which was too long + text=text.substring(overLeng); } } + //add playlist to library + var playlist = library.addPlaylist(text); + addNewPlaylistToUI(playlist); } canAnimateNewPlaylist = true; From 9da1c61d9a3e4d38ec45de25fa6fde18a4f69605 Mon Sep 17 00:00:00 2001 From: Lazy_Boy Date: Mon, 5 Feb 2024 16:29:11 +0000 Subject: [PATCH 2/2] conform playlist name function wraps previous commit into a function, allowing it to be reused for the re-name playlist as well as the create playlist. --- .../controllers/MainController.java | 111 +++++++++--------- 1 file changed, 54 insertions(+), 57 deletions(-) diff --git a/src/main/java/app/musicplayer/controllers/MainController.java b/src/main/java/app/musicplayer/controllers/MainController.java index 3bc6508..b6f522e 100644 --- a/src/main/java/app/musicplayer/controllers/MainController.java +++ b/src/main/java/app/musicplayer/controllers/MainController.java @@ -43,6 +43,7 @@ import org.jaudiotagger.audio.AudioFile; import org.jaudiotagger.audio.AudioFileIO; import org.jaudiotagger.tag.Tag; +import org.jetbrains.annotations.NotNull; import java.io.ByteArrayInputStream; import java.io.File; @@ -240,6 +241,56 @@ private void addNewPlaylistToUI(Playlist playlist) { } } + //conform playlist name to max length and prevent duplicates via numbering + private String conformPlaylistName(@NotNull String input){ + //ensure playlist name is correct length + if (input.length() > MAX_PLAYLIST_TITLE_LENGTH) { + + input=input.substring(0, MAX_PLAYLIST_TITLE_LENGTH); + } + //is the playlist a duplicate + if (library.findPlaylistByTitle(input).isPresent()) { + //get user added duplicate number, if it exists + String previousDuplicateNumber=""; + int DuplicateLeng=1; + while (true){ + //loop through characters back to front to acquire user entered number + char digit =input.charAt(input.length()-DuplicateLeng); + if(Character.isDigit(digit)){ + //append number to front of string + previousDuplicateNumber=digit+previousDuplicateNumber; + DuplicateLeng++; + }else { + //reached end of number, exit loop + DuplicateLeng--; + break; + } + } + //add number to end to create unique name + int DuplicateNumber=2; + + if(!previousDuplicateNumber.isEmpty()){ + //set duplicate number to the number from text + input=input.substring(0,input.length()-DuplicateLeng); + DuplicateNumber=Integer.parseInt(previousDuplicateNumber); + } + //increment duplicate number until a unique value is found + while (library.findPlaylistByTitle(input+ DuplicateNumber).isPresent()){ + + DuplicateNumber++; + } + input+=DuplicateNumber; + + //make sure duplicate number does not make the name too long + if (input.length() > MAX_PLAYLIST_TITLE_LENGTH) { + int overLeng=-input.length()-MAX_PLAYLIST_TITLE_LENGTH; + //remove front of string to signify to user this was a duplicate, which was too long + input=input.substring(overLeng); + } + } + return input; + } + @FXML private void onClickAddNewPlaylist() { if (!canAnimateNewPlaylist) @@ -260,53 +311,9 @@ private void onClickAddNewPlaylist() { if (!text.isEmpty()) { - //ensure playlist name is correct length - if (text.length() > MAX_PLAYLIST_TITLE_LENGTH) { - text=text.substring(0, MAX_PLAYLIST_TITLE_LENGTH); - } - //is the playlist a duplicate - if (library.findPlaylistByTitle(text).isPresent()) { - //get user added duplicate number, if it exists - String previousDuplicateNumber=""; - int DuplicateLeng=1; - while (true){ - //loop through characters back to front to acquire user entered number - char digit =text.charAt(text.length()-DuplicateLeng); - if(Character.isDigit(digit)){ - //append number to front of string - previousDuplicateNumber=digit+previousDuplicateNumber; - DuplicateLeng++; - }else { - //reached end of number, exit loop - DuplicateLeng--; - break; - } - } - //add number to end to create unique name - int DuplicateNumber=2; - - if(!previousDuplicateNumber.isEmpty()){ - //set duplicate number to the number from text - text=text.substring(0,text.length()-DuplicateLeng); - DuplicateNumber=Integer.parseInt(previousDuplicateNumber); - } - //increment duplicate number until a unique value is found - while (library.findPlaylistByTitle(text+ DuplicateNumber).isPresent()){ - - DuplicateNumber++; - } - text+=DuplicateNumber; - - //make sure duplicate number does not make the name too long - if (text.length() > MAX_PLAYLIST_TITLE_LENGTH) { - int overLeng=-text.length()-MAX_PLAYLIST_TITLE_LENGTH; - //remove front of string to signify to user this was a duplicate, which was too long - text=text.substring(overLeng); - } - } //add playlist to library - var playlist = library.addPlaylist(text); + var playlist = library.addPlaylist(conformPlaylistName(text)); addNewPlaylistToUI(playlist); } @@ -373,18 +380,8 @@ public void onClickPlaylistMenu(MouseEvent e, Playlist playlist) { String text = textBox.getText(); - if (!text.isEmpty()) { - if (library.findPlaylistByTitle(text).isPresent()) { - System.out.println("TODO: Playlist already exists"); - } else { - - if (text.length() > MAX_PLAYLIST_TITLE_LENGTH) { - System.out.println("TODO: Playlist title is too long"); - } else { - // all good - playlist.setTitle(text); - } - } + if (!text.isEmpty()&& !Objects.equals(playlist.getTitle(), text)) { + playlist.setTitle(conformPlaylistName(text)); } } });