Skip to content

Commit

Permalink
Add SessionMonitor for session management and update data model
Browse files Browse the repository at this point in the history
  • Loading branch information
kasianov-mikhail committed Dec 13, 2024
1 parent 3f98803 commit 37c7540
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
59 changes: 59 additions & 0 deletions Sources/Scout/Core/SessionMonitor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// Copyright 2024 Mikhail Kasianov
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

import CoreData

struct SessionMonitor {

/// Registers a new session.
///
/// This method is responsible for initializing and registering a new session
/// within the application. It sets up necessary configurations and ensures
/// that the session is properly tracked and managed.
///
static func trigger() async throws {
try await persistentContainer.performBackgroundTask { context in
let session = Session(context: context)
session.startDate = Date()
session.uuid = UUID()
try context.save()
}
}

/// An error that occurs when completing a session.
/// - sessionNotFound: The session to be completed was not found.
///
enum CompleteError: LocalizedError {
case sessionNotFound

var errorDescription: String? {
switch self {
case .sessionNotFound:
return "Session not found"
}
}
}

/// Completes the current session by performing necessary cleanup and finalization tasks.
/// This method should be called when the session is ready to be terminated.
///
static func complete() async throws {
try await persistentContainer.performBackgroundTask { context in
let request = NSFetchRequest<Session>(entityName: "Session")
request.predicate = NSPredicate(format: "endDate == nil")
request.sortDescriptors = [NSSortDescriptor(key: "startDate", ascending: false)]
request.fetchLimit = 1

guard let session = try context.fetch(request).first else {
throw CompleteError.sessionNotFound
}

session.endDate = Date()
try context.save()
}
}
}
7 changes: 6 additions & 1 deletion Sources/Scout/Scout.xcdatamodeld/Scout.xcdatamodel/contents
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="23507" systemVersion="24B83" minimumToolsVersion="Automatic" sourceLanguage="Swift" usedWithSwiftData="YES" userDefinedModelVersionIdentifier="">
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="23605" systemVersion="24C101" minimumToolsVersion="Automatic" sourceLanguage="Swift" usedWithSwiftData="YES" userDefinedModelVersionIdentifier="">
<entity name="EventModel" representedClassName="EventModel" syncable="YES" codeGenerationType="class">
<attribute name="date" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="hour" attributeType="Date" usesScalarValueType="NO"/>
Expand All @@ -12,4 +12,9 @@
<attribute name="uuid" attributeType="UUID" usesScalarValueType="NO"/>
<attribute name="week" attributeType="Date" usesScalarValueType="NO"/>
</entity>
<entity name="Session" representedClassName="Session" syncable="YES" codeGenerationType="class">
<attribute name="endDate" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="startDate" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="uuid" attributeType="UUID" usesScalarValueType="NO"/>
</entity>
</model>

0 comments on commit 37c7540

Please sign in to comment.