Fixed issues with assignment progress tracking + added subject progress tracking

This commit is contained in:
Teodor
2026-04-23 04:37:34 +02:00
parent a35353f45b
commit 9f214a9451
9 changed files with 205 additions and 14 deletions

View File

@@ -1,4 +1,5 @@
import { defaultStyles } from '@/constants/defaultStyles';
import { CheckSubjectCompletion } from '@/lib/progress';
import { supabase } from '@/lib/supabase';
import type { Assignment, Task } from '@/lib/types';
import { Ionicons } from '@expo/vector-icons';
@@ -97,7 +98,7 @@ export default function Assignments() {
}, [session])
);
const DeleteAssignment = async (aId: string) => {
const DeleteAssignment = async (aId: string, sId: string) => {
Alert.alert(
'Delete Assignment',
'Are you sure you want to delete this assignment?',
@@ -121,6 +122,13 @@ export default function Assignments() {
}
Alert.alert('Assignment deleted successfully!');
try {
await CheckSubjectCompletion(sId);
} catch {
Alert.alert("Failed to update subject status");
}
GetAssignments();
},
},
@@ -296,7 +304,7 @@ export default function Assignments() {
<Pressable
className="flex-1 items-center justify-center rounded-2xl border border-app-border bg-app-surface py-3"
onPress={() => DeleteAssignment(item.aId)}
onPress={() => DeleteAssignment(item.aId, item.sId)}
>
<Text className="text-sm font-bold text-status-danger">
Delete

View File

@@ -1,6 +1,6 @@
import { defaultStyles } from '@/constants/defaultStyles';
import { supabase } from '@/lib/supabase';
import type { Subject } from '@/lib/types';
import type { Assignment, Subject } from '@/lib/types';
import { Ionicons } from '@expo/vector-icons';
import { Session } from '@supabase/supabase-js';
import { router, Stack, useFocusEffect } from 'expo-router';
@@ -15,6 +15,7 @@ import {
export default function Subjects() {
const [subjects, SetSubjects] = useState<Subject[]>([]);
const [assignmentsBySubject, SetAssignmentsBySubject] = useState<Record<string, Assignment[]>>({});
const [session, SetSession] = useState<Session | null>(null);
const subjectSections = [
@@ -45,17 +46,47 @@ export default function Subjects() {
}, []);
const GetSubjects = async () => {
const { data, error } = await supabase
const { data: subjectsData, error: subjectsError } = await supabase
.from('subjects')
.select('*')
.order('lastChanged', { ascending: false });
if (error) {
if (subjectsError) {
Alert.alert('Subjects could not be fetched, please try again');
return;
}
SetSubjects(data ?? []);
const subjectRows = subjectsData ?? [];
SetSubjects(subjectsData ?? []);
if (subjectRows.length === 0) {
SetAssignmentsBySubject({});
return;
}
const sIds = subjectRows.map((subject) => subject.sId);
const { data: assignmentsData, error: assignmentsError } = await supabase
.from('assignments')
.select('*')
.in('sId', sIds);
if (assignmentsError) {
Alert.alert('Subject assignments could not be fetched, please try again');
SetAssignmentsBySubject({});
return;
}
const groupedAssignments: Record<string, Assignment[]> = {};
for (const assignment of assignmentsData ?? []) {
if (!groupedAssignments[assignment.sId]) {
groupedAssignments[assignment.sId] = [];
}
groupedAssignments[assignment.sId].push(assignment);
}
SetAssignmentsBySubject(groupedAssignments);
};
useFocusEffect(
@@ -169,6 +200,9 @@ export default function Subjects() {
renderItem={({ item }) => {
const isOwner = session?.user.id === item.uId;
const subjectAssignments = assignmentsBySubject[item.sId] ?? [];
const progress = subjectAssignments.length === 0 ? 0 : Math.round((subjectAssignments.filter(assignment => assignment.isCompleted).length / subjectAssignments.length) * 100);
return (
<View className="mb-4 rounded-3xl border border-app-border bg-app-surface p-4 shadow-sm">
<Pressable
@@ -219,6 +253,27 @@ export default function Subjects() {
{item.isActive ? 'Active' : 'Inactive'}
</Text>
</View>
<View style={{ marginTop: 10 }}>
<Text style={{ marginBottom: 4 }}>{progress}%</Text>
<View
style={{
width: "100%",
height: 12,
backgroundColor: "#D9D9D9",
borderRadius: 999,
overflow: "hidden",
}}
>
<View
style={{
width: `${progress}%`,
height: "100%",
backgroundColor: "#4CAF50",
}}
/>
</View>
</View>
</View>
</View>
</Pressable>

View File

@@ -1,4 +1,5 @@
import { defaultStyles } from '@/constants/defaultStyles';
import { CheckAssignmentCompletion } from '@/lib/progress';
import { supabase } from '@/lib/supabase';
import type { Task } from '@/lib/types';
import { Ionicons } from '@expo/vector-icons';
@@ -63,7 +64,7 @@ export default function Tasks() {
}, [session])
);
const DeleteTask = async (tId: string) => {
const DeleteTask = async (tId: string, aId: string) => {
Alert.alert(
'Delete Task',
'Are you sure you want to delete this task?',
@@ -87,6 +88,13 @@ export default function Tasks() {
}
Alert.alert('Task deleted successfully!');
try {
await CheckAssignmentCompletion(aId);
} catch {
Alert.alert("Failed to update assignment completion state");
}
GetTasks();
},
},
@@ -237,7 +245,7 @@ export default function Tasks() {
<Pressable
className="flex-1 items-center justify-center rounded-2xl border border-app-border bg-app-surface py-3"
onPress={() => DeleteTask(item.tId)}
onPress={() => DeleteTask(item.tId, item.aId)}
>
<Text className="text-sm font-bold text-status-danger">
Delete

View File

@@ -1,5 +1,6 @@
import { defaultStyles } from '@/constants/defaultStyles';
import * as AsyncStorage from '@/lib/asyncStorage';
import { CheckSubjectCompletion } from '@/lib/progress';
import { supabase } from '@/lib/supabase';
import * as Notifications from 'expo-notifications';
import { router, Stack, useLocalSearchParams } from 'expo-router';
@@ -93,6 +94,14 @@ export default function CreateAssignment() {
}
}
if (sId) {
try {
await CheckSubjectCompletion(sId);
} catch {
Alert.alert("Failed to update subject status");
}
}
SetTitle('');
SetDescription('');
SetDeadline('');

View File

@@ -1,5 +1,6 @@
import { defaultStyles } from '@/constants/defaultStyles';
import { GetAssignmentNotificationId, RemoveAssignmentNotificationId, SaveAssignmentNotificationId } from '@/lib/asyncStorage';
import { CheckSubjectCompletion } from '@/lib/progress';
import { supabase } from '@/lib/supabase';
import type { Assignment } from '@/lib/types';
import * as Notifications from 'expo-notifications';
@@ -115,6 +116,14 @@ export default function EditAssignment() {
}
}
if (assignmentData.sId) {
try {
await CheckSubjectCompletion(assignmentData.sId);
} catch {
Alert.alert("Failed to update subject status");
}
}
router.back();
}

View File

@@ -1,4 +1,5 @@
import { defaultStyles } from '@/constants/defaultStyles';
import { CheckAssignmentCompletion, CheckSubjectCompletion } from '@/lib/progress';
import { supabase } from '@/lib/supabase';
import type { Assignment, Task } from '@/lib/types';
import { Session } from '@supabase/supabase-js';
@@ -78,6 +79,17 @@ export default function ViewDetailsAssignment() {
}
Alert.alert("Assignment deleted successfully!");
const sId = assignment?.sId;
if (sId) {
try {
await CheckSubjectCompletion(sId);
} catch {
Alert.alert("Failed to update subject status");
}
}
router.back();
}
}
@@ -106,6 +118,15 @@ export default function ViewDetailsAssignment() {
}
Alert.alert("Task deleted successfully!");
if (aId) {
try {
await CheckAssignmentCompletion(aId);
} catch {
Alert.alert("Failed to update assignment completion state");
}
}
GetTasks(aId);
}
}
@@ -153,9 +174,10 @@ export default function ViewDetailsAssignment() {
<View style={defaultStyles.checkbox}>
{assignment.isCompleted && <Text style={defaultStyles.checkboxMark}></Text>}
</View>
<View style={{ width: "100%", marginTop: 8 }}>
<Text style={defaultStyles.body}>{assignment.lastChanged}</Text>
<View style={{ marginTop: 10 }}>
<Text style={{ marginBottom: 4 }}>{progress}%</Text>
<View
style={{
width: "100%",
@@ -174,7 +196,6 @@ export default function ViewDetailsAssignment() {
/>
</View>
</View>
<Text style={defaultStyles.body}>{assignment.lastChanged}</Text>
<Button title="Edit" onPress={() => router.push({pathname: "/assignment/editAssignment", params: { aId: assignment.aId }})} />
<Button title="Delete" onPress={() => DeleteAssignment(assignment.aId)} />

View File

@@ -1,4 +1,5 @@
import { defaultStyles } from '@/constants/defaultStyles';
import { CheckSubjectCompletion } from '@/lib/progress';
import { supabase } from '@/lib/supabase';
import type { Assignment, Subject } from '@/lib/types';
import { Session } from '@supabase/supabase-js';
@@ -106,6 +107,15 @@ export default function ViewDetailsSubject() {
}
Alert.alert("Assignment deleted successfully!");
if (sId) {
try {
await CheckSubjectCompletion(sId);
} catch {
Alert.alert("Failed to update subject status");
}
}
GetAssignments(sId);
}
}
@@ -113,9 +123,7 @@ export default function ViewDetailsSubject() {
)
}
const CalculateSubjectCompletion = async () => {
}
const progress = assignments.length === 0 ? 0 : Math.round((assignments.filter(assignment => assignment.isCompleted).length / assignments.length) * 100);
return (
<View style={defaultStyles.container}>
@@ -155,6 +163,27 @@ export default function ViewDetailsSubject() {
{subject.isActive && <Text style={defaultStyles.checkboxMark}></Text>}
</View>
<Text style={defaultStyles.body}>{subject.lastChanged}</Text>
<View style={{ marginTop: 10 }}>
<Text style={{ marginBottom: 4 }}>{progress}%</Text>
<View
style={{
width: "100%",
height: 12,
backgroundColor: "#D9D9D9",
borderRadius: 999,
overflow: "hidden",
}}
>
<View
style={{
width: `${progress}%`,
height: "100%",
backgroundColor: "#4CAF50",
}}
/>
</View>
</View>
<Button title="Edit" onPress={() => router.push({pathname: "/subject/editSubject", params: { sId: subject.sId }})} />
<Button title="Delete" onPress={() => DeleteSubject(subject.sId)} />

View File

@@ -11,5 +11,19 @@ export async function CheckAssignmentCompletion(aId: string) {
const { error: updateError } = await supabase.from("assignments").update({ isCompleted: allCompleted, lastChanged: new Date().toISOString()}).eq("aId", aId);
if (updateError) throw updateError;
}
export async function CheckSubjectCompletion(sId: string) {
const { data, error } = await supabase.from("assignments").select("aId, isCompleted").eq("sId", sId);
if (error) throw error;
const assignments = data ?? [];
const allCompleted = assignments.length > 0 && assignments.every((assignment) => assignment.isCompleted === true);
const { error: updateError } = await supabase.from("subjects").update({ isActive: allCompleted, lastChanged: new Date().toISOString()}).eq("sId", sId);
if (updateError) throw updateError;
}

View File

@@ -0,0 +1,38 @@
# Progress Tracking (Assignments & Subjects)
## What was done
- Implemented progress tracking for both assignments and subjects
- Used `Task.isCompleted` as the source of truth
- Synced `Assignment.isCompleted` based on task completion
## Logic implemented
- Created `CheckAssignmentCompletion(aId)`
- Assignment is marked completed only if all its tasks are completed
- Assignment remains incomplete if:
- Any task is incomplete
- No tasks exist
## Data handling
- Fetched assignments from Supabase
- Fetched all related tasks using assignment IDs
- Grouped tasks by `aId` into `tasksByAssignment`
- Used grouped data to calculate progress efficiently
## Progress calculation
- Assignment progress:
- completed tasks / total tasks
- Subject progress:
- completed tasks across all assignments / total tasks
## UI work
- Added progress bars to:
- Assignment cards
- Subject views
- Used basic inline styling for progress bars
- Fixed layout issues caused by incorrect placement inside `flex-row`
- Moved progress bar into content column to prevent UI breaking
## Result
- Progress updates dynamically based on task completion
- Assignment completion stays in sync with tasks
- UI correctly displays both assignment and subject progress