From 856ab2853ec4fe6a8d9fbc18e94334f23503b599 Mon Sep 17 00:00:00 2001 From: Andre Henriques Date: Sat, 19 Aug 2023 14:46:34 +0100 Subject: [PATCH] added clear toggle --- src/animations.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 1 + 2 files changed, 62 insertions(+) diff --git a/src/animations.rs b/src/animations.rs index 35d1463..214d2b6 100644 --- a/src/animations.rs +++ b/src/animations.rs @@ -674,3 +674,64 @@ pub async fn toggle_animation( return get_error_message("Failed to get list"); } + +/** + * + * Start animations + * + */ +#[get("/cleartoggle/")] +pub async fn cleartoggle_animation( + name: &str, + action: &State>, + messages: &State>, + db: &State, +) -> String { + + let r = action.lock().unwrap().send(Action::Clear); + + if r.is_err() || r.ok().is_none() { + return get_error_message("Something went wrong"); + } + + let animation = get_animation_or_string_error(name, db); + if let Err(error) = animation { + return error; + } + let animation = animation.unwrap(); + + let action = action.lock().unwrap(); + + let send = action.send(Action::GetActiveList); + if send.is_err() || send.ok().is_none() { + return get_error_message("Failed to get list"); + } + + let data = messages.lock().unwrap().recv(); + if data.is_err() || data.as_ref().ok().is_none() { + return get_error_message("Failed to get list"); + } + + if let RenderMessage::ActiveList(list) = data.ok().unwrap() { + if list.contains(&name.to_string()) { + // Disable + let r = action.send(Action::Stop(name.to_string())); + + if r.is_err() || r.ok().is_none() { + return get_error_message("Something went wrong"); + } + + return create_message(200, "Configuration was successful"); + } else { + // Enable + let r = action.send(Action::Start(animation)); + if r.is_err() || r.as_ref().ok().is_none() { + return get_error_message("Probelms with the sender"); + } + + return create_message(200, "Started animation"); + } + } + + return get_error_message("Failed to get list"); +} diff --git a/src/main.rs b/src/main.rs index 8f99701..f0644d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -232,6 +232,7 @@ async fn main() -> GResult { animations::clear_all, animations::get_animation_request, animations::toggle_animation, + animations::cleartoggle_animation, animations::get_animations, animations::get_animation_by_name, ],