fui_core 0.18.0

Core library of FUI MVVM UI Framework
Documentation
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
use crate::{Event, ObservableCollection, Subscription, VecDiff};
use std::cell::{Cell, RefCell};
use std::rc::Rc;

///
/// ObservableCollectionFlatMap.
///
pub struct ObservableCollectionFlatMap<T: 'static + Clone> {
    items: Rc<RefCell<Vec<T>>>,

    _sub_collection_data: Rc<RefCell<Vec<SubCollectionData>>>,
    _sub_collection_data_indexes: Rc<RefCell<Vec<Rc<Cell<i32>>>>>,

    changed_event: Rc<RefCell<Event<VecDiff<T>>>>,
    _items_changed_event_subscription: Option<Subscription>,
}

struct SubCollectionData {
    pub pos: i32,  // starting position in the output collection
    pub size: i32, // number of elements
    _items_changed_event_subscription: Option<Subscription>,
}

impl<T: 'static + Clone> ObservableCollection<T> for ObservableCollectionFlatMap<T> {
    fn len(&self) -> usize {
        self.items.borrow().len()
    }

    fn get(&self, index: usize) -> Option<T> {
        self.items
            .borrow()
            .as_slice()
            .get(index)
            .map(|el| el.clone())
    }

    fn on_changed(&self, f: Box<dyn FnMut(VecDiff<T>)>) -> Option<Subscription> {
        Some(Subscription::EventSubscription(
            self.changed_event.borrow_mut().subscribe(f),
        ))
    }
}

pub trait ObservableCollectionFlatMapExt<TSrc>
where
    TSrc: Clone + 'static,
{
    fn flat_map<TDst, TDstColl, F>(&self, f: F) -> ObservableCollectionFlatMap<TDst>
    where
        TDst: Clone + 'static,
        TDstColl: ObservableCollection<TDst> + IntoIterator<Item = TDst>,
        F: 'static + FnMut(&TSrc) -> TDstColl;
}

