From 885be4aaaf23aa12082be4366e2ca85db15979b2 Mon Sep 17 00:00:00 2001 From: Ladislas de Toldi Date: Thu, 15 Feb 2024 13:07:37 +0100 Subject: [PATCH] :sparkles: (ContentKit): List sample activities --- Modules/ContentKit/Sources/ContentKit.swift | 31 ++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/Modules/ContentKit/Sources/ContentKit.swift b/Modules/ContentKit/Sources/ContentKit.swift index 098329ee18..766cd879d1 100644 --- a/Modules/ContentKit/Sources/ContentKit.swift +++ b/Modules/ContentKit/Sources/ContentKit.swift @@ -2,10 +2,39 @@ // Copyright APF France handicap // SPDX-License-Identifier: Apache-2.0 +import Foundation import LogKit +import Yams let log = LogKit.createLoggerFor(module: "ContentKit") // MARK: - ContentKit -public enum ContentKit {} +public enum ContentKit { + public static func listSampleActivities() -> [Activity]? { + let bundle = Bundle.module + let files = bundle.paths(forResourcesOfType: "activity.yml", inDirectory: nil) + + var activities: [Activity] = [] + + for file in files { + let data = try? String(contentsOfFile: file, encoding: .utf8) + + guard let data else { + log.error("Error reading file: \(file)") + continue + } + + let activity = try? YAMLDecoder().decode(Activity.self, from: data) + + guard let activity else { + log.error("Error decoding file: \(file)") + continue + } + + activities.append(activity) + } + + return activities.sorted { $0.name < $1.name } + } +}