@nstudio/ui-collectionview 5.1.8 → 5.1.9-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.ios.js CHANGED
@@ -1,6 +1,5 @@
1
1
  import { ChangeType, ContentView, Observable, Property, ProxyViewContainer, Trace, Utils, View, paddingBottomProperty, paddingLeftProperty, paddingRightProperty, paddingTopProperty, profile } from '@nativescript/core';
2
- import { reorderLongPressEnabledProperty, reorderingEnabledProperty, reverseLayoutProperty, scrollBarIndicatorVisibleProperty } from '.';
3
- import { CLog, CLogTypes, CollectionViewBase, ListViewViewTypes, isBounceEnabledProperty, isScrollEnabledProperty, itemTemplatesProperty, orientationProperty } from './common';
2
+ import { CLog, CLogTypes, CollectionViewBase, ViewTemplateType, isBounceEnabledProperty, isScrollEnabledProperty, itemTemplatesProperty, orientationProperty, reorderLongPressEnabledProperty, reorderingEnabledProperty, reverseLayoutProperty, scrollBarIndicatorVisibleProperty, getUUID } from './common';
4
3
  export * from './common';
5
4
  const infinity = Utils.layout.makeMeasureSpec(0, Utils.layout.UNSPECIFIED);
6
5
  export var ContentInsetAdjustmentBehavior;
@@ -41,10 +40,10 @@ export class CollectionView extends CollectionViewBase {
41
40
  this.reorderEndingRow = -1;
42
41
  this.manualDragging = false;
43
42
  this.scrollEnabledBeforeDragging = true;
43
+ this.cellRegistrations = {};
44
44
  this.needsScrollStartEvent = false;
45
45
  this.isScrolling = false;
46
46
  this._map = new Map();
47
- // this._sizes = new Array<number[]>();
48
47
  }
49
48
  createNativeView() {
50
49
  let layout;
@@ -60,25 +59,20 @@ export class CollectionView extends CollectionViewBase {
60
59
  }
61
60
  const view = UICollectionView.alloc().initWithFrameCollectionViewLayout(CGRectMake(0, 0, 0, 0), layout);
62
61
  view.backgroundColor = UIColor.clearColor;
63
- this._itemTemplatesInternal.forEach((t) => {
64
- view.registerClassForCellWithReuseIdentifier(CollectionViewCell.class(), t.key.toLowerCase());
65
- });
66
62
  view.autoresizesSubviews = false;
67
63
  view.autoresizingMask = 0 /* UIViewAutoresizing.None */;
68
64
  this.lastContentOffset = view.contentOffset;
69
65
  return view;
70
66
  }
71
- onTemplateAdded(t) {
72
- super.onTemplateAdded(t);
73
- if (this.nativeViewProtected) {
74
- this.nativeViewProtected.registerClassForCellWithReuseIdentifier(CollectionViewCell.class(), t.key.toLowerCase());
75
- }
76
- }
67
+ // onTemplateAdded(t) {
68
+ // super.onTemplateAdded(t);
69
+ // if (this.nativeViewProtected) {
70
+ // // this.nativeViewProtected.registerClassForCellWithReuseIdentifier(CollectionViewCell.class(), t.key.toLowerCase());
71
+ // }
72
+ // }
77
73
  initNativeView() {
78
74
  super.initNativeView();
79
- const nativeView = this.nativeView;
80
- this._dataSource = CollectionViewDataSource.initWithOwner(this);
81
- nativeView.dataSource = this._dataSource;
75
+ this.setupDataSource();
82
76
  // delegate will be set in first onLayout because we need computed _effectiveColWidth and _effectiveRowHeight
83
77
  this._measureCellMap = new Map();
84
78
  // waterfall requires the delegate to be set as soon as possible
@@ -91,6 +85,126 @@ export class CollectionView extends CollectionViewBase {
91
85
  }
92
86
  this._setNativeClipToBounds();
93
87
  }
