-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfolding-list-view.ios.ts
755 lines (631 loc) · 29.3 KB
/
folding-list-view.ios.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
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
/*! *****************************************************************************
Copyright (c) 2018 Tangra Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
***************************************************************************** */
import { Observable } from "data/observable";
import { Color, EventData, KeyedTemplate, Length, PercentLength, View, layout } from "ui/core/view";
import * as utils from "utils/utils";
import { ItemEventData } from ".";
import {
FoldingListViewBase,
backViewColorProperty,
containerItemTemplatesProperty,
foregroundItemTemplatesProperty,
} from "./folding-list-view-common";
export * from "./folding-list-view-common";
const infinity = layout.makeMeasureSpec(0, layout.UNSPECIFIED);
interface Constraints {
_constraintTop?: number;
_constraintLeft?: number;
_constraintBottom?: number;
_constraintRight?: number;
}
type ConstraintedView = View & Constraints;
interface FoldingCellView {
foreground: ConstraintedView;
container: ConstraintedView;
index: number;
}
// NOTE: Heights will include constraints (if any) and are in device pixels!
interface FoldingCellHeight {
foreground: number;
container: number;
}
export class FoldingListView extends FoldingListViewBase {
public _ios: UITableView;
public _map: Map<FoldingListViewCell, FoldingCellView>;
public widthMeasureSpec: number = 0;
private _dataSource;
private _delegate;
private _heights: FoldingCellHeight[];
private _preparingCell: boolean;
private _isDataDirty: boolean;
constructor() {
super();
this.nativeViewProtected = this._ios = UITableView.new();
this._ios.registerClassForCellReuseIdentifier(FoldingListViewCell.class(), this._defaultForegroundItemTemplate.key);
this._ios.separatorColor = utils.ios.getter(UIColor, UIColor.clearColor);
this._ios.rowHeight = UITableViewAutomaticDimension;
this._ios.dataSource = this._dataSource = FoldingListViewDataSource.initWithOwner(new WeakRef(this));
this._delegate = FoldingListViewDelegate.initWithOwner(new WeakRef(this));
this._heights = new Array<FoldingCellHeight>();
this._map = new Map<FoldingListViewCell, FoldingCellView>();
this._setNativeClipToBounds();
}
public _setNativeClipToBounds() {
// Always set clipsToBounds for list-view
this._ios.clipsToBounds = true;
}
public onLoaded() {
super.onLoaded();
if (this._isDataDirty) {
this.refresh();
}
this._ios.delegate = this._delegate;
}
public onUnloaded() {
this._ios.delegate = null;
super.onUnloaded();
}
public get ios(): UITableView {
return this._ios;
}
public get _childrenCount(): number {
return this._map.size;
}
public eachChildView(callback: (child: View) => boolean): void {
this._map.forEach((view, key) => {
callback(view.foreground);
callback(view.container);
});
}
public scrollToIndex(index: number, animated: boolean = true) {
if (!this._ios) {
return;
}
const itemsLength = this.items ? this.items.length : 0;
if (itemsLength > 0) {
if (index < 0) {
index = 0;
}
else if (index >= itemsLength) {
index = itemsLength - 1;
}
this._ios.scrollToRowAtIndexPathAtScrollPositionAnimated(
NSIndexPath.indexPathForItemInSection(index, 0),
UITableViewScrollPosition.Top,
animated,
);
}
}
public refresh() {
// clear bindingContext when it is not observable because otherwise bindings to items won't reevaluate
this._map.forEach((view, nativeView) => {
if (!(view.foreground.bindingContext instanceof Observable)) {
view.foreground.bindingContext = null;
}
if (!(view.container.bindingContext instanceof Observable)) {
view.foreground.bindingContext = null;
}
});
if (this.isLoaded) {
this._ios.reloadData();
this.requestLayout();
this._isDataDirty = false;
}
else {
this._isDataDirty = true;
}
}
public getHeight(index: number): FoldingCellHeight {
return this._heights[index];
}
public setHeight(index: number, value: FoldingCellHeight): void {
this._heights[index] = value;
}
public _onFoldedRowHeightPropertyChanged(oldValue: Length, newValue: Length) {
const value = layout.toDeviceIndependentPixels(this._effectiveFoldedRowHeight);
if (value > 0) {
this._ios.estimatedRowHeight = value;
}
super._onFoldedRowHeightPropertyChanged(oldValue, newValue);
}
public requestLayout(): void {
// When preparing cell don't call super - no need to invalidate our measure when cell desiredSize is changed.
if (!this._preparingCell) {
super.requestLayout();
}
}
public measure(widthMeasureSpec: number, heightMeasureSpec: number): void {
this.widthMeasureSpec = widthMeasureSpec;
const changed = (this as any)._setCurrentMeasureSpecs(widthMeasureSpec, heightMeasureSpec);
super.measure(widthMeasureSpec, heightMeasureSpec);
if (changed) {
this._ios.reloadData();
}
}
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
this._map.forEach((cellView, listViewCell) => {
View.measureChild(
this,
cellView.foreground,
(cellView.foreground as any)._currentWidthMeasureSpec,
(cellView.foreground as any)._currentHeightMeasureSpec,
);
View.measureChild(
this,
cellView.container,
(cellView.container as any)._currentWidthMeasureSpec,
(cellView.container as any)._currentHeightMeasureSpec,
);
});
}
public onLayout(left: number, top: number, right: number, bottom: number): void {
super.onLayout(left, top, right, bottom);
this._map.forEach((cellView, listViewCell) => {
const width = layout.getMeasureSpecSize(this.widthMeasureSpec);
const cellHeight = this.getHeight(cellView.index);
if (cellHeight) {
this._layoutConstraintedView(cellView.foreground, width, cellHeight.foreground);
this._layoutConstraintedView(cellView.container, width, cellHeight.container);
}
});
}
public _prepareCell(cell: FoldingListViewCell, indexPath: NSIndexPath): FoldingCellHeight {
let cellHeight: FoldingCellHeight;
let isForegroundViewToBeConstrainedIn: boolean = false;
let isContainerViewToBeConstrainedIn: boolean = false;
const index = indexPath.row;
try {
this._preparingCell = true;
let foregroundView = cell.foregroundViewTNS;
if (!foregroundView) {
foregroundView = this._getForegroundItemTemplate(index).createView();
}
let containerView = cell.containerViewTNS;
if (!containerView) {
containerView = this._getContainerItemTemplate(index).createView();
}
this.notify({
eventName: FoldingListView.itemLoadingEvent,
object: this,
index,
view: {
foreground: foregroundView,
container: containerView,
},
ios: cell,
android: undefined,
} as ItemEventData);
foregroundView = this._checkAndWrapProxyContainers(foregroundView);
containerView = this._checkAndWrapProxyContainers(containerView);
// If cell is reused it have old content - remove it first.
// Foreground
if (!cell.foregroundViewTNS) {
cell.foregroundViewWeakRef = new WeakRef(foregroundView);
isForegroundViewToBeConstrainedIn = true;
}
else if (cell.foregroundViewTNS !== foregroundView) {
isForegroundViewToBeConstrainedIn = true;
this._removeContainer(cell);
cell.foregroundViewTNS.nativeViewProtected.removeFromSuperview();
cell.foregroundViewWeakRef = new WeakRef(foregroundView);
}
this._prepareItem(foregroundView, index);
// Container
if (!cell.containerViewTNS) {
isContainerViewToBeConstrainedIn = true;
cell.containerViewWeakRef = new WeakRef(containerView);
}
else if (cell.containerViewTNS !== containerView) {
isContainerViewToBeConstrainedIn = true;
this._removeContainer(cell);
cell.containerViewTNS.nativeViewProtected.removeFromSuperview();
cell.containerViewWeakRef = new WeakRef(containerView);
}
if (!this.detailDataLoader) {
this._prepareItem(containerView, index);
}
else {
const cachedData = this._getCachedDetailData(index);
if (cachedData) {
cell._bindContainerView(index, cachedData);
}
else if (this._getIsCellExpandedIn(index)) {
this._getDetailDataLoaderPromise(index)
.then((result) => {
this._setCachedDetailData(index, result);
containerView.bindingContext = result;
})
.catch((e) => { console.error("ERROR LOADING DETAILS:", e); });
}
}
const cellView: FoldingCellView = {
foreground: foregroundView,
container: containerView,
index,
};
this._map.set(cell, cellView);
// We expect that views returned from itemLoading are new (e.g. not reused).
if (foregroundView && !foregroundView.parent) {
this._addView(foregroundView);
}
if (containerView && !containerView.parent) {
this._addView(containerView);
}
// NOTE: In the native source the top constraints of both views are unified
// so we are doing the same here to keep the {N} and native properties in sync!
containerView.marginTop = foregroundView.marginTop;
if (isForegroundViewToBeConstrainedIn) {
this._prepareConstrainedView(foregroundView);
}
if (isContainerViewToBeConstrainedIn) {
this._prepareConstrainedView(containerView);
}
cellHeight = this._layoutCell(cellView);
const estimatedRowHeight = layout.toDeviceIndependentPixels(cellHeight.foreground);
if (this._ios.estimatedRowHeight !== estimatedRowHeight) {
this._ios.estimatedRowHeight = estimatedRowHeight;
}
cell.resetNativeViews(cellHeight);
}
finally {
this._preparingCell = false;
}
return cellHeight;
}
public _removeContainer(cell: FoldingListViewCell): void {
const foregroundView = cell.foregroundViewTNS;
const containerView = cell.containerViewTNS;
// This is to clear the StackLayout that is used to wrap ProxyViewContainer instances.
if (!(foregroundView.parent instanceof FoldingListView)) {
this._removeView(foregroundView.parent);
}
if (!(containerView.parent instanceof FoldingListView)) {
this._removeView(containerView.parent);
}
// No need to request layout when we are removing cells.
const preparing = this._preparingCell;
this._preparingCell = true;
foregroundView.parent._removeView(foregroundView);
containerView.parent._removeView(containerView);
this._preparingCell = preparing;
this._map.delete(cell);
}
public isItemAtIndexVisible(index: number): boolean {
const indexes: NSIndexPath[] = Array.from(this._ios.indexPathsForVisibleRows);
return indexes.some((visIndex) => visIndex.row === index);
}
public [backViewColorProperty.getDefault](): UIColor {
return (FoldingListViewCell.alloc().init()).backViewColor;
}
public [backViewColorProperty.setNative](value: Color | UIColor) {
const actualColor: UIColor = value instanceof Color ? value.ios : value;
this._map.forEach((view, cell) => { cell.backViewColor = actualColor; });
}
public [foregroundItemTemplatesProperty.getDefault](): KeyedTemplate[] {
return null;
}
public [foregroundItemTemplatesProperty.setNative](value: KeyedTemplate[]) {
this._foregroundItemTemplatesInternal = new Array<KeyedTemplate>(this._defaultForegroundItemTemplate);
if (value) {
for (const item of value) {
this._ios.registerClassForCellReuseIdentifier(FoldingListViewCell.class(), item.key);
}
this._foregroundItemTemplatesInternal = this._foregroundItemTemplatesInternal.concat(value);
}
this.refresh();
}
public [containerItemTemplatesProperty.getDefault](): KeyedTemplate[] {
return null;
}
public [containerItemTemplatesProperty.setNative](value: KeyedTemplate[]) {
this._containerItemTemplatesInternal = new Array<KeyedTemplate>(this._defaultContainerItemTemplate);
if (value) {
for (const item of value) {
this._ios.registerClassForCellReuseIdentifier(FoldingListViewCell.class(), item.key);
}
this._containerItemTemplatesInternal = this._containerItemTemplatesInternal.concat(value);
}
this.refresh();
}
public _measureConstraintedChild(view: ConstraintedView, measuredHeight: number) {
return View.measureChild(
this,
view,
this.widthMeasureSpec - (view._constraintLeft + view._constraintRight),
measuredHeight,
);
}
public _layoutConstraintedView(view: ConstraintedView, width: number, height: number) {
View.layoutChild(
this,
view,
0,
0,
width - (view._constraintLeft + view._constraintRight),
height - view._constraintTop
);
}
private _layoutCell(cellView: FoldingCellView): FoldingCellHeight {
if (cellView) {
const measureForegroundSize = this._measureConstraintedChild(cellView.foreground, layout.makeMeasureSpec(this._effectiveFoldedRowHeight, layout.EXACTLY));
const measuredContainerSize = this._measureConstraintedChild(cellView.container, infinity);
const height: FoldingCellHeight = {
// This is needed since we use this height to return the rowheight to the table view.
// Bottom constraints are not applied.
foreground: measureForegroundSize.measuredHeight + cellView.foreground._constraintTop,
container: measuredContainerSize.measuredHeight + cellView.container._constraintTop,
};
this.setHeight(cellView.index, height);
return height;
}
return {
foreground: this._effectiveFoldedRowHeight,
container: this._effectiveFoldedRowHeight,
};
}
private _prepareConstrainedView(view: ConstraintedView) {
view._constraintTop = PercentLength.toDevicePixels(view.marginTop);
view._constraintLeft = PercentLength.toDevicePixels(view.marginLeft) + this.effectivePaddingLeft;
view._constraintBottom = PercentLength.toDevicePixels(view.marginBottom);
view._constraintRight = PercentLength.toDevicePixels(view.marginRight) + this.effectivePaddingRight;
view.margin = "0";
}
}
class FoldingListViewCell extends FoldingCell {
public foregroundViewWeakRef: WeakRef<ConstraintedView>;
public containerViewWeakRef: WeakRef<ConstraintedView>;
public get foregroundViewTNS(): ConstraintedView {
return this.foregroundViewWeakRef ? this.foregroundViewWeakRef.get() : null;
}
public get containerViewTNS(): ConstraintedView {
return this.containerViewWeakRef ? this.containerViewWeakRef.get() : null;
}
private _containerViewHeightConstraints: NSArray<NSLayoutConstraint>;
public willMoveToSuperview(newSuperview: UIView): void {
const parent = (this.foregroundViewTNS ? this.foregroundViewTNS.parent as FoldingListView : null);
// When inside ListView and there is no newSuperview this cell is
// removed from native visual tree so we remove it from our tree too.
if (parent && !newSuperview) {
parent._removeContainer(this);
}
}
public resetNativeViews(cellHeight: FoldingCellHeight) {
const parent = (this.foregroundViewTNS ? this.foregroundViewTNS.parent as FoldingListView : null);
for (let loop = this.contentView.subviews.count - 1; loop >= 0; loop--) {
this.contentView.subviews.objectAtIndex(loop).removeFromSuperview();
}
this._initForegroundView(cellHeight.foreground);
this._initContainerView(cellHeight.container);
this.backViewColor = parent.backViewColor.ios;
this.commonInit();
}
public resetContainerViewHeightContraint(newHeight: number) {
const topConstraintValue = layout.toDeviceIndependentPixels(this.containerViewTNS._constraintTop);
if (this._containerViewHeightConstraints) {
NSLayoutConstraint.deactivateConstraints(this._containerViewHeightConstraints);
}
this._containerViewHeightConstraints = NSLayoutConstraint.constraintsWithVisualFormatOptionsMetricsViews(
`V:[layer(==${layout.toDeviceIndependentPixels(newHeight) - topConstraintValue})]`,
0,
null,
{ layer: this.containerView } as any,
);
NSLayoutConstraint.activateConstraints(this._containerViewHeightConstraints);
}
public animationDurationType(itemIndex: number, type: AnimationType): number {
const parent = (this.foregroundViewTNS ? this.foregroundViewTNS.parent as FoldingListView : null);
return parent.foldAnimationDuration / 1000;
}
public _bindContainerView(index: number, dataItem: any) {
const containerView = this.containerViewTNS;
const parent = containerView.parent as FoldingListView;
const width = layout.getMeasureSpecSize(parent.widthMeasureSpec);
containerView.bindingContext = dataItem;
const size = parent._measureConstraintedChild(containerView, infinity);
const cellHeight = parent.getHeight(index);
cellHeight.container = size.measuredHeight + containerView._constraintTop;
parent.setHeight(index, cellHeight);
if (containerView && (containerView as any).isLayoutRequired) {
parent._layoutConstraintedView(containerView, width, cellHeight.container);
}
this.resetContainerViewHeightContraint(cellHeight.container);
}
private _initForegroundView(height: number) {
const topConstraintValue = layout.toDeviceIndependentPixels(this.foregroundViewTNS._constraintTop);
const foregroundView = RotatedView.alloc().initWithFrame(CGRectZero);
foregroundView.translatesAutoresizingMaskIntoConstraints = false;
foregroundView.addSubview(this.foregroundViewTNS.nativeViewProtected);
this.contentView.addSubview(foregroundView);
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormatOptionsMetricsViews(
`V:[layer(==${layout.toDeviceIndependentPixels(height) - topConstraintValue})]`,
0,
null,
{ layer: foregroundView } as any,
));
// NOTE: We need to add the UITableView insets to the right as otherwise the views get shifted
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormatOptionsMetricsViews(
`H:|-${layout.toDeviceIndependentPixels(this.foregroundViewTNS._constraintLeft)}-[layer]-${layout.toDeviceIndependentPixels(this.foregroundViewTNS._constraintRight)}-|`,
0,
null,
{ layer: foregroundView } as any,
));
const top = NSLayoutConstraint.constraintsWithVisualFormatOptionsMetricsViews(
`V:|-${topConstraintValue}-[layer]`,
0,
null,
{ layer: foregroundView } as any,
);
NSLayoutConstraint.activateConstraints(top);
this.foregroundView = foregroundView;
this.foregroundViewTop = top.objectAtIndex(0);
}
private _initContainerView(height: number) {
const topConstraintValue = layout.toDeviceIndependentPixels(this.containerViewTNS._constraintTop);
const containerView = UIView.alloc().initWithFrame(CGRectZero);
containerView.translatesAutoresizingMaskIntoConstraints = false;
containerView.addSubview(this.containerViewTNS.nativeViewProtected);
this.contentView.addSubview(containerView);
this.containerView = containerView;
this.resetContainerViewHeightContraint(height);
// NOTE: We need to add the UITableView insets to the right as otherwise the views get shifted
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormatOptionsMetricsViews(
`H:|-${layout.toDeviceIndependentPixels(this.containerViewTNS._constraintLeft)}-[layer]-${layout.toDeviceIndependentPixels(this.containerViewTNS._constraintRight)}-|`,
0,
null,
{ layer: containerView } as any,
));
const top = NSLayoutConstraint.constraintsWithVisualFormatOptionsMetricsViews(
`V:|-${topConstraintValue}-[layer]`,
0,
null,
{ layer: containerView } as any,
);
NSLayoutConstraint.activateConstraints(top);
this.containerViewTop = top.objectAtIndex(0);
containerView.layoutIfNeeded();
}
}
@ObjCClass(UITableViewDelegate)
class FoldingListViewDelegate extends NSObject implements UITableViewDelegate {
public static initWithOwner(owner: WeakRef<FoldingListView>): FoldingListViewDelegate {
const delegate = FoldingListViewDelegate.new() as FoldingListViewDelegate;
delegate._owner = owner;
return delegate;
}
private _owner: WeakRef<FoldingListView>;
public tableViewHeightForRowAtIndexPath(tableView: UITableView, indexPath: NSIndexPath): number {
const owner = this._owner.get();
const cellHeight = owner.getHeight(indexPath.row);
// In cases of very big heights it could happen that we do not know the heights,
// ini which case we return the folded row height.
if (!cellHeight) {
return layout.toDeviceIndependentPixels(owner._effectiveFoldedRowHeight);
}
return layout.toDeviceIndependentPixels(owner._getIsCellExpandedIn(indexPath.row) ? cellHeight.container : cellHeight.foreground);
}
public tableViewDidSelectRowAtIndexPath(tableView: UITableView, indexPath: NSIndexPath): void {
const cell = tableView.cellForRowAtIndexPath(indexPath) as FoldingListViewCell;
const owner = this._owner.get();
if (cell.isAnimating()) {
return;
}
const isExpandedIn = !owner._getIsCellExpandedIn(indexPath.row);
const index = indexPath.row;
if (isExpandedIn && owner.detailDataLoader) {
owner._getDetailDataLoaderPromise(index)
.then((value) => {
cell._bindContainerView(index, value);
owner._setCachedDetailData(index, value);
// HACK: this is needed as seems the value of bounds.size is not immediately updated
setTimeout(() => { this._performCellUnfold(cell, index, isExpandedIn); }, 1);
})
.catch((e) => { console.error("ERROR LOADING DETAILS:", e); });
}
else {
this._performCellUnfold(cell, index, isExpandedIn);
}
// If cell is collapsed clear the cached data so it can be loaded again on expand.
if (!isExpandedIn) {
owner.invalidateChachedDetailData(index);
}
}
public tableViewWillDisplayCellForRowAtIndexPath(tableView: UITableView, cell: UITableViewCell, indexPath: NSIndexPath) {
const foldingCell = cell as FoldingListViewCell;
const owner = this._owner.get();
const isExpandedIn = owner._getIsCellExpandedIn(indexPath.row);
if (owner && (indexPath.row === owner.items.length - 1)) {
owner.notify({
eventName: FoldingListViewBase.loadMoreItemsEvent,
object: owner,
} as EventData);
}
foldingCell.unfoldAnimatedCompletion(isExpandedIn, false, null);
}
private _performCellUnfold(cell: FoldingListViewCell, index: number, isExpandedIn: boolean) {
const owner = this._owner.get();
if (owner.toggleMode && isExpandedIn) {
const expandedIndex = owner._cellExpanded.findIndex((value) => value);
let expandedCell: FoldingListViewCell;
owner._map.forEach((value, key) => {
if (value.index === expandedIndex) {
expandedCell = key;
}
});
// cell has been reused so simply mark is not expanded
if (!expandedCell) {
owner._setIsCellExpandedIn(expandedIndex, false);
}
else {
this._performCellUnfold(expandedCell, expandedIndex, false);
}
}
owner._setIsCellExpandedIn(index, isExpandedIn);
cell.unfoldAnimatedCompletion(isExpandedIn, true, null);
let duration: number = owner.foldsCount * (owner.foldAnimationDuration / 1000);
if (isExpandedIn) {
duration /= 2;
}
UIView.animateWithDurationDelayOptionsAnimationsCompletion(
duration,
0,
UIViewAnimationOptions.CurveEaseOut,
() => {
owner.ios.beginUpdates();
owner.ios.endUpdates();
},
null,
);
}
}
@ObjCClass(UITableViewDataSource)
class FoldingListViewDataSource extends NSObject implements UITableViewDataSource {
public static initWithOwner(owner: WeakRef<FoldingListView>): FoldingListViewDataSource {
const dataSource = FoldingListViewDataSource.new() as FoldingListViewDataSource;
dataSource._owner = owner;
return dataSource;
}
private _owner: WeakRef<FoldingListView>;
public tableViewNumberOfRowsInSection(tableView: UITableView, section: number) {
const owner = this._owner.get();
return (owner && owner.items) ? owner.items.length : 0;
}
public tableViewCellForRowAtIndexPath(tableView: UITableView, indexPath: NSIndexPath): UITableViewCell {
// We call this method because ...ForIndexPath calls tableViewHeightForRowAtIndexPath immediately (before we can prepare and measure it).
const owner = this._owner.get();
let cell: FoldingListViewCell;
if (owner) {
const template = owner._getForegroundItemTemplate(indexPath.row);
cell = (tableView.dequeueReusableCellWithIdentifier(template.key) || FoldingListViewCell.new()) as FoldingListViewCell;
const cellHeight = owner._prepareCell(cell, indexPath);
const width = layout.getMeasureSpecSize(owner.widthMeasureSpec);
const foregroundView = cell.foregroundViewTNS;
if (foregroundView && (foregroundView as any).isLayoutRequired) {
// Arrange cell views. We do it here instead of _layoutCell because _layoutCell is called
// from 'tableViewHeightForRowAtIndexPath' method too (in iOS 7.1) and we don't want to arrange the fake cell.
owner._layoutConstraintedView(foregroundView, width, cellHeight.foreground);
}
const containerView = cell.containerViewTNS;
if (containerView && (containerView as any).isLayoutRequired) {
// Arrange cell views. We do it here instead of _layoutCell because _layoutCell is called
// from 'tableViewHeightForRowAtIndexPath' method too (in iOS 7.1) and we don't want to arrange the fake cell.
owner._layoutConstraintedView(containerView, width, cellHeight.container);
}
cell.itemCount = owner.foldsCount;
}
else {
cell = FoldingListViewCell.new() as FoldingListViewCell;
}
return cell;
}
}