-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
utils.tsx
115 lines (103 loc) · 2.97 KB
/
utils.tsx
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
import {Platform} from 'react-native';
import rnTextSize from 'react-native-text-size';
import SInfo from 'react-native-sensitive-info';
import RNIap, {ProductPurchase, Subscription} from 'react-native-iap';
import {GAD_IOS, GAD_ANDROID} from '@env';
export const itemSkus: string[] | undefined = Platform.select({
android: ['premium'],
ios: ['premium'],
});
export const getLargestWidths = async (
arr: Array<Array<any>>,
): Promise<Array<number>> => {
// first remap array with indexed
const remappedArr: any[] = arr[0].map((_, idx: number) =>
arr.map(row => row[idx]),
);
// console.log('remapped', remappedArr);
//now find the highest length in the remapped array
const largestValues: string[] = remappedArr.map(item => {
// console.log('map ', item);
// user the reduce on each item and find the largest element and return its length
return item.reduce((acc: any, cur: any) => {
if (acc === null || cur === null) {
return acc;
}
return acc.toString().length >= cur.toString().length
? acc.toString()
: cur.toString();
}, '');
});
// console.log('largest values', largestValues);
const widths: Array<number> = await Promise.all(
largestValues.map(async (item: string) => {
const val = await rnTextSize.measure({text: item, width: 220});
return val.width + 18;
}),
);
return widths;
};
export const debounce = (callback: any, delay = 250) => {
let timeoutId: ReturnType<typeof setTimeout>;
return (...args: []) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
clearTimeout(timeoutId);
callback(...args);
}, delay);
};
};
export const getInterstitialId = (): string => {
const testAdId = 'ca-app-pub-3940256099942544/8691691433';
return Platform.select({
ios: GAD_IOS,
android: GAD_ANDROID,
default: testAdId,
});
};
// returns 75/50 true false
export const shouldShowAd = (): boolean => {
const rand: number = Math.floor(Math.random() * 5);
// console.log('rand', rand);
if (rand === 1) {
return true;
} else {
return false;
}
};
export const savePremium = async (): Promise<void> => {
try {
await SInfo.setItem('purchase', 'sql.premium', {});
} catch (err) {
console.log('Error in saving purchase', err);
}
};
export const restorePremium = async (): Promise<boolean> => {
try {
const restore: Array<ProductPurchase | Subscription> =
await RNIap.getAvailablePurchases();
// console.log('your item was', restore);
if (itemSkus && restore[0].productId === itemSkus[0]) {
savePremium();
return true;
} else {
return false;
}
} catch (error) {
console.log(error);
return false;
}
};
export const getIsPremium = async (): Promise<boolean> => {
try {
const res = await SInfo.getItem('purchase', {});
if (res === 'sql.premium') {
return true;
} else {
return false;
}
} catch (error) {
console.error(error);
return false;
}
};