-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ (ContentKit): Add activity type (one-one-one, group)
- Loading branch information
Showing
4 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ public struct Activity: Decodable, Identifiable { | |
self.authors = try container.decode([String].self, forKey: .authors) | ||
self.skills = try container.decode([String].self, forKey: .skills) | ||
self.hmi = try container.decode([String].self, forKey: .hmi) | ||
self.types = try container.decode([String].self, forKey: .types) | ||
self.tags = try container.decode([String].self, forKey: .tags) | ||
|
||
let localeStrings = try container.decode([String].self, forKey: .locales) | ||
|
@@ -42,6 +43,7 @@ public struct Activity: Decodable, Identifiable { | |
public let authors: [String] // TODO: (@ladislas) - implement authors | ||
Check warning on line 43 in Modules/ContentKit/Sources/Activity/Activity.swift GitHub Actions / swiftlint
|
||
public let skills: [String] // TODO: (@ladislas) - implement skills | ||
Check warning on line 44 in Modules/ContentKit/Sources/Activity/Activity.swift GitHub Actions / swiftlint
|
||
public let hmi: [String] // TODO: (@ladislas) - implement hmi | ||
Check warning on line 45 in Modules/ContentKit/Sources/Activity/Activity.swift GitHub Actions / swiftlint
|
||
public let types: [String] // TODO: (@ladislas) - implement types | ||
Check warning on line 46 in Modules/ContentKit/Sources/Activity/Activity.swift GitHub Actions / swiftlint
|
||
public let tags: [String] // TODO: (@ladislas) - implement tags | ||
Check warning on line 47 in Modules/ContentKit/Sources/Activity/Activity.swift GitHub Actions / swiftlint
|
||
|
||
public let locales: [Locale] | ||
|
@@ -73,6 +75,7 @@ public struct Activity: Decodable, Identifiable { | |
case authors | ||
case skills | ||
case hmi | ||
case types | ||
case tags | ||
case status | ||
case locales | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Leka - iOS Monorepo | ||
// Copyright APF France handicap | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import Foundation | ||
import LocalizationKit | ||
import LogKit | ||
import Version | ||
import Yams | ||
|
||
// MARK: - ActivityTypes | ||
|
||
public class ActivityTypes: Codable { | ||
// MARK: Lifecycle | ||
|
||
private init() { | ||
self.container = Self.loadTypes() | ||
} | ||
|
||
// MARK: Public | ||
|
||
public static var list: [ActivityType] { | ||
shared.container.list | ||
} | ||
|
||
public static func type(id: String) -> ActivityType? { | ||
self.list.first(where: { $0.id == id }) | ||
} | ||
|
||
// MARK: Private | ||
|
||
private struct TypesContainer: Codable { | ||
let list: [ActivityType] | ||
} | ||
|
||
private static let shared: ActivityTypes = .init() | ||
|
||
private let container: TypesContainer | ||
|
||
private static func loadTypes() -> TypesContainer { | ||
if let fileURL = Bundle.module.url(forResource: "activity_types", withExtension: "yml") { | ||
do { | ||
let yamlString = try String(contentsOf: fileURL, encoding: .utf8) | ||
let container = try YAMLDecoder().decode(TypesContainer.self, from: yamlString) | ||
return container | ||
} catch { | ||
log.error("Failed to read YAML file: \(error)") | ||
return TypesContainer(list: []) | ||
} | ||
} else { | ||
log.error("activity_types.yml not found") | ||
return TypesContainer(list: []) | ||
} | ||
} | ||
} | ||
|
||
// MARK: - ActivityType | ||
|
||
public struct ActivityType: Codable, Identifiable { | ||
// MARK: Lifecycle | ||
|
||
public init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
self.id = try container.decode(String.self, forKey: .id) | ||
|
||
self.l10n = try container.decode([ActivityType.Localization].self, forKey: .l10n) | ||
|
||
let availableLocales = self.l10n.map(\.locale) | ||
|
||
let currentLocale = availableLocales.first(where: { | ||
$0.language.languageCode == LocalizationKit.l10n.language | ||
}) ?? Locale(identifier: "en_US") | ||
|
||
self.name = self.l10n.first(where: { $0.locale == currentLocale })!.name | ||
self.description = self.l10n.first(where: { $0.locale == currentLocale })!.description | ||
} | ||
|
||
// MARK: Public | ||
|
||
public let id: String | ||
public let name: String | ||
public let description: String | ||
|
||
// MARK: Private | ||
|
||
private let l10n: [Localization] | ||
} | ||
|
||
// MARK: ActivityType.Localization | ||
|
||
public extension ActivityType { | ||
struct Localization: Codable { | ||
// MARK: Lifecycle | ||
|
||
public init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
self.locale = try Locale(identifier: container.decode(String.self, forKey: .locale)) | ||
self.name = try container.decode(String.self, forKey: .name) | ||
self.description = try container.decode(String.self, forKey: .description) | ||
} | ||
|
||
// MARK: Internal | ||
|
||
let locale: Locale | ||
let name: String | ||
let description: String | ||
} | ||
} |