1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//! use_list hook for list state management
//!
//! Provides convenient methods for managing list state including
//! push, pop, insert, remove, and more.
//!
//! # Example
//!
//! ```rust,ignore
//! use rnk::prelude::*;
//!
//! fn todo_app() -> Element {
//! let todos = use_list(vec!["Buy milk", "Walk dog"]);
//!
//! use_input(move |input, key| {
//! if input == "a" {
//! todos.push("New item");
//! } else if input == "d" && !todos.is_empty() {
//! todos.pop();
//! }
//! });
//!
//! // Render todos...
//! }
//! ```
use crate::hooks::use_signal::{Signal, use_signal};
/// Handle for list operations
#[derive(Clone)]
pub struct ListHandle<T> {
signal: Signal<Vec<T>>,
}
impl<T> ListHandle<T>
where
T: Clone + Send + Sync + 'static,
{
/// Get a clone of the current list
pub fn get(&self) -> Vec<T> {
self.signal.get()
}
/// Get the length of the list
pub fn len(&self) -> usize {
self.signal.with(|v| v.len())
}
/// Check if the list is empty
pub fn is_empty(&self) -> bool {
self.signal.with(|v| v.is_empty())
}
/// Push an item to the end
pub fn push(&self, item: T) {
self.signal.update(|v| v.push(item));
}
/// Pop an item from the end
pub fn pop(&self) -> Option<T> {
let mut result = None;
self.signal.update(|v| {
result = v.pop();
});
result
}
/// Insert an item at the given index
pub fn insert(&self, index: usize, item: T) {
self.signal.update(|v| {
if index <= v.len() {
v.insert(index, item);
}
});
}
/// Remove an item at the given index
pub fn remove(&self, index: usize) -> Option<T> {
let mut result = None;
self.signal.update(|v| {
if index < v.len() {
result = Some(v.remove(index));
}
});
result
}
/// Clear all items
pub fn clear(&self) {
self.signal.update(|v| v.clear());
}
/// Set the entire list
pub fn set(&self, items: Vec<T>) {
self.signal.set(items);
}
/// Get an item at the given index
pub fn get_at(&self, index: usize) -> Option<T> {
self.signal.with(|v| v.get(index).cloned())
}
/// Update an item at the given index
pub fn update_at(&self, index: usize, item: T) {
self.signal.update(|v| {
if index < v.len() {
v[index] = item;
}
});
}
/// Swap two items
pub fn swap(&self, a: usize, b: usize) {
self.signal.update(|v| {
if a < v.len() && b < v.len() {
v.swap(a, b);
}
});
}
/// Move an item from one index to another
pub fn move_item(&self, from: usize, to: usize) {
self.signal.update(|v| {
if from < v.len() && to < v.len() && from != to {
let item = v.remove(from);
v.insert(to, item);
}
});
}
/// Reverse the list
pub fn reverse(&self) {
self.signal.update(|v| v.reverse());
}
/// Get the first item
pub fn first(&self) -> Option<T> {
self.signal.with(|v| v.first().cloned())
}
/// Get the last item
pub fn last(&self) -> Option<T> {
self.signal.with(|v| v.last().cloned())
}
/// Apply a function to each item
pub fn for_each<F>(&self, f: F)
where
F: FnMut(&T),
{
self.signal.with(|v| v.iter().for_each(f));
}
}
impl<T> ListHandle<T>
where
T: Clone + Send + Sync + PartialEq + 'static,
{
/// Check if the list contains an item
pub fn contains(&self, item: &T) -> bool {
self.signal.with(|v| v.contains(item))
}
/// Find the index of an item
pub fn index_of(&self, item: &T) -> Option<usize> {
self.signal.with(|v| v.iter().position(|x| x == item))
}
/// Remove the first occurrence of an item
pub fn remove_item(&self, item: &T) -> bool {
let mut removed = false;
self.signal.update(|v| {
if let Some(pos) = v.iter().position(|x| x == item) {
v.remove(pos);
removed = true;
}
});
removed
}
}
/// Create a list state with the given initial items
pub fn use_list<T>(initial: Vec<T>) -> ListHandle<T>
where
T: Clone + Send + Sync + 'static,
{
let signal = use_signal(|| initial);
ListHandle { signal }
}
/// Create an empty list state
pub fn use_list_empty<T>() -> ListHandle<T>
where
T: Clone + Send + Sync + 'static,
{
use_list(Vec::new())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_use_list_compiles() {
fn _test() {
let list = use_list(vec![1, 2, 3]);
list.push(4);
list.pop();
list.insert(0, 0);
list.remove(0);
list.clear();
let _ = list.len();
let _ = list.is_empty();
}
}
#[test]
fn test_use_list_empty_compiles() {
fn _test() {
let list: ListHandle<String> = use_list_empty();
list.push("hello".to_string());
}
}
#[test]
fn test_list_operations_compile() {
fn _test() {
let list = use_list(vec!["a", "b", "c"]);
list.swap(0, 1);
list.move_item(0, 2);
list.reverse();
let _ = list.first();
let _ = list.last();
let _ = list.contains(&"a");
let _ = list.index_of(&"b");
list.remove_item(&"c");
}
}
}