Improved homepage

This commit is contained in:
Keannu Bernasol 2023-07-19 17:20:29 +08:00
parent 258435cbdd
commit de97dca9d3
3 changed files with 61 additions and 50 deletions

View file

@ -1,7 +1,7 @@
import * as React from "react"; import * as React from "react";
import { Pressable, GestureResponderEvent } from "react-native"; import { Pressable, GestureResponderEvent } from "react-native";
import styles from "../../styles"; import styles from "../../styles";
import { colors } from "../../styles";
export interface props { export interface props {
children: React.ReactNode; children: React.ReactNode;
onPress: (event: GestureResponderEvent) => void; onPress: (event: GestureResponderEvent) => void;
@ -10,6 +10,9 @@ export interface props {
} }
export default function Button({ disabled = false, ...props }: props) { export default function Button({ disabled = false, ...props }: props) {
if (!props.color) {
props.color = colors.secondary_3;
}
return ( return (
<Pressable <Pressable
disabled={disabled} disabled={disabled}

View file

@ -1,4 +1,4 @@
import styles from "../../styles"; import styles, { Viewport } from "../../styles";
import { View, Text } from "react-native"; import { View, Text } from "react-native";
import { useSelector } from "react-redux"; import { useSelector } from "react-redux";
import { RootState } from "../../features/redux/Store/Store"; import { RootState } from "../../features/redux/Store/Store";
@ -9,7 +9,6 @@ import * as Location from "expo-location";
import GetDistance from "../../components/GetDistance/GetDistance"; import GetDistance from "../../components/GetDistance/GetDistance";
import Button from "../../components/Button/Button"; import Button from "../../components/Button/Button";
import { colors } from "../../styles"; import { colors } from "../../styles";
import { startActivityAsync, ActivityAction } from "expo-intent-launcher";
type LocationType = Location.LocationObject; type LocationType = Location.LocationObject;
export default function Home() { export default function Home() {
@ -18,47 +17,59 @@ export default function Home() {
const [feedback, setFeedback] = useState( const [feedback, setFeedback] = useState(
"To continue, please allow Stud-E permission to location services" "To continue, please allow Stud-E permission to location services"
); );
async function requestLocation() {
let { status } = await Location.requestForegroundPermissionsAsync();
if (status === "granted") {
getLocation();
return;
} else if (status === "denied") {
setFeedback("Stud-E requires location services to function");
setTimeout(() => {
startActivityAsync(ActivityAction.LOCATION_SOURCE_SETTINGS);
}, 3000);
console.log("Location Permission denied");
}
}
async function getLocation() {
let location = await Location.getCurrentPositionAsync({});
setLocation(location);
let dist = GetDistance(
location.coords.latitude,
location.coords.longitude,
8.4857,
124.6565
);
setDist(Math.round(dist));
}
useEffect(() => {
requestLocation();
}, []);
const ustpCoords = { const ustpCoords = {
latitude: 8.4857, latitude: 8.4857,
longitude: 124.6565, longitude: 124.6565,
latitudeDelta: 0.000235, latitudeDelta: 0.000235,
longitudeDelta: 0.000067, longitudeDelta: 0.000067,
}; };
async function requestLocation() {
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== "granted") {
setFeedback(
"Permission to access location was denied. Please allow permission"
);
return;
}
if (status == "granted") {
let location = await Location.getCurrentPositionAsync({});
if (location) {
setLocation(location);
getDistance(location);
}
}
}
useEffect(() => {
requestLocation();
}, [location]);
async function getDistance(location: LocationType) {
let dist = GetDistance(
location.coords.latitude,
location.coords.longitude,
8.4857, // LatitudeDelta
124.6565 // LongitudeDelta
);
setDist(Math.round(dist));
}
function CustomMap() { function CustomMap() {
if (dist !== null && location !== null) { if (dist && location) {
if (dist <= 1.5) { if (dist <= 1.5) {
// Just switch this condition for map debugging // Just switch this condition for map debugging
return <MapView style={styles.map} initialRegion={ustpCoords} />; return (
<MapView
style={styles.map}
initialRegion={ustpCoords}
showsUserLocation={true}
scrollEnabled={false}
zoomEnabled={false}
rotateEnabled={false}
followsUserLocation={true}
minZoomLevel={15}
/>
);
} else { } else {
return ( return (
<View> <View>
@ -68,8 +79,8 @@ export default function Home() {
</Text> </Text>
<MapView <MapView
style={{ style={{
height: 256, height: Viewport.height * 0.5,
width: 256, width: Viewport.width * 0.8,
alignSelf: "center", alignSelf: "center",
}} }}
showsUserLocation={true} showsUserLocation={true}
@ -95,14 +106,13 @@ export default function Home() {
return ( return (
<AnimatedContainer> <AnimatedContainer>
<Text style={styles.text_white_medium}>{feedback}</Text> <Text style={styles.text_white_medium}>{feedback}</Text>
<Button onPress={() => requestLocation()} color={colors.secondary_3}> <Button onPress={() => requestLocation()}>
<Text style={styles.text_white_small}>Allow Access</Text> <Text style={styles.text_white_medium}>Allow Access</Text>
</Button> </Button>
</AnimatedContainer> </AnimatedContainer>
); );
} }
} }
const creds = useSelector((state: RootState) => state.user.user);
return ( return (
<View style={styles.background}> <View style={styles.background}>
<AnimatedContainer> <AnimatedContainer>

View file

@ -1,10 +1,9 @@
import { StyleSheet, Dimensions } from "react-native"; import { StyleSheet, Dimensions } from "react-native";
const width = Dimensions.get("window").width; export const Viewport = {
const height = Dimensions.get("window").height; width: Dimensions.get("window").width,
height: Dimensions.get("window").height,
const containerWidth = width - width * 0.08; };
const containerHeight = height - height * 0.01;
export const colors = { export const colors = {
primary_1: "#1C2C3F", primary_1: "#1C2C3F",
@ -117,7 +116,7 @@ const styles = StyleSheet.create({
marginHorizontal: 8, marginHorizontal: 8,
padding: 8, padding: 8,
borderRadius: 16, borderRadius: 16,
width: width * 0.4, width: Viewport.width * 0.4,
}, },
text_input: { text_input: {
color: colors.text_default, color: colors.text_default,
@ -126,7 +125,7 @@ const styles = StyleSheet.create({
borderWidth: 1, borderWidth: 1,
padding: 10, padding: 10,
borderRadius: 8, borderRadius: 8,
width: width * 0.5, width: Viewport.width * 0.5,
}, },
dropdown_template: { dropdown_template: {
backgroundColor: colors.primary_2, backgroundColor: colors.primary_2,
@ -135,9 +134,8 @@ const styles = StyleSheet.create({
width: "70%", width: "70%",
}, },
map: { map: {
flex: 1, height: Viewport.height * 0.8,
height: containerHeight, width: Viewport.width * 0.8,
width: containerWidth,
alignSelf: "center", alignSelf: "center",
}, },
profile: { profile: {