@ -1,78 +1,36 @@
import Navigation from './Navigation' ;
import { StyleSheet, SafeAreaView , AsyncStorage } from 'react-native' ;
import { AsyncStorage } from 'react-native' ;
import { SafeAreaProvider } from 'react-native-safe-area-context' ;
import StartNavigation from './StartNavigation' ;
import { Provider , useDispatch , useSelector } from 'react-redux' ;
import store from '../redux/store' ;
import { useCallback , useEffect , useState } from 'react' ;
import { useDispatch , useSelector } from 'react-redux' ;
import { useEffect , useState } from 'react' ;
import * as SplashScreen from 'expo-splash-screen' ;
import { View } from 'react-native' ;
import { ChangeMode , getRefreshToken } from '../redux/thunk/authThunk' ;
import * as Location from 'expo-location' ;
import { theService } from '../App' ;
import { getCurrentUserMusic } from '../redux/thunk/spotThunk' ;
import axios from 'axios' ;
import Music from '../Model/Music' ;
// const LOCATION_TASK_NAME = 'flad-background-location-task';
export default function AuthNavigation() {
//@ts-ignore
const tokenProcesed : boolean = useSelector ( state = > state . userReducer . loading ) ;
// //@ts-ignore
// const appIsReady: boolean = useSelector(state => state.userReducer.loading);
//@ts-ignore
const isLogin : boolean = useSelector ( state = > state . userReducer . isLogedIn ) ;
//@ts-ignore
const currentMusic : Music = useSelector ( state = > state . appReducer . userCurrentMusic ) ;
const tokenSend : string = useSelector ( state = > state . userReducer . userFladToken ) ;
// const userToken : string = useSelector(state => state.userReducer.userFladToken);
const [ appIsReady , setAppIsReady ] = useState ( false ) ;
const dispatch = useDispatch ( ) ;
const [ location , setLocation ] = useState < Location.LocationObject > ( ) ;
const [ errorMsg , setErrorMsg ] = useState ( '' ) ;
// // seems background task but not optimized
// TaskManager.defineTask(LOCATION_TASK_NAME, async ({ data, error }) => {
// if (error) {
// console.log(error);
// return;
// }
// if (data) {
// const { locations } = data;
// // send location updates to server
// console.log(locations);
// }
// });
// const startLocationUpdates = async () => {
// try {
// await Location.startLocationUpdatesAsync(LOCATION_TASK_NAME, {
// accuracy: Location.Accuracy.Balanced,
// timeInterval: 5000, // send location updates every 5 seconds
// foregroundService: {
// notificationTitle: 'Location tracking',
// notificationBody: 'Location tracking is active.',
// },
// });
// } catch (error) {
// console.log(error);
// }
// };
// useEffect(() => {
// startLocationUpdates();
// return () => {
// Location.stopLocationUpdatesAsync(LOCATION_TASK_NAME);
// };
// }, []);
const [ setErrorMsg ] = useState ( '' ) ;
async function prepare() {
//@ts-ignore
await dispatch ( getRefreshToken ( ) )
if ( tokenProcesed && appIsReady ) {
await SplashScreen . hideAsync ( ) ;
} // await SplashScreen.hideAsync();
}
}
async function ChangeDarkMode() {
@ -91,109 +49,30 @@ export default function AuthNavigation() {
ChangeDarkMode ( ) ;
prepare ( ) ;
} , [ appIsReady , tokenProcesed ] ) ;
// const onStackRootView = useCallback(async () => {
// }, [appIsReady]);
useEffect ( ( ) = > {
const sendLocationUpdate = async ( ) = > {
try {
//@ts-ignore
await dispatch ( getCurrentUserMusic ( theService ) )
let { status } = await Location . requestForegroundPermissionsAsync ( ) ;
if ( status == 'granted' ) {
console . log ( appIsReady )
if ( true ) { // should app is ready
// see deeper the documentation
// const [status, requestPermission] = Location.useBackgroundPermissions();
const locationresp = await Location . getCurrentPositionAsync ( { } ) ;
setLocation ( locationresp ) ;
// send location to server
console . log ( locationresp ) ;
// if(locationresp !=location ){
console . log ( location ) ;
const body : Record < string , string | boolean | number | ( string | boolean | number ) [ ] > = {
longitude : locationresp.coords.longitude ,
latitude : locationresp.coords.latitude ,
currentMusic : currentMusic.id
}
console . log ( tokenSend )
console . log ( '((((((((((((((((((((()))))))))))))))))))))================================###############' ) ;
console . log ( '((((((((((((((((((((()))))))))))))))))))))================================###############' ) ;
console . log ( '((((((((((((((((((((())))))))))))))))))))================================###############' ) ;
console . log ( location ) ;
console . log ( '(((((((((((((((((((()))))))))))))))))))))================================###############' ) ; console . log ( '((((((((((((((((((((()))))))))))))))))))))================================###############' ) ;
console . log ( '((((((((((((((((((((()))))))))))))))))))))================================###############' ) ;
console . log ( '((((((((((((((((((((()))))))))))))))))))))================================###############' ) ;
console . log ( '**********************************************************************************' )
try {
const resp = await axios ( {
url : 'https://flad-api-production.up.railway.app/api/users/nextTo' ,
method : 'GET' ,
headers : {
Authorization : ` Bearer ${ tokenSend } ` ,
} ,
params : {
longitude : locationresp.coords.longitude ,
latitude : locationresp.coords.latitude ,
currentMusic : currentMusic.id
}
} ) ;
console . log ( '(((((((((((((((((((()))))))))))))))))))))================================###############' ) ;
console . log ( '((((((((((((((((((((())))))))))))))))))))================================###############' ) ;
console . log ( resp . status ) ;
console . log ( '**********************************************************************************' )
console . log ( '**********************************************************************************' )
console . log ( '**********************************************************************************' )
console . log ( '((((((((((((((((((((()))))))))))))))))))))================================###############' ) ;
console . log ( '((((((((((((((((((((()))))))))))))))))))))================================###############' ) ;
console . log ( resp ) ;
console . log ( resp . data ) ;
console . log ( '((((((((((((((((((((()))))))))))))))))))))================================###############' ) ;
console . log ( '((((((((((((((((((((()))))))))))))))))))))================================###############' ) ;
console . log ( '((((((((((((((((((((()))))))))))))))))))))================================###############' ) ;
console . log ( '=========((((((((((((((((()))))))))))))))))===============================###############' ) ;
} catch ( error ) {
console . log ( '*********************************************************************************************' ) ;
console . log ( '*********************************************************************************************' ) ;
console . log ( '********************************************************************************************' ) ;
console . log ( '*********************************************************************************************' ) ;
console . log ( '*********************************************************************************************' ) ;
console . log ( '*********************************************************************************************' ) ;
console . log ( '*********************************************************************************************' ) ;
console . log ( error ) ;
console . log ( error . message ) ;
console . log ( '*********************************************************************************************' ) ;
console . log ( '*********************************************************************************************' ) ;
console . log ( '*********************************************************************************************' ) ;
console . log ( '*********************************************************************************************' ) ;
console . log ( '*********************************************************************************************' ) ;
console . log ( '*********************************************************************************************' ) ;
console . log ( '*********************************************************************************************' ) ;
}
// }
console . log ( locationresp ) ;
console . log ( currentMusic ) ;
console . log ( 'Sending location update ================================###############' ) ;
console . log ( location ) ;
const body : Record < string , string | boolean | number | ( string | boolean | number ) [ ] > = {
longitude : locationresp.coords.longitude ,
latitude : locationresp.coords.latitude ,
currentMusic : currentMusic.id
}
}
}
else {
console . log ( ` Sending location update with error ${ status } ================================############### ` ) ;
else {
setErrorMsg ( 'Permission to access location was denied' ) ;
return ;
}
@ -201,7 +80,7 @@ export default function AuthNavigation() {
console . log ( error ) ;
}
} ;
const interval = setInterval ( sendLocationUpdate , 5000 ) ; // send location updates every 5 seconds
const interval = setInterval ( sendLocationUpdate , 5000 ) ;
return ( ) = > {
clearInterval ( interval ) ;
} ;
@ -210,18 +89,9 @@ export default function AuthNavigation() {
return null ;
}
// console.log(userToken, "k9 AuthNav")
return (
< SafeAreaProvider onLayout = { ( ) = > setAppIsReady ( true ) } >
{ isLogin ? (
/* {userToken != null ? ( */
// should set the reference to the function in Navigation to realy perform an on ready
// test purpose
< Navigation / >
) :
< StartNavigation / >