Skip to content

Commit 5fd8bb6

Browse files
committed
initial ios implementation
1 parent 7e2e26d commit 5fd8bb6

7 files changed

+240
-23
lines changed

pulltorefresh-common.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var __extends = (this && this.__extends) || function (d, b) {
66
};
77
var contentView = require("ui/content-view");
88
var dependencyObservable = require("ui/core/dependency-observable");
9+
var view = require("ui/core/view");
910
var proxy = require("ui/core/proxy");
1011
var PullToRefresh = (function (_super) {
1112
__extends(PullToRefresh, _super);
@@ -22,6 +23,13 @@ var PullToRefresh = (function (_super) {
2223
enumerable: true,
2324
configurable: true
2425
});
26+
PullToRefresh.prototype._addChildFromBuilder = function (name, value) {
27+
var originalColor = value.style.color || null;
28+
if (value instanceof view.View) {
29+
this.content = value;
30+
}
31+
value.style.color = originalColor;
32+
};
2533
PullToRefresh.refreshEvent = "refresh";
2634
PullToRefresh.refreshingProperty = new dependencyObservable.Property("refreshing", "PullToRefresh", new proxy.PropertyMetadata(false, dependencyObservable.PropertyMetadataSettings.None));
2735
return PullToRefresh;

pulltorefresh-common.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,16 @@ export class PullToRefresh extends contentView.ContentView implements definition
2424
set refreshing(value: boolean) {
2525
this._setValue(PullToRefresh.refreshingProperty, value);
2626
}
27-
27+
28+
public _addChildFromBuilder(name: string, value: any) {
29+
// Copy inheirtable style property values
30+
var originalColor = value.style.color || null;
31+
32+
if (value instanceof view.View) {
33+
this.content = value;
34+
}
35+
36+
// Reset inheritable style property values as we do not want those to be inherited
37+
value.style.color = originalColor;
38+
}
2839
}

pulltorefresh.android.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ var __extends = (this && this.__extends) || function (d, b) {
55
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
66
};
77
var common = require("./pulltorefresh-common");
8-
var view = require("ui/core/view");
98
var style = require("ui/styling/style");
109
function refreshingPropertyChanged(data) {
1110
var pullRefresh = data.object;
@@ -35,13 +34,6 @@ var PullToRefresh = (function (_super) {
3534
enumerable: true,
3635
configurable: true
3736
});
38-
PullToRefresh.prototype._addChildFromBuilder = function (name, value) {
39-
var originalColor = value.style.color || null;
40-
if (value instanceof view.View) {
41-
this.content = value;
42-
}
43-
value.style.color = originalColor;
44-
};
4537
PullToRefresh.prototype.setRefreshing = function (newValue) {
4638
this._android.setRefreshing(newValue);
4739
};

pulltorefresh.android.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,6 @@ export class PullToRefresh extends common.PullToRefresh {
3636
return this._android;
3737
}
3838

39-
public _addChildFromBuilder(name: string, value: any) {
40-
// Copy inheirtable style property values
41-
var originalColor = value.style.color || null;
42-
43-
if (value instanceof view.View) {
44-
this.content = value;
45-
}
46-
47-
// Reset inheritable style property values as we do not want those to be inherited
48-
value.style.color = originalColor;
49-
}
50-
5139
//Visibility methods
5240
public setRefreshing(newValue: boolean) {
5341
this._android.setRefreshing(newValue);

pulltorefresh.d.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Contains the PullToRefresh class, which represents a Layout that contains the UI pattern for pull-to-refresh
33
*/
4-
declare module "pulltorefresh" {
4+
declare module "nativescript-pulltorefresh" {
55
import dependencyObservable = require("ui/core/dependency-observable");
66
import view = require("ui/core/view");
77
import observable = require("data/observable");
@@ -22,7 +22,17 @@ declare module "pulltorefresh" {
2222
* Gets the native [android widget](http://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html) that represents the user interface for this component. Valid only when running on Android OS.
2323
*/
2424
android: any /* android.support.v4.widget.SwipeRefreshLayout */;
25-
25+
26+
/**
27+
* Because of iOS specific this returns the basic UIView. In order to access UIRefreshControl use the refreshControl property!
28+
*/
29+
ios: any
30+
31+
/**
32+
* Returns the native iOS UIRefreshControl
33+
*/
34+
refreshControl: UIRefreshControl
35+
2636
/*
2737
* Gets or sets if the view is refreshing
2838
*/

pulltorefresh.ios.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
"use strict";
2+
var __extends = (this && this.__extends) || function (d, b) {
3+
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
4+
function __() { this.constructor = d; }
5+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
6+
};
7+
var common = require("./pulltorefresh-common");
8+
var style = require("ui/styling/style");
9+
function refreshingPropertyChanged(data) {
10+
var pullRefresh = data.object;
11+
if (!pullRefresh.refreshControl) {
12+
return;
13+
}
14+
if (data.newValue) {
15+
pullRefresh.refreshControl.beginRefreshing();
16+
}
17+
else {
18+
pullRefresh.refreshControl.endRefreshing();
19+
}
20+
}
21+
common.PullToRefresh.refreshingProperty.metadata.onSetNativeValue = refreshingPropertyChanged;
22+
global.moduleMerge(common, exports);
23+
var PullToRefreshHandler = (function (_super) {
24+
__extends(PullToRefreshHandler, _super);
25+
function PullToRefreshHandler() {
26+
_super.apply(this, arguments);
27+
}
28+
PullToRefreshHandler.initWithOnwer = function (owner) {
29+
var impl = PullToRefreshHandler.new();
30+
impl._owner = owner;
31+
return impl;
32+
};
33+
PullToRefreshHandler.prototype.handleRefresh = function (refreshControl) {
34+
var pullToRefresh = this._owner.get();
35+
pullToRefresh.refreshing = true;
36+
pullToRefresh._emit(common.PullToRefresh.refreshEvent);
37+
};
38+
PullToRefreshHandler.ObjCExposedMethods = {
39+
"handleRefresh": { returns: interop.types.void, params: [UIRefreshControl] }
40+
};
41+
return PullToRefreshHandler;
42+
}(NSObject));
43+
var PullToRefresh = (function (_super) {
44+
__extends(PullToRefresh, _super);
45+
function PullToRefresh() {
46+
_super.call(this);
47+
this._refreshControl = new UIRefreshControl();
48+
this._handler = PullToRefreshHandler.initWithOnwer(new WeakRef(this));
49+
this._refreshControl.addTargetActionForControlEvents(this._handler, "handleRefresh", UIControlEvents.ValueChanged);
50+
}
51+
Object.defineProperty(PullToRefresh.prototype, "refreshControl", {
52+
get: function () {
53+
return this._refreshControl;
54+
},
55+
enumerable: true,
56+
configurable: true
57+
});
58+
PullToRefresh.prototype.onLoaded = function () {
59+
_super.prototype.onLoaded.call(this);
60+
if (this.content.ios instanceof UIScrollView) {
61+
this.content.ios.alwaysBounceVertical = true;
62+
this.content.ios.addSubview(this._refreshControl);
63+
}
64+
else {
65+
throw new Error("Content must inherit from UIScrollView!");
66+
}
67+
};
68+
return PullToRefresh;
69+
}(common.PullToRefresh));
70+
exports.PullToRefresh = PullToRefresh;
71+
var PullToRefreshStyler = (function () {
72+
function PullToRefreshStyler() {
73+
}
74+
PullToRefreshStyler.setBackgroundColor = function (pullToRefresh, value) {
75+
var native = pullToRefresh.refreshControl;
76+
native.backgroundColor = value;
77+
};
78+
PullToRefreshStyler.resetBackgroundColor = function (pullToRefresh, value) {
79+
var native = pullToRefresh.refreshControl;
80+
native.backgroundColor = value;
81+
};
82+
PullToRefreshStyler.setColor = function (pullToRefresh, value) {
83+
var native = pullToRefresh.refreshControl;
84+
native.tintColor = value;
85+
};
86+
PullToRefreshStyler.resetColor = function (pullToRefresh, value) {
87+
var native = pullToRefresh.refreshControl;
88+
native.tintColor = value;
89+
};
90+
PullToRefreshStyler.registerHandlers = function () {
91+
style.registerHandler(style.backgroundColorProperty, new style.StylePropertyChangedHandler(PullToRefreshStyler.setBackgroundColor, PullToRefreshStyler.resetBackgroundColor), "PullToRefresh");
92+
style.registerHandler(style.backgroundInternalProperty, style.ignorePropertyHandler, "PullToRefresh");
93+
style.registerHandler(style.colorProperty, new style.StylePropertyChangedHandler(PullToRefreshStyler.setColor, PullToRefreshStyler.resetColor), "PullToRefresh");
94+
};
95+
return PullToRefreshStyler;
96+
}());
97+
exports.PullToRefreshStyler = PullToRefreshStyler;
98+
PullToRefreshStyler.registerHandlers();

pulltorefresh.ios.ts

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import common = require("./pulltorefresh-common");
2+
import proxy = require("ui/core/proxy");
3+
import dependencyObservable = require("ui/core/dependency-observable");
4+
import style = require("ui/styling/style");
5+
6+
function refreshingPropertyChanged(data: dependencyObservable.PropertyChangeData) {
7+
var pullRefresh = <PullToRefresh>data.object;
8+
if (!pullRefresh.refreshControl) {
9+
return;
10+
}
11+
12+
if (data.newValue) {
13+
pullRefresh.refreshControl.beginRefreshing();
14+
}
15+
else {
16+
pullRefresh.refreshControl.endRefreshing();
17+
}
18+
}
19+
20+
// register the setNativeValue callback
21+
(<proxy.PropertyMetadata>common.PullToRefresh.refreshingProperty.metadata).onSetNativeValue = refreshingPropertyChanged;
22+
23+
global.moduleMerge(common, exports);
24+
25+
class PullToRefreshHandler extends NSObject {
26+
public static ObjCExposedMethods = {
27+
"handleRefresh": { returns: interop.types.void, params: [UIRefreshControl] }
28+
};
29+
30+
private _owner: WeakRef<PullToRefresh>;
31+
32+
public static initWithOnwer(owner: WeakRef<PullToRefresh>): PullToRefreshHandler {
33+
var impl = <PullToRefreshHandler>PullToRefreshHandler.new();
34+
impl._owner = owner;
35+
return impl;
36+
}
37+
38+
public handleRefresh(refreshControl: UIRefreshControl) {
39+
var pullToRefresh = this._owner.get();
40+
pullToRefresh.refreshing = true;
41+
pullToRefresh._emit(common.PullToRefresh.refreshEvent);
42+
}
43+
}
44+
45+
export class PullToRefresh extends common.PullToRefresh {
46+
private _refreshControl: UIRefreshControl;
47+
private _handler: PullToRefreshHandler;
48+
49+
constructor() {
50+
super();
51+
52+
this._refreshControl = new UIRefreshControl();
53+
this._handler = PullToRefreshHandler.initWithOnwer(new WeakRef(this));
54+
this._refreshControl.addTargetActionForControlEvents(this._handler, "handleRefresh", UIControlEvents.ValueChanged);
55+
}
56+
57+
// NOTE: We ccannot use the default ios property as the UIRefreshControl can be added only to UIScrollViews!
58+
get refreshControl(): UIRefreshControl {
59+
return this._refreshControl
60+
}
61+
62+
public onLoaded() {
63+
super.onLoaded();
64+
65+
if (this.content.ios instanceof UIScrollView) {
66+
// Ensure that we can trigger the refresh, even if the content is not large enough
67+
this.content.ios.alwaysBounceVertical = true;
68+
69+
this.content.ios.addSubview(this._refreshControl);
70+
}
71+
else {
72+
throw new Error("Content must inherit from UIScrollView!");
73+
}
74+
}
75+
}
76+
77+
export class PullToRefreshStyler implements style.Styler {
78+
private static setBackgroundColor(pullToRefresh: PullToRefresh, value: any) {
79+
var native = <UIRefreshControl>pullToRefresh.refreshControl;
80+
native.backgroundColor = value;
81+
}
82+
private static resetBackgroundColor(pullToRefresh: PullToRefresh, value: any) {
83+
var native = <UIRefreshControl>pullToRefresh.refreshControl;
84+
native.backgroundColor = value;
85+
}
86+
87+
private static setColor(pullToRefresh: PullToRefresh, value: any) {
88+
var native = <UIRefreshControl>pullToRefresh.refreshControl;
89+
native.tintColor = value;
90+
}
91+
private static resetColor(pullToRefresh: PullToRefresh, value: any) {
92+
var native = <UIRefreshControl>pullToRefresh.refreshControl;
93+
native.tintColor = value;
94+
}
95+
96+
public static registerHandlers() {
97+
style.registerHandler(style.backgroundColorProperty,
98+
new style.StylePropertyChangedHandler(PullToRefreshStyler.setBackgroundColor,
99+
PullToRefreshStyler.resetBackgroundColor),
100+
"PullToRefresh");
101+
style.registerHandler(style.backgroundInternalProperty,
102+
style.ignorePropertyHandler,
103+
"PullToRefresh");
104+
style.registerHandler(style.colorProperty,
105+
new style.StylePropertyChangedHandler(PullToRefreshStyler.setColor,
106+
PullToRefreshStyler.resetColor),
107+
"PullToRefresh");
108+
}
109+
}
110+
PullToRefreshStyler.registerHandlers();

0 commit comments

Comments
 (0)