ShowStack: Convert rest methods to return Result<T, Error>.

This commit is contained in:
Jordan Petridis 2018-02-07 01:19:07 +02:00
parent e196a6c905
commit 89564996df
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
2 changed files with 23 additions and 21 deletions

View File

@ -134,7 +134,12 @@ impl App {
Ok(Action::RefreshWidgetIfSame(id)) => content.update_widget_if_same(id), Ok(Action::RefreshWidgetIfSame(id)) => content.update_widget_if_same(id),
Ok(Action::RefreshEpisodesView) => content.update_episode_view(), Ok(Action::RefreshEpisodesView) => content.update_episode_view(),
Ok(Action::RefreshEpisodesViewBGR) => content.update_episode_view_if_baground(), Ok(Action::RefreshEpisodesViewBGR) => content.update_episode_view_if_baground(),
Ok(Action::ReplaceWidget(ref pd)) => content.get_shows().replace_widget(pd), Ok(Action::ReplaceWidget(ref pd)) => {
if let Err(err) = content.get_shows().replace_widget(pd) {
error!("Something went wrong while trying to update the ShowWidget.");
error!("Error: {}", err);
}
}
Ok(Action::ShowWidgetAnimated) => content.get_shows().switch_widget_animated(), Ok(Action::ShowWidgetAnimated) => content.get_shows().switch_widget_animated(),
Ok(Action::ShowShowsAnimated) => content.get_shows().switch_podcasts_animated(), Ok(Action::ShowShowsAnimated) => content.get_shows().switch_podcasts_animated(),
Ok(Action::HeaderBarShowTile(title)) => headerbar.switch_to_back(&title), Ok(Action::HeaderBarShowTile(title)) => headerbar.switch_to_back(&title),

View File

@ -63,7 +63,10 @@ impl Content {
} }
pub fn update_shows_view(&self) { pub fn update_shows_view(&self) {
self.shows.update_podcasts(); if let Err(err) = self.shows.update_podcasts() {
error!("Something went wrong while trying to update the ShowsView.");
error!("Error: {}", err);
}
} }
pub fn update_widget(&self) { pub fn update_widget(&self) {
@ -134,26 +137,22 @@ impl ShowStack {
// self.update_podcasts(); // self.update_podcasts();
// } // }
pub fn update_podcasts(&self) { pub fn update_podcasts(&self) -> Result<(), Error> {
let vis = self.stack.get_visible_child_name().unwrap(); let vis = self.stack.get_visible_child_name().unwrap();
let old = self.stack let old = self.stack
.get_child_by_name("podcasts") .get_child_by_name("podcasts")
// This is guaranted to exists, based on `ShowStack::new()`. .ok_or_else(|| format_err!("Faild to get \"podcasts\" child from the stack."))?
.unwrap()
.downcast::<gtk::Box>() .downcast::<gtk::Box>()
// This is guaranted to be a Box based on the `ShowsPopulated` impl. .map_err(|_| format_err!("Failed to downcast stack child to a Box."))?;
.unwrap();
debug!("Name: {:?}", WidgetExt::get_name(&old)); debug!("Name: {:?}", WidgetExt::get_name(&old));
let scrolled_window = old.get_children() let scrolled_window = old.get_children()
.first() .first()
// This is guaranted to exist based on the show_widget.ui file. .ok_or_else(|| format_err!("Box container has no childs."))?
.unwrap()
.clone() .clone()
.downcast::<gtk::ScrolledWindow>() .downcast::<gtk::ScrolledWindow>()
// This is guaranted based on the show_widget.ui file. .map_err(|_| format_err!("Failed to downcast stack child to a ScrolledWindow."))?;
.unwrap();
debug!("Name: {:?}", WidgetExt::get_name(&scrolled_window)); debug!("Name: {:?}", WidgetExt::get_name(&scrolled_window));
let pop = ShowsPopulated::new(self.sender.clone()); let pop = ShowsPopulated::new(self.sender.clone());
@ -174,16 +173,15 @@ impl ShowStack {
} }
old.destroy(); old.destroy();
Ok(())
} }
pub fn replace_widget(&self, pd: &Podcast) { pub fn replace_widget(&self, pd: &Podcast) -> Result<(), Error> {
let old = self.stack let old = self.stack
.get_child_by_name("widget") .get_child_by_name("widget")
// This is guaranted to exists, based on `ShowStack::new()`. .ok_or_else(|| format_err!("Faild to get \"widget\" child from the stack."))?
.unwrap()
.downcast::<gtk::Box>() .downcast::<gtk::Box>()
// This is guaranted to be a Box based on the `ShowWidget` impl. .map_err(|_| format_err!("Failed to downcast stack child to a Box."))?;
.unwrap();
debug!("Name: {:?}", WidgetExt::get_name(&old)); debug!("Name: {:?}", WidgetExt::get_name(&old));
let new = ShowWidget::new(pd, self.sender.clone()); let new = ShowWidget::new(pd, self.sender.clone());
@ -197,12 +195,10 @@ impl ShowStack {
if newid == oldid { if newid == oldid {
let scrolled_window = old.get_children() let scrolled_window = old.get_children()
.first() .first()
// This is guaranted to exist based on the show_widget.ui file. .ok_or_else(|| format_err!("Box container has no childs."))?
.unwrap()
.clone() .clone()
.downcast::<gtk::ScrolledWindow>() .downcast::<gtk::ScrolledWindow>()
// This is guaranted based on the show_widget.ui file. .map_err(|_| format_err!("Failed to downcast stack child to a ScrolledWindow."))?;
.unwrap();
debug!("Name: {:?}", WidgetExt::get_name(&scrolled_window)); debug!("Name: {:?}", WidgetExt::get_name(&scrolled_window));
// Copy the vertical scrollbar adjustment from the old view into the new one. // Copy the vertical scrollbar adjustment from the old view into the new one.
@ -213,6 +209,7 @@ impl ShowStack {
self.stack.remove(&old); self.stack.remove(&old);
self.stack.add_named(&new.container, "widget"); self.stack.add_named(&new.container, "widget");
Ok(())
} }
pub fn update_widget(&self) -> Result<(), Error> { pub fn update_widget(&self) -> Result<(), Error> {
@ -230,7 +227,7 @@ impl ShowStack {
let id = id.ok_or_else(|| format_err!("Failed to get widget's name."))?; let id = id.ok_or_else(|| format_err!("Failed to get widget's name."))?;
let pd = dbqueries::get_podcast_from_id(id.parse::<i32>()?)?; let pd = dbqueries::get_podcast_from_id(id.parse::<i32>()?)?;
self.replace_widget(&pd); self.replace_widget(&pd)?;
self.stack.set_visible_child_name(&vis); self.stack.set_visible_child_name(&vis);
old.destroy(); old.destroy();
Ok(()) Ok(())