EpisodesView: Custom Diesel model not really necessary.
This commit is contained in:
parent
895591f628
commit
914cad72f5
@ -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<Vec<Episode>> {
|
||||
.load::<Episode>(&*con)?)
|
||||
}
|
||||
|
||||
pub fn get_episodes_view_widgets_with_limit(limit: u32) -> Result<Vec<EpisodeViewWidgetQuery>> {
|
||||
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<Vec<EpisodeWidgetQuery>> {
|
||||
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<Vec<EpisodeVie
|
||||
episode::length,
|
||||
episode::played,
|
||||
episode::podcast_id,
|
||||
(podcast::image_uri).nullable(),
|
||||
))
|
||||
.order(episode::epoch.desc())
|
||||
.limit(i64::from(limit))
|
||||
.load::<EpisodeViewWidgetQuery>(&*con)?)
|
||||
.load::<EpisodeWidgetQuery>(&*con)?)
|
||||
}
|
||||
|
||||
pub fn get_podcast_from_id(pid: i32) -> Result<Podcast> {
|
||||
|
||||
@ -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)]
|
||||
|
||||
@ -320,53 +320,6 @@ impl EpisodeWidgetQuery {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<EpisodeViewWidgetQuery> 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<String>,
|
||||
local_uri: Option<String>,
|
||||
epoch: i32,
|
||||
length: Option<i32>,
|
||||
played: Option<i32>,
|
||||
// favorite: bool,
|
||||
// archive: bool,
|
||||
podcast_id: i32,
|
||||
image_uri: Option<String>,
|
||||
}
|
||||
|
||||
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")]
|
||||
|
||||
@ -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<Pixbuf> {
|
||||
let img_path = downloader::cache_image(pd)?;
|
||||
|
||||
@ -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<EpisodesView> {
|
||||
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 {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user