-
-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: shared view pools [wip] #68
Draft
rigor789
wants to merge
1
commit into
master
Choose a base branch
from
feat/shared-view-pools
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Trace, View } from '@nativescript/core'; | ||
import { CLog, CLogTypes, CollectionViewBase } from './index-common'; | ||
|
||
const poolMap = new Map<string, SharedCollectionViewPoolBase>(); | ||
export function getSharedCollectionViewPool(name: string): SharedCollectionViewPoolBase | undefined { | ||
return poolMap.get(name); | ||
} | ||
|
||
export class SharedCollectionViewPoolBase extends CollectionViewBase { | ||
protected collectionViews = new Set<CollectionViewBase>(); | ||
protected _name: string; | ||
|
||
set name(value: string) { | ||
if (this._name !== value) { | ||
poolMap.delete(this._name); | ||
} | ||
this._name = value; | ||
poolMap.set(this._name, this); | ||
} | ||
|
||
get name(): string { | ||
return this._name; | ||
} | ||
|
||
public onLoaded(): void { | ||
super.onLoaded(); | ||
poolMap.set(this.name, this); | ||
} | ||
|
||
public onUnloaded(): void { | ||
super.onUnloaded(); | ||
poolMap.delete(this.name); | ||
} | ||
|
||
public attachToCollectionView(collectionView: CollectionViewBase): void { | ||
if (Trace.isEnabled()) { | ||
CLog(CLogTypes.log, 'attachToCollectionView', collectionView); | ||
} | ||
|
||
this.collectionViews.add(collectionView); | ||
} | ||
|
||
public detachFromCollectionView(collectionView: CollectionViewBase): void { | ||
if (Trace.isEnabled()) { | ||
CLog(CLogTypes.log, 'detachFromCollectionView', collectionView); | ||
} | ||
|
||
this.collectionViews.delete(collectionView); | ||
} | ||
|
||
public refresh() { | ||
throw new Error('Method not implemented.'); | ||
} | ||
public refreshVisibleItems() { | ||
throw new Error('Method not implemented.'); | ||
} | ||
public isItemAtIndexVisible(index: number) { | ||
throw new Error('Method not implemented.'); | ||
} | ||
public scrollToIndex(index: number, animated: boolean) { | ||
throw new Error('Method not implemented.'); | ||
} | ||
public scrollToOffset(value: number, animated?: boolean) { | ||
throw new Error('Method not implemented.'); | ||
} | ||
getViewForItemAtIndex(index: number): View { | ||
throw new Error('Method not implemented.'); | ||
} | ||
startDragging(index: number) { | ||
throw new Error('Method not implemented.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { ContentView, Trace, View } from '@nativescript/core'; | ||
import { CLog, CLogTypes, CollectionViewBase } from './index-common'; | ||
import { SharedCollectionViewPoolBase } from './shared-pool-common'; | ||
|
||
interface CollectionViewCellHolder extends com.nativescript.collectionview.CollectionViewCellHolder { | ||
// tslint:disable-next-line:no-misused-new | ||
new (androidView: android.view.View): CollectionViewCellHolder; | ||
view: View; | ||
clickListener: android.view.View.OnClickListener; | ||
} | ||
|
||
export class SharedCollectionViewPool extends SharedCollectionViewPoolBase { | ||
private recyclerListener: androidx.recyclerview.widget.RecyclerView.RecyclerListener; | ||
private recycledViewPool: com.nativescript.collectionview.RecycledViewPool; | ||
private recycledViewPoolDisposeListener: com.nativescript.collectionview.RecycledViewPool.ViewPoolListener; | ||
|
||
// used to store viewHolder and thus their corresponding Views | ||
// used to "destroy" cells when possible | ||
public _viewHolders = new Set<CollectionViewCellHolder>(); | ||
|
||
public createNativeView() { | ||
// no need to create anything | ||
return null; | ||
} | ||
|
||
public initNativeView(): void { | ||
// const nativeView = this.nativeViewProtected; | ||
this.recycledViewPool = new com.nativescript.collectionview.RecycledViewPool(); | ||
this.recycledViewPoolDisposeListener = new com.nativescript.collectionview.RecycledViewPool.ViewPoolListener({ | ||
onViewHolderDisposed: (holder: CollectionViewCellHolder) => { | ||
if (Trace.isEnabled()) { | ||
CLog(CLogTypes.log, 'onViewHolderDisposed', holder); | ||
} | ||
if (this._viewHolders) { | ||
this._viewHolders.delete(holder); | ||
} | ||
const isNonSync = holder['defaultItemView'] === true; | ||
const view = isNonSync ? (holder.view as ContentView).content : holder.view; | ||
this.notifyForItemAtIndex(CollectionViewBase.itemDisposingEvent, view, holder.getAdapterPosition(), view.bindingContext, holder); | ||
if (view && view.isLoaded) { | ||
view.callUnloaded(); | ||
} | ||
view._isAddedToNativeVisualTree = false; | ||
//@ts-ignore | ||
view.parent = null; | ||
view._tearDownUI(); | ||
} | ||
}); | ||
(this.recycledViewPool as any).mListener = this.recycledViewPoolDisposeListener; | ||
|
||
this.recyclerListener = new androidx.recyclerview.widget.RecyclerView.RecyclerListener({ | ||
onViewRecycled: (holder: CollectionViewCellHolder) => { | ||
if (Trace.isEnabled()) { | ||
CLog(CLogTypes.log, 'onViewRecycled', holder); | ||
} | ||
const isNonSync = holder['defaultItemView'] === true; | ||
const view = isNonSync ? (holder.view as ContentView).content : holder.view; | ||
this.notifyForItemAtIndex(CollectionViewBase.itemRecyclingEvent, view, holder.getAdapterPosition(), view.bindingContext, holder); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should also likely be fired for all CollectionView instances as well (perhaps |
||
} | ||
}); | ||
} | ||
|
||
public disposeNativeView() { | ||
this.collectionViews.forEach((collectionView) => { | ||
const nativeView = collectionView.nativeViewProtected; | ||
nativeView.setRecyclerListener(null); | ||
nativeView.setRecycledViewPool(null); | ||
}); | ||
this.collectionViews.clear(); | ||
this.recycledViewPoolDisposeListener = null; | ||
this.recycledViewPool = null; | ||
super.disposeNativeView(); | ||
} | ||
|
||
public attachToCollectionView(collectionView: CollectionViewBase): void { | ||
super.attachToCollectionView(collectionView); | ||
|
||
const nativeView = collectionView.nativeViewProtected; | ||
nativeView.setRecyclerListener(this.recyclerListener); | ||
nativeView.setRecycledViewPool(this.recycledViewPool); | ||
} | ||
|
||
public detachFromCollectionView(collectionView: CollectionViewBase): void { | ||
super.detachFromCollectionView(collectionView); | ||
|
||
const nativeView = collectionView.nativeViewProtected; | ||
nativeView.setRecyclerListener(null); | ||
nativeView.setRecycledViewPool(null); | ||
} | ||
|
||
notifyForItemAtIndex(eventName: string, view: View, index: number, bindingContext?, native?: any) { | ||
const args = { eventName, object: this, index, view, ios: native, bindingContext }; | ||
this.notify(args); | ||
return args as any; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should likely be fired on the
CollectionView
instances as well?