Skip to content

Commit

Permalink
✨ (ContentKit): Add Authors + example
Browse files Browse the repository at this point in the history
  • Loading branch information
ladislas committed Feb 9, 2024
1 parent ca086b9 commit 673f333
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ name: contentkit-specs-jtd-tests-activity_good
status: published

authors:
- leka_at_apf_france_handicap
- leka
- aurore_kiesler
- julie_tuil

skills:
- spatial_understanding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,15 @@ struct MainView: View {

DisclosureGroup("**Authors**") {
ForEach(self.activity?.authors ?? [], id: \.self) { author in
Text(author)
let author = Authors.hmi(id: author)!
HStack {
Text(author.name)
Button {
self.selectedAuthor = author
} label: {
Image(systemName: "info.circle")
}
}
}
}

Expand Down Expand Up @@ -129,6 +137,13 @@ struct MainView: View {
Text(hmi.description)
}
})
.sheet(item: self.$selectedAuthor, onDismiss: { self.selectedAuthor = nil }, content: { author in
VStack(alignment: .leading) {
Text(author.name)
.font(.headline)
Text(author.description)
}
})
.onAppear {
self.activity = ContentKit.decodeActivity("activity")
print(self.activity ?? "not working")
Expand All @@ -148,13 +163,22 @@ struct MainView: View {
print("name: \(hmi.name)")
print("description: \(hmi.description)")
}

let authors = Authors.list
for (index, author) in authors.enumerated() {
print("author \(index + 1)")
print("id: \(author.id)")
print("name: \(author.name)")
print("description: \(author.description)")
}
}
}

// MARK: Private

@State private var selectedSkill: Skill?
@State private var selectedHMI: HMIDetails?
@State private var selectedAuthor: Author?

@State private var activity: Activity?
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/ContentKit/Resources/Content/authors/authors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ list:
name: Leka
website: https://leka.io
email: [email protected]
professions:
professions: []
l10n:
- locale: fr_FR
description: |
Expand Down
123 changes: 123 additions & 0 deletions Modules/ContentKit/Sources/Models/Authors.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Leka - iOS Monorepo
// Copyright APF France handicap
// SPDX-License-Identifier: Apache-2.0

import Foundation
import LocalizationKit
import LogKit
import Version
import Yams

// MARK: - Authors

public class Authors: Codable {
// MARK: Lifecycle

private init() {
self.container = Self.loadHMI()
}

// MARK: Public

public static var list: [Author] {
shared.container.list
}

public static func hmi(id: String) -> Author? {
self.list.first(where: { $0.id == id })
}

// MARK: Private

private struct AuthorsContainer: Codable {
let list: [Author]
}

private static let shared: Authors = .init()

private let container: AuthorsContainer

private static func loadHMI() -> AuthorsContainer {
if let fileURL = Bundle.module.url(forResource: "authors", withExtension: "yml") {
do {
let yamlString = try String(contentsOf: fileURL, encoding: .utf8)
let container = try YAMLDecoder().decode(AuthorsContainer.self, from: yamlString)
return container
} catch {
log.error("Failed to read YAML file: \(error)")
return AuthorsContainer(list: [])
}
} else {
log.error("hmi.yml not found")
return AuthorsContainer(list: [])
}
}
}

// MARK: - Author

public struct Author: 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.visibility = try container.decode(Visibility.self, forKey: .visibility)
self.name = try container.decode(String.self, forKey: .name)
self.website = try container.decode(String.self, forKey: .website)
self.email = try container.decode(String.self, forKey: .email)
self.professions = try container.decode([String].self, forKey: .professions)

self.l10n = try container.decode([Author.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.description = self.l10n.first(where: { $0.locale == currentLocale })!.description
}

// MARK: Public

public let id: String
public let visibility: Visibility
public let name: String
public let website: String
public let email: String
public let professions: [String]
public let description: String

// MARK: Private

private let l10n: [Localization]
}

// MARK: Author.Visibility

public extension Author {
enum Visibility: String, Codable {
case `public`
case `private`
}
}

// MARK: Author.Localization

public extension Author {
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.description = try container.decode(String.self, forKey: .description)
}

// MARK: Internal

let locale: Locale
let description: String
}
}

0 comments on commit 673f333

Please sign in to comment.