Skip to content

Commit 3d72238

Browse files
author
farfromrefuge
committed
fix: peaking / spacing fixes and global improvements
1 parent 61a8619 commit 3d72238

File tree

4 files changed

+99
-35
lines changed

4 files changed

+99
-35
lines changed

src/ui-pager/index.android.ts

Lines changed: 74 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,12 @@ export class Pager extends PagerBase {
126126
private enumerateViewHolders<T = any>(cb: (v: PagerViewHolder) => T) {
127127
let result: T, v: PagerViewHolder;
128128
for (let it = this._viewHolders.values(), cellItemView: PagerViewHolder = null; (cellItemView = it.next().value); ) {
129-
if (cellItemView) {
130-
result = cb(cellItemView);
131-
if (result) {
132-
return result;
133-
}
129+
if (cellItemView['position'] === undefined) {
130+
continue;
131+
}
132+
result = cb(cellItemView);
133+
if (result) {
134+
return result;
134135
}
135136
}
136137
return result;
@@ -383,11 +384,24 @@ export class Pager extends PagerBase {
383384
refresh() {
384385
const nativeView = this.nativeViewProtected;
385386
if (nativeView && this._pagerAdapter) {
386-
nativeView.requestLayout();
387+
// nativeView.requestLayout();
387388
nativeView.getAdapter().notifyDataSetChanged();
388389
}
389390
}
390-
391+
bindedViewHolders: Set<number> = new Set();
392+
refreshVisibleItems() {
393+
const view = this.nativeViewProtected;
394+
if (!view) {
395+
return;
396+
}
397+
const ids = Array.from(this.bindedViewHolders).sort((a, b) => a - b);
398+
console.log('refreshVisibleItems', ids);
399+
this.pagerAdapter.notifyItemRangeChanged(ids[0], ids[ids.length - 1] - ids[0] + 1);
400+
}
401+
402+
getViewForItemAtIndex(index: number) {
403+
return this.getChildView(index);
404+
}
391405
onUnloaded() {
392406
// this._android.setAdapter(null);
393407
super.onUnloaded();
@@ -743,18 +757,14 @@ function initPagerRecyclerAdapter() {
743757
view = owner._itemViewLoader(template.key);
744758
}
745759
const isNonSync = view === undefined;
760+
console.log('isNonSync', view);
746761
if (isNonSync || view instanceof ProxyViewContainer) {
747762
const parentView = new ContentView();
748763
parentView.id = 'pagerViewHolder';
749764
view = parentView;
750765
view[PLACEHOLDER] = true;
751766
}
752767
owner._addView(view);
753-
// sp._setupAsRootView(owner._context);
754-
// //@ts-ignore
755-
// sp.parent = owner;
756-
// sp._isAddedToNativeVisualTree = true;
757-
// sp.callLoaded();
758768
view.nativeView.setLayoutParams(new android.view.ViewGroup.LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT));
759769

760770
initPagerViewHolder();
@@ -810,6 +820,11 @@ function initPagerRecyclerAdapter() {
810820
index = index - 1;
811821
}
812822
}
823+
if (holder['position'] !== undefined) {
824+
owner.bindedViewHolders.delete(holder['position']);
825+
}
826+
holder['position'] = index;
827+
owner.bindedViewHolders.add(holder['position']);
813828
const bindingContext = owner._getDataItem(index);
814829
let view = holder.view;
815830
const isNonSync = holder.view[PLACEHOLDER] === true;
@@ -828,11 +843,24 @@ function initPagerRecyclerAdapter() {
828843
view = args.view;
829844
// the view has been changed on the event handler
830845
(holder.view as ContentView).content = args.view;
846+
} else if (view instanceof ContentView) {
847+
view = view.content;
831848
}
832849
owner._prepareItem(holder.view, index);
833850

834851
// TODO: find a way to add to existing margin
835-
view.marginLeft = view.marginRight = Utils.layout.toDeviceIndependentPixels(owner._lastPeaking);
852+
if (owner.orientation === 'vertical') {
853+
view.marginTop = view.marginBottom = Utils.layout.toDeviceIndependentPixels(owner._lastPeaking);
854+
} else {
855+
view.marginLeft = view.marginRight = Utils.layout.toDeviceIndependentPixels(owner._lastPeaking);
856+
}
857+
}
858+
}
859+
onViewRecycled(holder) {
860+
const owner = this.owner ? this.owner.get() : null;
861+
if (owner) {
862+
delete owner.bindedViewHolders[holder['position']];
863+
holder['position'] = undefined;
836864
}
837865
}
838866

@@ -916,45 +944,61 @@ function initStaticPagerStateAdapter() {
916944
return null;
917945
}
918946

919-
const view = owner._childrenViewsType.get(type);
920-
const sp = new StackLayout(); // Pager2 requires match_parent so add a parent with to fill
921-
if (view && !view.parent) {
922-
sp.addChild(view);
923-
} else {
924-
sp[PLACEHOLDER] = true;
925-
}
926-
owner._addView(sp);
947+
let view = owner._childrenViewsType.get(type);
948+
const isNonSync = view === undefined;
949+
// if (isNonSync || view instanceof ProxyViewContainer) {
950+
const parentView = new ContentView();
951+
parentView.id = 'pagerViewHolder';
952+
parentView.content = view;
953+
view = parentView;
954+
view[PLACEHOLDER] = true;
955+
// }
956+
owner._addView(view);
927957
// sp._setupAsRootView(owner._context);
928958
// //@ts-ignore
929959
// sp.parent = owner;
930960
// sp._isAddedToNativeVisualTree = true;
931961
// sp.callLoaded();
932-
sp.nativeView.setLayoutParams(new android.view.ViewGroup.LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT));
962+
view.nativeView.setLayoutParams(new android.view.ViewGroup.LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT));
933963

