forked from nstudio/nativescript-pulltorefresh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpulltorefresh.android.ts
More file actions
130 lines (103 loc) · 3.34 KB
/
pulltorefresh.android.ts
File metadata and controls
130 lines (103 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/// <reference path="./node_modules/tns-platform-declarations/android.d.ts" />
import { Color } from 'tns-core-modules/color';
import {
PullToRefreshBase,
backgroundColorProperty,
colorProperty,
refreshingProperty
} from './pulltorefresh-common';
export * from './pulltorefresh-common';
class CarouselFriendlySwipeRefreshLayout extends android.support.v4.widget
.SwipeRefreshLayout {
private _touchSlop: number;
private _previousX: number;
public constructor(
context: android.content.Context,
attrs: android.util.AttributeSet
) {
super(context, attrs);
this._touchSlop = android.view.ViewConfiguration.get(
context
).getScaledTouchSlop();
}
public onInterceptTouchEvent(event: android.view.MotionEvent): boolean {
switch (event.getAction()) {
case android.view.MotionEvent.ACTION_DOWN: {
this._previousX = android.view.MotionEvent.obtain(event).getX();
break;
}
case android.view.MotionEvent.ACTION_MOVE: {
const eventX = event.getX();
const xDifference = Math.abs(eventX - this._previousX);
if (xDifference > this._touchSlop) {
return false;
}
break;
}
}
return super.onInterceptTouchEvent(event);
}
}
export class PullToRefresh extends PullToRefreshBase {
private _androidViewId: number;
public nativeView: any; // android.support.v4.widget.SwipeRefreshLayout;
get android(): any {
return this.nativeView; // android.support.v4.widget.SwipeRefreshLayout
}
public createNativeView() {
const swipeRefreshLayout = new (CarouselFriendlySwipeRefreshLayout as any)(
this._context
);
if (!this._androidViewId) {
this._androidViewId = android.view.View.generateViewId();
}
swipeRefreshLayout.setId(this._androidViewId);
const refreshListener = new TNS_SwipeRefreshListener(new WeakRef(this));
swipeRefreshLayout.setOnRefreshListener(refreshListener);
(swipeRefreshLayout as any).refreshListener = refreshListener;
return swipeRefreshLayout;
}
public initNativeView() {
super.initNativeView();
const nativeView = this.nativeView as any;
nativeView.refreshListener.owner = new WeakRef(this);
}
public disposeNativeView() {
const nativeView = this.nativeView as any;
nativeView.refreshListener.owner = null;
super.disposeNativeView();
}
[refreshingProperty.getDefault](): boolean {
return false;
}
[refreshingProperty.setNative](value: boolean) {
this.nativeView.setRefreshing(value);
}
[colorProperty.setNative](value: Color | number) {
const color = value instanceof Color ? value.android : value;
this.nativeView.setColorSchemeColors([color]);
}
[backgroundColorProperty.setNative](value: Color | number) {
const color = value instanceof Color ? value.android : value;
this.nativeView.setProgressBackgroundColorSchemeColor(color);
}
}
@Interfaces([
(android.support.v4.widget as any).SwipeRefreshLayout.OnRefreshListener
])
class TNS_SwipeRefreshListener extends java.lang.Object {
constructor(private owner: WeakRef<PullToRefresh>) {
super();
return global.__native(this);
}
public onRefresh(v) {
const owner = this.owner.get();
if (owner) {
owner.refreshing = true;
owner.notify({
eventName: PullToRefreshBase.refreshEvent,
object: owner
});
}
}
}