forked from nstudio/nativescript-twitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter.ios.ts
161 lines (155 loc) · 5.98 KB
/
twitter.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { View, fromObject, Http, Utils } from "@nativescript/core";
import * as types from "@nativescript/core/utils/types";
declare const NSJSONSerialization, TWTRTwitter, TWTRAPIClient, TWTRLogInButton;
export class TNSTwitter {
public static init(key: string, secret: string) {
}
public static getCurrentUserEmail(): Promise<any> {
return new Promise((resolve, reject) => {
const client = TWTRAPIClient.clientWithCurrentUser();
client.requestEmailForCurrentUser((email: string, error) => {
if (error) {
reject({ message: error.localizedDescription });
} else {
resolve(email);
}
});
});
}
public static getCurrentUser(userID: string, token?: string, tokenSecret?: string): Promise<any> {
return new Promise((resolve, reject) => {
const client = TWTRAPIClient.clientWithCurrentUser();
client.loadUserWithIDCompletion(userID, (user: any, error) => {
if (error) {
reject({ message: error.localizedDescription });
} else {
resolve({
formattedScreenName: user.formattedScreenName,
isProtected: user.isProtected,
isVerified: user.isVerified,
name: user.name,
profileImageLargeURL: user.profileImageLargeURL,
profileImageMiniURL: user.profileImageMiniURL,
profileImageURL: user.profileImageURL,
profileURL: user.profileURL,
screenName: user.screenName,
userID: user.userID,
token,
tokenSecret
})
}
});
});
}
public static logIn(controller: any): Promise<any> {
return new Promise((resolve, reject) => {
TWTRTwitter.sharedInstance().logInWithViewControllerCompletion(controller, (session, error) => {
if (error) {
reject({ message: error.localizedDescription });
} else {
TNSTwitter.getCurrentUser(session.userID, session.authToken, session.authTokenSecret).then(user => {
resolve(user);
});
}
});
});
}
}
export class TNSTwitterButton extends View {
private _ios;
get ios() {
return this._ios;
}
public createNativeView() {
this._ios = TWTRLogInButton.buttonWithLogInCompletion((session, error) => {
if (error) {
this.notify({
eventName: 'loginStatus',
object: fromObject({ value: 'failed' })
});
} else {
this.notify({
eventName: 'loginStatus',
object: fromObject({ value: 'success', userName: session.userName, userID: session.userID })
});
}
});
return this._ios;
}
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number) {
const width = Utils.layout.getMeasureSpecSize(widthMeasureSpec);
const height = Utils.layout.getMeasureSpecSize(heightMeasureSpec);
this.setMeasuredDimension(width, height);
}
}
export class CustomApiService {
private _config;
private _token;
constructor() {
this._config = TWTRTwitter.sharedInstance.authConfig;
this._token = TWTRTwitter.sharedInstance.sessionStore.session();
}
makeRequest(url, method, options?): Promise<any> {
return new Promise((resolve, reject) => {
let nsError;
const client: any = TWTRAPIClient.clientWithCurrentUser();
const request = client.URLRequestWithMethodURLParametersError(method, url, null, nsError);
if (request) {
client.sendTwitterRequestCompletion(request, (res, data, error) => {
if (data) {
const json = NSJSONSerialization.JSONObjectWithDataOptionsError(data, 0);
resolve(this.toJsObject(json));
} else {
reject({ message: error.localizedDescription })
}
});
} else {
reject({ message: nsError.localizedDescription })
}
});
}
toJsObject = function (objCObj) {
if (objCObj === null || typeof objCObj != "object") {
return objCObj;
}
var node, key, i, l,
oKeyArr = objCObj.allKeys;
if (oKeyArr === undefined) {
// array
node = [];
for (i = 0, l = objCObj.count; i < l; i++) {
key = objCObj.objectAtIndex(i);
node.push(this.toJsObject(key));
}
} else {
// object
node = {};
for (i = 0, l = oKeyArr.count; i < l; i++) {
key = oKeyArr.objectAtIndex(i);
var val = objCObj.valueForKey(key);
if (val) {
switch (types.getClass(val)) {
case 'NSArray':
case 'NSMutableArray':
node[key] = this.toJsObject(val);
break;
case 'NSDictionary':
case 'NSMutableDictionary':
node[key] = this.toJsObject(val);
break;
case 'String':
node[key] = String(val);
break;
case 'Boolean':
node[key] = Boolean(val);
break;
case 'Number':
node[key] = Number(String(val));
break;
}
}
}
}
return node;
};
}