impl<TSrc> ObservableCollectionFlatMapExt<TSrc> for dyn ObservableCollection<TSrc>
where
    TSrc: Clone + 'static,
{
    /// Flat map creates new observable collection.
    ///
    /// It keeps mapped copy of every item.
    ///
    /// The only connection between it and original observable collection
    /// is by subscribing on the `on_changed` event of the source collection,
    /// so we don't have to keep implicit reference to the source collection.
    /// The `on_change` event of source collection keeps a weak reference to our handler.
    fn flat_map<TDst, TDstColl, F>(&self, mut f: F) -> ObservableCollectionFlatMap<TDst>
    where
        TDst: Clone + 'static,
        TDstColl: ObservableCollection<TDst> + IntoIterator<Item = TDst>,
        F: FnMut(&TSrc) -> TDstColl + 'static,
    {
        let changed_event_rc = Rc::new(RefCell::new(Event::new()));

        // all the items from all sub-collections
        let items_rc = Rc::new(RefCell::new(Vec::new()));

        // sub-collections data
        let sub_collection_data_rc = Rc::new(RefCell::new(Vec::new()));
        let sub_collection_data_indexes_rc = Rc::new(RefCell::new(Vec::new()));

        // copy items from sub-collections and subscribe to each sub-collection's changes
        let mut pos = 0;
        let mut index = 0i32;
        for src_item in self as &dyn ObservableCollection<TSrc> {
            let dest_items = f(&src_item);
            let size = dest_items.len();

            // update sub_collection_data_indexes
            let sub_collection_data_index = Rc::new(Cell::new(index));
            let mut sub_collection_data_indexes = sub_collection_data_indexes_rc.borrow_mut();
            sub_collection_data_indexes.push(sub_collection_data_index.clone());

            // subscribe to changes
            let subscription = subscribe_to_subcollection(
                &dest_items,
                items_rc.clone(),
                sub_collection_data_rc.clone(),
                sub_collection_data_index,
                changed_event_rc.clone(),
            );

            sub_collection_data_rc.borrow_mut().push(SubCollectionData {
                pos,
                size: size as i32,
                _items_changed_event_subscription: subscription,
            });

            // insert items
            items_rc.borrow_mut().extend(dest_items);

            pos += size as i32;
            index += 1;
        }

        let handler = Box::new({
            let items_rc = items_rc.clone();
            let sub_collection_data_rc = sub_collection_data_rc.clone();
            let sub_collection_data_indexes_rc = sub_collection_data_indexes_rc.clone();
            let changed_event_rc = changed_event_rc.clone();
            move |changed_args| match changed_args {
                VecDiff::Clear {} => {
                    // we are removing all sub-collections
                    items_rc.borrow_mut().clear();
                    sub_collection_data_rc.borrow_mut().clear();
                    sub_collection_data_indexes_rc.borrow_mut().clear();
                    changed_event_rc.borrow().emit(VecDiff::Clear {});
                }

                VecDiff::InsertAt { index, value } => {
                    // we are inserting new sub-collection
                    insert_collection_at(
                        index,
                        f(&value),
                        sub_collection_data_rc.clone(),
                        sub_collection_data_indexes_rc.clone(),
                        items_rc.clone(),
                        changed_event_rc.clone(),
                    );
                }

                VecDiff::RemoveAt { index } => {
                    // we are removing a single sub-collection
                    remove_collection_at(
                        index,
                        sub_collection_data_rc.clone(),
                        sub_collection_data_indexes_rc.clone(),
                        items_rc.clone(),
                        changed_event_rc.clone(),
                    );
                }

                VecDiff::Move {
                    old_index,
                    new_index,
                } => {
                    // we are moving a single sub-collection
                    let mut sub_collection_data = sub_collection_data_rc.borrow_mut();
                    let data = sub_collection_data.remove(old_index);
                    let items_from = data.pos;
                    let items_len = data.size;
                    sub_collection_data.insert(new_index, data);

                    let mut sub_collection_data_indexes =
                        sub_collection_data_indexes_rc.borrow_mut();
                    let index = sub_collection_data_indexes.remove(old_index);
                    sub_collection_data_indexes.insert(new_index, index);

                    // update indexes
                    for i in old_index.min(new_index)..sub_collection_data_indexes.len() {
                        sub_collection_data_indexes[i].set(i as i32)
                    }

                    // update data
                    for i in old_index.min(new_index)..sub_collection_data_indexes.len() {
                        sub_collection_data[i].pos = if i > 0 {
                            sub_collection_data[i - 1].pos + sub_collection_data[i - 1].size
                        } else {
                            0
                        };
                    }

                    let items_to = sub_collection_data[new_index].pos;

                    // move items
                    let mut items = items_rc.borrow_mut();
                    let changed_event = changed_event_rc.borrow();
                    if items_from > items_to {
                        for _ in 0..items_len {
                            let old_index = (items_from + items_len - 1) as usize;
                            let new_index = items_to as usize;

                            let item = items.remove(old_index);
                            items.insert(new_index, item);

                            changed_event.emit(VecDiff::Move {
                                old_index,
                                new_index,
                            });
                        }
                    } else if items_from < items_to {
                        for _ in 0..items_len {
                            let old_index = items_from as usize;
                            let new_index = (items_to + items_len - 1) as usize;

                            let item = items.remove(old_index);
                            items.insert(new_index, item);

                            changed_event.emit(VecDiff::Move {
                                old_index,
                                new_index,
                            });
                        }
                    }
                }

                VecDiff::Pop {} => {
                    // we are popping a single sub-collection
                    let len = sub_collection_data_rc.borrow().len();
                    remove_collection_at(
                        len - 1,
                        sub_collection_data_rc.clone(),
                        sub_collection_data_indexes_rc.clone(),
                        items_rc.clone(),
                        changed_event_rc.clone(),
                    );
                }

                VecDiff::Push { value } => {
                    // we are pushing a single sub-collection
                    let len = sub_collection_data_rc.borrow().len();
                    insert_collection_at(
                        len,
                        f(&value),
                        sub_collection_data_rc.clone(),
                        sub_collection_data_indexes_rc.clone(),
                        items_rc.clone(),
                        changed_event_rc.clone(),
                    );
                }
            }
        });
        let event_subscription = self.on_changed(handler);

        ObservableCollectionFlatMap {
            items: items_rc,
            _sub_collection_data: sub_collection_data_rc,
            _sub_collection_data_indexes: sub_collection_data_indexes_rc,
            changed_event: changed_event_rc,
            _items_changed_event_subscription: event_subscription,
        }
    }
}

