Skip to content

Commit

Permalink
🔀 Merge branch 'mathieu/feature/Finalise-Caregivers-Data-Model'
Browse files Browse the repository at this point in the history
  • Loading branch information
ladislas committed Feb 21, 2024
2 parents 4a7abc9 + 7cb8f77 commit 8d2972c
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 41 deletions.
7 changes: 5 additions & 2 deletions Modules/AccountKit/Sources/Database/DatabaseOperations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import Combine
import FirebaseAuth
import FirebaseFirestore
import FirebaseFirestoreSwift

public class DatabaseOperations {
// MARK: Lifecycle
Expand All @@ -19,10 +20,12 @@ public class DatabaseOperations {

public func create(data: some AccountDocument, in collection: DatabaseCollection) -> AnyPublisher<String, Error> {
Future<String, Error> { promise in
let docRef = self.database.collection(collection.rawValue).document()
var documentData = data
documentData.rootOwnerUid = Auth.auth().currentUser?.uid ?? ""

let docRef = self.database.collection(collection.rawValue).document()
documentData.id = docRef.documentID
documentData.createdAt = Date()
documentData.lastEditedAt = Date()

do {
try docRef.setData(from: documentData) { error in
Expand Down
32 changes: 32 additions & 0 deletions Modules/AccountKit/Sources/Extensions/ColorScheme+Codable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Leka - iOS Monorepo
// Copyright APF France handicap
// SPDX-License-Identifier: Apache-2.0

import SwiftUI

// MARK: - ColorScheme + Codable, RawRepresentabl

extension ColorScheme: Codable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let rawValue = try container.decode(String.self)
switch rawValue {
case "light": self = .light
case "dark": self = .dark
default: fatalError("Invalid ColorScheme")
}
}

public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(self.rawValue)
}

public var rawValue: String {
switch self {
case .light: return "light"
case .dark: return "dark"
@unknown default: return "light"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Leka - iOS Monorepo
// Copyright APF France handicap
// SPDX-License-Identifier: Apache-2.0

import SwiftUI

public extension Caregiver {
init(rootOwnerUid: String = "",
firstName: String = "",
lastName: String = "",
email: String = "",
avatar: String = "",
professions: [String] = [],
colorScheme: ColorScheme = .light,
colorTheme: ColorTheme = .darkBlue)
{
self.rootOwnerUid = rootOwnerUid
self.firstName = firstName
self.lastName = lastName
self.email = email
self.avatar = avatar
self.professions = professions
self.colorScheme = colorScheme
self.colorTheme = colorTheme
}
}
71 changes: 71 additions & 0 deletions Modules/AccountKit/Sources/Models/CaregiverModel/Caregiver.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Leka - iOS Monorepo
// Copyright APF France handicap
// SPDX-License-Identifier: Apache-2.0

import DesignKit
import FirebaseFirestore
import SwiftUI

// MARK: - Caregiver

public struct Caregiver: AccountDocument {
// MARK: Lifecycle

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.rootOwnerUid = try container.decode(String.self, forKey: .rootOwnerUid)
self.firstName = try container.decode(String.self, forKey: .firstName)
self.lastName = try container.decode(String.self, forKey: .lastName)
self.email = try container.decode(String.self, forKey: .email)
self.avatar = try container.decode(String.self, forKey: .avatar)
self.professions = try container.decode([String].self, forKey: .professions)
self.colorScheme = try container.decode(ColorScheme.self, forKey: .colorScheme)
self.colorTheme = try container.decode(ColorTheme.self, forKey: .colorTheme)
}

// MARK: Public

@DocumentID public var id: String?
@ServerTimestamp public var createdAt: Date?
@ServerTimestamp public var lastEditedAt: Date?

public var rootOwnerUid: String
public var firstName: String
public var lastName: String
public var email: String
public var avatar: String
public var professions: [String]
public var colorScheme: ColorScheme
public var colorTheme: ColorTheme

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.id, forKey: .id)
try container.encode(self.rootOwnerUid, forKey: .rootOwnerUid)
try container.encode(self.createdAt, forKey: .createdAt)
try container.encode(self.lastEditedAt, forKey: .lastEditedAt)
try container.encode(self.firstName, forKey: .firstName)
try container.encode(self.lastName, forKey: .lastName)
try container.encode(self.email, forKey: .email)
try container.encode(self.avatar, forKey: .avatar)
try container.encode(self.professions, forKey: .professions)
try container.encode(self.colorScheme, forKey: .colorScheme)
try container.encode(self.colorTheme, forKey: .colorTheme)
}

// MARK: Internal

enum CodingKeys: String, CodingKey {
case id
case rootOwnerUid = "root_owner_uid"
case createdAt = "created_at"
case lastEditedAt = "last_edited_at"
case firstName = "first_name"
case lastName = "last_name"
case email
case avatar
case professions
case colorScheme = "color_scheme"
case colorTheme = "color_theme"
}
}
32 changes: 32 additions & 0 deletions Modules/AccountKit/Sources/Models/ColorTheme.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Leka - iOS Monorepo
// Copyright APF France handicap
// SPDX-License-Identifier: Apache-2.0

import DesignKit
import SwiftUI

public enum ColorTheme: String, Codable {
case darkBlue
case blue
case purple
case red
case orange
case yellow
case green
case gray

// MARK: Public

public var color: Color {
switch self {
case .darkBlue: DesignKitAsset.Colors.lekaDarkBlue.swiftUIColor
case .blue: .blue
case .purple: .purple
case .red: .red
case .orange: .orange
case .yellow: .yellow
case .green: .green
case .gray: .gray
}
}
}
38 changes: 0 additions & 38 deletions Modules/AccountKit/Sources/Models/ProfileDataModels.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct RootAccount: AccountDocument {
}

@DocumentID var id: String?
var rootOwnerUid: String
@ServerTimestamp var createdAt: Date?
@ServerTimestamp var lastEditedAt: Date?
var rootOwnerUid: String
}

0 comments on commit 8d2972c

Please sign in to comment.