934964
initPagerViewHolder();
935-
const holder = new PagerViewHolder(sp, new WeakRef(owner));
965+
const holder = new PagerViewHolder(view, new WeakRef(owner));
936966
owner._viewHolders.add(holder);
937967
return holder;
938968
}
939969

940970
onBindViewHolder(holder: any, index: number): void {
941971
const owner = this.owner ? this.owner.get() : null;
942972
if (owner) {
973+
if (holder['position'] !== undefined) {
974+
owner.bindedViewHolders.delete(holder['position']);
975+
}
976+
holder['position'] = index;
977+
owner.bindedViewHolders.add(holder['position']);
978+
let view = holder.view;
979+
const isNonSync = holder.view[PLACEHOLDER] === true;
980+
view = isNonSync ? view.content : view;
943981
const args = {
944982
eventName: Pager.itemLoadingEvent,
945983
object: owner,
946984
android: holder,
947-
ios: undefined,
948985
index,
949-
view: holder.view[PLACEHOLDER] ? null : holder.view
986+
// bindingContext,
987+
view
950988
} as ItemEventData;
951989

952990
owner.notify(args);
953-
if (holder.view[PLACEHOLDER]) {
954-
if (args.view) {
955-
holder.view.addChild(args.view);
956-
}
957-
holder.view[PLACEHOLDER] = false;
991+
if (isNonSync && args.view !== view) {
992+
view = args.view;
993+
// the view has been changed on the event handler
994+
(holder.view as ContentView).content = args.view;
995+
} else if (view instanceof ContentView) {
996+
view = view.content;
997+
}
998+
if (owner.orientation === 'vertical') {
999+
view.marginTop = view.marginBottom = Utils.layout.toDeviceIndependentPixels(owner._lastPeaking);
1000+
} else {
1001+
view.marginLeft = view.marginRight = Utils.layout.toDeviceIndependentPixels(owner._lastPeaking);
9581002
}
9591003
}
9601004
}

src/ui-pager/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export declare class Pager extends PagerBase {
2121
updateNativeItems(oldItems: View[], newItems: View[]): void;
2222

2323
refresh(): void;
24+
refreshVisibleItems(): void;
2425

2526
disableSwipe: boolean;
2627
disableAnimation: boolean;
@@ -31,5 +32,6 @@ export declare class Pager extends PagerBase {
3132

3233
_onItemsChanged(oldValue: any, newValue: any);
3334

34-
getChildView(index: number): PagerItem;
35+
getChildView(index: number): View;
36+
getViewForItemAtIndex(index: number): View;
3537
}

src/ui-pager/index.ios.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ export class Pager extends PagerBase {
137137

138138
return result;
139139
}
140+
getViewForItemAtIndex(index: number) {
141+
return this.getChildView(index);
142+
}
140143

141144
_getRealWidthHeight(): { width: number; height: number } {
142145
let height = 0;
@@ -473,6 +476,19 @@ export class Pager extends PagerBase {
473476
this.indicator.setCount(this._childrenCount);
474477
}
475478
}
479+
refreshVisibleItems() {
480+
const view = this.nativeViewProtected;
481+
if (!view) {
482+
return;
483+
}
484+
485+
const visibles = view.indexPathsForVisibleItems;
486+
UIView.performWithoutAnimation(() => {
487+
view.performBatchUpdatesCompletion(() => {
488+
view.reloadItemsAtIndexPaths(visibles);
489+
}, null);
490+
});
491+
}
476492

477493
_isDataDirty = false;
478494
public onLoaded() {

src/ui-pager/svelte/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class SvelteKeyedTemplate {
3232
// TODO is StackLayout the best choice here?
3333
// const wrapper = createElement('StackLayout') as NativeViewElementNode<View>;
3434

35-
const nativeEl = new StackLayout();
35+
const nativeEl = new ContentView();
3636

3737
// because of the way {N} works we cant use that wrapper as the target for the component
3838
// it will trigger uncessary {N} component updates because the parent view is already attached
@@ -138,14 +138,16 @@ export default class PagerViewElement extends NativeViewElementNode<Pager> {
138138
_view.__SvelteComponentBuilder__ = null;
139139
_view.__CollectionViewCurrentIndex__ = args.index;
140140
const nativeEl = (dummy.firstElement() as NativeViewElementNode<View>).nativeView;
141-
(_view as LayoutBase).addChild(nativeEl);
141+
(_view as ContentView).content = nativeEl;
142142
}
143143
} else {
144144
// ensure we dont do unnecessary tasks if index did not change
145145
// console.log('updateListItem', args.index, _view.__CollectionViewCurrentIndex__);
146146
_view.__CollectionViewCurrentIndex__ = args.index;
147-
componentInstance.$set(props);
148-
flush(); // we need to flush to make sure update is applied right away
147+
_view._batchUpdate(() => {
148+
componentInstance.$set(props);
149+
flush(); // we need to flush to make sure update is applied right away
150+
});
149151
}
150152
}
151153

0 commit comments

Comments
 (0)