loads of polish and bug fixes

This commit is contained in:
Chris Sanden
2026-05-05 15:41:44 +02:00
parent a4f99a50d0
commit 2bb2ac63a0
8 changed files with 379 additions and 53 deletions

View File

@@ -3,15 +3,24 @@ import type { SessionType } from '@/lib/types';
const notificationKey = (aId: string) => `assignment_notification_${aId}`;
const activeSprintKey = 'active_sprint';
const studyCycleKey = 'study_cycle';
export type ActiveSession = {
sessionId: string;
sessionType: SessionType;
taskId: string | null;
returnTaskId?: string | null;
durationSeconds: number;
endTime: number;
};
export type StudyCycle = {
taskId: string;
completedFocusSessions: number;
lastCompletedSessionType: SessionType;
lastCompletedAt: number;
};
export async function SaveAssignmentNotificationId(aId: string, notificationId: string) {
await AsyncStorage.setItem(notificationKey(aId), notificationId);
}
@@ -41,3 +50,21 @@ export async function GetActiveSession() {
export async function RemoveActiveSession() {
await AsyncStorage.removeItem(activeSprintKey);
}
export async function SaveStudyCycle(studyCycle: StudyCycle) {
await AsyncStorage.setItem(studyCycleKey, JSON.stringify(studyCycle));
}
export async function GetStudyCycle() {
const studyCycle = await AsyncStorage.getItem(studyCycleKey);
if (!studyCycle) {
return null;
}
return JSON.parse(studyCycle) as StudyCycle;
}
export async function RemoveStudyCycle() {
await AsyncStorage.removeItem(studyCycleKey);
}

View File

@@ -1,2 +1,5 @@
export const DEFAULT_FOCUS_DURATION_MINUTES = 25;
export const DEFAULT_SHORT_BREAK_DURATION_MINUTES = 5;
export const DEFAULT_LONG_BREAK_DURATION_MINUTES = 15;
export const FOCUS_SESSIONS_PER_LONG_BREAK = 4;
export const STUDY_CYCLE_IDLE_RESET_MINUTES = 120;

37
lib/sessionLifecycle.ts Normal file
View File

@@ -0,0 +1,37 @@
import {
GetActiveSession,
RemoveActiveSession,
RemoveStudyCycle,
type ActiveSession,
} from '@/lib/asyncStorage';
import { supabase } from '@/lib/supabase';
export type FinalSessionStatus = 'completed' | 'cancelled' | 'expired';
export async function finalizeStoredSession(
finalStatus: FinalSessionStatus,
activeSessionOverride?: ActiveSession | null
) {
const activeSession = activeSessionOverride ?? await GetActiveSession();
if (!activeSession) {
return null;
}
await RemoveActiveSession();
if (finalStatus !== 'completed') {
await RemoveStudyCycle();
}
const { error } = await supabase.rpc('finalize_sprint_session', {
p_session_id: activeSession.sessionId,
p_final_status: finalStatus,
p_ended_at: new Date().toISOString(),
});
return {
activeSession,
error,
};
}