diff --git a/app/src/main/java/com/kabouzeid/gramophone/util/PlaylistsUtil.java b/app/src/main/java/com/kabouzeid/gramophone/util/PlaylistsUtil.java index 3bfa079ae..3c19f0a53 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/util/PlaylistsUtil.java +++ b/app/src/main/java/com/kabouzeid/gramophone/util/PlaylistsUtil.java @@ -8,12 +8,15 @@ import android.os.Environment; import android.provider.BaseColumns; import android.provider.MediaStore; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; + import android.widget.Toast; import com.kabouzeid.gramophone.R; import com.kabouzeid.gramophone.helper.M3UWriter; +import com.kabouzeid.gramophone.loader.PlaylistSongLoader; import com.kabouzeid.gramophone.model.Playlist; import com.kabouzeid.gramophone.model.PlaylistSong; import com.kabouzeid.gramophone.model.Song; @@ -111,7 +114,21 @@ public static void addToPlaylist(@NonNull final Context context, @NonNull final final Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId); Cursor cursor = null; int base = 0; - + List previousSong = PlaylistSongLoader.getPlaylistSongList(context, playlistId); + List newSongs = new ArrayList<>(); + for (Song song : songs) { + boolean flag = false; + for (Song previous : previousSong) { + if (song.id == previous.id) { + flag = true; + break; + } + } + if (!flag) { + newSongs.add(song); + } + } + final int newSize = newSongs.size(); try { try { cursor = resolver.query(uri, projection, null, null, null); @@ -126,12 +143,11 @@ public static void addToPlaylist(@NonNull final Context context, @NonNull final } int numInserted = 0; - for (int offSet = 0; offSet < size; offSet += 1000) - numInserted += resolver.bulkInsert(uri, makeInsertItems(songs, offSet, 1000, base)); - + for (int offSet = 0; offSet < newSize; offSet += 1000) + numInserted += resolver.bulkInsert(uri, makeInsertItems(newSongs, offSet, 1000, base)); if (showToastOnFinish) { Toast.makeText(context, context.getResources().getString( - R.string.inserted_x_songs_into_playlist_x, numInserted, getNameForPlaylist(context, playlistId)), Toast.LENGTH_SHORT).show(); + R.string.inserted_x_songs_into_playlist_x, numInserted, getNameForPlaylist(context, playlistId), size - numInserted), Toast.LENGTH_SHORT).show(); } } catch (SecurityException ignored) { } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index bded8aa70..eefa89bd6 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -66,7 +66,7 @@ Remove from playlist Grid size Grid size (land) - Inserted %1$d songs into the playlist %2$s. + Inserted %1$d songs into the playlist %2$s, %3$d of selected songs existed in this playlist. Created playlist %1$s. Deleted %1$d songs. Playlist %1$s already exists.