提交 d6f3b251 编写于 作者: ItbGcthate's avatar ItbGcthate

304_change geolocation

上级 b9f198ba
...@@ -27,9 +27,8 @@ class TwoPlayerPKHome extends Component { ...@@ -27,9 +27,8 @@ class TwoPlayerPKHome extends Component {
const data = JSON.parse(msg.data); const data = JSON.parse(msg.data);
console.log(data); console.log(data);
if (false) { if (false) {
const opponent = null;
//this.props.navigation.navigate('双人PK结算'); //this.props.navigation.navigate('双人PK结算');
this.props.navigation.navigate('双人PK中', {opponent: opponent}); this.props.navigation.navigate('双人PK中');
this.setState({isStarted: false, isCanceling: false, opponent: null}); this.setState({isStarted: false, isCanceling: false, opponent: null});
} }
if (data.hasOwnProperty('type') && data.type === 'MATCH_CANCEL_SUCCESS') { if (data.hasOwnProperty('type') && data.type === 'MATCH_CANCEL_SUCCESS') {
......
...@@ -44,10 +44,7 @@ class TwoPlayerPKStudy extends Component { ...@@ -44,10 +44,7 @@ class TwoPlayerPKStudy extends Component {
}; };
componentDidMount() { componentDidMount() {
const { const {navigation} = this.props;
navigation,
route: {params},
} = this.props;
navigation.setOptions({ navigation.setOptions({
// 引入SettingButton组件至header的右边 // 引入SettingButton组件至header的右边
// 并把this.props传给SettingButton组件以实现Navigation的跳转 // 并把this.props传给SettingButton组件以实现Navigation的跳转
...@@ -59,9 +56,6 @@ class TwoPlayerPKStudy extends Component { ...@@ -59,9 +56,6 @@ class TwoPlayerPKStudy extends Component {
this.forceUpdate(); this.forceUpdate();
}); });
// 以下不是标语风格切换相关 // 以下不是标语风格切换相关
if (params.opponent !== undefined && params.opponent !== null) {
this.setState({opponent: params.opponent, isStarted: true});
}
this.startTimer(); this.startTimer();
} }
......
global.defaultRangeID = 'notLocated'; // default value of rangeID
global.acID = -1; // a acID of range PK global.acID = -1; // a acID of range PK
global.rangeID = global.defaultRangeID; // rangeID for school buildings
global.websocket = new WebSocket('ws://localhost:8080'); // an instance of websocket global.websocket = new WebSocket('ws://localhost:8080'); // an instance of websocket
global.component = {};
...@@ -68,6 +68,20 @@ const LocationTable = { ...@@ -68,6 +68,20 @@ const LocationTable = {
}, },
}; };
export const DefaultRangeID = global.defaultRangeID;
export class rangeID {
static get() {
return global.rangeID;
}
static set(input) {
global.rangeID = input;
}
static setByLocation(longitude, latitude) {
this.set(parseLocation(longitude, latitude));
}
}
export class acID { export class acID {
static get() { static get() {
return global.acID; return global.acID;
...@@ -77,6 +91,15 @@ export class acID { ...@@ -77,6 +91,15 @@ export class acID {
} }
} }
export class component {
static get() {
return global.component;
}
static set(input) {
global.component = input;
}
}
export class websocket { export class websocket {
static get() { static get() {
return global.websocket; return global.websocket;
...@@ -196,22 +219,22 @@ export class sno { ...@@ -196,22 +219,22 @@ export class sno {
} }
} }
export function isLegalRangeID(rangeID) { export function isLegalRangeID() {
return ( return (
LocationTable.hasOwnProperty(rangeID) && LocationTable.hasOwnProperty(rangeID.get()) &&
rangeID !== 'notInRange' && rangeID.get() !== 'notInRange' &&
rangeID !== 'notLocated' rangeID.get() !== 'notLocated'
); );
} }
export function parseRangeID(rangeID) { export function parseRangeID() {
// console.log('parseRangeID rangID: ' + rangeID); let rangeName;
if (LocationTable.hasOwnProperty(rangeID)) { if (LocationTable.hasOwnProperty(rangeID.get())) {
rangeID = LocationTable[rangeID].name; rangeName = LocationTable[rangeID.get()].name;
} else { } else {
rangeID = 'error'; rangeName = 'error';
} }
return rangeID; return rangeName;
} }
function getRadian(angle) { function getRadian(angle) {
...@@ -238,7 +261,7 @@ function getDistance(longitude1, latitude1, longitude2, latitude2) { ...@@ -238,7 +261,7 @@ function getDistance(longitude1, latitude1, longitude2, latitude2) {
return distance; return distance;
} }
export function parseLocation(longitude, latitude) { function parseLocation(longitude, latitude) {
// console.log('longitude: ' + longitude + ' latitude: ' + latitude); // console.log('longitude: ' + longitude + ' latitude: ' + latitude);
let range = { let range = {
ID: 'notInRange', ID: 'notInRange',
...@@ -260,7 +283,7 @@ export function parseLocation(longitude, latitude) { ...@@ -260,7 +283,7 @@ export function parseLocation(longitude, latitude) {
range.distance = distance; range.distance = distance;
} }
} }
// console.log(range.ID + ' ' + range.distance); console.log(range.ID + ' ' + range.distance);
return range.ID; return range.ID;
} }
......
...@@ -16,8 +16,9 @@ import Geolocation from '@react-native-community/geolocation'; ...@@ -16,8 +16,9 @@ import Geolocation from '@react-native-community/geolocation';
import { import {
parseRangeID, parseRangeID,
isLegalRangeID, isLegalRangeID,
parseLocation,
acID, acID,
rangeID,
DefaultRangeID,
} from '../MyUtilities'; } from '../MyUtilities';
const {width, height} = Dimensions.get('window'); const {width, height} = Dimensions.get('window');
...@@ -33,18 +34,22 @@ class RangePKHome extends Component { ...@@ -33,18 +34,22 @@ class RangePKHome extends Component {
super(); super();
} }
componentDidMount() {
rangeID.set(DefaultRangeID);
}
componentWillUnmount() { componentWillUnmount() {
// this.cancelLocating(); // this.cancelLocating();
// 卸载异步操作设置状态 // 卸载异步操作设置状态
this.setState = (state, callback) => { this.setState = (state, callback) => {
return; return;
}; };
rangeID.set(DefaultRangeID);
} }
state = { state = {
isStarted: false, isStarted: false,
isLocated: false, isLocated: false,
rangeID: 'notLocated', // 初始rangeID
longitude: undefined, longitude: undefined,
latitude: undefined, latitude: undefined,
}; };
...@@ -62,25 +67,21 @@ class RangePKHome extends Component { ...@@ -62,25 +67,21 @@ class RangePKHome extends Component {
location => { location => {
const longitude = location.coords.longitude; //经度 const longitude = location.coords.longitude; //经度
const latitude = location.coords.latitude; //纬度 const latitude = location.coords.latitude; //纬度
const rangeID = parseLocation(longitude, latitude); rangeID.setByLocation(longitude, latitude);
// this.setState({isStarted: false, isLocated: true, rangeID: rangID});
this.setState({ this.setState({
isStarted: false, isStarted: false,
isLocated: true, isLocated: true,
rangeID: rangeID,
longitude: longitude, //经度
latitude: latitude, //纬度
}); });
}, },
error => { error => {
reject(error); reject(error);
this.setState({isStarted: false}); this.setState({isStarted: false});
if (error.code == 2) { if (error.code === 2) {
ToastAndroid.show( ToastAndroid.show(
'定位失败,请查看手机是否开启GPS定位服务', '定位失败,请查看手机是否开启GPS定位服务',
ToastAndroid.SHORT, ToastAndroid.SHORT,
); );
} else if (error.code == 3) { } else if (error.code === 3) {
ToastAndroid.show( ToastAndroid.show(
'定位超时,请尝试重新获取定位', '定位超时,请尝试重新获取定位',
ToastAndroid.SHORT, ToastAndroid.SHORT,
...@@ -100,12 +101,12 @@ class RangePKHome extends Component { ...@@ -100,12 +101,12 @@ class RangePKHome extends Component {
geoLocate = async () => { geoLocate = async () => {
if (Platform.OS === 'ios') { if (Platform.OS === 'ios') {
await this.getPositions().catch(error => {}); await this.getPositions().catch();
} else { } else {
const permissions = [PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION]; const permissions = [PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION];
const granted = await PermissionsAndroid.requestMultiple(permissions); const granted = await PermissionsAndroid.requestMultiple(permissions);
if (granted['android.permission.ACCESS_FINE_LOCATION'] === 'granted') { if (granted['android.permission.ACCESS_FINE_LOCATION'] === 'granted') {
await this.getPositions().catch(error => {}); await this.getPositions().catch();
} else { } else {
ToastAndroid.show('定位权限被禁止', ToastAndroid.SHORT); ToastAndroid.show('定位权限被禁止', ToastAndroid.SHORT);
} }
...@@ -116,7 +117,7 @@ class RangePKHome extends Component { ...@@ -116,7 +117,7 @@ class RangePKHome extends Component {
// // eslint-disable-next-line no-undef // // eslint-disable-next-line no-undef
// this.abortController = new AbortController(); // this.abortController = new AbortController();
this.setState({isStarted: true}); this.setState({isStarted: true});
this.geoLocate().then(r => {}); this.geoLocate().then();
}; };
// cancelLocating = () => { // cancelLocating = () => {
...@@ -129,16 +130,16 @@ class RangePKHome extends Component { ...@@ -129,16 +130,16 @@ class RangePKHome extends Component {
startStudy = () => { startStudy = () => {
//this.props.navigation.navigate(''); //this.props.navigation.navigate('');
this.props.navigation.navigate('区域PK中', {rangeID: this.state.rangeID}); this.props.navigation.navigate('区域PK中');
}; };
render() { render() {
const {isStarted, isLocated, rangeID} = this.state; const {isStarted, isLocated} = this.state;
return ( return (
<View style={styles.container}> <View style={styles.container}>
<Image source={isStarted ? Locating : Located} style={styles.image} /> <Image source={isStarted ? Locating : Located} style={styles.image} />
<Text numberOfLines={1} style={styles.text}> <Text numberOfLines={1} style={styles.text}>
当前所在区域{parseRangeID(rangeID)} 当前所在区域{parseRangeID()}
</Text> </Text>
{isStarted ? ( {isStarted ? (
<MyButton <MyButton
...@@ -155,7 +156,7 @@ class RangePKHome extends Component { ...@@ -155,7 +156,7 @@ class RangePKHome extends Component {
/> />
)} )}
<MyButton <MyButton
disabled={!isLegalRangeID(rangeID)} disabled={!isLegalRangeID()}
containerStyle={styles.myButton.container} containerStyle={styles.myButton.container}
title="开始自习" title="开始自习"
onPress={this.startStudy} onPress={this.startStudy}
......
...@@ -12,7 +12,14 @@ import MyTitle from '../MyTitle'; ...@@ -12,7 +12,14 @@ import MyTitle from '../MyTitle';
import SettingButton from '../SettingButton'; import SettingButton from '../SettingButton';
import MyBanner from '../MyBanner'; import MyBanner from '../MyBanner';
import RangePKState from '../RangePKState'; import RangePKState from '../RangePKState';
import {parseMinute, parseRangeID, parseTime, acID} from '../MyUtilities'; import {
parseMinute,
parseRangeID,
parseTime,
acID,
rangeID,
DefaultRangeID,
} from '../MyUtilities';
const {width, height} = Dimensions.get('window'); const {width, height} = Dimensions.get('window');
/** /**
...@@ -34,7 +41,6 @@ class RangePKStudy extends Component { ...@@ -34,7 +41,6 @@ class RangePKStudy extends Component {
} }
state = { state = {
rangeID: undefined,
user: { user: {
name: 'username', name: 'username',
rank: 'error', rank: 'error',
...@@ -57,10 +63,7 @@ class RangePKStudy extends Component { ...@@ -57,10 +63,7 @@ class RangePKStudy extends Component {
}; };
componentDidMount() { componentDidMount() {
const { const {navigation} = this.props;
navigation,
route: {params},
} = this.props;
navigation.setOptions({ navigation.setOptions({
// 引入SettingButton组件至header的右边 // 引入SettingButton组件至header的右边
// 并把this.props传给SettingButton组件以实现Navigation的跳转 // 并把this.props传给SettingButton组件以实现Navigation的跳转
...@@ -72,9 +75,6 @@ class RangePKStudy extends Component { ...@@ -72,9 +75,6 @@ class RangePKStudy extends Component {
this.forceUpdate(); this.forceUpdate();
}); });
// 以下不是标语风格切换相关 // 以下不是标语风格切换相关
if (params.rangeID !== undefined) {
this.setState({rangeID: params.rangeID});
}
this.startTimer(); this.startTimer();
} }
...@@ -140,8 +140,10 @@ class RangePKStudy extends Component { ...@@ -140,8 +140,10 @@ class RangePKStudy extends Component {
numberTop: parseInt(this.state.timeInTheMatch / 60, 10), numberTop: parseInt(this.state.timeInTheMatch / 60, 10),
numberCentre: 999, numberCentre: 999,
numberBottom: 9999, numberBottom: 9999,
range: parseRangeID(this.state.rangeID), range: parseRangeID(),
}); });
rangeID.set(DefaultRangeID);
// console.log(rangeID.get());
}, },
}, },
]); ]);
...@@ -184,7 +186,7 @@ class RangePKStudy extends Component { ...@@ -184,7 +186,7 @@ class RangePKStudy extends Component {
}, },
}} }}
/> />
<RangePKState range={parseRangeID(this.state.rangeID)} /> <RangePKState range={parseRangeID()} />
<MyStatistics <MyStatistics
containerStyle={styles.containerOnRight} containerStyle={styles.containerOnRight}
insideStyle={styles.containerInRightColumn} insideStyle={styles.containerInRightColumn}
...@@ -192,7 +194,7 @@ class RangePKStudy extends Component { ...@@ -192,7 +194,7 @@ class RangePKStudy extends Component {
onPress={this.openRankingList} onPress={this.openRankingList}
text={{ text={{
first: { first: {
top: parseRangeID(this.state.rangeID) + '区域', top: parseRangeID() + '区域',
bottom: 'Top 3', bottom: 'Top 3',
}, },
second: { second: {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册