forked from nstudio/nativescript-pulltorefresh
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.ios.ts
137 lines (117 loc) · 4.04 KB
/
index.ios.ts
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
131
132
133
134
135
136
137
import { Utils } from '@nativescript/core';
import {
PullToRefreshBase,
indicatorColorProperty,
indicatorFillColorProperty,
refreshingProperty,
} from './index.common';
export * from './index.common';
@NativeClass
class PullToRefreshHandler extends NSObject {
public static ObjCExposedMethods = {
handleRefresh: { returns: interop.types.void, params: [UIRefreshControl] },
};
private _owner: WeakRef<PullToRefresh>;
public static initWithOnwer(
owner: WeakRef<PullToRefresh>,
): PullToRefreshHandler {
const impl = PullToRefreshHandler.new() as PullToRefreshHandler;
impl._owner = owner;
return impl;
}
public handleRefresh(refreshControl: UIRefreshControl) {
const pullToRefresh = this._owner.get();
pullToRefresh.refreshing = true;
pullToRefresh.notify({
eventName: PullToRefreshBase.refreshEvent,
object: pullToRefresh,
});
}
}
const SUPPORT_REFRESH_CONTROL = Utils.ios.MajorVersion >= 10;
export class PullToRefresh extends PullToRefreshBase {
private mHandler: PullToRefreshHandler;
// NOTE: We cannot use the default ios property as the UIRefreshControl can be added only to UIScrollViews!
private mRefreshControl: UIRefreshControl;
constructor() {
super();
this.mRefreshControl = UIRefreshControl.alloc().init();
this.mHandler = PullToRefreshHandler.initWithOnwer(new WeakRef(this));
this.mRefreshControl.addTargetActionForControlEvents(
this.mHandler,
'handleRefresh',
UIControlEvents.ValueChanged,
);
}
onLoaded() {
super.onLoaded();
this._onContentChanged(null, this.content);
}
_onContentChanged(oldView, newView) {
if (!newView || !newView.nativeViewProtected) {
return;
}
const owner = newView.nativeViewProtected;
if (owner instanceof UIScrollView) {
if (SUPPORT_REFRESH_CONTROL) {
owner.refreshControl = this.mRefreshControl;
} else {
// ensure that we can trigger the refresh, even if the content is not large enough
owner.alwaysBounceVertical = true;
owner.addSubview(this.mRefreshControl);
}
} else if (owner instanceof WKWebView) {
if (SUPPORT_REFRESH_CONTROL) {
owner.scrollView.refreshControl = this.mRefreshControl;
} else {
// ensure that we can trigger the refresh, even if the content is not large enough
owner.scrollView.alwaysBounceVertical = true;
owner.scrollView.addSubview(this.mRefreshControl);
}
} else if (
typeof TKListView !== 'undefined' &&
owner instanceof TKListView
) {
if (SUPPORT_REFRESH_CONTROL) {
owner.collectionView.refreshControl = this.mRefreshControl;
} else {
// ensure that we can trigger the refresh, even if the content is not large enough
owner.collectionView.alwaysBounceVertical = true;
owner.collectionView.addSubview(this.mRefreshControl);
}
} else if (owner instanceof WKWebView) {
if (SUPPORT_REFRESH_CONTROL) {
owner.scrollView.refreshControl = this.mRefreshControl;
} else {
// ensure that we can trigger the refresh, even if the content is not large enough
owner.scrollView.alwaysBounceVertical = true;
owner.scrollView.addSubview(this.mRefreshControl);
}
} else {
throw new Error(
'Content must inherit from either UIScrollView or WKWebView!',
);
}
}
[refreshingProperty.setNative](value: boolean) {
if (value) {
this.mRefreshControl.beginRefreshing();
} else {
this.mRefreshControl.endRefreshing();
}
}
[indicatorColorProperty.getDefault](): UIColor {
return this.mRefreshControl.tintColor;
}
[indicatorColorProperty.setNative](value: any) {
const color = value ? value.ios : null;
this.mRefreshControl.tintColor = color;
}
[indicatorFillColorProperty.getDefault](): UIColor {
return this.mRefreshControl.backgroundColor;
}
[indicatorFillColorProperty.setNative](value: any) {
const color = value ? value.ios : null;
this.mRefreshControl.backgroundColor = color;
}
}