Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix]: Affichage distance établissement #568

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 84 additions & 47 deletions src/views/login/pronote/PronoteInstanceSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,19 @@ import {
ActivityIndicator,
Keyboard,
KeyboardEvent,
Text
Text,
} from "react-native";
import pronote from "pawnote";
import Reanimated, { LinearTransition, FlipInXDown, FadeInUp, FadeOutUp, ZoomIn, ZoomOut, Easing, ZoomInEasyDown } from "react-native-reanimated";
import Reanimated, {
LinearTransition,
FlipInXDown,
FadeInUp,
FadeOutUp,
ZoomIn,
ZoomOut,
Easing,
ZoomInEasyDown,
} from "react-native-reanimated";
import determinateAuthenticationView from "@/services/pronote/determinate-authentication-view";
import MaskStars from "@/components/FirstInstallation/MaskStars";
import PapillonShineBubble from "@/components/FirstInstallation/PapillonShineBubble";
Expand All @@ -20,7 +29,7 @@ import DuoListPressable from "@/components/FirstInstallation/DuoListPressable";
import { LinearGradient } from "expo-linear-gradient";
import { useTheme } from "@react-navigation/native";

import { Search, X, GraduationCap, } from "lucide-react-native";
import { Search, X, GraduationCap } from "lucide-react-native";
import { useAlert } from "@/providers/AlertProvider";
import { Audio } from "expo-av";
import getInstancesFromDataset from "@/services/pronote/dataset_geolocation";
Expand All @@ -30,10 +39,14 @@ const PronoteInstanceSelector: Screen<"PronoteInstanceSelector"> = ({
navigation,
}) => {
// `null` when loading, `[]` when no instances found.
const [instances, setInstances] = useState<pronote.GeolocatedInstance[] | null>(null);
const [originalInstances, setOriginalInstances] = useState<pronote.GeolocatedInstance[] | null>(null);

const {colors} = useTheme();
const [instances, setInstances] = useState<
pronote.GeolocatedInstance[] | null
>(null);
const [originalInstances, setOriginalInstances] = useState<
pronote.GeolocatedInstance[] | null
>(null);

const { colors } = useTheme();
const insets = useSafeAreaInsets();

const { showAlert } = useAlert();
Expand All @@ -47,6 +60,9 @@ const PronoteInstanceSelector: Screen<"PronoteInstanceSelector"> = ({
const [keyboardHeight, setKeyboardHeight] = useState(0);
const [sound, setSound] = useState<Audio.Sound | null>(null);

const routes = navigation.getState()?.routes;
const prevRoute = routes[routes.length - 2];

const keyboardDidShow = (event: KeyboardEvent) => {
setKeyboardOpen(true);
setKeyboardHeight(event.endCoordinates.height);
Expand Down Expand Up @@ -84,11 +100,13 @@ const PronoteInstanceSelector: Screen<"PronoteInstanceSelector"> = ({
};
}, []);


useEffect(() => {
if (params) {
void async function () {
const dataset_instances = await getInstancesFromDataset(params.longitude, params.latitude);
void (async function () {
const dataset_instances = await getInstancesFromDataset(
params.longitude,
params.latitude
);
const pronote_instances = await pronote.geolocation(params);

// On calcule la distance entre les instances et l'utilisateur.
Expand All @@ -104,9 +122,12 @@ const PronoteInstanceSelector: Screen<"PronoteInstanceSelector"> = ({
const dLat = lat2 - lat1;
const dLon = lon2 - lon1;

const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(lat1) * Math.cos(lat2) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
const a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(lat1) *
Math.cos(lat2) *
Math.sin(dLon / 2) *
Math.sin(dLon / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
const distance = R * c;

Expand All @@ -128,7 +149,7 @@ const PronoteInstanceSelector: Screen<"PronoteInstanceSelector"> = ({
// On met à jour les instances.
setInstances(instances);
setOriginalInstances(instances);
}();
})();
}
}, [params]);

Expand All @@ -149,14 +170,14 @@ const PronoteInstanceSelector: Screen<"PronoteInstanceSelector"> = ({
styles.container,
{
paddingTop: insets.top,
}
},
]}
>
<MaskStars />

<View style={{height: insets.top}} />
<View style={{ height: insets.top }} />

{!keyboardOpen &&
{!keyboardOpen && (
<Reanimated.View
entering={FadeInUp.duration(250).delay(200)}
exiting={FadeOutUp.duration(150)}
Expand All @@ -171,7 +192,7 @@ const PronoteInstanceSelector: Screen<"PronoteInstanceSelector"> = ({
style={{ marginTop: 0, zIndex: 9999 }}
/>
</Reanimated.View>
}
)}

<Reanimated.View
style={[
Expand All @@ -181,7 +202,7 @@ const PronoteInstanceSelector: Screen<"PronoteInstanceSelector"> = ({
// @ts-expect-error
color: colors.text,
borderColor: colors.border,
}
},
]}
layout={LinearTransition.springify().mass(1).stiffness(100).damping(40)}
>
Expand All @@ -197,19 +218,24 @@ const PronoteInstanceSelector: Screen<"PronoteInstanceSelector"> = ({
styles.searchInput,
{
color: colors.text,
}
},
]}
/>

{search.length > 0 && (
<Reanimated.View
layout={LinearTransition.springify().mass(1).stiffness(100).damping(40)}
layout={LinearTransition.springify()
.mass(1)
.stiffness(100)
.damping(40)}
entering={ZoomIn.springify()}
exiting={ZoomOut.springify()}
>
<TouchableOpacity onPress={() => {
setSearch("");
}}>
<TouchableOpacity
onPress={() => {
setSearch("");
}}
>
Slysoks marked this conversation as resolved.
Show resolved Hide resolved
<X size={24} color={colors.text + "55"} />
</TouchableOpacity>
</Reanimated.View>
Expand All @@ -223,15 +249,14 @@ const PronoteInstanceSelector: Screen<"PronoteInstanceSelector"> = ({
) : (
<Reanimated.View
style={styles.overScrollContainer}
layout={LinearTransition.springify().mass(1).stiffness(100).damping(40)}
layout={LinearTransition.springify()
.mass(1)
.stiffness(100)
.damping(40)}
>

<LinearGradient
pointerEvents="none"
colors={[
colors.background + "00",
colors.background,
]}
colors={[colors.background + "00", colors.background]}
style={{
position: "absolute",
bottom: 0,
Expand All @@ -244,7 +269,10 @@ const PronoteInstanceSelector: Screen<"PronoteInstanceSelector"> = ({

<Reanimated.ScrollView
style={styles.overScroll}
layout={LinearTransition.springify().mass(1).stiffness(100).damping(40)}
layout={LinearTransition.springify()
.mass(1)
.stiffness(100)
.damping(40)}
>
{instances.length === 0 && (
<Reanimated.Text
Expand All @@ -263,33 +291,39 @@ const PronoteInstanceSelector: Screen<"PronoteInstanceSelector"> = ({
)}

<Reanimated.View
style={[styles.list,
style={[
styles.list,
{
paddingBottom: keyboardHeight + insets.bottom + (keyboardHeight > 0 ? 0 : 20),
}
paddingBottom:
keyboardHeight +
insets.bottom +
(keyboardHeight > 0 ? 0 : 20),
},
]}
layout={LinearTransition.springify().mass(1).stiffness(100).damping(40)}
layout={LinearTransition.springify()
.mass(1)
.stiffness(100)
.damping(40)}
>
{instances.map((instance, index) => (
<Reanimated.View
style={{ width: "100%" }}
layout={LinearTransition.springify().mass(1).stiffness(150).damping(20)}
layout={LinearTransition.springify()
.mass(1)
.stiffness(150)
.damping(20)}
entering={
index < 10 &&
!hasSearched ?
FlipInXDown.springify().delay(100 * index)
index < 10 && !hasSearched
? FlipInXDown.springify().delay(100 * index)
// @ts-expect-error
: ZoomInEasyDown.duration(400).easing(Easing.bezier(0.25, 0.1, 0.25, 1)).delay(30 * index)
: ZoomInEasyDown.duration(400).easing(Easing.bezier(0.25, 0.1, 0.25, 1)).delay(30 * index)
Slysoks marked this conversation as resolved.
Show resolved Hide resolved
}
exiting={index < 10 ? FadeOutUp : void 0}
key={instance.url}
>
<DuoListPressable
leading={
<GraduationCap
size={24}
color={colors.text + "88"}
/>
<GraduationCap size={24} color={colors.text + "88"} />
}
onPress={async () => {
determinateAuthenticationView(
Expand All @@ -299,14 +333,17 @@ const PronoteInstanceSelector: Screen<"PronoteInstanceSelector"> = ({
);
}}
text={instance.name}
subtext={`à ${instance.distance.toFixed(2)}km de toi`}
subtext={
prevRoute.name === "PronoteManualLocation"
? undefined
: `à ${instance.distance.toFixed(2)}km de toi`
Slysoks marked this conversation as resolved.
Show resolved Hide resolved
}
/>
</Reanimated.View>
))}
</Reanimated.View>
</Reanimated.ScrollView>
</Reanimated.View>

)}
</View>
);
Expand Down Expand Up @@ -375,7 +412,7 @@ const styles = StyleSheet.create({
flex: 1,
fontSize: 17,
fontFamily: "medium",
}
},
});

export default PronoteInstanceSelector;
4 changes: 2 additions & 2 deletions src/views/settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import {
Route,
Scroll,
Settings as SettingsLucide,
Sparkles,
SunMoon,
Sparkles,
SunMoon,
Smile,
SwatchBook,
WandSparkles,
Expand Down
Loading