ShowWidget: Keep track of the podcast it was created from.

Since ShowStack now keeps a refference to ShowWidget we no
longer need to encode it in the widget name.
This commit is contained in:
Jordan Petridis 2018-04-24 15:25:34 +03:00
parent c4ed90dd5a
commit a56a80db88
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
2 changed files with 26 additions and 27 deletions

View File

@ -76,38 +76,33 @@ impl ShowStack {
pub fn replace_widget(&mut self, pd: Arc<Podcast>) -> Result<(), Error> {
let old = self.show.container.clone();
let oldname = WidgetExt::get_name(&old);
debug!("Name: {:?}", oldname);
oldname
.clone()
.and_then(|id| id.parse().ok())
// save the ShowWidget vertical scrollabar alignment
self.show
.podcast_id()
.map(|id| self.show.save_vadjustment(id));
let new = ShowWidget::new(pd, self.sender.clone());
// Each composite ShowWidget is a gtkBox with the Podcast.id encoded in the
// gtk::Widget name. It's a hack since we can't yet subclass GObject
// easily.
debug!(
"Old widget Name: {:?}\nNew widget Name: {:?}",
oldname,
WidgetExt::get_name(&new.container)
);
self.show = new;
self.stack.remove(&old);
self.stack.add_named(&self.show.container, "widget");
// The current visible child might change depending on
// removal and insertion in the gtk::Stack, so we have
// to make sure it will stay the same.
let s = self.state.clone();
self.switch_visible(s);
Ok(())
}
pub fn update_widget(&mut self) -> Result<(), Error> {
let old = self.show.container.clone();
let id = WidgetExt::get_name(&old);
if id == Some("GtkBox".to_string()) || id.is_none() {
let id = self.show.podcast_id();
if id.is_none() {
return Ok(());
}
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.unwrap_or_default())?;
self.replace_widget(Arc::new(pd))?;
// The current visible child might change depending on
@ -122,13 +117,11 @@ impl ShowStack {
// Only update widget if it's podcast_id is equal to pid.
pub fn update_widget_if_same(&mut self, pid: i32) -> Result<(), Error> {
let old = &self.show.container.clone();
let id = WidgetExt::get_name(old);
if id != Some(pid.to_string()) || id.is_none() {
if self.show.podcast_id() == Some(pid) {
debug!("Different widget. Early return");
return Ok(());
}
self.update_widget()
}

View File

@ -36,6 +36,7 @@ pub struct ShowWidget {
settings: gtk::MenuButton,
unsub: gtk::Button,
episodes: gtk::ListBox,
podcast_id: Option<i32>,
}
impl Default for ShowWidget {
@ -61,6 +62,7 @@ impl Default for ShowWidget {
link,
settings,
episodes,
podcast_id: None,
}
}
}
@ -68,27 +70,27 @@ impl Default for ShowWidget {
impl ShowWidget {
#[inline]
pub fn new(pd: Arc<Podcast>, sender: Sender<Action>) -> Rc<ShowWidget> {
let pdw = Rc::new(ShowWidget::default());
let mut pdw = ShowWidget::default();
pdw.init(pd.clone(), sender.clone());
let pdw = Rc::new(pdw);
populate_listbox(&pdw, pd, sender)
.map_err(|err| error!("Failed to populate the listbox: {}", err))
.ok();
pdw
}
#[inline]
pub fn init(&self, pd: Arc<Podcast>, sender: Sender<Action>) {
pub fn init(&mut self, pd: Arc<Podcast>, sender: Sender<Action>) {
let builder = gtk::Builder::new_from_resource("/org/gnome/hammond/gtk/show_widget.ui");
// Hacky workaround so the pd.id() can be retrieved from the `ShowStack`.
WidgetExt::set_name(&self.container, &pd.id().to_string());
self.unsub
.connect_clicked(clone!(pd, sender => move |bttn| {
on_unsub_button_clicked(pd.clone(), bttn, sender.clone());
}));
self.set_description(pd.description());
self.podcast_id = Some(pd.id());
self.set_cover(pd.clone())
.map_err(|err| error!("Failed to set a cover: {}", err))
@ -168,6 +170,10 @@ impl ShowWidget {
Ok(())
}
pub fn podcast_id(&self) -> Option<i32> {
self.podcast_id
}
}
#[inline]