From 914cad72f597e855de31e0c5f721b844ca5a7082 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Tue, 19 Dec 2017 20:45:40 +0200 Subject: [PATCH] EpisodesView: Custom Diesel model not really necessary. --- hammond-data/src/dbqueries.rs | 13 +++----- hammond-data/src/lib.rs | 2 +- hammond-data/src/models/queryables.rs | 47 --------------------------- hammond-gtk/src/utils.rs | 1 + hammond-gtk/src/views/episodes.rs | 21 ++++++------ 5 files changed, 17 insertions(+), 67 deletions(-) diff --git a/hammond-data/src/dbqueries.rs b/hammond-data/src/dbqueries.rs index b51f884..caaa76a 100644 --- a/hammond-data/src/dbqueries.rs +++ b/hammond-data/src/dbqueries.rs @@ -2,7 +2,7 @@ use diesel::prelude::*; use diesel; -use models::queryables::{Episode, EpisodeViewWidgetQuery, EpisodeWidgetQuery, Podcast, Source}; +use models::queryables::{Episode, EpisodeWidgetQuery, Podcast, Source}; use chrono::prelude::*; use errors::*; @@ -84,17 +84,13 @@ pub fn get_episodes_with_limit(limit: u32) -> Result> { .load::(&*con)?) } -pub fn get_episodes_view_widgets_with_limit(limit: u32) -> Result> { - use schema::{episode, podcast}; - - joinable!(episode -> podcast (podcast_id)); - allow_tables_to_appear_in_same_query!(episode, podcast); +pub fn get_episodes_widgets_with_limit(limit: u32) -> Result> { + use schema::episode; let db = connection(); let con = db.get()?; Ok(episode::table - .left_join(podcast::table) .select(( episode::rowid, episode::title, @@ -104,11 +100,10 @@ pub fn get_episodes_view_widgets_with_limit(limit: u32) -> Result(&*con)?) + .load::(&*con)?) } pub fn get_podcast_from_id(pid: i32) -> Result { diff --git a/hammond-data/src/lib.rs b/hammond-data/src/lib.rs index 711eb0c..97aec76 100644 --- a/hammond-data/src/lib.rs +++ b/hammond-data/src/lib.rs @@ -61,7 +61,7 @@ pub(crate) mod models; mod parser; mod schema; -pub use models::queryables::{Episode, EpisodeViewWidgetQuery, EpisodeWidgetQuery, Podcast, Source}; +pub use models::queryables::{Episode, EpisodeWidgetQuery, Podcast, Source}; /// [XDG Base Direcotory](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) Paths. #[allow(missing_debug_implementations)] diff --git a/hammond-data/src/models/queryables.rs b/hammond-data/src/models/queryables.rs index 5120f83..4290979 100644 --- a/hammond-data/src/models/queryables.rs +++ b/hammond-data/src/models/queryables.rs @@ -320,53 +320,6 @@ impl EpisodeWidgetQuery { } } -impl From for EpisodeWidgetQuery { - fn from(view: EpisodeViewWidgetQuery) -> EpisodeWidgetQuery { - EpisodeWidgetQuery { - rowid: view.rowid, - title: view.title, - uri: view.uri, - local_uri: view.local_uri, - epoch: view.epoch, - length: view.length, - played: view.played, - // favorite: view.favorite, - // archive: view.archive, - podcast_id: view.podcast_id, - } - } -} - -#[derive(Queryable, Debug, Clone)] -/// Diesel Model to be used for constructing `EpisodeWidgets`. -pub struct EpisodeViewWidgetQuery { - rowid: i32, - title: String, - uri: Option, - local_uri: Option, - epoch: i32, - length: Option, - played: Option, - // favorite: bool, - // archive: bool, - podcast_id: i32, - image_uri: Option, -} - -impl EpisodeViewWidgetQuery { - /// Get the `image_uri`. - /// - /// Represents the uri(url usually) that the Feed cover image is located at. - pub fn image_uri(&self) -> Option<&str> { - self.image_uri.as_ref().map(|s| s.as_str()) - } - - /// `Podcast` table foreign key. - pub fn podcast_id(&self) -> i32 { - self.podcast_id - } -} - #[derive(Queryable, Identifiable, AsChangeset, Associations, PartialEq)] #[belongs_to(Source, foreign_key = "source_id")] #[changeset_options(treat_none_as_null = "true")] diff --git a/hammond-gtk/src/utils.rs b/hammond-gtk/src/utils.rs index 66f5b54..3e22f8e 100644 --- a/hammond-gtk/src/utils.rs +++ b/hammond-gtk/src/utils.rs @@ -60,6 +60,7 @@ fn refresh_podcasts_view() -> glib::Continue { } // FIXME: use something that would just scale? +// TODO: make a diesel model with only title, local_uri pub fn get_pixbuf_from_path(pd: &Podcast) -> Option { let img_path = downloader::cache_image(pd)?; diff --git a/hammond-gtk/src/views/episodes.rs b/hammond-gtk/src/views/episodes.rs index 00a883b..91c3137 100644 --- a/hammond-gtk/src/views/episodes.rs +++ b/hammond-gtk/src/views/episodes.rs @@ -2,7 +2,7 @@ use gtk; use gtk::prelude::*; use hammond_data::dbqueries; -use hammond_data::EpisodeViewWidgetQuery; +use hammond_data::EpisodeWidgetQuery; use widgets::episode::EpisodeWidget; use utils::get_pixbuf_from_path_64; @@ -32,7 +32,7 @@ impl EpisodesView { pub fn new() -> Rc { let view = EpisodesView::default(); - let episodes = dbqueries::get_episodes_view_widgets_with_limit(100).unwrap(); + let episodes = dbqueries::get_episodes_widgets_with_limit(100).unwrap(); let frame = gtk::Frame::new(None); let list = gtk::ListBox::new(); @@ -45,8 +45,8 @@ impl EpisodesView { list.set_visible(true); list.set_selection_mode(gtk::SelectionMode::None); - episodes.into_iter().for_each(|ep| { - let viewep = EpisodesViewWidget::new(ep); + episodes.into_iter().for_each(|mut ep| { + let viewep = EpisodesViewWidget::new(&mut ep); list.add(&viewep.container); let sep = gtk::Separator::new(gtk::Orientation::Vertical); @@ -86,20 +86,21 @@ impl Default for EpisodesViewWidget { } impl EpisodesViewWidget { - fn new(episode: EpisodeViewWidgetQuery) -> EpisodesViewWidget { + fn new(episode: &mut EpisodeWidgetQuery) -> EpisodesViewWidget { let builder = gtk::Builder::new_from_resource("/org/gnome/hammond/gtk/episodes_view_widget.ui"); let container: gtk::Box = builder.get_object("container").unwrap(); let image: gtk::Image = builder.get_object("cover").unwrap(); // FIXME: - let pd = dbqueries::get_podcast_from_id(episode.podcast_id()).unwrap(); - let img = get_pixbuf_from_path_64(&pd); - if let Some(i) = img { - image.set_from_pixbuf(&i); + if let Ok(pd) = dbqueries::get_podcast_from_id(episode.podcast_id()) { + let img = get_pixbuf_from_path_64(&pd); + if let Some(i) = img { + image.set_from_pixbuf(&i); + } } - let ep = EpisodeWidget::new(&mut episode.into()); + let ep = EpisodeWidget::new(episode); container.pack_start(&ep.container, true, true, 5); EpisodesViewWidget {