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

Import and export subjects colors and names #392

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 8 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
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"cal-parser": "^1.0.2",
"date-fns": "^3.6.0",
"esup-multi.js": "^1.0.2",
"expo": "~51.0.28",
"expo-asset": "^10.0.10",
"expo-auth-session": "~5.5.2",
"expo-av": "~14.0.6",
Expand Down Expand Up @@ -55,6 +56,7 @@
"expo-sharing": "~12.0.1",
"expo-splash-screen": "~0.27.6",
"expo-status-bar": "~1.12.1",
"expo-store-review": "~7.0.2",
"expo-system-ui": "~3.0.7",
"expo-task-manager": "~11.8.2",
"expo-web-browser": "~13.0.3",
Expand Down Expand Up @@ -95,9 +97,7 @@
"scolengo-api": "^3.0.5",
"text-encoding": "^0.7.0",
"turboself-api": "^2.0.2",
"zustand": "^4.5.2",
"expo-store-review": "~7.0.2",
"expo": "~51.0.28"
"zustand": "^4.5.2"
},
"devDependencies": {
"@babel/core": "^7.20.0",
Expand Down
132 changes: 108 additions & 24 deletions src/views/settings/SettingsSubjects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import { NativeItem, NativeList, NativeText } from "@/components/Global/NativeCo
import { useCurrentAccount } from "@/stores/account";
import MissingItem from "@/components/Global/MissingItem";
import BottomSheet from "@/components/Modals/PapillonBottomSheet";
import { Trash2 } from "lucide-react-native";
import { Trash2, ArrowDownCircle, ArrowUpCircle } from "lucide-react-native";
import ColorIndicator from "@/components/Lessons/ColorIndicator";
import { COLORS_LIST } from "@/services/shared/Subject";
import type { Screen } from "@/router/helpers/types";
import SubjectContainerCard from "@/components/Settings/SubjectContainerCard";
import * as Clipboard from "expo-clipboard";

const MemoizedNativeItem = React.memo(NativeItem);
const MemoizedNativeList = React.memo(NativeList);
Expand All @@ -24,6 +25,7 @@ type Item = [key: string, value: { color: string; pretty: string; emoji: string;

const SettingsSubjects: Screen<"SettingsSubjects"> = ({ navigation }) => {
const account = useCurrentAccount(store => store.account!);
const subjectsForCopy = account.personalization.subjects;
const mutateProperty = useCurrentAccount(store => store.mutateProperty);
const insets = useSafeAreaInsets();
const colors = useTheme().colors;
Expand Down Expand Up @@ -58,6 +60,25 @@ const SettingsSubjects: Screen<"SettingsSubjects"> = ({ navigation }) => {
);
}, []);

const setSubjectsFromJson = (json: { [key: string]: { color: string; pretty: string; emoji: string; } }) => {
const newSubjects: Item[] = Object.entries(json) as Item[];
setSubjects(newSubjects);
setLocalSubjects(newSubjects);
mutateProperty("personalization", {
...account.personalization,
subjects: json,
});
};

const isJsonable = (strTest: string) => {
try {
JSON.parse(strTest);
return true;
} catch (e) {
return false;
}
};

const debouncedUpdateSubject = useMemo(
() => debounce((subjectKey: string, updates: Partial<Item[1]>) => {
updateSubject(subjectKey, updates);
Expand Down Expand Up @@ -118,30 +139,93 @@ const SettingsSubjects: Screen<"SettingsSubjects"> = ({ navigation }) => {
useLayoutEffect(() => {
navigation.setOptions({
headerRight: () => (
<TouchableOpacity
onPress={() => {
Alert.alert(
"Réinitialiser les matières",
"Voulez-vous vraiment réinitialiser les matières ?",
[
{ text: "Annuler", style: "cancel" },
{ text: "Réinitialiser", style: "destructive", onPress: () => {
setSubjects([]);
setLocalSubjects([]);
setCurrentTitle("");
<View style={{ flexDirection: "row", alignItems: "center" }}>

{/* Bouton exporter */}
<TouchableOpacity
onPress={() => {
Alert.alert(
"Exporter",
"Exporter les couleurs actuelles vers la presse papier ?",
codeuriii marked this conversation as resolved.
Show resolved Hide resolved
[
{ text: "Annuler", style: "cancel" },
{
text: "Exporter",
style: "destructive",
onPress: () => {
const writeToClipboard = async (text: string) => {
await Clipboard.setStringAsync(text);
};
writeToClipboard(JSON.stringify(subjectsForCopy));
}
},
]
); // Alerte qui s'affiche quand vous appuyez sur le bouton
}}
style={{ marginRight: 10 }} // Marge pour séparer du bouton poubelle
>
<ArrowUpCircle size={22} color={colors.primary} />
</TouchableOpacity>

{/* Bouton importer */}
<TouchableOpacity
onPress={() => {
Alert.alert(
"Importer",
"Importer une table de couleurs depuis la presse papier ?",
codeuriii marked this conversation as resolved.
Show resolved Hide resolved
[
{ text: "Annuler", style: "cancel" },
{
text: "Importer",
style: "destructive",
onPress: () => {
const getClipboard = async () => {
const result = await Clipboard.getStringAsync();
if (isJsonable(result)) {
setSubjectsFromJson(JSON.parse(result));
}
codeuriii marked this conversation as resolved.
Show resolved Hide resolved
};
getClipboard();
}
},
]
); // Alerte qui s'affiche quand vous appuyez sur le bouton
}}
style={{ marginRight: 10 }} // Marge pour séparer du bouton poubelle
>
<ArrowDownCircle size={22} color={colors.primary} />
</TouchableOpacity>

{/* Bouton poubelle */}
<TouchableOpacity
onPress={() => {
Alert.alert(
"Réinitialiser les matières",
"Voulez-vous vraiment réinitialiser les matières ?",
[
{ text: "Annuler", style: "cancel" },
{
text: "Réinitialiser",
style: "destructive",
onPress: () => {
setSubjects([]);
setLocalSubjects([]);
setCurrentTitle("");

mutateProperty("personalization", {
...account.personalization,
subjects: {},
});
}},
]
);
}}
style={{ marginRight: 2 }}
>
<Trash2 size={22} color={colors.primary} />
</TouchableOpacity>
mutateProperty("personalization", {
...account.personalization,
subjects: {},
});
}
},
]
);
}}
style={{ marginRight: 2 }}
>
<Trash2 size={22} color={colors.primary} />
</TouchableOpacity>
</View>
),
});
}, [navigation, colors.primary]);
Expand Down