Skip to content

Commit

Permalink
Fix playlist reorder doing nonsense
Browse files Browse the repository at this point in the history
  • Loading branch information
martpie committed Apr 3, 2024
1 parent c480835 commit b853f55
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
14 changes: 12 additions & 2 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ bonsaidb = { version = "0.5.0", features = ["local", "async"] }
dirs = "5.0.1"
futures = "0.3.30"
home-config = { version = "0.6.0", features = ["toml"] }
itertools = "0.12.1"
log = "0.4.21"
lofty = "0.18.2"
memoize = "0.4.2"
Expand Down
10 changes: 3 additions & 7 deletions src-tauri/src/plugins/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use bonsaidb::core::transaction::{Operation, Transaction};
use bonsaidb::local::config::{Builder as BonsaiBuilder, StorageConfiguration};
use bonsaidb::local::AsyncDatabase;
use bonsaidb::local::AsyncStorage;
use itertools::Itertools;
use lofty::{Accessor, AudioFile, ItemKey, TaggedFileExt};
use log::{error, info, warn};
use rayon::prelude::*;
Expand Down Expand Up @@ -197,13 +198,8 @@ impl DB {
if let Some(document) = self.playlists_collection().get(id).await? {
let mut playlist = self.decode_doc::<Playlist>(&document)?;

// Insert new tracks + make sure we remove duplicates (the UI does
// not play well with those).
playlist.tracks = tracks
.into_iter()
.collect::<HashSet<_>>()
.into_iter()
.collect::<Vec<_>>();
// Make sure we remove duplicates (the UI does not play well with those yet).
playlist.tracks = tracks.into_iter().unique().collect_vec();

match playlist.overwrite_into_async(&id, &self.playlists).await {
Ok(doc) => Ok(doc.contents),
Expand Down
4 changes: 4 additions & 0 deletions src/stores/PlaylistsAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ const reorderTracks = async (
try {
const playlist: Playlist = await database.getPlaylist(playlistID);

// Remove the current track
const newTracks = playlist.tracks.filter((id) => !tracksIDs.includes(id));

// Find where to insert the selected tracks
let targetIndex = newTracks.indexOf(targetTrackID);

if (targetIndex === -1) {
Expand All @@ -157,6 +160,7 @@ const reorderTracks = async (

newTracks.splice(targetIndex + 1, 0, ...tracksIDs);

// Save and reload the playlist
await database.setPlaylistTracks(playlistID, newTracks);
invalidate();
} catch (err) {
Expand Down

0 comments on commit b853f55

Please sign in to comment.