Implement tracks import

This commit is contained in:
Elias Projahn 2025-02-16 16:30:35 +01:00
parent fca6ce841c
commit 740ad0cf0b
3 changed files with 181 additions and 18 deletions

View file

@ -1,6 +1,6 @@
use super::tracks_editor_track_row::{PathType, TracksEditorTrackData};
use super::tracks_editor_track_row::{TrackLocation, TracksEditorTrackData};
use crate::{
db::models::{Recording, Work},
db::models::{Recording, Track, Work},
editor::{
recording_editor::MusicusRecordingEditor,
recording_selector_popover::RecordingSelectorPopover,
@ -37,6 +37,7 @@ mod imp {
pub recording: RefCell<Option<Recording>>,
pub recordings_popover: OnceCell<RecordingSelectorPopover>,
pub track_rows: RefCell<Vec<TracksEditorTrackRow>>,
pub removed_tracks: RefCell<Vec<Track>>,
#[template_child]
pub recording_row: TemplateChild<adw::ActionRow>,
@ -196,6 +197,9 @@ impl TracksEditor {
self.imp().track_list.remove(&track_row);
}
// Forget previously removed tracks (see above).
self.imp().removed_tracks.borrow_mut().clear();
let tracks = self
.library()
.tracks_for_recording(&recording.recording_id)
@ -208,8 +212,7 @@ impl TracksEditor {
self.add_track_row(
recording.clone(),
TracksEditorTrackData {
track_id: Some(track.track_id),
path: PathType::Library(track.path),
location: TrackLocation::Library(track.clone()),
parts: track.works,
},
);
@ -246,8 +249,7 @@ impl TracksEditor {
self.add_track_row(
recording.to_owned(),
TracksEditorTrackData {
track_id: None,
path: PathType::System(path),
location: TrackLocation::System(path),
parts: next_part,
},
);
@ -264,6 +266,10 @@ impl TracksEditor {
#[weak(rename_to = this)]
self,
move |row| {
if let TrackLocation::Library(track) = row.track_data().location {
this.imp().removed_tracks.borrow_mut().push(track);
}
this.imp().track_list.remove(row);
this.imp().track_rows.borrow_mut().retain(|p| p != row);
}
@ -278,7 +284,39 @@ impl TracksEditor {
#[template_callback]
fn save(&self) {
// TODO
for track in self.imp().removed_tracks.borrow_mut().drain(..) {
self.library().delete_track(&track).unwrap();
}
for (index, track_row) in self.imp().track_rows.borrow_mut().drain(..).enumerate() {
let track_data = track_row.track_data();
match track_data.location {
TrackLocation::Undefined => {
log::error!("Failed to save track: Undefined track location.");
}
TrackLocation::Library(track) => self
.library()
.update_track(&track.track_id, index as i32, track_data.parts)
.unwrap(),
TrackLocation::System(path) => {
if let Some(recording) = &*self.imp().recording.borrow() {
self.library()
.import_track(
&path,
&recording.recording_id,
index as i32,
track_data.parts,
)
.unwrap();
} else {
log::error!("Failed to save track: No recording set.");
}
}
}
self.imp().track_list.remove(&track_row);
}
self.navigation().pop();
}

View file

@ -1,5 +1,5 @@
use crate::{
db::models::{Recording, Work},
db::models::{Recording, Track, Work},
editor::tracks_editor_parts_popover::TracksEditorPartsPopover,
library::MusicusLibrary,
};
@ -90,10 +90,10 @@ impl TracksEditorTrackRow {
obj.set_activatable(!recording.work.parts.is_empty());
obj.set_subtitle(&match &track_data.path {
PathType::None => String::new(),
PathType::Library(path) => path.to_owned(),
PathType::System(path) => {
obj.set_subtitle(&match &track_data.location {
TrackLocation::Undefined => String::new(),
TrackLocation::Library(track) => track.path.clone(),
TrackLocation::System(path) => {
let format_string = gettext("Import from {}");
let file_name = path.file_name().unwrap().to_str().unwrap();
match formatx!(&format_string, file_name) {
@ -178,15 +178,14 @@ impl TracksEditorTrackRow {
#[derive(Clone, Default, Debug)]
pub struct TracksEditorTrackData {
pub track_id: Option<String>,
pub path: PathType,
pub location: TrackLocation,
pub parts: Vec<Work>,
}
#[derive(Clone, Default, Debug)]
pub enum PathType {
pub enum TrackLocation {
#[default]
None,
Library(String),
Undefined,
Library(Track),
System(PathBuf),
}