-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseCellular.ts
More file actions
77 lines (71 loc) · 1.87 KB
/
useCellular.ts
File metadata and controls
77 lines (71 loc) · 1.87 KB
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
import { useState, useEffect } from "react";
import * as Cellular from "expo-cellular";
import {
allowsVoipAsync,
getCarrierNameAsync,
getCellularGenerationAsync,
getIsoCountryCodeAsync,
getMobileCountryCodeAsync,
getMobileNetworkCodeAsync,
getPermissionsAsync,
requestPermissionsAsync,
} from "@/utils/cellular.utils";
export type CellularInfo = {
allowsVoip: boolean | null;
carrier: string | null;
cellularGeneration: Cellular.CellularGeneration;
isoCountryCode: string | null;
mobileCountryCode: string | null;
mobileNetworkCode: string | null;
permission: any | null;
};
export function useCeullar() {
const [cellularInfo, setCellularInfo] = useState<CellularInfo>({
allowsVoip: null,
carrier: null,
cellularGeneration: Cellular.CellularGeneration.UNKNOWN,
isoCountryCode: null,
mobileCountryCode: null,
mobileNetworkCode: null,
permission: null,
});
const loadCellularInfo = async () => {
try {
const [
allowsVoip,
carrier,
cellularGeneration,
isoCountryCode,
mobileCountryCode,
mobileNetworkCode,
permission,
] = await Promise.all([
allowsVoipAsync(),
getCarrierNameAsync(),
getCellularGenerationAsync(),
getIsoCountryCodeAsync(),
getMobileCountryCodeAsync(),
getMobileNetworkCodeAsync(),
getPermissionsAsync(),
]);
setCellularInfo({
allowsVoip,
carrier,
cellularGeneration,
isoCountryCode,
mobileCountryCode,
mobileNetworkCode,
permission,
});
} catch (error) {
console.error("Error loading cellular info:", error);
}
};
useEffect(() => {
loadCellularInfo();
}, []);
const refreshCellularInfo = () => {
loadCellularInfo();
};
return { cellularInfo, refreshCellularInfo, requestPermissionsAsync };
}