forked from nstudio/nativescript-twitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter.android.ts
159 lines (146 loc) · 6.09 KB
/
twitter.android.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
import { View, Http, Application, fromObject, Utils, AndroidApplication, AndroidActivityResultEventData } from "@nativescript/core";
declare const com: any, java;
export class TNSTwitter {
public static init(key: string, secret: string) {
const config = new com.twitter.sdk.android.core.TwitterConfig.Builder(Utils.ad.getApplicationContext())
.twitterAuthConfig(new com.twitter.sdk.android.core.TwitterAuthConfig(key, secret))
.build();
com.twitter.sdk.android.core.Twitter.initialize(config);
}
public static getCurrentUserEmail(): Promise<any> {
return new Promise((resolve, reject) => {
const session = com.twitter.sdk.android.core.TwitterCore.getInstance().getSessionManager().getActiveSession();
const client = new com.twitter.sdk.android.core.identity.TwitterAuthClient();
client.requestEmail(session, new com.twitter.sdk.android.core.Callback(
{
success(result) {
if (result.data && result.data.length > 0) {
resolve(result.data);
} else {
reject({ "message": "This user does not have an email address." })
}
}, failure(exception) {
reject({ message: exception.getMessage() })
}
}
));
});
}
public static getCurrentUser(userID: string, token?: string, tokenSecret?: string): Promise<any> {
return new Promise((resolve, reject) => {
const api = new CustomApiService();
api.makeRequest("https://api.twitter.com/1.1/account/verify_credentials.json", "get")
.then(
data => {
const user = data.content.toJSON();;
resolve({
formattedScreenName: user.screen_name,
isProtected: user.protected,
isVerified: user.verified,
name: user.name,
profileImageLargeURL: user.profile_image_url_https.replace('_normal', '_bigger'),
profileImageMiniURL: user.profile_image_url_https.replace('_normal', '_mini'),
profileImageURL: user.profile_image_url_https,
profileURL: user.url,
screenName: user.screen_name,
userID: user.id,
token,
tokenSecret
})
}, err => {
reject(err.message);
});
});
}
public static logIn(controller: any): Promise<any> {
return new Promise((resolve, reject) => {
reject('TODO for Android!!');
});
}
public static getNativeConfig() {
return com.twitter.sdk.android.core.TwitterCore.getInstance().getAuthConfig();
}
public static getNativeToken() {
return com.twitter.sdk.android.core.TwitterCore.getInstance().getSessionManager().getActiveSession() ? com.twitter.sdk.android.core.TwitterCore.getInstance().getSessionManager().getActiveSession().getAuthToken() : null;
}
}
export class TNSTwitterButton extends View {
private _android;
get android() {
return this._android;
}
public createNativeView() {
this._android = new com.twitter.sdk.android.core.identity.TwitterLoginButton(Application.android.foregroundActivity);
return this._android;
}
public initNativeView() {
const that = new WeakRef(this);
const _cb = com.twitter.sdk.android.core.Callback.extend({
owner: that.get(),
success(result) {
this.owner.notify({
eventName: 'loginStatus',
object: fromObject({ value: 'success', userName: result.data.getUserName(), userID: result.data.getUserId() })
});
},
failure(exception) {
this.owner.notify({
eventName: 'loginStatus',
object: fromObject({ value: 'failed', message: exception.getMessage() })
});
}
});
this._android.setCallback(new _cb());
Application.android.on(AndroidApplication.activityResultEvent, (args: AndroidActivityResultEventData) => {
this._android.onActivityResult(args.requestCode, args.resultCode, args.intent);
})
}
}
export class CustomApiService {
private _config;
private _token;
constructor() {
this._config = TNSTwitter.getNativeConfig();
this._token = TNSTwitter.getNativeToken();
}
makeRequest(url, method, options?): Promise<any> {
if (this._config && this._token) {
try {
const oauth = new com.twitter.sdk.android.core.OAuthSigning(this._config, this._token);
const auth = oauth.getAuthorizationHeader(method, url, options ? this.buildOptions(options) : null);
return Http.request({
url: url,
method: method,
headers: {
'Authorization': auth
}
})
} catch (ex) {
return new Promise((resolve, reject) => {
reject(ex);
});
}
} else {
return new Promise((resolve, reject) => {
reject('User is not logged in');
});
}
}
private buildOptions(value) {
let store = new java.util.HashMap();
Object.keys(value).forEach((item, key) => {
switch (typeof value[item]) {
case 'string':
store.put(item, value[item]);
break;
case 'boolean':
store.put(item, new java.lang.String(String(value[item])));
break;
case 'number':
store.put(item, value[item]);
break;
}
});
return store;
}
}