toggle offline/online mode

This commit is contained in:
Love 2023-03-08 20:46:03 +01:00
parent f03b676313
commit e73f186b0f
3 changed files with 26 additions and 9 deletions

View File

@ -31,6 +31,11 @@ impl GrandMother {
})
}
pub fn demote_to_offline(&mut self) {
let offline_mother = GrandMother::new_offline(self.config.clone());
(self.parser, self.playlist) = (offline_mother.parser, offline_mother.playlist);
}
pub async fn promote_to_online(&mut self) -> Result<(), Error> {
let online_mother = GrandMother::new_online(self.config.clone()).await?;
(self.parser, self.playlist) = (online_mother.parser, online_mother.playlist);

View File

@ -8,7 +8,10 @@ mod opt;
pub mod parser;
mod playlist;
use std::{io::{stdin, stdout, Stdin, StdoutLock, Write}, rc::Rc};
use std::{
io::{stdin, stdout, Stdin, StdoutLock, Write},
rc::Rc,
};
use async_recursion::async_recursion;
pub use config::Configuration;
@ -65,10 +68,10 @@ pub async fn get_gm(
mode: Mode,
readline: &mut Readline<'_>,
config: Rc<Configuration>,
) -> Result<GrandMother, String> {
) -> Result<(GrandMother, bool), String> {
match mode {
Mode::Online => GrandMother::new_online(config).await,
Mode::Offline => Ok(GrandMother::new_offline(config)),
Mode::Online => Ok((GrandMother::new_online(config).await?, true)),
Mode::Offline => Ok((GrandMother::new_offline(config), false)),
Mode::Ask => loop {
let input = readline
.input("Online/Offline mode? [1/2] ")

View File

@ -31,7 +31,7 @@ async fn main() {
" {} is to make entries availibe for offline use later on in the program",
"o".bold()
),
format!(" {} is to switch to online mode", "m".bold()),
format!(" {} is to switch between modes (toggle)", "m".bold()),
format!(" {} is to perform a new search", "s".bold()),
format!(" {} is to select all", "a".bold()),
format!(" {} is to toggtle fullscreen for mpv", "f".bold()),
@ -47,7 +47,7 @@ async fn main() {
let mut mpv_fs = false;
let mut search_result: Option<Rc<Vec<&M3u8>>> = None;
let mut readline = Readline::new();
let gm = get_gm(
let (gm, mut in_online) = get_gm(
opt.mode,
&mut readline,
Rc::new(Configuration::new().expect("Failed to write to configfile")),
@ -100,10 +100,19 @@ async fn main() {
continue;
}
"m" => {
let result = unsafe { get_mut_ref(&gm) }.promote_to_online().await;
if let Err(e) = result {
println!("Failed to switch to onlinemode {:?}", e);
if in_online {
unsafe { get_mut_ref(&gm) }.demote_to_offline();
println!("Switched to offline mode");
} else {
let result = unsafe { get_mut_ref(&gm) }.promote_to_online().await;
if let Err(e) = result {
println!("Failed to switch to onlinemode {:?}", e);
} else {
println!("Switched to online mode");
continue;
}
}
in_online = !in_online;
continue;
}
_ => {}