Skip to content

Commit 3219b1f

Browse files
committed
Merge pull request nstudio#3 from PeterStaev/ios-implementation
iOS implementation
2 parents 7e2e26d + a64159d commit 3219b1f

10 files changed

+274
-64
lines changed

README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# NativeScript-PullToRefresh :recycle:
22
NativeScript plugin to use Pull to Refresh on any view.
33

4-
**Android Only - PRs welcome for iOS**.
5-
64
#### [Android SwipeRefreshLayout](http://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html)
75
##### [Material Design Indicators Spec](https://www.google.com/design/spec/components/progress-activity.html#progress-activity-types-of-indicators)
86

@@ -44,14 +42,14 @@ function refreshList(args) {
4442
// Get reference to the PullToRefresh;
4543
var pullRefresh = args.object;
4644

47-
// Do work here... and when done call .setRefreshing(false) to stop the refreshing
45+
// Do work here... and when done call set refreshing property to false to stop the refreshing
4846
loadItems().then(function (resp) {
4947
// ONLY USING A TIMEOUT TO SIMULATE/SHOW OFF THE REFRESHING
5048
setTimeout(function () {
51-
pullRefresh.setRefreshing(false);
49+
pullRefresh.refreshing = false;
5250
}, 1000);
5351
}, function (err) {
54-
pullRefresh.setRefreshing(false);
52+
pullRefresh.refreshing = false;
5553
});
5654
}
5755
exports.refreshList = refreshList;
@@ -63,6 +61,6 @@ exports.refreshList = refreshList;
6361

6462
## API
6563

66-
### setRefreshing
64+
### refreshing
6765

68-
Notifies the widget that the refresh state has changed.
66+
Property. Notifies the widget that the refresh state has changed.

demo/app/main-page.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ var app = require("application");
22
var Observable = require("data/observable").Observable;
33
var ObservableArray = require("data/observable-array").ObservableArray;
44
var platformModule = require("platform");
5-
var color = require("color");
6-
5+
var color = require("color");
6+
77
var users = [
88
{ name: 'Billy Bob' },
99
{ name: 'Tweeder' },
@@ -16,21 +16,21 @@ var users = [
1616
];
1717
var viewModel = new Observable({
1818
users: new ObservableArray(users)
19-
});
20-
21-
function pageLoaded(args) {
22-
var page = args.object;
19+
});
20+
21+
function pageLoaded(args) {
22+
var page = args.object;
2323
// Change statusbar color on Lollipop
24-
24+
2525
if (app.android && platformModule.device.sdkVersion >= "21") {
2626
var window = app.android.startActivity.getWindow();
2727
window.setStatusBarColor(new color.Color("#1976D2").android);
28-
}
29-
page.bindingContext = viewModel;
30-
loadItems();
31-
}
32-
exports.pageLoaded = pageLoaded;
33-
28+
}
29+
page.bindingContext = viewModel;
30+
loadItems();
31+
}
32+
exports.pageLoaded = pageLoaded;
33+
3434
function loadItems() {
3535
return new Promise(function (resolve, reject) {
3636
try {
@@ -44,8 +44,8 @@ function loadItems() {
4444
reject(ex);
4545
}
4646
});
47-
}
48-
47+
}
48+
4949
function refreshList(args) {
5050

5151
var pullRefresh = args.object;
@@ -54,16 +54,16 @@ function refreshList(args) {
5454
console.log(response);
5555
// ONLY USING A TIMEOUT TO SIMULATE/SHOW OFF THE REFRESHING
5656
setTimeout(function () {
57-
pullRefresh.setRefreshing(false);
57+
pullRefresh.refreshing = false;
5858
}, 1000);
5959
}, function (err) {
60-
pullRefresh.setRefreshing(false);
60+
pullRefresh.refreshing = false;
6161
alert(err);
6262
});
63-
}
64-
exports.refreshList = refreshList;
65-
66-
63+
}
64+
exports.refreshList = refreshList;
65+
66+
6767

6868
Number.prototype.times = function (func) {
6969
for (var i = 0; i < Number(this) ; i++) {

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
22
"name": "nativescript-pulltorefresh",
3-
"version": "0.1.2",
3+
"version": "1.0.0",
44
"description": "A NativeScript plugin to provide the Pull to Refresh control on any view.",
55
"main": "pulltorefresh.js",
66
"nativescript": {
77
"platforms": {
8-
"android": "1.6.0"
8+
"android": "1.6.0",
9+
"ios": "1.6.0"
910
}
1011
},
1112
"repository": {
@@ -19,7 +20,9 @@
1920
"Pull Refresh",
2021
"SwipeRefreshLayout",
2122
"Material Design",
23+
"UIRefreshControl",
2224
"android",
25+
"ios",
2326
"bradmartin",
2427
"Thorum"
2528
],
@@ -31,7 +34,7 @@
3134
},
3235
{
3336
"name": "Peter Staev",
34-
"email": "peter.staev@gmail.com",
37+
"email": "peter@tangrasoft.com",
3538
"url": "https://github.com/PeterStaev"
3639
}
3740
],

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: 1 addition & 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
};
@@ -59,6 +51,7 @@ var PullToRefresh = (function (_super) {
5951
onRefresh: function (v) {
6052
var owner = that.get();
6153
if (owner) {
54+
owner.refreshing = true;
6255
owner._emit(common.PullToRefresh.refreshEvent);
6356
}
6457
}

pulltorefresh.android.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,6 @@ export class PullToRefresh extends common.PullToRefresh {
3535
get _nativeView(): android.support.v4.widget.SwipeRefreshLayout {
3636
return this._android;
3737
}
38-
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-
51-
//Visibility methods
52-
public setRefreshing(newValue: boolean) {
53-
this._android.setRefreshing(newValue);
54-
}
5538

5639
public _createUI() {
5740

@@ -72,6 +55,7 @@ export class PullToRefresh extends common.PullToRefresh {
7255
onRefresh: function (v) {
7356
var owner = that.get();
7457
if (owner) {
58+
owner.refreshing = true;
7559
owner._emit(common.PullToRefresh.refreshEvent);
7660
}
7761
}

pulltorefresh.d.ts

Lines changed: 12 additions & 7 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,17 +22,22 @@ 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
*/
2939
refreshing: boolean;
3040

31-
/*
32-
//* Notify the widget that refresh state has changed.
33-
//*/
34-
//setRefreshing: boolean;
35-
3641
/**
3742
* Raised when a refresh event occurs.
3843
*/

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();

0 commit comments

Comments
 (0)