diff --git a/App.tsx b/App.tsx index 53c4e1f..7672ecd 100644 --- a/App.tsx +++ b/App.tsx @@ -8,29 +8,28 @@ * @format */ -import React from 'react'; +import React, {useEffect} from 'react'; import { SafeAreaView, - ScrollView, StatusBar, StyleSheet, Text, View, Switch, Button, - Alert, PermissionsAndroid, // ToastAndroid, - NativeModules, + // NativeModules, } from 'react-native'; -import BackgroundFetch from 'react-native-background-fetch'; import Geolocation, { GeolocationError, - GeolocationResponse, + // GeolocationResponse, } from '@react-native-community/geolocation'; -const {ForegroundHeadlessModule} = NativeModules; +import NativeTaskRunner from './specs/NativeTaskRunner'; + +// const {ForegroundHeadlessModule} = NativeModules; const Colors = { gold: '#fedd1e', @@ -42,14 +41,37 @@ const Colors = { }; /// Util class for handling fetch-event peristence in AsyncStorage. -import Event from './src/Event'; +// import Event from './src/Event'; import AsyncStorage from '@react-native-async-storage/async-storage'; +import * as Location from 'expo-location'; Geolocation.setRNConfiguration({ skipPermissionRequests: true, locationProvider: 'auto', }); +/* +function geoLocationPromise(): Promise { + return new Promise((resolve, reject) => { + Geolocation.getCurrentPosition( + info => { + console.log(info); + resolve(info); + }, + error => { + console.log(error); + reject(error); + }, + { + timeout: 20000, + maximumAge: 0, + // enableHighAccuracy: true, + }, + ); + }); +} + +*/ const requestPermissions = async () => { try { const granted = await PermissionsAndroid.request( @@ -85,219 +107,32 @@ const requestPermissions = async () => { } }; -function geoLocationPromise(): Promise { - return new Promise((resolve, reject) => { - Geolocation.getCurrentPosition( - info => { - console.log(info); - resolve(info); - }, - error => { - console.log(error); - reject(error); - }, - { - timeout: 20000, - maximumAge: 0, - // enableHighAccuracy: true, - }, - ); - }); -} - const App = () => { const [enabled, setEnabled] = React.useState(false); - const [status, setStatus] = React.useState(-1); - const [events, setEvents] = React.useState([]); - React.useEffect(() => { - async function asyncTask() { - await requestPermissions(); - initBackgroundFetch(); - loadEvents(); + useEffect(() => { + async function getCurrentLocation() { + let location = await Location.getLastKnownPositionAsync({}); + console.log(location); } - - asyncTask(); - }, []); - - /// Configure BackgroundFetch. - /// - const initBackgroundFetch = async () => { - const status: number = await BackgroundFetch.configure( - { - minimumFetchInterval: 15, // <-- minutes (15 is minimum allowed) - stopOnTerminate: false, - enableHeadless: true, - startOnBoot: false, - // Android options - forceAlarmManager: false, // <-- Set true to bypass JobScheduler. - requiredNetworkType: BackgroundFetch.NETWORK_TYPE_NONE, // Default - requiresCharging: false, // Default - requiresDeviceIdle: false, // Default - requiresBatteryNotLow: false, // Default - requiresStorageNotLow: false, // Default - }, - async (taskId: string) => { - console.log('[BackgroundFetch] taskId', taskId); - - if (taskId === 'react-native-background-fetch') { - // console.log('[BackgroundFetch] taskId', taskId); - const event = await Event.create(taskId, false); - // Update state. - setEvents(prev => [...prev, event]); + async function xy() { + try { + await requestPermissions(); + const x = await AsyncStorage.getItem('locationtask'); + if (x === '1') { + setEnabled(true); + } else { + setEnabled(false); } - if (taskId === 'com.transistorsoft.customtask') { - // Geolocation.getCurrentPosition( - // async info => { - // console.log(info); - // const event = await Event.create(taskId, false, `lat-${info.coords.latitude};long-${info.coords.longitude}`); - // }, - // err => console.log(err), - // { - // timeout: 10000, - // maximumAge: 10000, - // enableHighAccuracy: false, - // }, - // ); - try { - const locInfo = (await geoLocationPromise()) as GeolocationResponse; - const event = await Event.create( - taskId, - false, - `lat-${locInfo.coords.latitude};long-${locInfo.coords.longitude}`, - ); - // Update state. - setEvents(prev => [...prev, event]); - } catch (error) { - const g = error as GeolocationError; - if (g.message) { - const event = await Event.create(taskId, false, `${g.message}`); - // Update state. - setEvents(prev => [...prev, event]); - } else { - const event = await Event.create(taskId, false); - // Update state. - setEvents(prev => [...prev, event]); - } - } - // console.log('Running [customtask] here!'); - } - - // Finish. - BackgroundFetch.finish(taskId); - }, - (taskId: string) => { - // Oh No! Our task took too long to complete and the OS has signalled - // that this task must be finished immediately. - console.log('[Fetch] TIMEOUT taskId:', taskId); - BackgroundFetch.finish(taskId); - }, - ); - setStatus(status); - setEnabled(true); - }; - - /// Load persisted events from AsyncStorage. - /// - const loadEvents = () => { - Event.all() - .then(data => { - setEvents(data as Event[]); - }) - .catch(error => { - Alert.alert('Error', 'Failed to load data from AsyncStorage: ' + error); - }); - }; - - /// Toggle BackgroundFetch ON/OFF - /// - const onClickToggleEnabled = (value: boolean) => { - setEnabled(value); - - if (value) { - BackgroundFetch.start(); - } else { - BackgroundFetch.stop(); - } - }; - - /// [Status] button handler. - /// - const onClickStatus = () => { - BackgroundFetch.status().then((status: number) => { - let statusConst = ''; - switch (status) { - case BackgroundFetch.STATUS_AVAILABLE: - statusConst = 'STATUS_AVAILABLE'; - break; - case BackgroundFetch.STATUS_DENIED: - statusConst = 'STATUS_DENIED'; - break; - case BackgroundFetch.STATUS_RESTRICTED: - statusConst = 'STATUS_RESTRICTED'; - break; + getCurrentLocation(); + } catch (error) { + console.log('Something went wrong', error); } - Alert.alert('BackgroundFetch.status()', `${statusConst} (${status})`); - }); - }; - - /// [scheduleTask] button handler. - /// Schedules a custom-task to fire in 5000ms - /// - const onClickScheduleTask = () => { - const delay = 600000; - BackgroundFetch.scheduleTask({ - taskId: 'com.transistorsoft.customtask', - stopOnTerminate: false, - enableHeadless: true, - delay: delay, - forceAlarmManager: false, - periodic: true, - }) - .then(() => { - Alert.alert( - 'scheduleTask', - 'Scheduled task with delay: ' + delay + 'ms', - ); - }) - .catch(error => { - Alert.alert('scheduleTask ERROR', error); - }); - }; - - /// Clear the Events list. - /// - const onClickClear = () => { - Event.destroyAll(); - setEvents([]); - }; - - /// Fetch events renderer. - /// - const renderEvents = () => { - if (!events.length) { - return ( - - Waiting for BackgroundFetch events... - - ); } - return events - .slice() - .reverse() - .map(event => ( - - - - {event.taskId} {event.isHeadless ? '[Headless]' : ''} - - - Remark - {event.location} - {event.timestamp} - - )); - }; + + xy(); + }, []); async function onLocPress(): Promise { try { @@ -306,7 +141,18 @@ const App = () => { // `lat-${locInfo.coords.latitude};long-${locInfo.coords.longitude}`, // 5000, // ); - ForegroundHeadlessModule.startService(); + if (enabled) { + return; + } + + try { + // ForegroundHeadlessModule.startService(); + NativeTaskRunner.startService(); + setEnabled(true); + AsyncStorage.setItem('locationtask', '1'); + } catch (error) { + console.log(error); + } console.log('Did it run?'); } catch (error) { const g = error as GeolocationError; @@ -319,19 +165,14 @@ const App = () => { } function onLocStop() { - ForegroundHeadlessModule.stopService(); - AsyncStorage.getItem('watchId', (err, result) => { - if (err) { - console.log('Couldnt get WatchID', err); - return; - } - - console.log(result); - - if (result) { - Geolocation.clearWatch(+result); - } - }); + try { + // ForegroundHeadlessModule.stopService(); + NativeTaskRunner.stopService(); + setEnabled(false); + AsyncStorage.setItem('locationtask', '0'); + } catch (error) { + console.log(error); + } } return ( @@ -342,19 +183,9 @@ const App = () => { BGFetch Demo