94 lines
2.6 KiB
JavaScript
94 lines
2.6 KiB
JavaScript
import React, { useEffect, useState } from 'react';
|
|
import { SafeAreaView, Text } from 'react-native';
|
|
// material
|
|
import { Button } from 'react-native-paper';
|
|
// redux
|
|
import { useDeviceList, useDeviceActive } from './provider/device.helper';
|
|
// core
|
|
import { Storage } from './core/storage';
|
|
import { useSMSRead } from './core/sms.read';
|
|
import { useSMSListener } from './core/sms.listen';
|
|
import { StartForegroundService } from './core/foreground';
|
|
// import { StartForegroundService } from './core/foreground';
|
|
// import { NotificationOnReciveSMS } from './core/notification';
|
|
// view
|
|
import { DialogDevice } from './views/Dialog.Device';
|
|
|
|
export const App = () => {
|
|
const [devices, setDevices] = useDeviceList();
|
|
const [deviceActive, setDeviceActive] = useDeviceActive();
|
|
|
|
const [visible, setVisable] = useState(false);
|
|
|
|
useEffect(() => {
|
|
onInit();
|
|
onForground();
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, []);
|
|
|
|
const onInit = async () => {
|
|
try {
|
|
// await StartForegroundService();
|
|
// load data from storage
|
|
const DeviceList = await Storage.ListDevice();
|
|
// set devices to redux
|
|
setDevices(DeviceList);
|
|
// load data from storage
|
|
const DeviceActive = await Storage.GetDeviceActive();
|
|
// set device active to redux
|
|
setDeviceActive(DeviceActive);
|
|
// read SMS
|
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
const SMSs = await useSMSRead({ phone: '09102802725' });
|
|
console.log(SMSs.count);
|
|
} catch (error) {
|
|
console.error(error);
|
|
}
|
|
};
|
|
|
|
const onForground = async () => {
|
|
console.log('forground/1');
|
|
|
|
StartForegroundService();
|
|
|
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
useSMSListener((message) => {
|
|
console.log(message.originatingAddress);
|
|
});
|
|
};
|
|
|
|
/**
|
|
* On Dialog device form close
|
|
* @param {null | {name: string, phone: string}} value return value from dialog
|
|
*/
|
|
const onDialogDeviceClose = (value) => {
|
|
if (value) {
|
|
const _devices = [...devices];
|
|
_devices.push(value);
|
|
setDevices(_devices);
|
|
Storage.SetDevice(_devices);
|
|
if (deviceActive === -1) {
|
|
setDeviceActive(0);
|
|
Storage.SetDeviceActive(0);
|
|
}
|
|
}
|
|
|
|
setVisable(false);
|
|
};
|
|
|
|
return (
|
|
<SafeAreaView>
|
|
<Text>{deviceActive}</Text>
|
|
{devices.map((item, index) => (
|
|
<Text key={index}>
|
|
{item.name} - {item.phone}
|
|
</Text>
|
|
))}
|
|
|
|
<Button onPress={() => setVisable(!visible)}>افزودن دستگاه جدید</Button>
|
|
|
|
<DialogDevice visible={visible} onClose={onDialogDeviceClose} />
|
|
</SafeAreaView>
|
|
);
|
|
};
|