fn subscribe_to_subcollection<TDst, TDstColl>(
    new_items: &TDstColl,
    items_rc: Rc<RefCell<Vec<TDst>>>,
    sub_collection_data_rc: Rc<RefCell<Vec<SubCollectionData>>>,
    sub_collection_data_index_rc: Rc<Cell<i32>>,
    changed_event_rc: Rc<RefCell<Event<VecDiff<TDst>>>>,
) -> Option<Subscription>
where
    TDst: Clone + 'static,
    TDstColl: ObservableCollection<TDst> + IntoIterator<Item = TDst>,
{
    let handler = Box::new({
        move |changed_args: VecDiff<TDst>| match changed_args {
            VecDiff::Clear {} => {
                // clear all elements from current sub-collection
                // (but not remove it)

                let collection_index = sub_collection_data_index_rc.get() as usize;

                // update sub_collection_data
                let mut sub_collection_data = sub_collection_data_rc.borrow_mut();
                let pos = sub_collection_data[collection_index].pos;
                let size = sub_collection_data[collection_index].size;
                sub_collection_data[collection_index].size = 0;
                for i in collection_index + 1..sub_collection_data.len() {
                    sub_collection_data[i].pos -= size;
                }

                // remove items
                let mut items = items_rc.borrow_mut();
                let changed_event = changed_event_rc.borrow();
                for i in (pos..pos + size).rev() {
                    items.remove(i as usize);
                    changed_event.emit(VecDiff::RemoveAt { index: i as usize });
                }
            }

            VecDiff::InsertAt { index, value } => insert_element_at(
                index,
                value,
                items_rc.clone(),
                sub_collection_data_rc.clone(),
                sub_collection_data_index_rc.clone(),
                changed_event_rc.clone(),
            ),

            VecDiff::RemoveAt { index } => {
                remove_element_at(
                    index,
                    items_rc.clone(),
                    sub_collection_data_rc.clone(),
                    sub_collection_data_index_rc.clone(),
                    changed_event_rc.clone(),
                );
            }

            VecDiff::Move {
                old_index,
                new_index,
            } => {
                let value = remove_element_at(
                    old_index,
                    items_rc.clone(),
                    sub_collection_data_rc.clone(),
                    sub_collection_data_index_rc.clone(),
                    changed_event_rc.clone(),
                );
                insert_element_at(
                    new_index,
                    value,
                    items_rc.clone(),
                    sub_collection_data_rc.clone(),
                    sub_collection_data_index_rc.clone(),
                    changed_event_rc.clone(),
                )
            }

            VecDiff::Pop {} => {
                let sub_collection_data = sub_collection_data_rc.borrow_mut();
                let collection_index = sub_collection_data_index_rc.get() as usize;
                let index = (sub_collection_data[collection_index].size - 1) as usize;
                remove_element_at(
                    index,
                    items_rc.clone(),
                    sub_collection_data_rc.clone(),
                    sub_collection_data_index_rc.clone(),
                    changed_event_rc.clone(),
                );
            }

            VecDiff::Push { value } => {
                let sub_collection_data = sub_collection_data_rc.borrow_mut();
                let collection_index = sub_collection_data_index_rc.get() as usize;
                let index = sub_collection_data[collection_index].size as usize;
                insert_element_at(
                    index,
                    value,
                    items_rc.clone(),
                    sub_collection_data_rc.clone(),
                    sub_collection_data_index_rc.clone(),
                    changed_event_rc.clone(),
                );
            }
        }
    });
    new_items.on_changed(handler)
}

fn remove_collection_at<T: 'static + Clone>(
    index: usize,
    sub_collection_data_rc: Rc<RefCell<Vec<SubCollectionData>>>,
    sub_collection_data_indexes_rc: Rc<RefCell<Vec<Rc<Cell<i32>>>>>,
    items_rc: Rc<RefCell<Vec<T>>>,
    changed_event_rc: Rc<RefCell<Event<VecDiff<T>>>>,
) {
    let mut sub_collection_data = sub_collection_data_rc.borrow_mut();

    // remove subscription
    let removed_data = sub_collection_data.remove(index);

    // update sub_collection_data_indexes
    let mut sub_collection_data_indexes = sub_collection_data_indexes_rc.borrow_mut();
    sub_collection_data_indexes.remove(index);
    for i in index..sub_collection_data_indexes.len() {
        sub_collection_data_indexes[i].set(i as i32)
    }

    // fix indexes of other sub-collections
    for i in index..sub_collection_data.len() {
        sub_collection_data[i].pos -= removed_data.size;
    }

    // remove elements
    let mut items = items_rc.borrow_mut();
    let changed_event = changed_event_rc.borrow();
    for i in (removed_data.pos..removed_data.pos + removed_data.size).rev() {
        items.remove(i as usize);
        changed_event.emit(VecDiff::RemoveAt { index: i as usize });
    }
}

