-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathlist-test-async.ts
120 lines (104 loc) · 4.16 KB
/
list-test-async.ts
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
import { Component, ChangeDetectionStrategy } from "@angular/core";
import { DataItem, DataService } from "./data.service";
import { Observable, BehaviorSubject } from "rxjs";
import { combineLatest } from "rxjs/operators";
@Component({
selector: "list-test-async",
styleUrls: ["./styles.css"],
providers: [DataService],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<GridLayout rows="auto * auto" columns="* *">
<Label text="ListView" class="list-title"></Label>
<Label text="*ngFor" class="list-title" col="1"></Label>
<ListView row="1" [items]="service.items$ | async" (itemTap)="onItemTap($event)" margin="10">
<ng-template let-item="item" let-i="index" let-odd="odd" let-even="even">
<StackLayout [class.odd]="odd" [class.even]="even">
<Label [text]='"name: " + item.name'></Label>
</StackLayout>
</ng-template>
</ListView>
<StackLayout row="1" col="1" margin="10">
<StackLayout *ngFor="let item of (service.items$ | async); let odd = odd; let even = even"
[class.odd]="odd" [class.even]="even" marginBottom="1">
<Label [text]='"name: " + item.name'></Label>
</StackLayout>
</StackLayout>
<Button
row="2" colSpan="2"
(tap)="toggleAsyncUpdates()"
[text]="isUpdating ? 'stop updates' : 'start updates'">
</Button>
</GridLayout>
`
})
export class ListTestAsync {
public isUpdating: boolean = false;
constructor(private service: DataService) {
}
public onItemTap(args) {
console.log("--> ItemTapped: " + args.index);
}
public toggleAsyncUpdates() {
if (this.isUpdating) {
this.service.stopAsyncUpdates();
} else {
this.service.startAsyncUpdates();
}
this.isUpdating = !this.isUpdating;
}
}
@Component({
selector: "list-test-async-filter",
styleUrls: ["./styles.css"],
providers: [DataService],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<GridLayout rows="auto * auto" columns="* *">
<Label text="ListView" class="list-title"></Label>
<Label text="*ngFor" class="list-title" col="1"></Label>
<ListView row="1" [items]="filteredItems$ | async" (itemTap)="onItemTap($event)" margin="10">
<ng-template let-item="item" let-i="index" let-odd="odd" let-even="even">
<StackLayout [class.odd]="odd" [class.even]="even">
<Label [text]='"name: " + item.name'></Label>
</StackLayout>
</ng-template>
</ListView>
<StackLayout row="1" col="1" margin="10">
<StackLayout *ngFor="let item of (filteredItems$ | async); let odd = odd; let even = even"
[class.odd]="odd" [class.even]="even" marginBottom="1">
<Label [text]='"name: " + item.name'></Label>
</StackLayout>
</StackLayout>
<StackLayout row="2" colSpan="2" orientation="horizontal">
<button (tap)="toggleAsyncUpdates()" [text]="isUpdating ? 'stop updates' : 'start updates'"></button>
<button (tap)="toggleFilter()" [text]="(filter$ | async) ? 'show all' : 'show even'"></button>
</StackLayout>
</GridLayout>
`
})
export class ListTestFilterAsync {
public isUpdating: boolean = false;
public filteredItems$: Observable<Array<DataItem>>;
private filter$ = new BehaviorSubject(false);
constructor(private service: DataService) {
this.filteredItems$ = this.service.items$.pipe(
combineLatest(this.filter$, (data, filter) => {
return filter ? data.filter(v => v.id % 2 === 0) : data;
}));
}
public onItemTap(args) {
console.log("--> ItemTapped: " + args.index);
}
public toggleAsyncUpdates() {
if (this.isUpdating) {
this.service.stopAsyncUpdates();
} else {
this.service.startAsyncUpdates();
}
this.isUpdating = !this.isUpdating;
}
public toggleFilter() {
this.filter$.next(!this.filter$.value);
}
}