EpisodeView: Reduce boilderplate.

This commit is contained in:
Jordan Petridis 2018-04-19 05:40:07 +03:00
parent e4fc7c336e
commit f49012ab51
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
3 changed files with 18 additions and 48 deletions

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.21.0 <!-- Generated with glade 3.22.0
Copyright (C) 2017 - 2018 Copyright (C) 2017 - 2018
@ -81,7 +81,6 @@ Tobias Bernard
<property name="spacing">24</property> <property name="spacing">24</property>
<child> <child>
<object class="GtkBox" id="today_box"> <object class="GtkBox" id="today_box">
<property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="no_show_all">True</property> <property name="no_show_all">True</property>
<property name="hexpand">True</property> <property name="hexpand">True</property>
@ -139,7 +138,6 @@ Tobias Bernard
</child> </child>
<child> <child>
<object class="GtkBox" id="yday_box"> <object class="GtkBox" id="yday_box">
<property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="no_show_all">True</property> <property name="no_show_all">True</property>
<property name="hexpand">True</property> <property name="hexpand">True</property>
@ -196,7 +194,6 @@ Tobias Bernard
</child> </child>
<child> <child>
<object class="GtkBox" id="week_box"> <object class="GtkBox" id="week_box">
<property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="no_show_all">True</property> <property name="no_show_all">True</property>
<property name="hexpand">True</property> <property name="hexpand">True</property>
@ -253,7 +250,6 @@ Tobias Bernard
</child> </child>
<child> <child>
<object class="GtkBox" id="month_box"> <object class="GtkBox" id="month_box">
<property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="no_show_all">True</property> <property name="no_show_all">True</property>
<property name="hexpand">True</property> <property name="hexpand">True</property>
@ -310,7 +306,6 @@ Tobias Bernard
</child> </child>
<child> <child>
<object class="GtkBox" id="rest_box"> <object class="GtkBox" id="rest_box">
<property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="no_show_all">True</property> <property name="no_show_all">True</property>
<property name="hexpand">True</property> <property name="hexpand">True</property>

View File

@ -67,6 +67,7 @@ impl EpisodeStack {
} }
} }
#[inline]
fn set_stack_visible(stack: &gtk::Stack) -> Result<(), DataError> { fn set_stack_visible(stack: &gtk::Stack) -> Result<(), DataError> {
if is_episodes_populated()? { if is_episodes_populated()? {
stack.set_visible_child_name("episodes"); stack.set_visible_child_name("episodes");

View File

@ -81,6 +81,8 @@ impl Default for EpisodesView {
impl EpisodesView { impl EpisodesView {
#[inline] #[inline]
pub fn new(sender: Sender<Action>) -> Result<Rc<EpisodesView>, Error> { pub fn new(sender: Sender<Action>) -> Result<Rc<EpisodesView>, Error> {
use self::ListSplit::*;
let view = Rc::new(EpisodesView::default()); let view = Rc::new(EpisodesView::default());
let ignore = get_ignored_shows()?; let ignore = get_ignored_shows()?;
let episodes = dbqueries::get_episodes_widgets_filter_limit(&ignore, 200)?; let episodes = dbqueries::get_episodes_widgets_filter_limit(&ignore, 200)?;
@ -89,52 +91,18 @@ impl EpisodesView {
let view_ = view.clone(); let view_ = view.clone();
let func = move |ep: EpisodeWidgetQuery| { let func = move |ep: EpisodeWidgetQuery| {
let epoch = ep.epoch(); let epoch = ep.epoch();
let viewep = EpisodesViewWidget::new(ep, sender.clone()); let widget = EpisodesViewWidget::new(ep, sender.clone());
let t = split(&now_utc, i64::from(epoch)); match split(&now_utc, i64::from(epoch)) {
match t { Today => add_to_box(&widget, &view_.today_list, &view_.today_box),
ListSplit::Today => { Yday => add_to_box(&widget, &view_.yday_list, &view_.yday_box),
view_.today_list.add(&viewep.container); Week => add_to_box(&widget, &view_.week_list, &view_.week_box),
} Month => add_to_box(&widget, &view_.month_list, &view_.month_box),
ListSplit::Yday => { Rest => add_to_box(&widget, &view_.rest_list, &view_.rest_box),
view_.yday_list.add(&viewep.container);
}
ListSplit::Week => {
view_.week_list.add(&viewep.container);
}
ListSplit::Month => {
view_.month_list.add(&viewep.container);
}
ListSplit::Rest => {
view_.rest_list.add(&viewep.container);
}
} }
}; };
let callback = clone!(view => move || { lazy_load_full(episodes, func, || {});
if view.today_list.get_children().is_empty() {
view.today_box.hide();
}
if view.yday_list.get_children().is_empty() {
view.yday_box.hide();
}
if view.week_list.get_children().is_empty() {
view.week_box.hide();
}
if view.month_list.get_children().is_empty() {
view.month_box.hide();
}
if view.rest_list.get_children().is_empty() {
view.rest_box.hide();
}
});
lazy_load_full(episodes, func, callback);
view.container.show_all(); view.container.show_all();
Ok(view) Ok(view)
} }
@ -146,6 +114,12 @@ impl EpisodesView {
} }
} }
#[inline]
fn add_to_box(widget: &EpisodesViewWidget, listbox: &gtk::ListBox, box_: &gtk::Box) {
listbox.add(&widget.container);
box_.show();
}
#[inline] #[inline]
fn split(now: &DateTime<Utc>, epoch: i64) -> ListSplit { fn split(now: &DateTime<Utc>, epoch: i64) -> ListSplit {
let ep = Utc.timestamp(epoch, 0); let ep = Utc.timestamp(epoch, 0);