use crate::app::managers::{QueueManager, StateManager};
pub use crate::app::managers::state_manager::AppState;
use crate::app::task_manager::TaskManager;
use crate::components::common::{ComponentId, Msg};
use crate::error::AppError;
use crate::error::ErrorReporter;
use crate::services::AuthService;
use quetty_server::service_bus_manager::ServiceBusManager;
use quetty_server::service_bus_manager::{ServiceBusCommand, ServiceBusResponse};
use quetty_server::taskpool::TaskPool;
use std::sync::Arc;
use std::sync::mpsc::Receiver;
use tokio::sync::Mutex;
use tuirealm::event::NoUserEvent;
use tuirealm::terminal::{TerminalAdapter, TerminalBridge};
use tuirealm::{Application, Update};
mod async_operations;
mod initialization;
mod popup_management;
mod state_management;
mod update_handler;
pub struct Model<T>
where
T: TerminalAdapter,
{
pub app: Application<ComponentId, Msg, NoUserEvent>,
pub terminal: TerminalBridge<T>,
pub taskpool: TaskPool,
pub rx_to_main: Receiver<Msg>,
pub service_bus_manager: Option<Arc<Mutex<ServiceBusManager>>>,
pub http_client: reqwest::Client,
pub error_reporter: ErrorReporter,
pub task_manager: TaskManager,
pub state_manager: StateManager,
pub queue_manager: QueueManager,
pub auth_service: Option<Arc<AuthService>>,
}
impl<T> Model<T>
where
T: TerminalAdapter,
{
pub fn update_outside_msg(&mut self) {
while let Ok(msg) = self.rx_to_main.try_recv() {
let mut msg = Some(msg);
while msg.is_some() {
msg = self.update(msg);
}
}
}
pub fn shutdown(&mut self) {
log::info!("Shutting down application");
self.taskpool.cancel_all();
self.taskpool.close();
self.state_manager.shutdown();
if let Some(service_bus_manager) = self.service_bus_manager.clone() {
self.task_manager
.execute("Disposing service bus resources...", async move {
let command = ServiceBusCommand::DisposeAllResources;
let response = service_bus_manager
.lock()
.await
.execute_command(command)
.await;
match response {
ServiceBusResponse::AllResourcesDisposed => Ok(()),
ServiceBusResponse::Error { error } => {
Err(AppError::ServiceBus(error.to_string()))
}
_ => Err(AppError::ServiceBus("Unexpected response".to_string())),
}
});
}
}
pub fn queue_state(&self) -> &crate::app::queue_state::QueueState {
&self.queue_manager.queue_state
}
pub fn queue_state_mut(&mut self) -> &mut crate::app::queue_state::QueueState {
&mut self.queue_manager.queue_state
}
pub fn set_app_state(&mut self, state: AppState) {
self.state_manager.set_app_state(state);
}
pub fn set_editing_message(&mut self, editing: bool) {
self.state_manager.set_editing_message(editing);
}
pub fn set_redraw(&mut self, redraw: bool) {
self.state_manager.redraw = redraw;
}
pub fn set_quit(&mut self, quit: bool) {
if quit {
self.state_manager.quit = true;
}
}
pub fn tx_to_main(&self) -> &std::sync::mpsc::Sender<Msg> {
&self.state_manager.tx_to_main
}
pub fn set_selected_namespace(&mut self, namespace: Option<String>) {
self.state_manager.selected_namespace = namespace;
}
pub fn set_pending_confirmation_action(&mut self, action: Option<Box<Msg>>) {
self.state_manager.pending_confirmation_action = action;
}
pub fn take_pending_confirmation_action(&mut self) -> Option<Box<Msg>> {
self.state_manager.take_pending_confirmation()
}
pub fn set_active_component(&mut self, component: ComponentId) {
self.state_manager.set_active_component(component);
}
pub fn get_current_page_size(&self) -> u32 {
self.state_manager.get_current_page_size()
}
}
impl<T> Update<Msg> for Model<T>
where
T: TerminalAdapter,
{
fn update(&mut self, msg: Option<Msg>) -> Option<Msg> {
self.handle_update(msg)
}
}