Mountain/ApplicationState/State/UIState/
UIState.rs1use std::{
36 collections::HashMap,
37 sync::{Arc, Mutex as StandardMutex},
38};
39
40use CommonLibrary::Error::CommonError::CommonError;
41
42use crate::dev_log;
43
44#[derive(Clone)]
46pub struct State {
47 pub PendingUserInterfaceRequest:
51 Arc<StandardMutex<HashMap<String, tokio::sync::oneshot::Sender<Result<serde_json::Value, CommonError>>>>>,
52}
53
54impl Default for State {
55 fn default() -> Self {
56 dev_log!("window", "[UIState] Initializing default UI state...");
57
58 Self { PendingUserInterfaceRequest:Arc::new(StandardMutex::new(HashMap::new())) }
59 }
60}
61
62impl State {
63 pub fn GetPendingRequests(&self) -> Vec<String> {
66 self.PendingUserInterfaceRequest
67 .lock()
68 .ok()
69 .map(|guard| guard.keys().cloned().collect())
70 .unwrap_or_default()
71 }
72
73 pub fn AddPendingRequest(
75 &self,
76 id:String,
77 sender:tokio::sync::oneshot::Sender<Result<serde_json::Value, CommonError>>,
78 ) {
79 if let Ok(mut guard) = self.PendingUserInterfaceRequest.lock() {
80 guard.insert(id, sender);
81 dev_log!("window", "[UIState] Pending UI request added");
82 }
83 }
84
85 pub fn RemovePendingRequest(
87 &self,
88 id:&str,
89 ) -> Option<tokio::sync::oneshot::Sender<Result<serde_json::Value, CommonError>>> {
90 if let Ok(mut guard) = self.PendingUserInterfaceRequest.lock() {
91 let sender = guard.remove(id);
92 dev_log!("window", "[UIState] Pending UI request removed: {}", id);
93 sender
94 } else {
95 None
96 }
97 }
98
99 pub fn ClearAll(&self) {
101 if let Ok(mut guard) = self.PendingUserInterfaceRequest.lock() {
102 guard.clear();
103 dev_log!("window", "[UIState] All pending UI requests cleared");
104 }
105 }
106
107 pub fn Count(&self) -> usize {
109 self.PendingUserInterfaceRequest
110 .lock()
111 .ok()
112 .map(|guard| guard.len())
113 .unwrap_or(0)
114 }
115
116 pub fn Contains(&self, id:&str) -> bool {
118 self.PendingUserInterfaceRequest
119 .lock()
120 .ok()
121 .map(|guard| guard.contains_key(id))
122 .unwrap_or(false)
123 }
124}