From 616e8c0170462b2464c0451effe7ca323ffdc4d0 Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Sun, 28 Mar 2021 18:37:17 +0200 Subject: [PATCH] database: Add track iterator for mediums --- database/src/medium.rs | 50 ++++++++++++++++++++++++++++ musicus/res/ui/medium_preview.ui | 35 +++++++++++++++++++ musicus/src/import/medium_preview.rs | 2 ++ 3 files changed, 87 insertions(+) diff --git a/database/src/medium.rs b/database/src/medium.rs index 7eea061..d6e357a 100644 --- a/database/src/medium.rs +++ b/database/src/medium.rs @@ -22,6 +22,13 @@ pub struct Medium { pub tracks: Vec, } +impl Medium { + /// Get an iterator that iterates through all tracks within this medium in order. + pub fn track_iter<'a>(&'a self) -> TrackIter<'a> { + TrackIter::new(self) + } +} + /// A set of tracks of one recording within a medium. #[derive(Serialize, Deserialize, Debug, Clone)] #[serde(rename_all = "camelCase")] @@ -77,6 +84,49 @@ struct TrackRow { pub path: String, } +/// An iterator that iterates through all tracks within a medium in order. +pub struct TrackIter<'a> { + medium: &'a Medium, + track_set_index: usize, + track_index: usize, +} + +impl<'a> TrackIter<'a> { + /// Create a new iterator that iterates through tracks within the provided medium. + fn new(medium: &'a Medium) -> Self { + Self { + medium, + track_set_index: 0, + track_index: 0, + } + } +} + +impl<'a> Iterator for TrackIter<'a> { + type Item = &'a Track; + + fn next(&mut self) -> Option<&'a Track> { + match self.medium.tracks.get(self.track_set_index) { + Some(track_set) => { + match track_set.tracks.get(self.track_index) { + Some(track) => { + if self.track_index + 1 < track_set.tracks.len() { + self.track_index += 1; + } else { + self.track_set_index += 1; + self.track_index = 0; + } + + Some(track) + } + None => None, + } + } + None => None, + } + } +} + impl Database { /// Update an existing medium or insert a new one. pub fn update_medium(&self, medium: Medium) -> Result<()> { diff --git a/musicus/res/ui/medium_preview.ui b/musicus/res/ui/medium_preview.ui index f6c3ed2..5f25d3f 100644 --- a/musicus/res/ui/medium_preview.ui +++ b/musicus/res/ui/medium_preview.ui @@ -18,6 +18,41 @@ go-previous-symbolic + + + False + + + crossfade + true + false + + + loading + + + True + + + + + + + ready + + + Import + + + + + + + + + diff --git a/musicus/src/import/medium_preview.rs b/musicus/src/import/medium_preview.rs index 6323204..0ec3bc6 100644 --- a/musicus/src/import/medium_preview.rs +++ b/musicus/src/import/medium_preview.rs @@ -28,6 +28,8 @@ impl Screen<(Arc, Medium), ()> for MediumPreview { get_widget!(builder, gtk::Box, widget); get_widget!(builder, gtk::Button, back_button); + get_widget!(builder, gtk::Button, import_button); + get_widget!(builder, gtk::Stack, done_stack); get_widget!(builder, gtk::Box, medium_box); get_widget!(builder, gtk::Label, name_label);