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

Reset-profile-icon #269

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions src/services/ecoledirecte/default-personalization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Personalization } from "@/stores/account/types";
import downloadAsBase64 from "@/utils/external/download-as-base64";
import { defaultTabs } from "@/consts/DefaultTabs";
import type { Account } from "pawdirecte";
import AsyncStorage from "@react-native-async-storage/async-storage";

import colors from "@/utils/data/colors.json";

Expand All @@ -16,6 +17,10 @@ const defaultEDTabs = [
] as typeof defaultTabs[number]["tab"][];

export default async function defaultPersonalization (account: Account): Promise<Partial<Personalization>> {

// Store the profile picture in the async storage
AsyncStorage.setItem("defaultProfilePictureB64", await downloadAsBase64(account.profile_picture_url ?? ""));

return {
color: colors[0],
magicEnabled: true,
Expand Down
5 changes: 5 additions & 0 deletions src/services/pronote/default-personalization.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Personalization } from "@/stores/account/types";
import downloadAsBase64 from "@/utils/external/download-as-base64";
import { defaultTabs } from "@/consts/DefaultTabs";
import AsyncStorage from "@react-native-async-storage/async-storage";
import type pronote from "pawnote";

import colors from "@/utils/data/colors.json";
Expand All @@ -18,6 +19,10 @@ const defaultPronoteTabs = [

const defaultPersonalization = async (instance: pronote.SessionHandle): Promise<Partial<Personalization>> => {
const user = instance.user.resources[0];

// Store the profile picture in the async storage
AsyncStorage.setItem("defaultProfilePictureB64", await downloadAsBase64(user.profilePicture ? user.profilePicture.url : ""));

return {
color: colors[0],
magicEnabled: true,
Expand Down
55 changes: 51 additions & 4 deletions src/views/settings/SettingsProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ import { Screen } from "@/router/helpers/types";
import { useCurrentAccount } from "@/stores/account";
import { useTheme } from "@react-navigation/native";
import * as ImagePicker from "expo-image-picker";
import { Camera, Plus, TextCursorInput, User2, UserCircle2, WholeWord } from "lucide-react-native";
import { Camera, TextCursorInput, Trash2, User2, UserCircle2, WholeWord, X } from "lucide-react-native";
import React, { useEffect, useRef, useState } from "react";
import { ActivityIndicator, Image, KeyboardAvoidingView, ScrollView, Switch, TextInput } from "react-native";
import { ActivityIndicator, Image, KeyboardAvoidingView, Platform, ScrollView, Switch, TextInput } from "react-native";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import * as Haptics from "expo-haptics";
import { useAlert } from "@/providers/AlertProvider";
import AsyncStorage from "@react-native-async-storage/async-storage";

const SettingsProfile: Screen<"SettingsProfile"> = ({ navigation }) => {
const theme = useTheme();
const { colors } = theme;
const insets = useSafeAreaInsets();
const account = useCurrentAccount(store => store.account!);
const mutateProperty = useCurrentAccount(store => store.mutateProperty);
Expand All @@ -23,6 +27,8 @@ const SettingsProfile: Screen<"SettingsProfile"> = ({ navigation }) => {
const [firstName, setFirstName] = useState(account.studentName?.first ?? "");
const [lastName, setLastName] = useState(account.studentName?.last ?? "");

const { showAlert } = useAlert();

// on name change, update the account name
useEffect(() => {
let newLastName = lastName;
Expand Down Expand Up @@ -101,6 +107,47 @@ const SettingsProfile: Screen<"SettingsProfile"> = ({ navigation }) => {
<NativeItem
chevron={true}
onPress={() => updateProfilePic()}
onLongPress={() => {
if (Platform.OS === "android") {
Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Heavy);
showAlert({
title: "Supprimer la photo de profil",
message: "Voulez-vous supprimer ou réinitialiser votre photo de profil ?",
actions: [
{
title: "Supprimer",
onPress: () => {
setProfilePic(undefined);
mutateProperty("personalization", {
...account.personalization,
profilePictureB64: undefined,
});
},
primary: true,
icon: <Trash2 />,
backgroundColor: "#CF0029",
},
{
title: "Réinitialiser",
onPress: () => {
AsyncStorage.getItem("defaultProfilePictureB64").then((defaultProfilePictureB64) => {
if (defaultProfilePictureB64) {
setProfilePic(defaultProfilePictureB64);
mutateProperty("personalization", {
...account.personalization,
profilePictureB64: defaultProfilePictureB64,
});
}
});
},
primary: true,
icon: <Camera />,
backgroundColor: colors.primary,
}
],
});
}
}}
leading={profilePic &&
<Image
source={{ uri: profilePic }}
Expand Down Expand Up @@ -149,7 +196,7 @@ const SettingsProfile: Screen<"SettingsProfile"> = ({ navigation }) => {
fontFamily: "semibold",
color: theme.colors.text,
}}
placeholder="Théo"
placeholder={oldFirstName}
placeholderTextColor={theme.colors.text + "80"}
value={firstName}
onChangeText={setFirstName}
Expand All @@ -171,7 +218,7 @@ const SettingsProfile: Screen<"SettingsProfile"> = ({ navigation }) => {
fontFamily: "semibold",
color: theme.colors.text,
}}
placeholder="Dubois"
placeholder={oldLastName}
placeholderTextColor={theme.colors.text + "80"}
value={lastName}
onChangeText={setLastName}
Expand Down