Skip to content

Commit 3c25475

Browse files
committed
fix: refactoring for faster loading time on android
1 parent 1dec848 commit 3c25475

File tree

3 files changed

+77
-28
lines changed

3 files changed

+77
-28
lines changed

src/collectionview-common.ts

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -147,35 +147,52 @@ export abstract class CollectionViewBase extends View implements CollectionViewD
147147

148148
public abstract refresh();
149149
public abstract scrollToIndex(index: number, animated: boolean);
150-
151-
_onSizeChanged() {
152-
super._onSizeChanged();
153-
this.onSizeChanged(this.getMeasuredWidth(), this.getMeasuredHeight());
154-
}
155-
@profile
156-
public onSizeChanged(measuredWidth: number, measuredHeight: number) {
157-
let changed = false;
158-
this._innerWidth = measuredWidth - this.effectivePaddingLeft - this.effectivePaddingRight;
150+
public onLayout(left: number, top: number, right: number, bottom: number) {
151+
super.onLayout(left, top, right, bottom);
152+
this._innerWidth = this.getMeasuredWidth() - this.effectivePaddingLeft - this.effectivePaddingRight;
159153
if (this.colWidth) {
160154
const newValue = PercentLength.toDevicePixels(this.colWidth, autoEffectiveColWidth, this._innerWidth); // We cannot use 0 for auto as it throws for android.
161155
if (newValue !== this._effectiveColWidth) {
162156
this._effectiveColWidth = newValue;
163-
changed = true;
164157
}
165158
}
166159

167-
this._innerHeight = measuredHeight - this.effectivePaddingTop - this.effectivePaddingBottom;
160+
this._innerHeight = this.getMeasuredHeight() - this.effectivePaddingTop - this.effectivePaddingBottom;
168161
if (this.rowHeight) {
169162
const newValue = PercentLength.toDevicePixels(this.rowHeight, autoEffectiveRowHeight, this._innerHeight);
170163
if (newValue !== this._effectiveRowHeight) {
171164
this._effectiveRowHeight = newValue;
172-
changed = true;
173165
}
174166
}
175-
if (changed) {
176-
this.refresh();
177-
}
178167
}
168+
// _onSizeChanged() {
169+
// super._onSizeChanged();
170+
// this.onSizeChanged(this.getMeasuredWidth(), this.getMeasuredHeight());
171+
// }
172+
// @profile
173+
// public onSizeChanged(measuredWidth: number, measuredHeight: number) {
174+
// let changed = false;
175+
// this._innerWidth = measuredWidth - this.effectivePaddingLeft - this.effectivePaddingRight;
176+
// if (this.colWidth) {
177+
// const newValue = PercentLength.toDevicePixels(this.colWidth, autoEffectiveColWidth, this._innerWidth); // We cannot use 0 for auto as it throws for android.
178+
// if (newValue !== this._effectiveColWidth) {
179+
// this._effectiveColWidth = newValue;
180+
// changed = true;
181+
// }
182+
// }
183+
184+
// this._innerHeight = measuredHeight - this.effectivePaddingTop - this.effectivePaddingBottom;
185+
// if (this.rowHeight) {
186+
// const newValue = PercentLength.toDevicePixels(this.rowHeight, autoEffectiveRowHeight, this._innerHeight);
187+
// if (newValue !== this._effectiveRowHeight) {
188+
// this._effectiveRowHeight = newValue;
189+
// changed = true;
190+
// }
191+
// }
192+
// if (changed) {
193+
// this.refresh();
194+
// }
195+
// }
179196
// public onLayout(left: number, top: number, right: number, bottom: number) {
180197
// super.onLayout(left, top, right, bottom);
181198
// }
@@ -214,6 +231,7 @@ export abstract class CollectionViewBase extends View implements CollectionViewD
214231
spanCount = Math.max(Math.floor(this._innerWidth / this._effectiveColWidth), 1) || 1;
215232
}
216233
}
234+
console.log('computeSpanCount', this._innerWidth, this._innerHeight, this._effectiveRowHeight, this._effectiveColWidth, spanCount);
217235
return spanCount;
218236
}
219237
public _onRowHeightPropertyChanged(oldValue: PercentLength, newValue: PercentLength) {
@@ -380,7 +398,7 @@ export abstract class CollectionViewBase extends View implements CollectionViewD
380398
_isDataDirty = false;
381399
onLoaded() {
382400
super.onLoaded();
383-
if (this._isDataDirty) {
401+
if (this._isDataDirty && this._effectiveColWidth !== undefined && this._effectiveRowHeight !== undefined) {
384402
this.refresh();
385403
}
386404
}
@@ -422,7 +440,9 @@ export const colWidthProperty = new Property<CollectionViewBase, PercentLength>(
422440
equalityComparer: PercentLength.equals,
423441
valueConverter: PercentLength.parse,
424442
valueChanged: (target, oldValue, newValue) => {
425-
target._effectiveColWidth = PercentLength.toDevicePixels(newValue, autoEffectiveColWidth, target._innerWidth);
443+
if (target._innerWidth !== 0) {
444+
target._effectiveColWidth = PercentLength.toDevicePixels(newValue, autoEffectiveColWidth, target._innerWidth);
445+
}
426446
target._onColWidthPropertyChanged(oldValue, newValue);
427447
},
428448
});

src/collectionview.android.ts

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ import { CLog, CLogTypes, CollectionViewBase, ListViewViewTypes, isScrollEnabled
2020

2121
export * from './collectionview-common';
2222

23+
24+
declare module '@nativescript/core/ui/core/view' {
25+
interface View {
26+
layoutChangeListenerIsSet: boolean;
27+
layoutChangeListener: android.view.View.OnLayoutChangeListener;
28+
_raiseLayoutChangedEvent();
29+
}
30+
}
31+
2332
// Snapshot friendly GridViewAdapter
2433
interface CellViewHolder extends com.nativescript.collectionview.CollectionViewCellHolder {
2534
// tslint:disable-next-line:no-misused-new
@@ -114,15 +123,13 @@ export class CollectionView extends CollectionViewBase {
114123

115124
@profile
116125
public initNativeView() {
126+
this.setOnLayoutChangeListener();
117127
super.initNativeView();
118-
119128
const nativeView = this.nativeViewProtected;
120129
nativeView.owner = new WeakRef(this);
121-
nativeView.sizeChangedListener = new com.nativescript.collectionview.SizeChangedListener({
122-
onSizeChanged: (w, h, oldW, oldH) => {
123-
this.onSizeChanged(w, h);
124-
},
125-
});
130+
// nativeView.sizeChangedListener = new com.nativescript.collectionview.SizeChangedListener({
131+
// onSizeChanged: (w, h, oldW, oldH) => this.onSizeChanged(w, h),
132+
// });
126133

127134
// const orientation = this._getLayoutManagarOrientation();
128135

@@ -412,6 +419,26 @@ export class CollectionView extends CollectionViewBase {
412419
// });
413420
// }
414421

422+
private setOnLayoutChangeListener() {
423+
if (this.nativeViewProtected) {
424+
const owner = this;
425+
this.layoutChangeListenerIsSet = true;
426+
this.layoutChangeListener =
427+
this.layoutChangeListener ||
428+
new android.view.View.OnLayoutChangeListener({
429+
onLayoutChange(v: android.view.View, left: number, top: number, right: number, bottom: number, oldLeft: number, oldTop: number, oldRight: number, oldBottom: number): void {
430+
if (left !== oldLeft || top !== oldTop || right !== oldRight || bottom !== oldBottom) {
431+
owner.onLayout(left, top, right, bottom);
432+
if (owner.hasListeners(View.layoutChangedEvent)) {
433+
owner._raiseLayoutChangedEvent();
434+
}
435+
}
436+
},
437+
});
438+
439+
this.nativeViewProtected.addOnLayoutChangeListener(this.layoutChangeListener);
440+
}
441+
}
415442
public onLayout(left: number, top: number, right: number, bottom: number) {
416443
super.onLayout(left, top, right, bottom);
417444
const p = CollectionViewBase.plugins[this.layoutStyle];
@@ -425,6 +452,7 @@ export class CollectionView extends CollectionViewBase {
425452
if (this.layoutManager && this.layoutManager['setSpanCount']) {
426453
this.layoutManager['setSpanCount'](this.computeSpanCount());
427454
}
455+
setTimeout(()=>this.refresh(),0);
428456
}
429457
public onSourceCollectionChanged(event: ChangedData<any>) {
430458
if (!this._listViewAdapter) {
@@ -488,7 +516,7 @@ export class CollectionView extends CollectionViewBase {
488516
}
489517

490518
// nativeView.adapter.owner = new WeakRef(this);
491-
519+
console.log('refresh', new Error().stack);
492520
const layoutManager = view.getLayoutManager();
493521
if (layoutManager['setSpanCount']) {
494522
layoutManager['setSpanCount'](this.computeSpanCount());

src/collectionview.ios.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ export class CollectionView extends CollectionViewBase {
103103
super.disposeNativeView();
104104
}
105105

106-
_onSizeChanged() {
107-
super._onSizeChanged();
108-
this.onSizeChanged(this.getMeasuredWidth(), this.getMeasuredHeight());
109-
}
106+
// _onSizeChanged() {
107+
// super._onSizeChanged();
108+
// this.onSizeChanged(this.getMeasuredWidth(), this.getMeasuredHeight());
109+
// }
110110

111111
get _childrenCount(): number {
112112
return this._map.size;
@@ -182,6 +182,7 @@ export class CollectionView extends CollectionViewBase {
182182
}
183183
this.layoutCell(cellView._listViewItemIndex, cell, cellView);
184184
});
185+
this.refresh();
185186
}
186187

187188
public isHorizontal() {

0 commit comments

Comments
 (0)