diff --git a/hammond-data/src/dbqueries.rs b/hammond-data/src/dbqueries.rs index 7adb91c..5e26fcd 100644 --- a/hammond-data/src/dbqueries.rs +++ b/hammond-data/src/dbqueries.rs @@ -84,6 +84,28 @@ pub fn get_episodes_with_limit(limit: u32) -> Result> { .load::(&*con)?) } +pub fn get_episodeswidgets_with_limit(limit: u32) -> Result> { + use schema::episode::dsl::*; + + let db = connection(); + let con = db.get()?; + + Ok(episode + .select(( + rowid, + title, + uri, + local_uri, + epoch, + length, + played, + podcast_id, + )) + .order(epoch.desc()) + .limit(i64::from(limit)) + .load::(&*con)?) +} + pub fn get_podcast_from_id(pid: i32) -> Result { use schema::podcast::dsl::*; diff --git a/hammond-gtk/resources/gtk/episode_widget.ui b/hammond-gtk/resources/gtk/episode_widget.ui index e7cf0a5..e0aa126 100644 --- a/hammond-gtk/resources/gtk/episode_widget.ui +++ b/hammond-gtk/resources/gtk/episode_widget.ui @@ -177,6 +177,7 @@ Cancel True True + True center @@ -191,6 +192,7 @@ delete_button True True + True end center @@ -213,6 +215,7 @@ True True True + True end center True @@ -235,6 +238,7 @@ True True + True center @@ -271,6 +275,7 @@ False + True False diff --git a/hammond-gtk/src/content.rs b/hammond-gtk/src/content.rs index 3bdf019..0ea2bfb 100644 --- a/hammond-gtk/src/content.rs +++ b/hammond-gtk/src/content.rs @@ -156,7 +156,7 @@ struct EpisodeStack { impl EpisodeStack { fn new() -> Rc { - let episodes = EpisodesView::default(); + let episodes = EpisodesView::new(); let empty = EmptyView::new(); let stack = gtk::Stack::new(); diff --git a/hammond-gtk/src/views/episodes.rs b/hammond-gtk/src/views/episodes.rs index e8eb3da..dc8c923 100644 --- a/hammond-gtk/src/views/episodes.rs +++ b/hammond-gtk/src/views/episodes.rs @@ -1,8 +1,12 @@ use gtk; use gtk::prelude::*; +use hammond_data::dbqueries; + use widgets::episode::EpisodeWidget; +use std::rc::Rc; + #[derive(Debug, Clone)] pub struct EpisodesView { pub container: gtk::Box, @@ -22,6 +26,38 @@ impl Default for EpisodesView { } } +impl EpisodesView { + pub fn new() -> Rc { + let view = EpisodesView::default(); + + let episodes = dbqueries::get_episodeswidgets_with_limit(100).unwrap(); + let frame = gtk::Frame::new("Recent Episodes"); + let list = gtk::ListBox::new(); + + view.frame_parent.add(&frame); + frame.add(&list); + + list.set_vexpand(false); + list.set_hexpand(false); + list.set_visible(true); + list.set_selection_mode(gtk::SelectionMode::None); + + episodes.into_iter().for_each(|mut ep| { + let widget = EpisodeWidget::new(&mut ep); + list.add(&widget.container); + + let sep = gtk::Separator::new(gtk::Orientation::Vertical); + sep.set_sensitive(false); + sep.set_can_focus(false); + + list.add(&sep); + sep.show() + }); + + Rc::new(view) + } +} + #[derive(Debug, Clone)] struct EpisodesViewWidget { container: gtk::Box, diff --git a/hammond-gtk/src/widgets/episode.rs b/hammond-gtk/src/widgets/episode.rs index 8beb2b0..d872fe5 100644 --- a/hammond-gtk/src/widgets/episode.rs +++ b/hammond-gtk/src/widgets/episode.rs @@ -82,16 +82,16 @@ impl Default for EpisodeWidget { } impl EpisodeWidget { - pub fn new(episode: &mut EpisodeWidgetQuery, pd: &Podcast) -> EpisodeWidget { + pub fn new(episode: &mut EpisodeWidgetQuery) -> EpisodeWidget { let widget = EpisodeWidget::default(); - widget.init(episode, pd); + widget.init(episode); widget } // TODO: calculate lenght. // TODO: wire the progress_bar to the downloader. // TODO: wire the cancel button. - fn init(&self, episode: &mut EpisodeWidgetQuery, pd: &Podcast) { + fn init(&self, episode: &mut EpisodeWidgetQuery) { self.title.set_xalign(0.0); self.title.set_text(episode.title()); @@ -147,7 +147,6 @@ impl EpisodeWidget { download.show(); })); - let pd_title = pd.title().to_owned(); let play = &self.play; let delete = &self.delete; let cancel = &self.cancel; @@ -155,7 +154,6 @@ impl EpisodeWidget { self.download.connect_clicked( clone!(play, delete, episode, cancel, progress => move |dl| { on_download_clicked( - &pd_title, &mut episode.clone(), dl, &play, @@ -170,7 +168,6 @@ impl EpisodeWidget { // TODO: show notification when dl is finished. fn on_download_clicked( - pd_title: &str, ep: &mut EpisodeWidgetQuery, download_bttn: >k::Button, play_bttn: >k::Button, @@ -194,7 +191,8 @@ fn on_download_clicked( }), ); - let pd_title = pd_title.to_owned(); + let pd = dbqueries::get_podcast_from_id(ep.podcast_id()).unwrap(); + let pd_title = pd.title().to_owned(); let mut ep = ep.clone(); cancel_bttn.show(); progress_bar.show(); @@ -270,7 +268,7 @@ pub fn episodes_listbox(pd: &Podcast) -> Result { let list = gtk::ListBox::new(); episodes.into_iter().for_each(|mut ep| { - let widget = EpisodeWidget::new(&mut ep, pd); + let widget = EpisodeWidget::new(&mut ep); list.add(&widget.container); let sep = gtk::Separator::new(gtk::Orientation::Vertical);