From bdf8901dd83535e326fbd6719baca03cfe5a2fa1 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Tue, 13 Feb 2018 02:23:32 +0200 Subject: [PATCH] This compiles. Instead of having a Wrapper of StateMachinesWrappers, use only the desired possible states in A new struct with only 1 Wrapper that covers all 3 of the embeded state machines. I don't even know if the comment makes any sense. Sorry. --- hammond-gtk/src/widgets/episode_states.rs | 175 ++++++++-------------- 1 file changed, 60 insertions(+), 115 deletions(-) diff --git a/hammond-gtk/src/widgets/episode_states.rs b/hammond-gtk/src/widgets/episode_states.rs index b7cdef9..edd6d19 100644 --- a/hammond-gtk/src/widgets/episode_states.rs +++ b/hammond-gtk/src/widgets/episode_states.rs @@ -192,9 +192,6 @@ impl DurationMachine { } } -#[derive(Debug, Clone)] -pub struct LocalShown; - #[derive(Debug, Clone)] pub struct TotalShown; @@ -213,6 +210,12 @@ pub struct Size { state: S, } +impl Size { + fn set_total_size(&self, text: &str) { + self.total_size.set_text(text) + } +} + impl Size { fn new( local_size: gtk::Label, @@ -235,23 +238,6 @@ impl Size { } } -impl From> for Size { - fn from(f: Size) -> Self { - f.prog_separator.hide(); - f.total_size.hide(); - f.local_size.show(); - f.separator.show(); - - Size { - local_size: f.local_size, - total_size: f.total_size, - separator: f.separator, - prog_separator: f.prog_separator, - state: LocalShown {}, - } - } -} - impl From> for Size { fn from(f: Size) -> Self { f.prog_separator.show(); @@ -286,51 +272,6 @@ impl From> for Size { } } -impl From> for Size { - fn from(f: Size) -> Self { - f.prog_separator.hide(); - f.total_size.hide(); - f.local_size.show(); - f.separator.show(); - - Size { - local_size: f.local_size, - total_size: f.total_size, - separator: f.separator, - prog_separator: f.prog_separator, - state: LocalShown {}, - } - } -} - -#[derive(Debug, Clone)] -pub enum SizeMachine { - LocalShown(Size), - TotallShown(Size), - Unkown(Size), - InProgress(Size), -} - -impl SizeMachine { - pub fn new( - local_size: gtk::Label, - total_size: gtk::Label, - separator: gtk::Label, - prog_separator: gtk::Label, - ) -> Self { - SizeMachine::Unkown(Size::::new( - local_size, - total_size, - separator, - prog_separator, - )) - } - - pub fn determine_state(self) -> Self { - unimplemented!() - } -} - #[derive(Debug, Clone)] pub struct Download; @@ -437,44 +378,6 @@ impl From> for DownloadPlay { } } -pub enum DownloadPlayMachine { - Play(DownloadPlay), - Download(DownloadPlay), - Hidden(DownloadPlay), -} - -impl DownloadPlayMachine { - pub fn new(play: gtk::Button, download: gtk::Button) -> Self { - DownloadPlayMachine::Hidden(DownloadPlay::::new(play, download)) - } - - pub fn determine_state(self, downloaded: bool, should_hide: bool) -> Self { - match (self, downloaded, should_hide) { - (DownloadPlayMachine::Play(val), true, false) => DownloadPlayMachine::Play(val.into()), - (DownloadPlayMachine::Play(val), false, false) => { - DownloadPlayMachine::Download(val.into()) - } - (DownloadPlayMachine::Download(val), true, false) => { - DownloadPlayMachine::Play(val.into()) - } - (DownloadPlayMachine::Download(val), false, false) => { - DownloadPlayMachine::Download(val.into()) - } - (DownloadPlayMachine::Hidden(val), true, false) => { - DownloadPlayMachine::Play(val.into()) - } - (DownloadPlayMachine::Hidden(val), false, false) => { - DownloadPlayMachine::Download(val.into()) - } - (DownloadPlayMachine::Play(val), _, true) => DownloadPlayMachine::Hidden(val.into()), - (DownloadPlayMachine::Download(val), _, true) => { - DownloadPlayMachine::Hidden(val.into()) - } - (DownloadPlayMachine::Hidden(val), _, true) => DownloadPlayMachine::Hidden(val.into()), - } - } -} - #[derive(Debug, Clone)] pub struct Progress { bar: gtk::ProgressBar, @@ -522,22 +425,64 @@ impl From> for Progress { } #[derive(Debug, Clone)] -pub enum ProgressMachine { - Hidden(Progress), - Shown(Progress), +pub struct Media { + dl: DownloadPlay, + progress: Progress

, + size: Size, } -impl ProgressMachine { - pub fn new(bar: gtk::ProgressBar, cancel: gtk::Button) -> Self { - ProgressMachine::Hidden(Progress::::new(bar, cancel)) +#[derive(Debug, Clone)] +pub enum MediaMachine { + NewWithSize(Media), + NewWithoutSize(Media), + // TODO: Since you've download it you probably know it's size + // Adjust accordignly + PlayableWithSize(Media), + PlayableWithoutSize(Media), + InProgress(Media), +} + +impl MediaMachine { + pub fn new( + play: gtk::Button, + download: gtk::Button, + bar: gtk::ProgressBar, + cancel: gtk::Button, + local_size: gtk::Label, + total_size: gtk::Label, + separator: gtk::Label, + prog_separator: gtk::Label, + ) -> Self { + let dl = DownloadPlay::::from(DownloadPlay::::new(play, download)); + let progress = Progress::::new(bar, cancel); + let size = Size::::new(local_size, total_size, separator, prog_separator); + + MediaMachine::NewWithoutSize(Media { dl, progress, size }) } - pub fn determine_state(self, is_active: bool) -> Self { - match (self, is_active) { - (ProgressMachine::Hidden(val), false) => ProgressMachine::Hidden(val.into()), - (ProgressMachine::Hidden(val), true) => ProgressMachine::Shown(val.into()), - (ProgressMachine::Shown(val), false) => ProgressMachine::Hidden(val.into()), - (ProgressMachine::Shown(val), true) => ProgressMachine::Shown(val.into()), + pub fn determine_state(self, is_downloaded: bool, is_active: bool, size: Option<&str>) -> Self { + match (self, is_downloaded, is_active, size) { + (MediaMachine::NewWithSize(val), _, true, Some(s)) => { + let Media { dl, progress, size } = val; + size.set_total_size(s); + + MediaMachine::InProgress(Media { + dl: dl.into(), + progress: progress.into(), + size: size.into(), + }) + } + (MediaMachine::NewWithSize(val), _, true, None) => { + let Media { dl, progress, size } = val; + size.set_total_size("Unkown"); + + MediaMachine::InProgress(Media { + dl: dl.into(), + progress: progress.into(), + size: size.into(), + }) + } + _ => unimplemented!(), } } }