-
-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathindex.android.ts
216 lines (185 loc) · 5.29 KB
/
index.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { AndroidApplication, Application, Utils, AndroidActivityResultEventData } from '@nativescript/core';
import { ISession, ITwitterUser } from './common';
export class TwitterError extends Error {
#native: com.twitter.sdk.android.core.TwitterException;
static fromNative(native, message?: string) {
const error = new TwitterError(message || native?.getMessage?.());
error.#native = native;
return error;
}
get native() {
return this.#native;
}
}
export class TwitterUser implements ITwitterUser {
#native: com.twitter.sdk.android.core.models.User;
static fromNative(user: com.twitter.sdk.android.core.models.User) {
if (user instanceof com.twitter.sdk.android.core.models.User) {
const usr = new TwitterUser();
usr.#native = user;
return usr;
}
return null;
}
get formattedScreenName(): string {
return com.twitter.sdk.android.core.internal.UserUtils.formatScreenName(this.native.screenName);
}
get isProtected(): boolean {
return this.native.protectedUser;
}
get isVerified(): boolean {
return this.native.verified;
}
get name(): string {
return this.native.name;
}
get profileImageUrl(): string {
return this.native.profileImageUrl;
}
get profileUrl(): string {
return this.native.url;
}
get screenName(): string {
return this.native.screenName;
}
get userId(): string {
return this.native.idStr;
}
get native() {
return this.#native;
}
get android() {
return this.native;
}
toJSON() {
return {
formattedScreenName: this.formattedScreenName,
isProtected: this.isProtected,
isVerified: this.isVerified,
name: this.name,
profileImageUrl: this.profileImageUrl,
profileUrl: this.profileUrl,
screenName: this.screenName,
userId: this.userId
}
}
}
export class Twitter {
static init(consumerKey: string, consumerSecret: string) {
const config = new com.twitter.sdk.android.core.TwitterConfig.Builder(Utils.android.getApplicationContext());
config.twitterAuthConfig(new com.twitter.sdk.android.core.TwitterAuthConfig(consumerKey, consumerSecret));
com.twitter.sdk.android.core.Twitter.initialize(config.build());
}
}
export class Session implements ISession {
#native: com.twitter.sdk.android.core.TwitterSession;
#authToken: string;
#authTokenSecret: string;
static fromNative(ts: com.twitter.sdk.android.core.TwitterSession) {
if (ts instanceof com.twitter.sdk.android.core.TwitterSession) {
const session = new Session();
session.#native = ts;
return session;
}
return null;
}
get authToken() {
if (!this.#authToken) {
this.#authToken = this.native.getAuthToken().token;
}
return this.#authToken;
}
get authTokenSecret(): string {
if (!this.#authTokenSecret) {
this.#authTokenSecret = this.native.getAuthToken().secret;
}
return this.#authTokenSecret;
}
get userName() {
return this.native.getUserName();
}
get userId() {
return String(this.native.getUserId());
}
get native() {
return this.#native;
}
get android() {
return this.native;
}
}
let Callback;
function ensureClass() {
if (Callback) {
return;
}
@NativeClass()
class CallbackImpl extends com.twitter.sdk.android.core.Callback<com.twitter.sdk.android.core.TwitterSession> {
_resolve;
_reject;
constructor(resolve, reject) {
super();
this._resolve = resolve;
this._reject = reject;
return global.__native(this);
}
success(param0: com.twitter.sdk.android.core.Result<com.twitter.sdk.android.core.TwitterSession>) {
this._resolve?.(Session.fromNative(param0.data));
}
failure(param0: com.twitter.sdk.android.core.TwitterException) {
this._reject?.(TwitterError.fromNative(param0));
}
}
Callback = CallbackImpl;
}
export class TwitterSignIn {
static #client: com.twitter.sdk.android.core.identity.TwitterAuthClient;
static #didAttach = false;
static logIn() {
ensureClass();
return new Promise((resolve, reject) => {
if (!this.#client) {
this.#client = new com.twitter.sdk.android.core.identity.TwitterAuthClient();
}
if (!this.#didAttach) {
Application.android.on(AndroidApplication.activityResultEvent, (data: AndroidActivityResultEventData) => {
if (this.#client && this.#client.getRequestCode() === data.requestCode) {
this.#client.onActivityResult(data.requestCode, data.resultCode, data.intent);
}
});
this.#didAttach = true;
}
this.#client.authorize(Application.android.foregroundActivity || Application.android.startActivity, new Callback(resolve, reject));
});
}
static logOut() {
try {
const instance = com.twitter.sdk.android.core.TwitterCore.getInstance();
const sessionManager = instance.getSessionManager();
const sessions = sessionManager.getSessionMap();
const sessids = sessions.keySet().toArray();
for (let i = 0; i < sessids.length; i++) {
const sessid = sessids[i];
instance.getSessionManager().clearSession(sessid);
}
sessionManager.clearActiveSession();
} catch (e) { }
}
static getCurrentUser() {
return new Promise((resolve, reject) => {
org.nativescript.plugins.twitter.Twitter.verifyUser(
false,
false,
true,
new org.nativescript.plugins.twitter.Twitter.Callback<com.twitter.sdk.android.core.models.User>({
onSuccess(user) {
resolve(TwitterUser.fromNative(user));
},
onError(e) {
reject(TwitterError.fromNative(e));
},
})
);
});
}
}