fn insert_collection_at<TDst, TDstColl>(
    index: usize,
    value: TDstColl,
    sub_collection_data_rc: Rc<RefCell<Vec<SubCollectionData>>>,
    sub_collection_data_indexes_rc: Rc<RefCell<Vec<Rc<Cell<i32>>>>>,
    items_rc: Rc<RefCell<Vec<TDst>>>,
    changed_event_rc: Rc<RefCell<Event<VecDiff<TDst>>>>,
) where
    TDst: Clone + 'static,
    TDstColl: ObservableCollection<TDst> + IntoIterator<Item = TDst>,
{
    let mut sub_collection_data = sub_collection_data_rc.borrow_mut();
    let new_pos = if index > 0 {
        sub_collection_data[index - 1].pos + sub_collection_data[index - 1].size
    } else {
        0
    };

    // update sub_collection_data_indexes
    let sub_collection_data_index = Rc::new(Cell::new(index as i32));
    let mut sub_collection_data_indexes = sub_collection_data_indexes_rc.borrow_mut();
    sub_collection_data_indexes.insert(index, sub_collection_data_index.clone());
    for i in index + 1..sub_collection_data_indexes.len() {
        sub_collection_data_indexes[i].set(i as i32)
    }

    // get new items
    let new_items = value;

    // subscribe to changes
    let subscription = subscribe_to_subcollection(
        &new_items,
        items_rc.clone(),
        sub_collection_data_rc.clone(),
        sub_collection_data_index,
        changed_event_rc.clone(),
    );

    // update sub_collection_data
    let size = new_items.len() as i32;
    sub_collection_data.insert(
        index,
        SubCollectionData {
            pos: new_pos,
            size,
            _items_changed_event_subscription: subscription,
        },
    );
    for index in index + 1..sub_collection_data.len() {
        sub_collection_data[index].pos += size;
    }

    // insert new items
    for (index, new_item) in new_items.into_iter().enumerate() {
        items_rc
            .borrow_mut()
            .insert(new_pos as usize + index, new_item.clone());
        changed_event_rc.borrow().emit(VecDiff::InsertAt {
            index: new_pos as usize + index,
            value: new_item,
        });
    }
}

fn remove_element_at<T: Clone + 'static>(
    index: usize,
    items_rc: Rc<RefCell<Vec<T>>>,
    sub_collection_data_rc: Rc<RefCell<Vec<SubCollectionData>>>,
    sub_collection_data_index_rc: Rc<Cell<i32>>,
    changed_event_rc: Rc<RefCell<Event<VecDiff<T>>>>,
) -> T {
    let collection_index = sub_collection_data_index_rc.get() as usize;

    // update sub_collection_data
    let mut sub_collection_data = sub_collection_data_rc.borrow_mut();
    let pos = sub_collection_data[collection_index].pos as usize;
    sub_collection_data[collection_index].size -= 1;
    for i in collection_index + 1..sub_collection_data.len() {
        sub_collection_data[i].pos -= 1;
    }

    // remove item
    let mut items = items_rc.borrow_mut();
    let value = items.remove(pos + index);
    changed_event_rc
        .borrow()
        .emit(VecDiff::RemoveAt { index: pos + index });
    value
}

fn insert_element_at<T: Clone + 'static>(
    index: usize,
    value: T,
    items_rc: Rc<RefCell<Vec<T>>>,
    sub_collection_data_rc: Rc<RefCell<Vec<SubCollectionData>>>,
    sub_collection_data_index_rc: Rc<Cell<i32>>,
    changed_event_rc: Rc<RefCell<Event<VecDiff<T>>>>,
) {
    let collection_index = sub_collection_data_index_rc.get() as usize;

    // update sub_collection_data
    let mut sub_collection_data = sub_collection_data_rc.borrow_mut();
    let pos = sub_collection_data[collection_index].pos as usize;
    sub_collection_data[collection_index].size += 1;
    for i in collection_index + 1..sub_collection_data.len() {
        sub_collection_data[i].pos += 1;
    }

    // insert item
    let mut items = items_rc.borrow_mut();
    items.insert(pos + index, value.clone());
    changed_event_rc.borrow().emit(VecDiff::InsertAt {
        index: pos + index,
        value,
    });
}

impl<TSrc, TSrcColl> ObservableCollectionFlatMapExt<TSrc> for TSrcColl
where
    TSrc: Clone + 'static,
    TSrcColl: ObservableCollection<TSrc> + 'static,
{
    /// Flat map creates new observable collection.
    ///
    /// It keeps mapped copy of every item.
    ///
    /// The only connection between it and original observable collection
    /// is by subscribing on the `on_changed` event of the source collection,
    /// so we don't have to keep implicit reference to the source collection.
    /// The `on_change` event of source collection keeps a weak reference to our handler.
    fn flat_map<TDst, TDstColl, F>(&self, f: F) -> ObservableCollectionFlatMap<TDst>
    where
        TDst: Clone + 'static,
        TDstColl: ObservableCollection<TDst> + IntoIterator<Item = TDst>,
        F: FnMut(&TSrc) -> TDstColl + 'static,
    {
        (self as &dyn ObservableCollection<TSrc>).flat_map(f)
    }
}