Skip to content

Commit

Permalink
✨ (ContentKit): Add activity type (one-one-one, group)
Browse files Browse the repository at this point in the history
  • Loading branch information
ladislas committed Feb 22, 2024
1 parent e32483f commit 05dfa70
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,27 @@ struct ActivityDetailsView: View {
Text(hmi.description)
}
})

DisclosureGroup("**Activity Types**") {
ForEach(self.activity.types, id: \.self) { type in
let type = ActivityTypes.type(id: type)!
HStack {
Text(type.name)
Button {
self.selectedType = type
} label: {
Image(systemName: "info.circle")
}
}
}
}
.sheet(item: self.$selectedType, onDismiss: { self.selectedType = nil }, content: { type in
VStack(alignment: .leading) {
Text(type.name)
.font(.headline)
Text(type.description)
}
})
}

Section {
Expand Down Expand Up @@ -130,6 +151,7 @@ struct ActivityDetailsView: View {
@State private var selectedAuthor: Author?
@State private var selectedSkill: Skill?
@State private var selectedHMI: HMIDetails?
@State private var selectedType: ActivityType?

private let navigation: Navigation = .shared
}
Expand Down
3 changes: 3 additions & 0 deletions Modules/ContentKit/Sources/Activity/Activity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

View workflow job for this annotation

GitHub Actions / swiftlint

TODOs should be resolved ((@ladislas) - implement author...) (todo)
public let skills: [String] // TODO: (@ladislas) - implement skills

Check warning on line 44 in Modules/ContentKit/Sources/Activity/Activity.swift

View workflow job for this annotation

GitHub Actions / swiftlint

TODOs should be resolved ((@ladislas) - implement skills) (todo)
public let hmi: [String] // TODO: (@ladislas) - implement hmi

Check warning on line 45 in Modules/ContentKit/Sources/Activity/Activity.swift

View workflow job for this annotation

GitHub Actions / swiftlint

TODOs should be resolved ((@ladislas) - implement hmi) (todo)
public let types: [String] // TODO: (@ladislas) - implement types

Check warning on line 46 in Modules/ContentKit/Sources/Activity/Activity.swift

View workflow job for this annotation

GitHub Actions / swiftlint

TODOs should be resolved ((@ladislas) - implement types) (todo)
public let tags: [String] // TODO: (@ladislas) - implement tags

Check warning on line 47 in Modules/ContentKit/Sources/Activity/Activity.swift

View workflow job for this annotation

GitHub Actions / swiftlint

TODOs should be resolved ((@ladislas) - implement tags) (todo)

public let locales: [Locale]
Expand Down Expand Up @@ -73,6 +75,7 @@ public struct Activity: Decodable, Identifiable {
case authors
case skills
case hmi
case types
case tags
case status
case locales
Expand Down
4 changes: 4 additions & 0 deletions Modules/ContentKit/Sources/Mocks/Activity+Mock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public extension Activity {
- magic_cards
- tablet_robot
types:
- one_on_one
- group
locales:
- en_US
- fr_FR
Expand Down
108 changes: 108 additions & 0 deletions Modules/ContentKit/Sources/Models/ActivityTypes.swift
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
}
}

0 comments on commit 05dfa70

Please sign in to comment.