loads of polish and bug fixes
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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
37
lib/sessionLifecycle.ts
Normal 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,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user