Add confirmation imodal when adding duplicate tracks to a playlist
Use similar copy as official iOS client
This commit is contained in:
1 parent
e48bbb99df
commit
512a411292
6 files changed
+248
-6
No files matched your search
@@ -254,6 +254,14 @@ pub struct PlaylistGenre {
|
||||
pub slug: Option<String>,
|
||||
}
|
||||
|
||||
/// Result of checking for duplicate tracks in a playlist
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct PlaylistDuplicateResult {
|
||||
pub total_tracks: usize,
|
||||
pub duplicate_count: usize,
|
||||
pub duplicate_track_ids: std::collections::HashSet<u64>,
|
||||
}
|
||||
|
||||
/// Image set with multiple resolutions
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||
pub struct ImageSet {
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
use serde::Serialize;
|
||||
use tauri::State;
|
||||
|
||||
use crate::api::models::{Playlist, SearchResultsPage, Track};
|
||||
use crate::api::models::{Playlist, PlaylistDuplicateResult, SearchResultsPage, Track};
|
||||
use crate::api::performers::{parse_performers, Performer};
|
||||
use crate::AppState;
|
||||
|
||||
@@ -43,6 +43,50 @@ pub async fn get_playlist(
|
||||
.map_err(|e| format!("Failed to get playlist: {}", e))
|
||||
}
|
||||
|
||||
/// Check for duplicate tracks in a playlist
|
||||
#[tauri::command]
|
||||
pub async fn check_playlist_duplicates(
|
||||
playlist_id: u64,
|
||||
track_ids: Vec<u64>,
|
||||
state: State<'_, AppState>,
|
||||
) -> Result<PlaylistDuplicateResult, String> {
|
||||
log::debug!(
|
||||
"Command: check_playlist_duplicates {} ({} tracks)",
|
||||
playlist_id,
|
||||
track_ids.len()
|
||||
);
|
||||
|
||||
let client = state.client.lock().await;
|
||||
|
||||
let playlist = client
|
||||
.get_playlist(playlist_id)
|
||||
.await
|
||||
.map_err(|e| format!("Failed to get playlist: {}", e))?;
|
||||
|
||||
let incoming_track_ids: std::collections::HashSet<u64> = track_ids.iter().copied().collect();
|
||||
|
||||
let duplicate_track_ids: std::collections::HashSet<u64> = playlist
|
||||
.tracks
|
||||
.map(|tracks| {
|
||||
tracks
|
||||
.items
|
||||
.into_iter()
|
||||
.filter(|track| incoming_track_ids.contains(&track.id))
|
||||
.map(|track| track.id)
|
||||
.collect()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
|
||||
let duplicate_count = duplicate_track_ids.len();
|
||||
let total_tracks = track_ids.len();
|
||||
|
||||
Ok(PlaylistDuplicateResult {
|
||||
total_tracks,
|
||||
duplicate_count,
|
||||
duplicate_track_ids,
|
||||
})
|
||||
}
|
||||
|
||||
/// Search playlists
|
||||
#[tauri::command]
|
||||
pub async fn search_playlists(
|
||||
|
||||
@@ -490,6 +490,7 @@ pub fn run() {
|
||||
// Playlist commands
|
||||
commands::get_user_playlists,
|
||||
commands::get_playlist,
|
||||
commands::check_playlist_duplicates,
|
||||
commands::search_playlists,
|
||||
commands::create_playlist,
|
||||
commands::delete_playlist,
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
<script lang="ts">
|
||||
import { createEventDispatcher } from 'svelte';
|
||||
import Modal from './Modal.svelte';
|
||||
import type { PlaylistDuplicateResult } from '$lib/types/index';
|
||||
|
||||
export let isOpen = false;
|
||||
export let duplicateResult: PlaylistDuplicateResult | null = null;
|
||||
export let loading = false;
|
||||
|
||||
const dispatch = createEventDispatcher();
|
||||
|
||||
function handleAddAll() {
|
||||
dispatch('addAll');
|
||||
}
|
||||
|
||||
function handleSkipDuplicates() {
|
||||
dispatch('skipDuplicates');
|
||||
}
|
||||
|
||||
function handleCancel() {
|
||||
dispatch('cancel');
|
||||
}
|
||||
|
||||
function handleClose() {
|
||||
if (!loading) {
|
||||
dispatch('cancel');
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<Modal {isOpen} onClose={handleClose} title={'You\'ve added some of these tracks before'}>
|
||||
{#if duplicateResult}
|
||||
<div class="duplicate-info">
|
||||
<p>
|
||||
This playlist already contains <strong>{duplicateResult.duplicate_count}</strong> of the
|
||||
track{duplicateResult.duplicate_count !== 1 ? 's' : ''} you're adding.
|
||||
Add only the new ones, or add everything including duplicates ({duplicateResult.total_tracks} track{duplicateResult.total_tracks !== 1 ? 's' : ''}).
|
||||
</p>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#snippet footer()}
|
||||
<div class="footer-right">
|
||||
<button
|
||||
class="btn btn-secondary"
|
||||
on:click={handleAddAll}
|
||||
disabled={loading}
|
||||
>
|
||||
{#if loading}
|
||||
Adding...
|
||||
{:else}
|
||||
Add all tracks
|
||||
{/if}
|
||||
</button>
|
||||
|
||||
<button
|
||||
class="btn btn-primary"
|
||||
on:click={handleSkipDuplicates}
|
||||
disabled={loading || duplicateResult?.duplicate_count === 0}
|
||||
>
|
||||
{#if loading}
|
||||
Adding...
|
||||
{:else}
|
||||
Add only new tracks
|
||||
{/if}
|
||||
</button>
|
||||
</div>
|
||||
{/snippet}
|
||||
</Modal>
|
||||
|
||||
<style>
|
||||
.duplicate-info {
|
||||
margin-bottom: 24px;
|
||||
}
|
||||
|
||||
.duplicate-info p {
|
||||
margin: 0;
|
||||
color: var(--text-secondary);
|
||||
line-height: 1.4;
|
||||
}
|
||||
|
||||
.footer-right {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
margin-left: auto;
|
||||
}
|
||||
</style>
|
||||
@@ -5,6 +5,8 @@
|
||||
import { subscribe as subscribeOffline, getStatus, createPendingPlaylist } from '$lib/stores/offlineStore';
|
||||
import { showToast } from '$lib/stores/toastStore';
|
||||
import { t } from '$lib/i18n';
|
||||
import PlaylistDuplicateConfirmModal from './PlaylistDuplicateConfirmModal.svelte';
|
||||
import type { PlaylistDuplicateResult } from '$lib/types/index';
|
||||
import {
|
||||
subscribe as subscribeFolders,
|
||||
getVisibleFolders,
|
||||
@@ -76,6 +78,10 @@
|
||||
let localTrackCounts = $state<Map<number, number>>(new Map());
|
||||
let showDeleteConfirm = $state(false);
|
||||
|
||||
// State for duplicate checking
|
||||
let showDuplicateConfirm = $state(false);
|
||||
let duplicateResult = $state<PlaylistDuplicateResult | null>(null);
|
||||
|
||||
// Load local track counts
|
||||
async function loadLocalTrackCounts() {
|
||||
try {
|
||||
@@ -340,6 +346,48 @@
|
||||
return;
|
||||
}
|
||||
|
||||
// For pending playlists and local tracks add directly
|
||||
if (selectedPlaylistId < 0 || isLocalTracks) {
|
||||
await handleDirectAddToPlaylist();
|
||||
return;
|
||||
}
|
||||
|
||||
loading = true;
|
||||
error = null;
|
||||
|
||||
try {
|
||||
// Check for duplicates in regular playlists with Qobuz tracks
|
||||
const result = await invoke<PlaylistDuplicateResult>('check_playlist_duplicates', {
|
||||
playlistId: selectedPlaylistId,
|
||||
trackIds
|
||||
});
|
||||
|
||||
duplicateResult = {
|
||||
...result,
|
||||
duplicate_track_ids: new Set(result.duplicate_track_ids)
|
||||
};
|
||||
|
||||
if (duplicateResult.duplicate_count > 0) {
|
||||
// There are duplicates. Show confirmation dialog
|
||||
// to allow the user to decide on what action to take
|
||||
showDuplicateConfirm = true;
|
||||
} else {
|
||||
await executeAddTracks(false);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Failed to check playlist duplicates:', err);
|
||||
error = String(err);
|
||||
} finally {
|
||||
loading = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function handleDirectAddToPlaylist() {
|
||||
if (!selectedPlaylistId || trackIds.length === 0) {
|
||||
error = 'Please select a playlist';
|
||||
return;
|
||||
}
|
||||
|
||||
loading = true;
|
||||
error = null;
|
||||
|
||||
@@ -382,11 +430,7 @@
|
||||
}
|
||||
} else {
|
||||
// Regular playlist with Qobuz tracks
|
||||
await invoke('add_tracks_to_playlist', {
|
||||
playlistId: selectedPlaylistId,
|
||||
trackIds
|
||||
});
|
||||
void logPlaylistAdd(trackIds, selectedPlaylistId);
|
||||
await executeAddTracks(false);
|
||||
}
|
||||
onSuccess?.();
|
||||
onClose();
|
||||
@@ -398,6 +442,48 @@
|
||||
}
|
||||
}
|
||||
|
||||
async function executeAddTracks(skipDuplicates: boolean) {
|
||||
try {
|
||||
let tracksToAdd = trackIds;
|
||||
|
||||
if (skipDuplicates) {
|
||||
tracksToAdd = trackIds.filter(id => !duplicateResult?.duplicate_track_ids.has(id));
|
||||
}
|
||||
|
||||
if (tracksToAdd.length === 0) {
|
||||
onSuccess?.();
|
||||
onClose();
|
||||
showDuplicateConfirm = false;
|
||||
return;
|
||||
}
|
||||
|
||||
await invoke('add_tracks_to_playlist', {
|
||||
playlistId: selectedPlaylistId!,
|
||||
trackIds: tracksToAdd
|
||||
});
|
||||
void logPlaylistAdd(trackIds, selectedPlaylistId!);
|
||||
onSuccess?.();
|
||||
onClose();
|
||||
showDuplicateConfirm = false;
|
||||
} catch (err) {
|
||||
console.error('Failed to add tracks to playlist:', err);
|
||||
error = String(err);
|
||||
}
|
||||
}
|
||||
|
||||
function handleDuplicateAddAll() {
|
||||
executeAddTracks(false);
|
||||
}
|
||||
|
||||
function handleDuplicateSkipDuplicates() {
|
||||
executeAddTracks(true);
|
||||
}
|
||||
|
||||
function handleDuplicateCancel() {
|
||||
showDuplicateConfirm = false;
|
||||
duplicateResult = null;
|
||||
}
|
||||
|
||||
async function handleCreateAndAdd() {
|
||||
if (!name.trim()) {
|
||||
error = 'Please enter a playlist name';
|
||||
@@ -733,6 +819,15 @@
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<PlaylistDuplicateConfirmModal
|
||||
bind:isOpen={showDuplicateConfirm}
|
||||
bind:duplicateResult={duplicateResult}
|
||||
bind:loading={loading}
|
||||
on:addAll={handleDuplicateAddAll}
|
||||
on:skipDuplicates={handleDuplicateSkipDuplicates}
|
||||
on:cancel={handleDuplicateCancel}
|
||||
/>
|
||||
|
||||
<style>
|
||||
.modal-overlay {
|
||||
position: fixed;
|
||||
|
||||
@@ -113,6 +113,12 @@ export interface QobuzPlaylist {
|
||||
duration?: number;
|
||||
}
|
||||
|
||||
export interface PlaylistDuplicateResult {
|
||||
total_tracks: number;
|
||||
duplicate_count: number;
|
||||
duplicate_track_ids: Set<number>;
|
||||
}
|
||||
|
||||
export interface QobuzArtist {
|
||||
id: number;
|
||||
name: string;
|
||||
|
||||
Reference in new issue
Block a user