euv_ui/hook/use_storage/impl.rs
1use super::*;
2
3/// Inherent implementation of [`UseStorage`].
4impl UseStorage {
5 /// Creates a handle seeded with the stored value, falling back to
6 /// `initial` when the key is not in storage yet.
7 ///
8 /// # Arguments
9 ///
10 /// - `&'static str` - The storage key.
11 /// - `UseStorageKind` - Which storage to read from.
12 /// - `&str` - The storage key.
13 ///
14 /// # Returns
15 ///
16 /// - `UseStorage` - The fresh handle.
17 pub(crate) fn new(key: &'static str, kind: UseStorageKind, initial: &str) -> Self {
18 let stored: Option<String> = read_storage(kind, key);
19 let value: String = stored.unwrap_or_else(|| initial.to_owned());
20 Self {
21 key,
22 value: Signal::create(value),
23 kind,
24 }
25 }
26
27 /// Returns the current value as a snapshot.
28 ///
29 /// # Returns
30 ///
31 /// - `String` - The stored value.
32 pub fn current(&self) -> String {
33 self.get_value().get()
34 }
35
36 /// Writes `value` to storage and updates the signal.
37 ///
38 /// # Arguments
39 ///
40 /// - `&str` - The value to store.
41 pub fn set_value(&self, value: &str) {
42 write_storage(self.get_kind(), self.get_key(), value);
43 self.get_value().set(value.to_owned());
44 }
45
46 /// Removes the key from storage and clears the signal.
47 pub fn clear(&self) {
48 remove_storage(self.get_kind(), self.get_key());
49 self.get_value().set(String::new());
50 }
51}