Hangry/app/containers/Menu/init.js

254 lines
7.1 KiB
JavaScript
Executable File

import React, { Component } from "react";
import {
Platform,
StyleSheet,
View,
SafeAreaView,
TouchableOpacity,
ImageBackground,
NetInfo,
Geolocation,
Image,
Text
} from "react-native";
import { observable, transaction } from "mobx";
import Icon2 from "react-native-vector-icons/dist/MaterialCommunityIcons";
import Permissions from "react-native-permissions";
import firebase from "react-native-firebase";
import { observer, inject } from "mobx-react/native";
import Size from "../../config/size";
import AsyncStorageHelper from "../../config/asyncStorageHelper";
import { width, height } from "../../config/screen";
import { scale, verticalScale, moderateScale } from "react-native-size-matters";
import debounce from "lodash.debounce";
import theme from '../../config/colors'
const size = new Size();
const asyncStorageHelper = new AsyncStorageHelper();
@inject(["menuStore"], ["userStore"])
@observer
class Init extends Component {
constructor(props) {
super(props);
this.store = this.props.menuStore;
this.userStore = this.props.userStore;
firebase.analytics().setAnalyticsCollectionEnabled(true);
}
static navigationOptions = {
gesturesEnabled: false
}
navigatieAction(page,Param) {
console.log(page)
this.props.navigation.navigate(page,Param);
}
debounceInput = debounce(() => {
this.init();
}, 100);
login() {
asyncStorageHelper.getData("userInfo", userInfo => {
if (userInfo != null) {
console.log(userInfo);
this.userStore.loginPost(userInfo, this, false);
}
});
}
notificationInit() {
firebase
.messaging()
.hasPermission()
.then(enabled => {
if (enabled) {
firebase
.messaging()
.getToken()
.then(notificationToken => {
console.log("LOG: ", notificationToken);
this.userStore.regUserNotificationTokenWithNoMid(notificationToken)
});
// user has permissions
} else {
firebase
.messaging()
.requestPermission()
.then(() => {
// alert("User Now Has Permission");
})
.catch(error => {
console.log(error);
// alert("Error", error);
// User has rejected permissions
});
}
});
this.notificationListener = firebase
.notifications()
.onNotification(notification => {
// Process your notification as required
const {
body,
data,
notificationId,
sound,
subtitle,
title
} = notification;
console.log("LOG: ", title, body, JSON.stringify(data));
});
}
getMenuItemFromInit(){
this.store.getMenuItem(this);
}
init() {
var testdate = new Date(2019, 2, 20, 11, 1, 0, 0)
console.log('testdate: '+ testdate)
console.log('utc: '+testdate.getUTCDate())
console.log('utc: '+testdate.getDate())
this.userStore.languageInit()
asyncStorageHelper.getData("userInfo", userInfo => {
console.log('test')
if (userInfo != null) {
//this.store.getMenuItem(this);
this.login();
// console.log('testing '+ this.store.pickupPointId)
asyncStorageHelper.getData("pickupPointId", pickupPointId => {
this.userStore.pickupPointId = pickupPointId
this.store.pickupPointId = pickupPointId
console.log('testing '+ this.store.pickupPointId)
this.userStore.pickupLoaction(this, true,pickupPointId);
})
} else {
this.notificationInit();
asyncStorageHelper.getData("pickupPointId", pickupPointId => {
console.log(pickupPointId)
if (pickupPointId != null) {
this.userStore.pickupPointId = pickupPointId
this.store.pickupPointId = pickupPointId
console.log('init pickupPointId: ' + pickupPointId)
this.userStore.pickupLoaction(this, true,pickupPointId);
//this.store.getMenuItem(this);
} else {
this.userStore.pickupLoaction(this, false,0);
}
});
}
});
}
getUserLocation() {
navigator.geolocation.getCurrentPosition(
position => {
this.userStore.userLocation = position;
console.log(position);
},
error => {
console.log(error);
},
{ enableHighAccuracy: false, timeout: 10000, maximumAge: 10000 }
);
}
async createNotificationListeners() {
/*
* Triggered when a particular notification has been received in foreground
* */
this.notificationListener = firebase
.notifications()
.onNotification(notification => {
const { title, body } = notification;
// this.showAlert(title, body);
});
/*
* If your app is in background, you can listen for when a notification is clicked / tapped / opened as follows:
* */
this.notificationOpenedListener = firebase
.notifications()
.onNotificationOpened(notificationOpen => {
const { title, body } = notificationOpen.notification;
// this.showAlert(title, body);
});
/*
* If your app is closed, you can check if it was opened by a notification being clicked / tapped / opened as follows:
* */
const notificationOpen = await firebase
.notifications()
.getInitialNotification();
if (notificationOpen) {
const { title, body } = notificationOpen.notification;
// this.showAlert(title, body);
}
/*
* Triggered for data only payload in foreground
* */
this.messageListener = firebase.messaging().onMessage(message => {
//process data message
console.log(JSON.stringify(message));
});
}
showAlert(title, body) {
Alert.alert(
title,
body,
[{ text: "OK", onPress: () => console.log("OK Pressed") }],
{ cancelable: false }
);
}
componentDidMount() {
NetInfo.isConnected.addEventListener("connectionChange", isConnected => {
console.log("Network status:" + (isConnected ? "online" : "offline"));
});
this.debounceInput();
this.createNotificationListeners();
Permissions.check("location", { type: "whenInUse" }).then(response => {
console.log(response);
});
// this._requestPermission();
console.log(this.userStore.logined);
}
_requestPermission = () => {
Permissions.request("location").then(response => {
// Returns once the user has chosen to 'allow' or to 'not allow' access
// Response is one of: 'authorized', 'denied', 'restricted', or 'undetermined'
// this.setState({ photoPermission: response })
console.log(response);
});
};
render() {
return (
<View style={{ flex: 1, alignItems: "center", justifyContent: "center",backgroundColor:theme.mainColor }}>
<Image
style={{ tintColor: "white",height:scale(70),width:scale(70) }}
source={require("../../images/appicon.png")}
resizeMode = {'contain'}
/>
<Text>
{this.store.errorMessage}
</Text>
</View>
);
}
}
export default Init;