001project_wildgrowth/ios/WildGrowth/WildGrowth/LearningService.swift

66 lines
2.3 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Foundation
class LearningService {
static let shared = LearningService()
private let apiClient = APIClient.shared
//
func getNodeContent(nodeId: String) async throws -> LessonData {
// Token
// Token isLoggedIn Token
let hasToken = TokenManager.shared.getToken() != nil
let response: LessonResponse = try await apiClient.request(
endpoint: "/api/lessons/\(nodeId)/content",
method: "GET",
requiresAuth: hasToken // Token
)
return response.data
}
//
func startLesson(nodeId: String) async throws {
let _: StartLessonResponse = try await apiClient.request(
endpoint: "/api/lessons/\(nodeId)/start",
method: "POST",
requiresAuth: true
)
}
//
func updateProgress(nodeId: String, currentSlide: Int, studyTime: Int) async {
do {
let _: SimpleResponse = try await apiClient.request(
endpoint: "/api/lessons/\(nodeId)/progress",
method: "POST",
body: ["current_slide": currentSlide, "study_time": studyTime],
requiresAuth: true
)
} catch {
print("Progress update failed: \(error)")
}
}
//
func completeLesson(nodeId: String, totalStudyTime: Int, completedSlides: Int) async throws -> CompleteLessonData {
let response: CompleteLessonResponse = try await apiClient.request(
endpoint: "/api/lessons/\(nodeId)/complete",
method: "POST",
body: ["total_study_time": totalStudyTime, "completed_slides": completedSlides],
requiresAuth: true
)
return response.data
}
//
func getLessonSummary(nodeId: String) async throws -> LessonSummaryData {
let response: LessonSummaryResponse = try await apiClient.request(
endpoint: "/api/lessons/\(nodeId)/summary",
method: "GET",
requiresAuth: true
)
return response.data
}
}