-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDevice.native.ts
executable file
·39 lines (34 loc) · 1.17 KB
/
Device.native.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
import NetInfo from '@react-native-community/netinfo';
import {Share, Platform, NativeModules} from 'react-native';
import type {DeviceBase} from './Device.interface';
export class DeviceService implements DeviceBase {
async share({url, title}: {url?: string, title?: string}) {
if (!url) return false;
const result = await Share.share({url, title, message: url}, {
dialogTitle: title,
});
return result.action === Share.sharedAction;
}
async isOnline() {
const netinfo = await NetInfo.fetch();
return !!(netinfo.isConnected && netinfo.isInternetReachable);
}
suscribeOnline(update: (isOnline: boolean) => void) {
return NetInfo.addEventListener(e => () =>
update(!!(e.isConnected && e.isInternetReachable))
);
}
getLocale(): string {
switch (Platform.OS) {
case 'ios':
return ((NativeModules.SettingsManager.settings.AppleLocale
|| NativeModules.SettingsManager.settings.AppleLanguages[0])
?.split('-')?.shift()) || 'en';
case 'android':
return (NativeModules.I18nManager.localeIdentifier
?.split('_')?.shift()) || 'en';
default:
return 'en';
}
}
}