-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
646 lines (565 loc) · 16.6 KB
/
api.js
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
import Storage from 'react-native-storage';
import { AsyncStorage, Platform, Alert } from 'react-native';
import Speech from 'react-native-tts';
import * as Localization from 'expo-localization';
import * as Haptics from 'expo-haptics';
import * as Permissions from 'expo-permissions';
import * as Device from 'expo-device';
import * as InAppPurchases from 'expo-in-app-purchases';
import * as Notifications from 'expo-notifications';
import { Analytics, ScreenHit, Event as Avent } from 'expo-analytics-safe';
import { CacheManager } from "react-native-expo-image-cache";
import Constants from 'expo-constants';
import NetInfo from '@react-native-community/netinfo';
import Event from './js/event';
import makeid from './js/makeid';
import styles from './js/styles';
import uitext from './uitext';
const APP = require("./app.json");
// For test cases
const _DEVELOPMENT = false;
const _NETWORK_STATUS = true;
const _FLUSH = false;
const _DEVUSERIDENTIFIER = "109677539152659927717";
const _DEVLOCALE = "en-US";
const _ISPREMIUM = false;
const ANALYTICS_KEY = APP.analyticsKey;
const API_ENDPOINT = APP.apiEndpoint;
const ASSET_ENDPOINT = APP.assetEndpoint;
const ASSET_VERSION = APP.assetVersion;
const RTL = ["ar","ur","he"];
let storage = new Storage({
size: 1000,
storageBackend: Platform.OS === "web" ? localStorage : AsyncStorage,
defaultExpires: null,
enableCache: true,
sync: {}
});
class Api {
constructor(){
if(_DEVELOPMENT && _FLUSH){
AsyncStorage.clear();
CacheManager.clearCache();
}
this.user = {};
this.cards = {};
this.storeId = APP.storeId;
this.searchArray = [];
this.development = _DEVELOPMENT;
this.styles = styles;
this.setSpeechEngine();
this.config = APP.config;
this.event = Event;
this.analytics = new Analytics(_DEVELOPMENT ? "DEVELOPMENT" : ANALYTICS_KEY, {slug: APP.name, name: APP.displayName, version: APP.expo.version});
this.isTablet = false;
this._checkIfTablet();
this.version = ASSET_VERSION;
this.assetEndpoint = ASSET_ENDPOINT;
if(_DEVELOPMENT){
this.isOnline = _NETWORK_STATUS;
}else{
this.isOnline = true;
}
this.locked = true;
this.premium = "determining";
this.premiumPlans = [];
console.log("API: Created instance");
if(!_DEVELOPMENT){
this._listenNetwork();
}
this._initSubscriptions();
}
isRTL(){
if(this.user.language){
return RTL.includes(this.user.language);
}else{
return false;
}
}
requestSpeechInstall(){
if(Platform.OS == "android"){
Speech.getInitStatus().then(() => {
Speech.requestInstallData();
}, (err) => {
if (err.code === 'no_engine') {
Speech.requestInstallEngine();
}
});
}
}
setSpeechEngine(){
if(Platform.OS == "android"){
Speech.engines().then(engines => {
engines.forEach(engine => {
if(engine.label){
if(engine.label.includes("Google") || engine.label.includes("google") || engine.label.includes("google")){
if(!engine.default){
Speech.setDefaultEngine(engine.name);
}
}
}
});
});
}
}
initSpeech(){
console.log("Speech Initialized");
Speech.setDefaultVoice(this.user.voice).then(res => {
console.log(res);
}, (err) => {
console.log("Error: ", err);
});
Speech.setIgnoreSilentSwitch("ignore");
Speech.setDucking(true);
Speech.addEventListener('tts-start', () => {});
Speech.addEventListener('tts-finish', () => {});
Speech.addEventListener('tts-cancel', () => {});
}
hit(screen){
this.analytics.hit(new ScreenHit(screen))
.then(() => {
// hit done
})
.catch(e => console.log(e.message));
}
avent(a,b,c){
this.analytics.event(new Avent(a, b, c))
}
_listenNetwork(){
NetInfo.addEventListener(state => {
console.log('Connection type', state.type);
console.log('Is connected?', state.isConnected);
this.isOnline = state.isConnected;
});
}
async _checkIfTablet(){
let deviceType = await Device.getDeviceTypeAsync()
this.isTablet = deviceType == Device.DeviceType.TABLET;
}
isPremium(){
if(_DEVELOPMENT){
return _ISPREMIUM;
}
if(this.premium.includes("lifetime") || this.premium.includes("yearly") || this.premium.includes("monthly")){
return true;
}else{
if(this.isGift){
return true;
}else{
return false;
}
}
}
haptics(style){
if(Platform.OS != "web"){
if(this.user.haptic !== "0"){
switch (style) {
case "touch":
Haptics.selectionAsync()
break;
case "impact":
Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium)
break;
default:
Haptics.selectionAsync()
}
}
}else{
console.log("BZZ");
}
}
async registerForPushNotificationsAsync() {
await Notifications.requestPermissionsAsync({
ios: {
allowAlert: true,
allowBadge: true,
allowSound: true,
allowAnnouncements: true,
},
});
let experienceId = undefined;
if (!Constants.manifest) {
// Absence of the manifest means we're in bare workflow
experienceId = '@burak/'+APP.name;
}
const expoPushToken = await Notifications.getExpoPushTokenAsync({
experienceId,
});
console.log(expoPushToken.data);
this.scheduleNotif();
}
async scheduleNotif(){
let scheduledNotifs = await Notifications.getAllScheduledNotificationsAsync();
console.log("##Notif", scheduledNotifs);
if(scheduledNotifs.length == 0){
let content = {
sound: 'default',
title: this.t("setup_notification_badge_title"),
body: this.t("setup_notification_badge_description"),
};
Notifications.scheduleNotificationAsync({
content: content,
trigger: {
seconds: 60*60*24*2, // 2 days = 60*60*24*2
repeats: true
},
});
}
}
async setup(profile){
let lang = profile.language ? profile.language : Localization.locale.substr(0, 2);
let voice = await this.getBestAvailableVoiceDriver(lang);
let user = {
name: profile.name,
avatar: profile.avatar,
voice: voice.identifier,
language: lang,
haptic: 1,
pressIn: 0
}
await this.setData("user", JSON.stringify(user));
console.log("Setup completed.");
this.user = user;
this.initSpeech();
}
signout(){
Alert.alert(
this.t("alert_signout_title"),
this.t("alert_signout_description"),
[
{
text: this.t("alert_cancel"),
onPress: () => console.log("Cancel Pressed"),
style: "cancel"
},
{ text: this.t("alert_ok"), onPress: () => {
//AsyncStorage.clear();
this.event.emit("refresh", "signout");
} }
],
{ cancelable: true }
);
}
async update(fields, values){
let user = JSON.parse(await this.getData("user"));
fields.forEach((itemProperty, i) => {
user[itemProperty] = values[i];
});
this.user = user;
await this.setData("user", JSON.stringify(user));
this.event.emit("refresh");
}
speak(text, speed, voice){
if(voice){
Speech.setDefaultVoice(voice);
}
//text = this.phrase()
let rate = 0.5;
if(speed == "slow"){
rate = 0.25;
}
if(this.user.voice != "unsupported"){
Speech.speak(text, {
language: this.user.language,
pitch: 1,
rate: rate,
androidParams: {
KEY_PARAM_STREAM: 'STREAM_MUSIC'
}
});
}
}
async getAvailableVoicesAsync(recall){
let voices = await Speech.voices();
if(voices.length == 0){
if(recall){
return [];
}else{
await new Promise(function(resolve) {
setTimeout(resolve, 8000);
});
return await this.getAvailableVoicesAsync(true);
}
}else{
voices.map(voice => {
voice.name = voice.id;
voice.identifier = voice.id;
voice.quality == 500 ? voice.quality = "Enhanced" : voice.quality = "Optimal";
});
return voices;
}
}
async getBestAvailableVoiceDriver(language){
let allVoices = await this.getAvailableVoicesAsync();
let voices = allVoices.filter(voice => voice.language.includes(language));
if(voices.length == 0){
return "unsupported";
}else if(voices.length == 1){
return voices[0];
}else if(voices.length > 1){
let localeString = this.localeString().toLowerCase().replace(/_/g, "-");
let localeVoices = voices.filter(voice => localeString.includes(voice.language.toLowerCase().replace(/_/g, "-")));
if(localeVoices.length == 0){
// check if there is an enhanced one
return voices.sort((a, b) => {
let aQ = !(a.quality == "Enhanced");
let bQ = !(b.quality == "Enhanced");
if (aQ < bQ) return -1
if (aQ > bQ) return 1
return 0
})[0];
}else if(localeVoices.length == 1){
return localeVoices[0];
}else if(localeVoices.length > 1){
// check if there is an enhanced one
return localeVoices.sort((a, b) => {
let aQ = !(a.quality == "Enhanced");
let bQ = !(b.quality == "Enhanced");
if (aQ < bQ) return -1
if (aQ > bQ) return 1
return 0
})[0];
}
}
}
async getPacks(force){
var url = ASSET_ENDPOINT + "packs/" + this.user.language + "/metadata.json?v="+this.version;
if(this.packs && force == null){
console.log("pulling from ram");
return this.packs;
}else{
let packsResponse;
try {
packsResponse = await fetch(url, {cache: "no-cache"})
.then(res => res.json());
this.setData("packs", JSON.stringify(packsResponse));
} catch(error){
console.log("Offline, Falling back to cached packdata!", error);
let packsResponseString = await this.getData("packs");
if(packsResponseString){
packsResponse = JSON.parse(packsResponseString);
}
}
this.packs = packsResponse;
return packsResponse;
}
}
async _initSubscriptions(){
try{
if(_DEVELOPMENT){
if(!_ISPREMIUM){
this.premium = "none";
this.event.emit("premium");
}
}
if(!Constants.isDevice) {
this.premium = "none";
this.event.emit("premium");
}
await InAppPurchases.connectAsync();
InAppPurchases.setPurchaseListener(({ responseCode, results, errorCode }) => {
// Purchase was successful
if (responseCode === InAppPurchases.IAPResponseCode.OK) {
results.forEach(async (purchase) => {
if (!purchase.acknowledged) {
console.log(`Successfully purchased ${purchase.productId}`);
// Process transaction here and unlock content...
this.premium = purchase.productId;
if(purchase.productId.includes("lifetime")){
this.premium = "lifetime";
}else if(purchase.productId.includes("monthly")){
this.premium = "monthly";
}else if(purchase.productId.includes("yearly")){
this.premium = "yearly";
}
let consume = (purchase.productId.includes("lifetime"));
console.log("Should I consume?", consume);
// Then when you're done
let resfinish = await InAppPurchases.finishTransactionAsync(purchase, consume);
alert(`Successfully purchased ${purchase.productId.replace(APP.planPrefix, "")}, you can now use the premium version of the app!`);
this.event.emit("premium");
this.event.emit("premiumPurchase", this.premium);
await this.setData("premium", this.premium);
this.event.emit("refresh");
}
});
}
// Else find out what went wrong
if (responseCode === InAppPurchases.IAPResponseCode.USER_CANCELED) {
console.log('User canceled the transaction');
} else if (responseCode === InAppPurchases.IAPResponseCode.DEFERRED) {
console.log('User does not have permissions to buy but requested parental approval (iOS only)');
} else {
console.warn(`Something went wrong with the purchase. Received errorCode ${responseCode}`);
}
});
this.restorePurchases();
} catch(err){
console.log("###### maybe no internet, or the app reloaded in dev mode", err);
this.premium = await this.getData("premium");
if(!this.premium) {
this.premium = "none";
this.setData("premium", "none");
}
this.event.emit("premium");
}
}
async restorePurchases(){
const history = await InAppPurchases.getPurchaseHistoryAsync(Platform.OS == "ios");
if (history.responseCode === InAppPurchases.IAPResponseCode.OK) {
// get to know if user is premium or npt.
let lifetime = history.results.filter(res => res.productId.includes("lifetime"))[0];
if(lifetime){
this.premium = "lifetime";
}else{
let orderedHistory = history.results.sort((a, b) => (a.purchaseTime > b.purchaseTime) ? 1 : -1);
if(orderedHistory[0]){
if(orderedHistory[0].productId.includes("monthly")){
this.premium = "monthly";
}else if(orderedHistory[0].productId.includes("yearly")){
this.premium = "yearly";
}else{
this.premium = "none";
}
}else{
this.premium = "none";
}
}
this.event.emit("premium");
await this.setData("premium", this.premium);
this.getPlans(); // async fetch the plans for later use.
}else{
console.log("#### Appstore status is not ok.");
this.premium = await this.getData("premium");
if(!this.premium) {
this.premium = "none";
this.setData("premium", "none");
}
this.event.emit("premium");
}
}
async getPlans(){
if(this.premiumPlans.length != 0){
return this.premiumPlans;
}else{
try {
const { responseCode, results } = await InAppPurchases.getProductsAsync([APP.planPrefix + "monthly", APP.planPrefix + "yearly", APP.planPrefix + "lifetime"]);
console.log("plans", results);
if (responseCode === InAppPurchases.IAPResponseCode.OK) {
this.premiumPlans = results;
}
}catch (err) {
console.log("Issues with fetching products: ", err);
}
return this.premiumPlans;
}
}
async purchasePremium(productId, oldProductId){
if(oldProductId && oldProductId != "none"){
await InAppPurchases.purchaseItemAsync(productId, oldProductId);
}else{
await InAppPurchases.purchaseItemAsync(productId);
}
this.avent("Premium", "PurchaseClick", productId);
}
async getCards(slug, force){
var url = ASSET_ENDPOINT + "packs/" + this.user.language + "/"+ slug +".json?v="+this.version;
if(this.cards[slug] && force == null){
console.log("pulling from ram", "cardsFor", slug);
return this.cards[slug];
}else{
let cardsResponse;
try {
cardsResponse = await fetch(url, {cache: "no-cache"})
.then(res => res.json());
this.setData("cards:"+slug, JSON.stringify(cardsResponse));
} catch(error){
console.log("Offline, Falling back to cached cardData!", error, slug);
let cardsResponseString = await this.getData("cards:"+slug);
if(cardsResponseString){
cardsResponse = JSON.parse(cardsResponseString);
}
}
this.cards[slug] = cardsResponse;
return cardsResponse;
}
}
async getAllApps(){
var url = ASSET_ENDPOINT + "apps/metadata.json?v="+this.version;
let appsResponse = [];
try {
appsResponse = await fetch(url, {cache: "no-cache"})
.then(res => res.json());
appsResponse = appsResponse.apps;
appsResponse.map(app => {
app.tagline = app.tagline[this.user.language];
app.description = app.description[this.user.language];
return app;
})
} catch(error){
console.log("Offline, Falling back to cached cardData!", error);
}
return appsResponse;
}
getCardData(slug, pack){
console.log(slug, pack);
return this.cards[pack].filter(ramCard => ramCard.slug == slug)[0];
}
localeString(){
if(_DEVELOPMENT){
return _DEVLOCALE;
}else{
return Localization.locales.join("|");
}
}
async getIdentifier(){
if(_DEVELOPMENT && Platform.OS === 'android'){
return _DEVUSERIDENTIFIER;
}
return await this.getData("identifier");
}
t(UITextIdentifier, variableArray){
let lang = "en";
if(this.user){
lang = this.user.language ? this.user.language : Localization.locale.substr(0, 2);
}else{
lang = Localization.locale.substr(0, 2);
}
if(!uitext[lang + "_json"]){
lang = "en";
}
if(typeof variableArray == "string" || typeof variableArray == "number"){
let text = uitext[lang + "_json"][UITextIdentifier];
if(text) return text.replace("$1", variableArray);
return "UnSupportedIdentifier";
}else if(typeof variableArray == "array"){
let text = uitext[lang + "_json"][UITextIdentifier];
if(text){
variableArray.forEach((variable, i) => {
let variableIdentifier = `${i+1}`;
text = text.replace(variableIdentifier, variable);
});
return text;
}else{
return "UnSupportedIdentifier";
}
}else{
let text = uitext[lang + "_json"][UITextIdentifier];
if(text) return text;
return "UnSupportedIdentifier";
}
}
setData(key, data){
return storage.save({key, data});
}
async getData(key){
// returns promise
try {
return await storage.load({key});
} catch (error) {
return "";
}
}
}
const _api = new Api();
export default _api;