Skip to content

Commit 7cbea3e

Browse files
authored
fix: Fix unloading (#3)
* fix: Fix unloading
1 parent 4d21a11 commit 7cbea3e

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

src/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-swipe-view",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"description": "NativeScript plugin to connect with Azure Notification Hubs",
55
"main": "dist/swipe-view",
66
"typings": "swipe-view.d.ts",

src/swipe-view.ts

+31-24
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
import {
1818
Builder,
1919
CoreTypes,
20+
EventData,
21+
GestureStateTypes,
2022
GridLayout,
2123
ItemSpec,
2224
PanGestureEventData,
@@ -57,7 +59,6 @@ export class SwipeView extends GridLayout implements definition.SwipeView {
5759
private _previousDelta = 0;
5860
private _isParentPanIn = false;
5961
private _animationDuration = 250;
60-
private _prevPanState: number;
6162

6263
public refresh() {
6364
if (!this._swipeView) {
@@ -87,28 +88,23 @@ export class SwipeView extends GridLayout implements definition.SwipeView {
8788

8889
this.style.padding = 0;
8990

90-
this.parent?.on("pan", (e: PanGestureEventData) => {
91-
this._isParentPanIn = e.state === 2 && Math.abs(e.deltaY) > 5;
92-
if (this._isParentPanIn
93-
&& this.getChildAt(1)?.translateX !== 0) {
94-
this._resetTransition();
95-
}
96-
});
97-
this.parent?.on(SwipeView.swipeViewSwipeStartedEvent, (e) => {
98-
if (e.object !== this) {
99-
this._resetTransition();
100-
}
101-
});
102-
this.parent?.on("itemTap", this._resetTransition.bind(this));
91+
this.parent?.on("pan", this._onParentPan, this);
92+
this.parent?.on(SwipeView.swipeViewSwipeStartedEvent, this._onSwipeViewSwipeStarted, this);
93+
this.parent?.on("itemTap", this._resetTransition, this);
10394

104-
this.on("pan", this._onPan.bind(this));
105-
this._swipeView.on("tap", this._resetTransition.bind(this));
95+
this.on("pan", this._onPan, this);
96+
this._swipeView.on("tap", this._resetTransition, this);
10697
}
10798

10899
public onUnloaded(): void {
109-
this.off("pan");
110-
this.off("tap");
100+
this.parent?.off("pan", this._onParentPan, this);
101+
this.parent?.off(SwipeView.swipeViewSwipeStartedEvent, this._onSwipeViewSwipeStarted, this);
102+
this.parent?.off("itemTap", this._resetTransition, this);
103+
104+
this.off("pan", this._onPan, this);
105+
this._swipeView.off("tap", this._resetTransition, this);
111106

107+
this.removeChild(this._swipeView);
112108
this._swipeView.removeChildren();
113109
this._swipeView = undefined;
114110
this._leftActionsTemplateView = undefined;
@@ -130,9 +126,8 @@ export class SwipeView extends GridLayout implements definition.SwipeView {
130126
return;
131127
}
132128

133-
// Swipe start
134-
if (e.state === 2
135-
&& this._prevPanState !== e.state) {
129+
// Pan start
130+
if (e.state === GestureStateTypes.began) {
136131
this.parent?.notify({
137132
eventName: SwipeView.swipeViewSwipeStartedEvent,
138133
object: this,
@@ -150,14 +145,12 @@ export class SwipeView extends GridLayout implements definition.SwipeView {
150145
this._swipeView.height = Utils.layout.toDeviceIndependentPixels(finalHeight);
151146
}
152147

153-
this._prevPanState = e.state;
154-
155148
const itemView = this.getChildAt(1);
156149
const leftActionsViewMeasuredWidth = Utils.layout.toDeviceIndependentPixels(this._leftActionsTemplateView?.getMeasuredWidth());
157150
const rightActionsViewMeasuredWidth = Utils.layout.toDeviceIndependentPixels(this._rightActionsTemplateView?.getMeasuredWidth());
158151

159152
// Pan Stop
160-
if (e.state === 3) {
153+
if (e.state === GestureStateTypes.ended) {
161154
let translateX = 0;
162155
if (e.deltaX < 0 && itemView.translateX < 0) {
163156
translateX = -rightActionsViewMeasuredWidth;
@@ -202,6 +195,20 @@ export class SwipeView extends GridLayout implements definition.SwipeView {
202195
}
203196
}
204197
}
198+
199+
private _onParentPan(e: PanGestureEventData) {
200+
this._isParentPanIn = e.state === GestureStateTypes.changed && Math.abs(e.deltaY) > 5;
201+
if (this._isParentPanIn
202+
&& this.getChildAt(1)?.translateX !== 0) {
203+
this._resetTransition();
204+
}
205+
}
206+
207+
private _onSwipeViewSwipeStarted(e: EventData) {
208+
if (e.object !== this) {
209+
this._resetTransition();
210+
}
211+
}
205212
}
206213

207214
leftActionsTemplateProperty.register(SwipeView);

0 commit comments

Comments
 (0)