88
+ setupDataSource() {
89
+ // Important: cell's must be registered before creating the datasource
90
+ // eg: they can *not* be created within the initWithCollectionViewCellProvider
91
+ const templateKeys = this._itemTemplatesInternal.keys();
92
+ for (const key of templateKeys) {
93
+ // register cell for each template type
94
+ this.cellRegistrations[key] = UICollectionViewCellRegistration.registrationWithCellClassConfigurationHandler(CollectionViewCell.class(), (view, indexPath, identifier) => {
95
+ const templateType = this._getItemTemplateType(indexPath);
96
+ // console.log('registrationWithCellClassConfigurationHandler templateType:', templateType)
97
+ const cell = view;
98
+ const firstRender = !cell.view;
99
+ if (Trace.isEnabled()) {
100
+ CLog(CLogTypes.log, 'cellProvider for row:', indexPath.row, ' templateType:', templateType);
101
+ }
102
+ this._prepareCell(cell, indexPath, templateType);
103
+ // the cell layout will be called from NSCellView layoutSubviews
104
+ const cellView = cell.view;
105
+ if (!firstRender && cellView['isLayoutRequired']) {
106
+ this.layoutCell(indexPath.row, cell, cellView);
107
+ }
108
+ return cell;
109
+ });
110
+ }
111
+ this._dataSource = UICollectionViewDiffableDataSource.alloc().initWithCollectionViewCellProvider(this.nativeView, (view, indexPath, identifier) => {
112
+ return this.nativeViewProtected.dequeueConfiguredReusableCellWithRegistrationForIndexPathItem(this.cellRegistrations[this._getItemTemplateType(indexPath)], indexPath, identifier);
113
+ });
114
+ this.setupHeaderFooter();
115
+ if (!this.sections) {
116
+ // every collectionview must have at least 1 section
117
+ this.sections = [{
118
+ identifier: getUUID(),
119
+ key: 'default'
120
+ }];
121
+ }
122
+ if (this.items?.length) {
123
+ this.refreshDataSourceSnapshot(this.getDefaultSectionIdentifier());
124
+ }
125
+ this.nativeView.dataSource = this._dataSource;
126
+ }
127
+ refreshDataSourceSnapshot(sectionIdentifier) {
128
+ if (this.items) {
129
+ this.modifyDataSourceSnapshot(ChangeType.Add, [], sectionIdentifier, false, true);
130
+ }
131
+ }
132
+ modifyDataSourceSnapshot(type, identifiers, sectionIdentifier, animate = true, reload = false) {
133
+ if (this.items) {
134
+ if (!this._dataSourceSnapshot || reload) {
135
+ this._dataSourceSnapshot = NSDiffableDataSourceSnapshot.alloc().init();
136
+ this._dataSourceSnapshot.appendSectionsWithIdentifiers(this.sections.map(s => s.identifier));
137
+ }
138
+ else {
139
+ this._dataSourceSnapshot = this._dataSource.snapshot();
140
+ }
141
+ if (Trace.isEnabled()) {
142
+ CLog(CLogTypes.info, 'modifyDataSourceSnapshot identifiers: ', type, identifiers);
143
+ }
144
+ // console.log('modifyDataSourceSnapshot identifiers: ', type, identifiers);
145
+ switch (type) {
146
+ case ChangeType.Add:
147
+ const itemIdentifiers = [];
148
+ if (reload) {
149
+ this.items.forEach(() => {
150
+ // forEach works well with ObservableArray and Array
151
+ itemIdentifiers.push(getUUID());
152
+ });
153
+ }
154
+ if (identifiers.length) {
155
+ itemIdentifiers.push(...identifiers);
156
+ }
157
+ if (sectionIdentifier) {
158
+ this._dataSourceSnapshot.appendItemsWithIdentifiersIntoSectionWithIdentifier(itemIdentifiers, sectionIdentifier);
159
+ }
160
+ else {
161
+ this._dataSourceSnapshot.appendItemsWithIdentifiers(itemIdentifiers);
162
+ }
163
+ break;
164
+ case ChangeType.Update:
165
+ this._dataSourceSnapshot.reloadItemsWithIdentifiers(identifiers);
166
+ break;
167
+ case ChangeType.Delete:
168
+ this._dataSourceSnapshot.deleteItemsWithIdentifiers(identifiers);
169
+ break;
170
+ }
171
+ this._dataSource.applySnapshotAnimatingDifferences(this._dataSourceSnapshot, animate);
172
+ }
173
+ }
174
+ getDefaultSectionIdentifier() {
175
+ // each collectionview must have at least 1 section
176
+ return this.sections[0].identifier;
177
+ }
178
+ setupHeaderFooter() {
179
+ if (!this.headerKey) {
180
+ // TODO: work on keyed header for multiple sections
181
+ this.headerKey = ViewTemplateType.Header;
182
+ }
183
+ if (this.headerItemTemplate) {
184
+ this.headerRegistration = UICollectionViewSupplementaryRegistration.registrationWithSupplementaryClassElementKindConfigurationHandler(CollectionViewCell.class(), this.headerKey, (cell, elementKind, indexPath) => {
185
+ this._prepareHeaderFooter(cell, indexPath, this.headerKey, ViewTemplateType.Header);
186
+ });
187
+ }
188
+ if (!this.footerKey) {
189
+ // TODO: work on keyed footer for multiple sections
190
+ this.footerKey = ViewTemplateType.Footer;
191
+ }
192
+ if (this.footerItemTemplate) {
193
+ this.footerRegistration = UICollectionViewSupplementaryRegistration.registrationWithSupplementaryClassElementKindConfigurationHandler(CollectionViewCell.class(), this.footerKey, (cell, elementKind, indexPath) => {
194
+ this._prepareHeaderFooter(cell, indexPath, this.footerKey, ViewTemplateType.Footer);
195
+ });
196
+ }
197
+ if (this.headerItemTemplate || this.footerItemTemplate) {
198
+ this._dataSource.supplementaryViewProvider = (view, elementKind, indexPath) => {
199
+ if (this.headerRegistration && elementKind == this.headerKey) {
200
+ return this.nativeViewProtected.dequeueConfiguredReusableSupplementaryViewWithRegistrationForIndexPath(this.headerRegistration, indexPath);
201
+ }
202
+ else if (this.footerRegistration) {
203
+ return this.nativeViewProtected.dequeueConfiguredReusableSupplementaryViewWithRegistrationForIndexPath(this.footerRegistration, indexPath);
204
+ }
205
+ };
206
+ }
207
+ }
94
208
  disposeNativeView() {
95
209
  if (Trace.isEnabled()) {
96
210
  CLog(CLogTypes.log, 'disposeNativeView');
@@ -365,122 +479,94 @@ export class CollectionView extends CollectionViewBase {
365
479
  if (Trace.isEnabled()) {
366
480
  CLog(CLogTypes.log, 'onItemsChanged', ChangeType.Update, event.action, event.index, event.addedCount, event.removed && event.removed.length);
367
481
  }
368
- // we need to clear stored cell sizes and it wont be correct anymore
369
- // this.clearCellSize();
370
- const sizes = this._delegate instanceof UICollectionViewDelegateImpl ? this._delegate.cachedSizes : null;
482
+ // console.log('----')
483
+ // console.log('event.action:', event.action)
484
+ // console.log('event.addedCount:', event.addedCount);
485
+ // console.log('event.removed:', event.removed);
486
+ // console.log('event.index:', event.index)
487
+ // console.log(' >')
488
+ const sectionIdentifier = this._dataSource.sectionIdentifierForIndex(0);
489
+ // console.log(' sectionIdentifier:', sectionIdentifier)
371
490
  switch (event.action) {
372
491
  case ChangeType.Delete: {
373
- const indexes = NSMutableArray.new();
492
+ const identifiers = [];
374
493
  for (let index = 0; index < event.addedCount; index++) {
375
- indexes.addObject(NSIndexPath.indexPathForRowInSection(event.index + index, 0));
376
- if (sizes) {
377
- sizes.removeObjectAtIndex(event.index);
378
- }
494
+ const indexPath = NSIndexPath.indexPathForRowInSection(event.index + index, sectionIdentifier);
495
+ const identifier = this._dataSource.itemIdentifierForIndexPath(indexPath);
496
+ // console.log(' delete identifier:', identifier)
497
+ identifiers.push(identifier);
379
498
  }
380
- // this._sizes.splice(event.index, event.addedCount);
381
499
  this.unbindUnusedCells(event.removed);
382
- if (Trace.isEnabled()) {
383
- CLog(CLogTypes.info, 'deleteItemsAtIndexPaths', indexes.count);
384
- }
385
- view.performBatchUpdatesCompletion(() => {
386
- view.deleteItemsAtIndexPaths(indexes);
387
- }, null);
500
+ this.modifyDataSourceSnapshot(ChangeType.Delete, identifiers, sectionIdentifier);
388
501
  return;
389
502
  }
390
503
  case ChangeType.Update: {
391
- const indexes = NSMutableArray.new();
392
- indexes.addObject(NSIndexPath.indexPathForRowInSection(event.index, 0));
393
- if (sizes) {
394
- sizes.replaceObjectAtIndexWithObject(event.index, NSValue.valueWithCGSize(CGSizeZero));
395
- }
396
- // this._sizes[event.index] = null;
397
- if (Trace.isEnabled()) {
398
- CLog(CLogTypes.info, 'reloadItemsAtIndexPaths', event.index, indexes.count);
399
- }
400
- view.performBatchUpdatesCompletion(() => {
401
- view.reloadItemsAtIndexPaths(indexes);
402
- }, null);
504
+ const identifiers = [];
505
+ const indexPath = NSIndexPath.indexPathForRowInSection(event.index, sectionIdentifier);
506
+ const identifier = this._dataSource.itemIdentifierForIndexPath(indexPath);
507
+ // console.log(' update identifier:', identifier)
508
+ identifiers.push(identifier);
509
+ this.modifyDataSourceSnapshot(ChangeType.Update, identifiers, sectionIdentifier);
403
510
  return;
404
511
  }
405
512
  case ChangeType.Add: {
406
- const indexes = NSMutableArray.new();
513
+ const identifiers = [];
407
514
  for (let index = 0; index < event.addedCount; index++) {
408
- indexes.addObject(NSIndexPath.indexPathForRowInSection(event.index + index, 0));
409
- if (sizes) {
410
- sizes.insertObjectAtIndex(NSValue.valueWithCGSize(CGSizeZero), event.index);
411
- }
412
- // this._sizes.splice(index, 0, null);
515
+ const indexPath = NSIndexPath.indexPathForRowInSection(event.index + index, sectionIdentifier);
516
+ const identifier = this._dataSource.itemIdentifierForIndexPath(indexPath) || getUUID();
517
+ // console.log(' add identifier:', identifier)
518
+ identifiers.push(identifier);
413
519
  }
414
- if (Trace.isEnabled()) {
415
- CLog(CLogTypes.info, 'insertItemsAtIndexPaths', indexes.count);
416
- }
417
- view.performBatchUpdatesCompletion(() => {
418
- view.insertItemsAtIndexPaths(indexes);
419
- }, null);
520
+ this.modifyDataSourceSnapshot(ChangeType.Add, identifiers, sectionIdentifier);
420
521
  return;
421
522
  }
422
523
  case ChangeType.Splice: {
423
- view.performBatchUpdatesCompletion(() => {
424
- const added = event.addedCount;
425
- const removed = (event.removed && event.removed.length) || 0;
426
- if (added > 0 && added === removed) {
427
- const indexes = NSMutableArray.new();
428
- for (let index = 0; index < added; index++) {
429
- indexes.addObject(NSIndexPath.indexPathForRowInSection(event.index + index, 0));
430
- if (sizes) {
431
- sizes.replaceObjectAtIndexWithObject(event.index + index, NSValue.valueWithCGSize(CGSizeZero));
432
- }
433
- // this._sizes[event.index + index] = null;
434
- }
435
- view.reloadItemsAtIndexPaths(indexes);
524
+ const added = event.addedCount;
525
+ const removed = (event.removed && event.removed.length) || 0;
526
+ if (added > 0 && added === removed) {
527
+ const identifiers = [];
528
+ for (let index = 0; index < added; index++) {
529
+ const indexPath = NSIndexPath.indexPathForRowInSection(event.index + index, sectionIdentifier);
530
+ const identifier = this._dataSource.itemIdentifierForIndexPath(indexPath) || getUUID();
531
+ // console.log(' splice, update identifier:', identifier)
532
+ identifiers.push(identifier);
436
533
  }
437
- else {
438
- if (event.removed && event.removed.length > 0) {
439
- const indexes = NSMutableArray.new();
440
- for (let index = 0; index < event.removed.length; index++) {
441
- indexes.addObject(NSIndexPath.indexPathForItemInSection(event.index + index, 0));
442
- if (sizes) {
443
- sizes.removeObjectAtIndex(event.index);
444
- }
445
- }
446
- // this._sizes.splice(event.index, event.removed.length);
447
- this.unbindUnusedCells(event.removed);
448
- if (Trace.isEnabled()) {
449
- CLog(CLogTypes.info, 'deleteItemsAtIndexPaths', indexes.count);
450
- }
451
- view.deleteItemsAtIndexPaths(indexes);
534
+ this.modifyDataSourceSnapshot(ChangeType.Update, identifiers, sectionIdentifier);
535
+ }
536
+ else {
537
+ if (event.removed && event.removed.length > 0) {
538
+ const identifiers = [];
539
+ for (let index = 0; index < event.removed.length; index++) {
540
+ const indexPath = NSIndexPath.indexPathForItemInSection(event.index + index, sectionIdentifier);
541
+ const identifier = this._dataSource.itemIdentifierForIndexPath(indexPath);
542
+ // console.log(' splice, remove identifier:', identifier)
543
+ identifiers.push(identifier);
452
544
  }
453
- if (event.addedCount > 0) {
454
- const indexes = NSMutableArray.alloc().init();
455
- for (let index = 0; index < event.addedCount; index++) {
456
- indexes.addObject(NSIndexPath.indexPathForItemInSection(event.index + index, 0));
457
- if (sizes) {
458
- sizes.insertObjectAtIndex(NSValue.valueWithCGSize(CGSizeZero), event.index);
459
- }
460
- // this._sizes.splice(event.index, 0, null);
461
- }
462
- if (Trace.isEnabled()) {
463
- CLog(CLogTypes.info, 'insertItemsAtIndexPaths', indexes.count);
464
- }
465
- view.insertItemsAtIndexPaths(indexes);
545
+ this.unbindUnusedCells(event.removed);
546
+ this.modifyDataSourceSnapshot(ChangeType.Delete, identifiers, sectionIdentifier);
547
+ }
548
+ if (event.addedCount > 0) {
549
+ const identifiers = [];
550
+ for (let index = 0; index < event.addedCount; index++) {
551
+ const indexPath = NSIndexPath.indexPathForItemInSection(event.index + index, sectionIdentifier);
552
+ const identifier = this._dataSource.itemIdentifierForIndexPath(indexPath) || getUUID();
553
+ // console.log(' splice, add identifier:', identifier)
554
+ identifiers.push(identifier);
466
555
  }
556
+ this.modifyDataSourceSnapshot(ChangeType.Add, identifiers, sectionIdentifier);
467
557
  }
468
- // view.collectionViewLayout.invalidateLayout();
469
- }, null);
558
+ }
559
+ // view.collectionViewLayout.invalidateLayout();
470
560
  return;
471
561
  }
472
562
  }
473
563
  this.refresh();
474
564
  }
475
- onItemTemplatesChanged(oldValue, newValue) {
476
- super.onItemTemplatesChanged(oldValue, newValue);
477
- if (!this.nativeViewProtected) {
478
- return;
479
- }
480
- const view = this.nativeViewProtected;
481
- this._itemTemplatesInternal.forEach((t) => {
482
- view.registerClassForCellWithReuseIdentifier(CollectionViewCell.class(), t.key.toLowerCase());
483
- });
565
+ clearEmbeddedViews() {
566
+ this.clearRealizedCells();
567
+ // if (this.itemViewDisposer !== undefined) {
568
+ // this.itemViewDisposer();
569
+ // }
484
570
  }
485
571
  unbindUnusedCells(removedDataItems) {
486
572
  this._map.forEach((view, nativeView, map) => {
@@ -497,14 +583,7 @@ export class CollectionView extends CollectionViewBase {
497
583
  if (!view) {
498
584
  return;
499
585
  }
500
- const sizes = this._delegate instanceof UICollectionViewDelegateImpl ? this._delegate.cachedSizes : null;
501
586
  const visibles = view.indexPathsForVisibleItems;
502
- if (sizes?.count) {
503
- const indexes = Array.from(visibles);
504
- indexes.forEach((value) => {
505
- sizes.replaceObjectAtIndexWithObject(value.row, NSValue.valueWithCGSize(CGSizeZero));
506
- });
507
- }
508
587
  UIView.performWithoutAnimation(() => {
509
588
  view.performBatchUpdatesCompletion(() => {
510
589
  view.reloadItemsAtIndexPaths(visibles);
@@ -529,18 +608,13 @@ export class CollectionView extends CollectionViewBase {
529
608
  if (Trace.isEnabled()) {
530
609
  CLog(CLogTypes.info, 'refresh');
531
610
  }
532
- // we need to clear stored cell sizes and it wont be correct anymore
533
- // this.clearCellSize();
534
- const sizes = this._delegate instanceof UICollectionViewDelegateImpl ? this._delegate.cachedSizes : null;
535
- if (sizes) {
536
- sizes.removeAllObjects();
537
- }
538
611
  // clear bindingContext when it is not observable because otherwise bindings to items won't reevaluate
539
612
  this._map.forEach((view, nativeView, map) => {
540
613
  if (!(view.bindingContext instanceof Observable)) {
541
614
  view.bindingContext = null;
542
615
  }
543
616
  });
617
+ this.refreshDataSourceSnapshot(this.getDefaultSectionIdentifier());
544
618
  // TODO: this is ugly look here: https://github.com/nativescript-vue/nativescript-vue/issues/525
545
619
  // this.clearRealizedCells();
546
620
  // dispatch_async(main_queue, () => {
@@ -577,17 +651,64 @@ export class CollectionView extends CollectionViewBase {
577
651
  return args;
578
652
  }
579
653
  _getItemTemplateType(indexPath) {
580
- const selector = this._itemTemplateSelector;
581
- let type = this._defaultTemplate.key;
582
- if (selector) {
583
- type = selector.call(this, this.getItemAtIndex(indexPath.item), indexPath.item, this.items);
654
+ return (this._itemTemplateSelector ? this._itemTemplateSelector.call(this, this.getItemAtIndex(indexPath.row), indexPath.row, this.items) : this._defaultTemplate.key).toLowerCase();
655
+ }
656
+ disableIosOverflowSafeArea(parentView) {
657
+ if (parentView) {
658
+ parentView.iosOverflowSafeAreaEnabled = false;
584
659
  }
585
- return type.toLowerCase();
586
660
  }
587
- getItemTemplateContent(index, templateType) {
588
- return this.getViewForViewType(ListViewViewTypes.ItemView, templateType);
661
+ _prepareHeaderFooter(cell, indexPath, templateKey, templateType, notForCellSizeComp = true) {
662
+ let cellSize;
663
+ try {
664
+ this._preparingCell = true;
665
+ const firstRender = !cell.view;
666
+ let view = cell.view;
667
+ const index = indexPath.row;
668
+ if (!view) {
669
+ view = this.getViewForTemplateType(templateKey, templateType);
670
+ }
671
+ if (Trace.isEnabled()) {
672
+ CLog(CLogTypes.log, '_prepareHeaderFooter', index, templateType, !!cell.view, !!view, cell.view !== view, notForCellSizeComp);
673
+ }
674
+ if (view) {
675
+ if (firstRender) {
676
+ view['iosIgnoreSafeArea'] = true;
677
+ }
678
+ view.bindingContext = this.bindingContext;
679
+ if (view instanceof ProxyViewContainer) {
680
+ const sp = new ContentView();
681
+ sp.content = view;
682
+ view = sp;
683
+ }
684
+ if (!cell.view) {
685
+ cell.owner = new WeakRef(view);
686
+ }
687
+ else if (cell.view !== view) {
688
+ this._removeContainer(cell);
689
+ if (cell.view?.nativeViewProtected) {
690
+ cell.view.nativeViewProtected.removeFromSuperview();
691
+ }
692
+ cell.owner = new WeakRef(view);
693
+ }
694
+ cell.currentIndex = indexPath.row;
695
+ if (view && !view.parent) {
696
+ this._addView(view);
697
+ const innerView = NSCellView.new();
698
+ innerView.autoresizingMask = 2 /* UIViewAutoresizing.FlexibleWidth */ | 16 /* UIViewAutoresizing.FlexibleHeight */;
699
+ innerView.view = new WeakRef(view);
700
+ innerView.addSubview(view.nativeViewProtected);
701
+ cell.addSubview(innerView);
702
+ }
703
+ cellSize = this.measureCell(cell, view, indexPath.row);
704
+ }
705
+ }
706
+ finally {
707
+ this._preparingCell = false;
708
+ }
709
+ return cellSize;
589
710
  }
590
- _prepareCell(cell, indexPath, templateType, notForCellSizeComp = true) {
711
+ _prepareCell(cell, indexPath, templateKey, notForCellSizeComp = true) {
591
712
  let cellSize;
592
713
  try {
593
714
  this._preparingCell = true;
@@ -595,11 +716,11 @@ export class CollectionView extends CollectionViewBase {
595
716
  let view = cell.view;
596
717
  const index = indexPath.row;
597
718
  if (!view) {
598
- view = this.getItemTemplateContent(index, templateType);
719
+ view = this.getViewForTemplateType(templateKey);
599
720
  }
600
721
  const bindingContext = this._prepareItem(view, index);
601
722
  if (Trace.isEnabled()) {
602
- CLog(CLogTypes.log, '_prepareCell', index, templateType, !!cell.view, !!view, cell.view !== view, notForCellSizeComp);
723
+ CLog(CLogTypes.log, '_prepareCell', index, templateKey, !!cell.view, !!view, cell.view !== view, notForCellSizeComp);
603
724
  }
604
725
  const args = this.notifyForItemAtIndex(CollectionViewBase.itemLoadingEvent, view, indexPath.row, bindingContext, cell);
605
726
  view = args.view;
@@ -631,25 +752,21 @@ export class CollectionView extends CollectionViewBase {
631
752
  const innerView = NSCellView.new();
632
753
  innerView.autoresizingMask = 2 /* UIViewAutoresizing.FlexibleWidth */ | 16 /* UIViewAutoresizing.FlexibleHeight */;
633
754
  innerView.view = new WeakRef(view);
634
- if (notForCellSizeComp && this.autoReloadItemOnLayout) {
635
- // for a cell to update correctly on cell layout change we need
636
- // to do it ourself instead of "propagating it"
637
- view['performLayout'] = () => {
638
- if (!this._preparingCell) {
639
- const index = cell.currentIndex;
640
- const nativeView = this.nativeViewProtected;
641
- const sizes = this._delegate instanceof UICollectionViewDelegateImpl ? this._delegate.cachedSizes : null;
642
- if (sizes) {
643
- sizes.replaceObjectAtIndexWithObject(index, NSValue.valueWithCGSize(CGSizeZero));
644
- }
645
- nativeView.performBatchUpdatesCompletion(() => {
646
- this.measureCell(cell, view, index);
647
- this.notifyForItemAtIndex(CollectionViewBase.itemLoadingEvent, view, indexPath.row, view.bindingContext, cell);
648
- }, null);
649
- nativeView.collectionViewLayout.invalidateLayout();
650
- }
651
- };
652
- }
755
+ // if (notForCellSizeComp && this.autoReloadItemOnLayout) {
756
+ // // for a cell to update correctly on cell layout change we need
757
+ // // to do it ourself instead of "propagating it"
758
+ // view['performLayout'] = () => {
759
+ // if (!this._preparingCell) {
760
+ // const index = cell.currentIndex;
761
+ // const nativeView = this.nativeViewProtected;
762
+ // nativeView.performBatchUpdatesCompletion(() => {
763
+ // this.measureCell(cell, view, index);
764
+ // this.notifyForItemAtIndex(CollectionViewBase.itemLoadingEvent, view, indexPath.row, view.bindingContext, cell);
765
+ // }, null);
766
+ // nativeView.collectionViewLayout.invalidateLayout();
767
+ // }
768
+ // };
769
+ // }
653
770
  innerView.addSubview(view.nativeViewProtected);
654
771
  cell.contentView.addSubview(innerView);
655
772
  }
@@ -667,7 +784,6 @@ export class CollectionView extends CollectionViewBase {
667
784
  return cellSize;
668
785
  }
669
786
  getCellSize(index) {
670
- // let result = this._sizes[index];
671
787
  let result;
672
788
  // CLog(CLogTypes.log, 'getCellSize', index, result, this._effectiveColWidth, this._effectiveRowHeight, this.getMeasuredWidth(), this.getMeasuredHeight());
673
789
  if (!result) {
@@ -697,12 +813,6 @@ export class CollectionView extends CollectionViewBase {
697
813
  // return undefined;
698
814
  return result;
699
815
  }
700
- // public storeCellSize(index: number, value) {
701
- // this._sizes[index] = value;
702
- // }
703
- // public clearCellSize() {
704
- // this._sizes = new Array<number[]>();
705
- // }
706
816
  measureCell(cell, cellView, position) {
707
817
  if (cellView) {
708
818
  let width = this._effectiveColWidth;
@@ -813,6 +923,28 @@ export class CollectionView extends CollectionViewBase {
813
923
  }
814
924
  return cell;
815
925
  }
926
+ // collectionViewViewForSupplementaryElementOfKindAtIndexPath(view: UICollectionView, kind: string, indexPath: NSIndexPath): UICollectionReusableView {
927
+ // // const templateType = kind === UICollectionElementKindSectionHeader ? this._headerTemplate.key : this._footerTemplate.key;
928
+ // const templateType = kind;
929
+ // console.log('templateType:', templateType)
930
+ // // let cell = collectionView.dequeueConfiguredReusableSupplementaryViewWithRegistrationForIndexPath(kind, templateType, indexPath) as CollectionViewReusableView;
931
+ // let cell = this._dataSource.supplementaryViewProvider(view, kind, indexPath);
932
+ // console.log('cell:', cell)
933
+ // // if (!cell) {
934
+ // // cell = CollectionViewReusableView.new() as CollectionViewReusableView;
935
+ // // }
936
+ // // const firstRender = !cell.view;
937
+ // // if (Trace.isEnabled()) {
938
+ // // CLog(CLogTypes.log, 'collectionViewViewForSupplementaryElementOfKindAtIndexPath', indexPath.row, templateType, !!cell.view, cell);
939
+ // // }
940
+ // // this._prepareHeaderFooter(cell, indexPath, templateType);
941
+ // // the cell layout will be called from NSCellView layoutSubviews
942
+ // // const cellView: View = cell.view;
943
+ // // if (!firstRender && cellView['isLayoutRequired']) {
944
+ // // this.layoutCell(indexPath.row, cell, cellView);
945
+ // // }
946
+ // return cell;
947
+ // }
816
948
  collectionViewWillDisplayCellForItemAtIndexPath(collectionView, cell, indexPath) {
817
949
  if (this.reverseLayout) {
818
950
  cell.transform = CGAffineTransformMakeRotation(-Math.PI);
@@ -1007,66 +1139,20 @@ var CollectionViewCell = /** @class */ (function (_super) {
1007
1139
  };
1008
1140
  return CollectionViewCell;
1009
1141
  }(UICollectionViewCell));
1010
- var CollectionViewDataSource = /** @class */ (function (_super) {
1011
- __extends(CollectionViewDataSource, _super);
1012
- function CollectionViewDataSource() {
1142
+ var CollectionViewReusableView = /** @class */ (function (_super) {
1143
+ __extends(CollectionViewReusableView, _super);
1144
+ function CollectionViewReusableView() {
1013
1145
  return _super !== null && _super.apply(this, arguments) || this;
1014
1146
  }
1015
- CollectionViewDataSource.initWithOwner = function (owner) {
1016
- var delegate = CollectionViewDataSource.new();
1017
- delegate._owner = new WeakRef(owner);
1018
- return delegate;
1019
- };
1020
- CollectionViewDataSource.prototype.numberOfSectionsInCollectionView = function (collectionView) {
1021
- var owner = this._owner.deref();
1022
- if (owner) {
1023
- return owner.numberOfSectionsInCollectionView(collectionView);
1024
- }
1025
- return 0;
1026
- };
1027
- CollectionViewDataSource.prototype.collectionViewNumberOfItemsInSection = function (collectionView, section) {
1028
- var owner = this._owner.deref();
1029
- if (owner) {
1030
- return owner.collectionViewNumberOfItemsInSection(collectionView, section);
1031
- }
1032
- return 0;
1033
- };
1034
- CollectionViewDataSource.prototype.collectionViewCellForItemAtIndexPath = function (collectionView, indexPath) {
1035
- var owner = this._owner.deref();
1036
- if (owner) {
1037
- return owner.collectionViewCellForItemAtIndexPath(collectionView, indexPath);
1038
- }
1039
- return null;
1040
- };
1041
- CollectionViewDataSource.prototype.collectionViewMoveItemAtIndexPathToIndexPath = function (collectionView, sourceIndexPath, destinationIndexPath) {
1042
- var owner = this._owner.deref();
1043
- if (owner) {
1044
- owner.reorderStartingRow = sourceIndexPath.row;
1045
- owner.reorderEndingRow = destinationIndexPath.row;
1046
- owner._reorderItemInSource(sourceIndexPath.row, destinationIndexPath.row, false);
1047
- }
1048
- };
1049
- CollectionViewDataSource.prototype.collectionViewTargetIndexPathForMoveFromItemAtIndexPathToProposedIndexPath = function (collectionView, originalIndexPath, proposedIndexPath) {
1050
- var owner = this._owner.deref();
1051
- if (owner) {
1052
- owner.reorderEndingRow = proposedIndexPath.row;
1053
- }
1054
- return proposedIndexPath;
1055
- };
1056
- CollectionViewDataSource.prototype.collectionViewCanMoveItemAtIndexPath = function (collectionView, indexPath) {
1057
- var owner = this._owner.deref();
1058
- if (owner) {
1059
- var result = owner.shouldMoveItemAtIndex(indexPath.row);
1060
- if (result) {
1061
- owner.reorderStartingRow = indexPath.row;
1062
- }
1063
- return result;
1064
- }
1065
- return false;
1066
- };
1067
- CollectionViewDataSource.ObjCProtocols = [UICollectionViewDataSource];
1068
- return CollectionViewDataSource;
1069
- }(NSObject));
1147
+ Object.defineProperty(CollectionViewReusableView.prototype, "view", {
1148
+ get: function () {
1149
+ return this.owner ? this.owner.deref() : null;
1150
+ },
1151
+ enumerable: true,
1152
+ configurable: true
1153
+ });
1154
+ return CollectionViewReusableView;
1155
+ }(UICollectionReusableView));
1070
1156
  var UICollectionViewDelegateImpl = /** @class */ (function (_super) {
1071
1157
  __extends(UICollectionViewDelegateImpl, _super);
1072
1158
  function UICollectionViewDelegateImpl() {