EpisodeWidget: Shrink the Size state Machine.

This commit is contained in:
Jordan Petridis 2018-02-13 05:03:16 +02:00
parent bdf8901dd8
commit 02de2059db
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6

View File

@ -110,6 +110,7 @@ pub struct Duration<S> {
impl<S> Duration<S> { impl<S> Duration<S> {
// This needs a better name. // This needs a better name.
// TODO: make me mut
fn set_duration(&self, minutes: i64) { fn set_duration(&self, minutes: i64) {
self.duration.set_text(&format!("{} min", minutes)); self.duration.set_text(&format!("{} min", minutes));
} }
@ -192,82 +193,55 @@ impl DurationMachine {
} }
} }
#[derive(Debug, Clone)]
pub struct TotalShown;
#[derive(Debug, Clone)]
pub struct Unkown;
#[derive(Debug, Clone)]
pub struct InProgress;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Size<S> { pub struct Size<S> {
local_size: gtk::Label, local_size: gtk::Label,
total_size: gtk::Label,
separator: gtk::Label, separator: gtk::Label,
prog_separator: gtk::Label, prog_separator: gtk::Label,
state: S, state: S,
} }
impl<S> Size<S> { impl Size<Hidden> {
fn set_total_size(&self, text: &str) { fn new(local_size: gtk::Label, separator: gtk::Label, prog_separator: gtk::Label) -> Self {
self.total_size.set_text(text)
}
}
impl Size<Unkown> {
fn new(
local_size: gtk::Label,
total_size: gtk::Label,
separator: gtk::Label,
prog_separator: gtk::Label,
) -> Self {
local_size.hide(); local_size.hide();
total_size.hide();
separator.hide(); separator.hide();
prog_separator.hide(); prog_separator.hide();
Size { Size {
local_size, local_size,
total_size,
separator, separator,
prog_separator, prog_separator,
state: Unkown {}, state: Hidden {},
} }
} }
} }
impl From<Size<TotalShown>> for Size<InProgress> { impl From<Size<Hidden>> for Size<Shown> {
fn from(f: Size<TotalShown>) -> Self { fn from(f: Size<Hidden>) -> Self {
f.prog_separator.show(); f.prog_separator.show();
f.total_size.show();
f.local_size.show(); f.local_size.show();
f.separator.show(); f.separator.show();
Size { Size {
local_size: f.local_size, local_size: f.local_size,
total_size: f.total_size,
separator: f.separator, separator: f.separator,
prog_separator: f.prog_separator, prog_separator: f.prog_separator,
state: InProgress {}, state: Shown {},
} }
} }
} }
impl From<Size<Unkown>> for Size<InProgress> { impl From<Size<Shown>> for Size<Hidden> {
fn from(f: Size<Unkown>) -> Self { fn from(f: Size<Shown>) -> Self {
f.prog_separator.show(); f.prog_separator.hide();
f.total_size.show(); f.local_size.hide();
f.local_size.show(); f.separator.hide();
f.separator.show();
Size { Size {
local_size: f.local_size, local_size: f.local_size,
total_size: f.total_size,
separator: f.separator, separator: f.separator,
prog_separator: f.prog_separator, prog_separator: f.prog_separator,
state: InProgress {}, state: Hidden {},
} }
} }
} }
@ -425,21 +399,17 @@ impl From<Progress<Shown>> for Progress<Hidden> {
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Media<D, P, S> { pub struct Media<D, S> {
dl: DownloadPlay<D>, dl: DownloadPlay<D>,
progress: Progress<P>, progress: Progress<S>,
size: Size<S>, size: Size<S>,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum MediaMachine { pub enum MediaMachine {
NewWithSize(Media<Download, Hidden, TotalShown>), New(Media<Download, Hidden>),
NewWithoutSize(Media<Download, Hidden, Unkown>), Playable(Media<Play, Hidden>),
// TODO: Since you've download it you probably know it's size InProgress(Media<Hidden, Shown>),
// Adjust accordignly
PlayableWithSize(Media<Play, Hidden, TotalShown>),
PlayableWithoutSize(Media<Play, Hidden, Unkown>),
InProgress(Media<Hidden, Shown, InProgress>),
} }
impl MediaMachine { impl MediaMachine {
@ -449,40 +419,41 @@ impl MediaMachine {
bar: gtk::ProgressBar, bar: gtk::ProgressBar,
cancel: gtk::Button, cancel: gtk::Button,
local_size: gtk::Label, local_size: gtk::Label,
total_size: gtk::Label,
separator: gtk::Label, separator: gtk::Label,
prog_separator: gtk::Label, prog_separator: gtk::Label,
) -> Self { ) -> Self {
let dl = DownloadPlay::<Download>::from(DownloadPlay::<Hidden>::new(play, download)); let dl = DownloadPlay::<Download>::from(DownloadPlay::<Hidden>::new(play, download));
let progress = Progress::<Hidden>::new(bar, cancel); let progress = Progress::<Hidden>::new(bar, cancel);
let size = Size::<Unkown>::new(local_size, total_size, separator, prog_separator); let size = Size::<Hidden>::new(local_size, separator, prog_separator);
MediaMachine::NewWithoutSize(Media { dl, progress, size }) MediaMachine::New(Media { dl, progress, size })
} }
pub fn determine_state(self, is_downloaded: bool, is_active: bool, size: Option<&str>) -> Self { pub fn determine_state(self, is_downloaded: bool, is_active: bool) -> Self {
match (self, is_downloaded, is_active, size) { match (self, is_downloaded, is_active) {
(MediaMachine::NewWithSize(val), _, true, Some(s)) => { (MediaMachine::New(val), _, true) => MediaMachine::InProgress(Media {
let Media { dl, progress, size } = val; dl: val.dl.into(),
size.set_total_size(s); progress: val.progress.into(),
size: val.size.into(),
MediaMachine::InProgress(Media { }),
dl: dl.into(), (MediaMachine::New(val), true, false) => MediaMachine::Playable(Media {
progress: progress.into(), dl: val.dl.into(),
size: size.into(), progress: val.progress.into(),
}) size: val.size.into(),
} }),
(MediaMachine::NewWithSize(val), _, true, None) => { (MediaMachine::Playable(val), _, true) => MediaMachine::InProgress(Media {
let Media { dl, progress, size } = val; dl: val.dl.into(),
size.set_total_size("Unkown"); progress: val.progress.into(),
size: val.size.into(),
MediaMachine::InProgress(Media { }),
dl: dl.into(), (MediaMachine::Playable(val), false, false) => MediaMachine::New(Media {
progress: progress.into(), dl: val.dl.into(),
size: size.into(), progress: val.progress.into(),
}) size: val.size.into(),
} }),
_ => unimplemented!(), (n @ MediaMachine::New(_), false, false) => n,
(n @ MediaMachine::Playable(_), true, false) => n,
(n @ MediaMachine::InProgress(_), _, _) => n,
} }
} }
} }