mirror of
https://github.com/johrpan/musicus.git
synced 2025-10-26 11:47:25 +01:00
database: Add track iterator for mediums
This commit is contained in:
parent
545bacabab
commit
616e8c0170
3 changed files with 87 additions and 0 deletions
|
|
@ -22,6 +22,13 @@ pub struct Medium {
|
|||
pub tracks: Vec<TrackSet>,
|
||||
}
|
||||
|
||||
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<()> {
|
||||
|
|
|
|||
|
|
@ -18,6 +18,41 @@
|
|||
<property name="icon-name">go-previous-symbolic</property>
|
||||
</object>
|
||||
</child>
|
||||
<child type="end">
|
||||
<object class="GtkButton" id="import_button">
|
||||
<property name="sensitive">False</property>
|
||||
<child>
|
||||
<object class="GtkStack" id="done_stack">
|
||||
<property name="transition-type">crossfade</property>
|
||||
<property name="interpolate-size">true</property>
|
||||
<property name="hhomogeneous">false</property>
|
||||
<child>
|
||||
<object class="GtkStackPage">
|
||||
<property name="name">loading</property>
|
||||
<property name="child">
|
||||
<object class="GtkSpinner">
|
||||
<property name="spinning">True</property>
|
||||
</object>
|
||||
</property>
|
||||
</object>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkStackPage">
|
||||
<property name="name">ready</property>
|
||||
<property name="child">
|
||||
<object class="GtkLabel">
|
||||
<property name="label" translatable="yes">Import</property>
|
||||
</object>
|
||||
</property>
|
||||
</object>
|
||||
</child>
|
||||
</object>
|
||||
</child>
|
||||
<style>
|
||||
<class name="suggested-action"/>
|
||||
</style>
|
||||
</object>
|
||||
</child>
|
||||
</object>
|
||||
</child>
|
||||
<child>
|
||||
|
|
|
|||
|
|
@ -28,6 +28,8 @@ impl Screen<(Arc<ImportSession>, 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);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue