redesigned completion and reopening subjects/assignments/tasks and how it is rendered

This commit is contained in:
Fhj0607
2026-05-01 12:36:58 +02:00
parent e3c0b286b8
commit ae613f8707
4 changed files with 239 additions and 124 deletions

View File

@@ -1,18 +1,26 @@
import { SUBJECT_COLORS } from '@/lib/subjectColors'; import { SUBJECT_COLORS, type SubjectColor } from '@/lib/subjectColors';
import { supabase } from '@/lib/supabase'; import { supabase } from '@/lib/supabase';
import { Subject } from '@/lib/types'; import { Subject } from '@/lib/types';
import { Session } from '@supabase/supabase-js'; import { Session } from '@supabase/supabase-js';
import { router, Stack, useFocusEffect } from 'expo-router'; import { router, Stack, useFocusEffect } from 'expo-router';
import { useCallback, useEffect, useState } from 'react'; import { useCallback, useEffect, useState } from 'react';
import { ActivityIndicator, Alert, Pressable, ScrollView, Text, View } from 'react-native'; import {
ActivityIndicator,
import type { SubjectColor } from '@/lib/subjectColors'; Alert,
Pressable,
ScrollView,
Text,
View,
} from 'react-native';
export default function Subjects() { export default function Subjects() {
const [subjects, SetSubjects] = useState<Subject[]>([]); const [subjects, SetSubjects] = useState<Subject[]>([]);
const [session, SetSession] = useState<Session | null>(null); const [session, SetSession] = useState<Session | null>(null);
const [isLoading, SetIsLoading] = useState(true); const [isLoading, SetIsLoading] = useState(true);
const activeSubjects = subjects.filter((subject) => subject.isActive);
const inactiveSubjects = subjects.filter((subject) => !subject.isActive);
useEffect(() => { useEffect(() => {
supabase.auth.getSession().then(({ data }) => { supabase.auth.getSession().then(({ data }) => {
SetSession(data.session ?? null); SetSession(data.session ?? null);
@@ -59,6 +67,73 @@ export default function Subjects() {
}, [session]) }, [session])
); );
const RenderSubjectCard = (subject: Subject) => {
const colorKey: SubjectColor = subject.color ?? 'slate';
const colorSet = SUBJECT_COLORS[colorKey];
const firstLetter = subject.title?.trim().charAt(0).toUpperCase() || '?';
return (
<Pressable
key={subject.sId}
className="mb-4 rounded-3xl bg-app-surface p-4"
style={{
borderWidth: 1,
borderColor: colorSet.strong,
}}
onPress={() =>
router.push({
pathname: '/subject/viewDetailsSubject',
params: { sId: subject.sId },
})
}
>
<View className="flex-row items-center">
<View
className="mr-3 h-12 w-12 items-center justify-center rounded-2xl"
style={{ backgroundColor: colorSet.soft }}
>
<Text
className="text-base font-bold"
style={{ color: colorSet.strong }}
>
{firstLetter}
</Text>
</View>
<View className="flex-1">
<Text
className="text-base font-bold text-text-main"
numberOfLines={1}
>
{subject.title}
</Text>
<Text
className="mt-1 text-sm leading-5 text-text-secondary"
numberOfLines={2}
>
{subject.description || 'No description added.'}
</Text>
</View>
<View className="ml-3">
<View
className="rounded-full px-3 py-1"
style={{ backgroundColor: colorSet.soft }}
>
<Text
className="text-xs font-semibold"
style={{ color: colorSet.strong }}
>
{subject.isActive ? 'Active' : 'Inactive'}
</Text>
</View>
</View>
</View>
</Pressable>
);
};
return ( return (
<View className="flex-1 bg-app-bg"> <View className="flex-1 bg-app-bg">
<Stack.Screen <Stack.Screen
@@ -86,13 +161,6 @@ export default function Subjects() {
}} }}
showsVerticalScrollIndicator={false} showsVerticalScrollIndicator={false}
> >
<View className="mb-6">
<Text className="text-3xl font-bold text-text-main">Subjects</Text>
<Text className="mt-2 text-base leading-6 text-text-secondary">
Pick a subject to manage assignments and tasks.
</Text>
</View>
{isLoading ? ( {isLoading ? (
<View className="items-center justify-center rounded-3xl border border-app-border bg-app-surface p-5"> <View className="items-center justify-center rounded-3xl border border-app-border bg-app-surface p-5">
<ActivityIndicator size="large" color="#2563eb" /> <ActivityIndicator size="large" color="#2563eb" />
@@ -111,74 +179,55 @@ export default function Subjects() {
</View> </View>
) : ( ) : (
<View> <View>
{subjects.map((subject) => { <View className="mb-3 mt-2 flex-row items-center justify-between">
const colorKey: SubjectColor = subject.color ?? 'slate'; <Text className="text-lg font-bold text-text-main">
const colorSet = SUBJECT_COLORS[colorKey]; Active Subjects
</Text>
const firstLetter = <View className="rounded-full bg-app-subtle px-3 py-1">
subject.title?.trim().charAt(0).toUpperCase() || '?'; <Text className="text-xs font-semibold text-text-muted">
{activeSubjects.length}
</Text>
</View>
</View>
return ( {activeSubjects.length === 0 ? (
<Pressable <View className="mb-6 rounded-3xl border border-app-border bg-app-surface p-5">
key={subject.sId} <Text className="text-center text-base font-semibold text-text-secondary">
className="mb-4 rounded-3xl bg-app-surface p-4" No active subjects
style={{ </Text>
borderWidth: 1, <Text className="mt-1 text-center text-sm text-text-muted">
borderColor: colorSet.strong, Subjects with ongoing work will show up here.
}} </Text>
onPress={() => </View>
router.push({ ) : (
pathname: '/subject/viewDetailsSubject', activeSubjects.map(RenderSubjectCard)
params: { sId: subject.sId }, )}
})
}
>
<View className="flex-row items-center">
<View
className="mr-3 h-12 w-12 items-center justify-center rounded-2xl"
style={{ backgroundColor: colorSet.soft }}
>
<Text
className="text-base font-bold"
style={{ color: colorSet.strong }}
>
{firstLetter}
</Text>
</View>
<View className="flex-1"> <View className="mb-3 mt-2 flex-row items-center justify-between">
<Text <Text className="text-lg font-bold text-text-main">
className="text-base font-bold text-text-main" Inactive Subjects
numberOfLines={1} </Text>
>
{subject.title}
</Text>
<Text <View className="rounded-full bg-app-subtle px-3 py-1">
className="mt-1 text-sm leading-5 text-text-secondary" <Text className="text-xs font-semibold text-text-muted">
numberOfLines={2} {inactiveSubjects.length}
> </Text>
{subject.description || 'No description added.'} </View>
</Text> </View>
</View>
<View className="ml-3"> {inactiveSubjects.length === 0 ? (
<View <View className="mb-6 rounded-3xl border border-app-border bg-app-surface p-5">
className="rounded-full px-3 py-1" <Text className="text-center text-base font-semibold text-text-secondary">
style={{ backgroundColor: colorSet.soft }} No inactive subjects
> </Text>
<Text <Text className="mt-1 text-center text-sm text-text-muted">
className="text-xs font-semibold" Completed or paused subjects will show up here.
style={{ color: colorSet.strong }} </Text>
> </View>
{subject.isActive ? 'Active' : 'Inactive'} ) : (
</Text> inactiveSubjects.map(RenderSubjectCard)
</View> )}
</View>
</View>
</Pressable>
);
})}
</View> </View>
)} )}

View File

@@ -167,6 +167,32 @@ export default function ViewDetailsAssignment() {
) )
} }
const ToggleTaskCompletion = async (task: Task) => {
const nextIsCompleted = !task.isCompleted;
const { error } = await supabase
.from("tasks")
.update({
isCompleted: nextIsCompleted,
lastChanged: new Date().toISOString(),
})
.eq("tId", task.tId);
if (error) {
Alert.alert("Task could not be updated, please try again");
return;
}
try {
await CheckAssignmentCompletion(task.aId);
} catch {
Alert.alert("Failed to update assignment completion state");
}
await GetTasks(task.aId);
await GetAssignment(task.aId);
}
const colorSet = getSubjectColorSet(subjectMeta.color); const colorSet = getSubjectColorSet(subjectMeta.color);
const completedTasks = tasks.filter((task) => task.isCompleted).length; const completedTasks = tasks.filter((task) => task.isCompleted).length;
@@ -234,7 +260,7 @@ export default function ViewDetailsAssignment() {
<SectionList <SectionList
className="flex-1" className="flex-1"
contentContainerStyle={{ paddingHorizontal: 20, paddingTop: 20, paddingBottom: 32 }} contentContainerStyle={{ paddingHorizontal: 20, paddingTop: 20, paddingBottom: 32 }}
sections={taskSections} sections={totalTasks === 0 ? [] : taskSections}
keyExtractor={(item) => item.tId} keyExtractor={(item) => item.tId}
showsVerticalScrollIndicator={false} showsVerticalScrollIndicator={false}
stickySectionHeadersEnabled={false} stickySectionHeadersEnabled={false}
@@ -248,18 +274,6 @@ export default function ViewDetailsAssignment() {
}} }}
> >
<View className="flex-row items-start"> <View className="flex-row items-start">
<View
className="mr-3 mt-1 h-6 w-6 items-center justify-center rounded-md border-2"
style={{
borderColor: assignment.isCompleted ? colorSet.strong : '#DDD6C8',
backgroundColor: assignment.isCompleted ? colorSet.strong : '#EFEBE3',
}}
>
{assignment.isCompleted && (
<Text className="text-sm font-bold text-text-inverse"></Text>
)}
</View>
<View className="flex-1"> <View className="flex-1">
<Text className="text-2xl font-bold text-text-main"> <Text className="text-2xl font-bold text-text-main">
{assignment.title} {assignment.title}
@@ -399,18 +413,6 @@ export default function ViewDetailsAssignment() {
} }
> >
<View className="flex-row items-start"> <View className="flex-row items-start">
<View
className="mr-3 mt-1 h-6 w-6 items-center justify-center rounded-md border-2"
style={{
borderColor: item.isCompleted ? colorSet.strong : '#DDD6C8',
backgroundColor: item.isCompleted ? colorSet.strong : '#EFEBE3',
}}
>
{item.isCompleted && (
<Text className="text-sm font-bold text-text-inverse"></Text>
)}
</View>
<View className="flex-1"> <View className="flex-1">
<Text <Text
className={`text-base font-bold ${ className={`text-base font-bold ${
@@ -434,6 +436,19 @@ export default function ViewDetailsAssignment() {
{isOwner && ( {isOwner && (
<View className="mt-4 flex-row border-t border-app-border pt-4"> <View className="mt-4 flex-row border-t border-app-border pt-4">
<Pressable
className="mr-3 flex-1 items-center justify-center rounded-2xl py-3"
style={{ backgroundColor: item.isCompleted ? '#EFEBE3' : colorSet.soft }}
onPress={() => ToggleTaskCompletion(item)}
>
<Text
className="text-sm font-bold"
style={{ color: colorSet.strong }}
>
{item.isCompleted ? 'Reopen' : 'Complete'}
</Text>
</Pressable>
<Pressable <Pressable
className="mr-3 flex-1 items-center justify-center rounded-2xl border border-app-border bg-app-subtle py-3" className="mr-3 flex-1 items-center justify-center rounded-2xl border border-app-border bg-app-subtle py-3"
onPress={() => onPress={() =>
@@ -459,6 +474,19 @@ export default function ViewDetailsAssignment() {
</View> </View>
); );
}} }}
ListEmptyComponent={
<View
className="mb-6 rounded-3xl border border-app-border bg-app-surface p-5"
style={{ borderColor: colorSet.strong }}
>
<Text className="text-center text-base font-semibold text-text-secondary">
No tasks needed yet
</Text>
<Text className="mt-1 text-center text-sm text-text-muted">
Add tasks if this assignment needs smaller steps.
</Text>
</View>
}
renderSectionFooter={({ section }) => renderSectionFooter={({ section }) =>
section.data.length === 0 ? ( section.data.length === 0 ? (
<View className="mb-6 rounded-3xl border border-app-border bg-app-surface p-5" style={{ borderColor: colorSet.strong }}> <View className="mb-6 rounded-3xl border border-app-border bg-app-surface p-5" style={{ borderColor: colorSet.strong }}>

View File

@@ -78,6 +78,32 @@ export default function ViewDetailsSubject() {
SetAssignments(data ?? []); SetAssignments(data ?? []);
}; };
const ToggleAssignmentCompletion = async (assignment: Assignment) => {
const nextIsCompleted = !assignment.isCompleted;
const { error } = await supabase
.from('assignments')
.update({
isCompleted: nextIsCompleted,
lastChanged: new Date().toISOString(),
})
.eq('aId', assignment.aId);
if (error) {
Alert.alert('Assignment could not be updated, please try again');
return;
}
try {
await CheckSubjectCompletion(assignment.sId);
} catch {
Alert.alert('Failed to update subject status');
}
await GetAssignments(assignment.sId);
await GetSubject(assignment.sId);
};
useFocusEffect( useFocusEffect(
useCallback(() => { useCallback(() => {
if (!session || !sId) { if (!session || !sId) {
@@ -267,7 +293,7 @@ export default function ViewDetailsSubject() {
paddingTop: 20, paddingTop: 20,
paddingBottom: 32, paddingBottom: 32,
}} }}
sections={assignmentSections} sections={totalAssignments === 0 ? [] : assignmentSections}
keyExtractor={(item) => item.aId} keyExtractor={(item) => item.aId}
showsVerticalScrollIndicator={false} showsVerticalScrollIndicator={false}
stickySectionHeadersEnabled={false} stickySectionHeadersEnabled={false}
@@ -396,7 +422,7 @@ export default function ViewDetailsSubject() {
} }
> >
<Text className="text-base font-bold text-text-inverse"> <Text className="text-base font-bold text-text-inverse">
Create Assignment Add Assignment
</Text> </Text>
</Pressable> </Pressable>
</View> </View>
@@ -422,15 +448,16 @@ export default function ViewDetailsSubject() {
borderColor: colorSet.strong, borderColor: colorSet.strong,
}} }}
> >
<Pressable <View className="flex-row items-center">
onPress={() => <Pressable
router.push({ className="flex-1"
pathname: '/assignment/viewDetailsAssignment', onPress={() =>
params: { aId: item.aId }, router.push({
}) pathname: '/assignment/viewDetailsAssignment',
} params: { aId: item.aId },
> })
<View className="flex-row items-center"> }
>
<View className="flex-1"> <View className="flex-1">
<Text <Text
className={`text-base font-bold ${ className={`text-base font-bold ${
@@ -453,11 +480,24 @@ export default function ViewDetailsSubject() {
Deadline: {formatDate(item.deadline)} Deadline: {formatDate(item.deadline)}
</Text> </Text>
</View> </View>
</View> </Pressable>
</Pressable> </View>
{isOwner && ( {isOwner && (
<View className="mt-4 flex-row border-t border-app-border pt-4"> <View className="mt-4 flex-row border-t border-app-border pt-4">
<Pressable
className="mr-3 flex-1 items-center justify-center rounded-2xl py-3"
style={{ backgroundColor: item.isCompleted ? '#EFEBE3' : colorSet.soft }}
onPress={() => ToggleAssignmentCompletion(item)}
>
<Text
className="text-sm font-bold"
style={{ color: colorSet.strong }}
>
{item.isCompleted ? 'Reopen' : 'Complete'}
</Text>
</Pressable>
<Pressable <Pressable
className="mr-3 flex-1 items-center justify-center rounded-2xl border border-app-border bg-app-subtle py-3" className="mr-3 flex-1 items-center justify-center rounded-2xl border border-app-border bg-app-subtle py-3"
onPress={() => onPress={() =>
@@ -485,6 +525,16 @@ export default function ViewDetailsSubject() {
</View> </View>
); );
}} }}
ListEmptyComponent={
<View className="mb-6 rounded-3xl border border-app-border bg-app-surface p-5">
<Text className="text-center text-base font-semibold text-text-secondary">
No assignments yet
</Text>
<Text className="mt-1 text-center text-sm text-text-muted">
Add one when this subject has work to track.
</Text>
</View>
}
renderSectionFooter={({ section }) => renderSectionFooter={({ section }) =>
section.data.length === 0 ? ( section.data.length === 0 ? (
<View className="mb-6 rounded-3xl border border-app-border bg-app-surface p-5"> <View className="mb-6 rounded-3xl border border-app-border bg-app-surface p-5">

View File

@@ -212,18 +212,6 @@ export default function ViewDetailsTask() {
}} }}
> >
<View className="flex-row items-start"> <View className="flex-row items-start">
<View
className="mr-3 mt-1 h-6 w-6 items-center justify-center rounded-md border-2"
style={{
borderColor: task.isCompleted ? colorSet.strong : '#DDD6C8',
backgroundColor: task.isCompleted ? colorSet.strong : '#EFEBE3',
}}
>
{task.isCompleted && (
<Text className="text-sm font-bold text-text-inverse"></Text>
)}
</View>
<View className="flex-1"> <View className="flex-1">
<Text <Text
className={`text-2xl font-bold ${ className={`text-2xl font-bold ${