From 83abb5a825857f38c2c67ea3840b9b168363da9c Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Fri, 6 Apr 2018 21:30:03 +0300 Subject: [PATCH] Move the lazy_load logic to a Generic function. --- hammond-gtk/src/widgets/episode.rs | 39 +++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/hammond-gtk/src/widgets/episode.rs b/hammond-gtk/src/widgets/episode.rs index 19694bb..034b2f6 100644 --- a/hammond-gtk/src/widgets/episode.rs +++ b/hammond-gtk/src/widgets/episode.rs @@ -383,17 +383,9 @@ pub fn episodes_listbox(pd: Arc, sender: Sender) -> Result return glib::Continue(false), }; - let mut idx = 0; - gtk::idle_add(clone!(list, sender => move || { - if idx >= episodes.len() { return glib::Continue(false) } - - episodes.get(idx).cloned().map(|ep| { - let widget = EpisodeWidget::new(ep, sender.clone()); - list.add(&widget.container); - }); - - idx += 1; - glib::Continue(true) + lazy_load(episodes, list.clone(), clone!(sender => move |ep| { + let w = EpisodeWidget::new(ep, sender.clone()); + w.container.clone() })); glib::Continue(false) @@ -401,3 +393,28 @@ pub fn episodes_listbox(pd: Arc, sender: Sender) -> Result(data: Vec, container: Z, mut predicate: P) +where + T: Clone + 'static, + Z: ContainerExt + 'static, + P: FnMut(T) -> U + 'static, + U: IsA, +{ + let mut idx = 0; + gtk::idle_add(move || { + if idx >= data.len() { + return glib::Continue(false); + } + + data.get(idx).cloned().map(|x| { + let widget = predicate(x); + container.add(&widget); + }); + + idx += 1; + glib::Continue(true) + }); +}