Switch to a Generic Struct instead of an Enum.

This commit is contained in:
Jordan Petridis 2017-12-07 15:24:15 +02:00
parent 41b0a36b81
commit 1266c6e971
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
2 changed files with 56 additions and 58 deletions

View File

@ -37,109 +37,111 @@ impl Content {
} }
fn replace_widget(&mut self, pdw: PodcastWidget) { fn replace_widget(&mut self, pdw: PodcastWidget) {
let vis = self.stack.get_visible_child_name().unwrap();
let old = self.stack.get_child_by_name("widget").unwrap(); let old = self.stack.get_child_by_name("widget").unwrap();
self.stack.remove(&old); self.stack.remove(&old);
self.widget = pdw; self.widget = pdw;
self.stack.add_named(&self.widget.container, "widget"); self.stack.add_named(&self.widget.container, "widget");
self.stack.set_visible_child_name(&vis);
old.destroy(); old.destroy();
} }
fn replace_podcasts(&mut self, pop: PopulatedView) { fn replace_podcasts(&mut self, pop: PopulatedView) {
let vis = self.stack.get_visible_child_name().unwrap();
let old = self.stack.get_child_by_name("podcasts").unwrap(); let old = self.stack.get_child_by_name("podcasts").unwrap();
self.stack.remove(&old); self.stack.remove(&old);
self.podcasts = pop; self.podcasts = pop;
self.stack.add_named(&self.podcasts.container, "podcasts"); self.stack.add_named(&self.podcasts.container, "podcasts");
self.stack.set_visible_child_name(&vis);
old.destroy(); old.destroy();
} }
} }
trait UpdateView { #[derive(Debug)]
pub struct ContentState<S> {
content: Content,
state: S,
}
pub trait UpdateView {
fn update(&mut self); fn update(&mut self);
} }
#[derive(Debug)] #[derive(Debug)]
struct Empty { pub struct Empty {}
content: Content
}
#[derive(Debug)] #[derive(Debug)]
struct PodcastsView { pub struct PodcastsView {}
content: Content
}
#[derive(Debug)] #[derive(Debug)]
struct WidgetsView { pub struct WidgetsView {}
content: Content
}
#[derive(Debug)] impl Into<ContentState<PodcastsView>> for ContentState<Empty> {
pub enum ContentState { fn into(self) -> ContentState<PodcastsView> {
empty(Empty),
pop(PodcastsView),
pd(WidgetsView),
}
impl Into<PodcastsView> for Empty {
fn into(self) -> PodcastsView {
self.content.stack.set_visible_child_name("podcasts"); self.content.stack.set_visible_child_name("podcasts");
PodcastsView { ContentState{
content: self.content content: self.content,
state: PodcastsView{},
} }
} }
} }
impl UpdateView for Empty { impl UpdateView for ContentState<Empty> {
fn update(&mut self) {} fn update(&mut self) {}
} }
impl Into<Empty> for PodcastsView { impl Into<ContentState<Empty>> for ContentState<PodcastsView> {
fn into(self) -> Empty { fn into(self) -> ContentState<Empty> {
self.content.stack.set_visible_child_name("empty"); self.content.stack.set_visible_child_name("empty");
Empty { ContentState{
content: self.content content: self.content,
state: Empty{},
} }
} }
} }
impl Into<WidgetsView> for PodcastsView { impl Into<ContentState<WidgetsView>> for ContentState<PodcastsView> {
fn into(self) -> WidgetsView { fn into(self) -> ContentState<WidgetsView> {
self.content.stack.set_visible_child_name("widget"); self.content.stack.set_visible_child_name("widget");
WidgetsView { ContentState{
content: self.content content: self.content,
state: WidgetsView{},
} }
} }
} }
impl UpdateView for PodcastsView { impl UpdateView for ContentState<PodcastsView> {
fn update(&mut self) { fn update(&mut self) {
let pop = PopulatedView::new_initialized(&self.content.stack); let pop = PopulatedView::new_initialized(&self.content.stack);
self.content.replace_podcasts(pop) self.content.replace_podcasts(pop)
} }
} }
impl Into<PodcastsView> for WidgetsView { impl Into<ContentState<PodcastsView>> for ContentState<WidgetsView> {
fn into(self) -> PodcastsView { fn into(self) -> ContentState<PodcastsView> {
self.content.stack.set_visible_child_name("podcasts"); self.content.stack.set_visible_child_name("podcasts");
PodcastsView { ContentState{
content: self.content content: self.content,
state: PodcastsView{},
} }
} }
} }
impl Into<Empty> for WidgetsView { impl Into<ContentState<Empty>> for ContentState<WidgetsView> {
fn into(self) -> Empty { fn into(self) -> ContentState<Empty> {
self.content.stack.set_visible_child_name("empty"); self.content.stack.set_visible_child_name("empty");
Empty { ContentState{
content: self.content content: self.content,
state: Empty{},
} }
} }
} }
impl UpdateView for WidgetsView { impl UpdateView for ContentState<WidgetsView> {
fn update(&mut self) { fn update(&mut self) {
let old = self.content.stack.get_child_by_name("widget").unwrap(); let old = self.content.stack.get_child_by_name("widget").unwrap();
let id = WidgetExt::get_name(&old).unwrap(); let id = WidgetExt::get_name(&old).unwrap();
@ -150,34 +152,29 @@ impl UpdateView for WidgetsView {
} }
} }
impl ContentState { impl ContentState<PodcastsView> {
pub fn new() -> ContentState { pub fn new() -> Result<ContentState<PodcastsView>, ContentState<Empty>> {
let content = Content::new(); let content = Content::new();
content.podcasts.init(&content.stack); content.podcasts.init(&content.stack);
if content.podcasts.flowbox.get_children().is_empty() { if content.podcasts.flowbox.get_children().is_empty() {
content.stack.set_visible_child_name("empty"); content.stack.set_visible_child_name("empty");
return ContentState::empty(Empty { content }) return Err(
ContentState{
content,
state: Empty{},
});
} }
content.stack.set_visible_child_name("podcasts"); content.stack.set_visible_child_name("podcasts");
ContentState::pop(PodcastsView{ content }) Ok(ContentState {
content,
state: PodcastsView{},
})
} }
pub fn get_stack(&self) -> gtk::Stack { pub fn get_stack(&self) -> gtk::Stack {
match *self { self.content.stack.clone()
ContentState::empty(ref e) => e.content.stack.clone(),
ContentState::pop(ref e) => e.content.stack.clone(),
ContentState::pd(ref e) => e.content.stack.clone(),
}
}
pub fn update(&mut self) {
match *self {
ContentState::empty(ref mut e) => e.update(),
ContentState::pop(ref mut e) => e.update(),
ContentState::pd(ref mut e) => e.update(),
}
} }
} }

View File

@ -63,7 +63,8 @@ fn build_ui(app: &gtk::Application) {
let window = gtk::ApplicationWindow::new(app); let window = gtk::ApplicationWindow::new(app);
window.set_default_size(1150, 650); window.set_default_size(1150, 650);
let ct = content::ContentState::new(); // TODO: this will blow horribly
let ct = content::ContentState::new().unwrap();
let stack = ct.get_stack(); let stack = ct.get_stack();
window.add(&stack); window.add(&stack);