Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔥 (AccountKit): Remove DatabaseDocument from all Library Types #1641

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions Modules/AccountKit/Sources/Extensions/Library+Codable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,24 @@
// Copyright APF France handicap
// SPDX-License-Identifier: Apache-2.0

import FirebaseFirestore
import SwiftUI

extension Library: Codable {
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

self.id = try container.decodeIfPresent(String.self, forKey: .id)
self.rootOwnerUid = try container.decode(String.self, forKey: .rootOwnerUid)
self.savedActivities = try container.decodeIfPresent([SavedActivity].self, forKey: .savedActivities) ?? []
self.savedCurriculums = try container.decodeIfPresent([SavedCurriculum].self, forKey: .savedCurriculums) ?? []
self.savedStories = try container.decodeIfPresent([SavedStory].self, forKey: .savedStories) ?? []
self.savedGamepads = try container.decodeIfPresent([SavedGamepad].self, forKey: .savedGamepads) ?? []
self.createdAt = try container.decodeIfPresent(Timestamp.self, forKey: .createdAt)?.dateValue()
self.lastEditedAt = try container.decodeIfPresent(Timestamp.self, forKey: .lastEditedAt)?.dateValue()
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encodeIfPresent(self.id, forKey: .id)
try container.encode(self.rootOwnerUid, forKey: .rootOwnerUid)
try container.encode(self.savedActivities, forKey: .savedActivities)
try container.encode(self.savedCurriculums, forKey: .savedCurriculums)
try container.encode(self.savedStories, forKey: .savedStories)
try container.encode(self.savedGamepads, forKey: .savedGamepads)
try container.encodeIfPresent(self.createdAt.map { Timestamp(date: $0) }, forKey: .createdAt)
try container.encodeIfPresent(self.lastEditedAt.map { Timestamp(date: $0) }, forKey: .lastEditedAt)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public extension RootAccount {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.id = try container.decodeIfPresent(String.self, forKey: .id)
self.rootOwnerUid = try container.decode(String.self, forKey: .rootOwnerUid)
self.library = try container.decodeIfPresent(Library.self, forKey: .library) ?? Library()
self.library = try container.decode(Library.self, forKey: .library)
self.createdAt = try container.decodeIfPresent(Date.self, forKey: .createdAt)
self.lastEditedAt = try container.decodeIfPresent(Date.self, forKey: .lastEditedAt)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@ extension SavedActivity: Codable {
let container = try decoder.container(keyedBy: CodingKeys.self)

self.id = try container.decodeIfPresent(String.self, forKey: .id)
self.rootOwnerUid = try container.decode(String.self, forKey: .rootOwnerUid)
self.caregiverID = try container.decodeIfPresent(String.self, forKey: .caregiverID) ?? ""
self.createdAt = try container.decodeIfPresent(Timestamp.self, forKey: .createdAt)?.dateValue()
self.lastEditedAt = try container.decodeIfPresent(Timestamp.self, forKey: .lastEditedAt)?.dateValue()
self.createdAt = try container.decode(Date.self, forKey: .createdAt)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encodeIfPresent(self.id, forKey: .id)
try container.encode(self.rootOwnerUid, forKey: .rootOwnerUid)
try container.encode(self.caregiverID, forKey: .caregiverID)
try container.encodeIfPresent(self.createdAt.map { Timestamp(date: $0) }, forKey: .createdAt)
try container.encodeIfPresent(self.lastEditedAt.map { Timestamp(date: $0) }, forKey: .lastEditedAt)
try container.encode(self.createdAt, forKey: .createdAt)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@ extension SavedCurriculum: Codable {
let container = try decoder.container(keyedBy: CodingKeys.self)

self.id = try container.decodeIfPresent(String.self, forKey: .id)
self.rootOwnerUid = try container.decode(String.self, forKey: .rootOwnerUid)
self.caregiverID = try container.decodeIfPresent(String.self, forKey: .caregiverID) ?? ""
self.createdAt = try container.decodeIfPresent(Timestamp.self, forKey: .createdAt)?.dateValue()
self.lastEditedAt = try container.decodeIfPresent(Timestamp.self, forKey: .lastEditedAt)?.dateValue()
self.createdAt = try container.decode(Date.self, forKey: .createdAt)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encodeIfPresent(self.id, forKey: .id)
try container.encode(self.rootOwnerUid, forKey: .rootOwnerUid)
try container.encode(self.caregiverID, forKey: .caregiverID)
try container.encodeIfPresent(self.createdAt.map { Timestamp(date: $0) }, forKey: .createdAt)
try container.encodeIfPresent(self.lastEditedAt.map { Timestamp(date: $0) }, forKey: .lastEditedAt)
try container.encodeIfPresent(self.createdAt, forKey: .createdAt)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@ extension SavedGamepad: Codable {
let container = try decoder.container(keyedBy: CodingKeys.self)

self.id = try container.decodeIfPresent(String.self, forKey: .id)
self.rootOwnerUid = try container.decode(String.self, forKey: .rootOwnerUid)
self.caregiverID = try container.decodeIfPresent(String.self, forKey: .caregiverID) ?? ""
self.createdAt = try container.decodeIfPresent(Timestamp.self, forKey: .createdAt)?.dateValue()
self.lastEditedAt = try container.decodeIfPresent(Timestamp.self, forKey: .lastEditedAt)?.dateValue()
self.createdAt = try container.decode(Date.self, forKey: .createdAt)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encodeIfPresent(self.id, forKey: .id)
try container.encode(self.rootOwnerUid, forKey: .rootOwnerUid)
try container.encode(self.caregiverID, forKey: .caregiverID)
try container.encodeIfPresent(self.createdAt.map { Timestamp(date: $0) }, forKey: .createdAt)
try container.encodeIfPresent(self.lastEditedAt.map { Timestamp(date: $0) }, forKey: .lastEditedAt)
try container.encode(self.createdAt, forKey: .createdAt)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@ extension SavedStory: Codable {
let container = try decoder.container(keyedBy: CodingKeys.self)

self.id = try container.decodeIfPresent(String.self, forKey: .id)
self.rootOwnerUid = try container.decode(String.self, forKey: .rootOwnerUid)
self.caregiverID = try container.decodeIfPresent(String.self, forKey: .caregiverID) ?? ""
self.createdAt = try container.decodeIfPresent(Timestamp.self, forKey: .createdAt)?.dateValue()
self.lastEditedAt = try container.decodeIfPresent(Timestamp.self, forKey: .lastEditedAt)?.dateValue()
self.createdAt = try container.decode(Date.self, forKey: .createdAt)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encodeIfPresent(self.id, forKey: .id)
try container.encode(self.rootOwnerUid, forKey: .rootOwnerUid)
try container.encode(self.caregiverID, forKey: .caregiverID)
try container.encodeIfPresent(self.createdAt.map { Timestamp(date: $0) }, forKey: .createdAt)
try container.encodeIfPresent(self.lastEditedAt.map { Timestamp(date: $0) }, forKey: .lastEditedAt)
try container.encode(self.createdAt, forKey: .createdAt)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class RootAccountManager {
return
}

let savedActivity = SavedActivity(id: activityID, rootOwnerUid: rootAccount.rootOwnerUid, caregiverID: caregiverID)
let savedActivity = SavedActivity(id: activityID, caregiverID: caregiverID)

guard !rootAccount.library.savedActivities.contains(where: { $0.id == activityID }) else {
log.info("\(activityID) is already saved.")
Expand Down Expand Up @@ -99,7 +99,7 @@ public class RootAccountManager {
return
}

let savedCurriculum = SavedCurriculum(id: curriculumID, rootOwnerUid: rootAccount.rootOwnerUid, caregiverID: caregiverID)
let savedCurriculum = SavedCurriculum(id: curriculumID, caregiverID: caregiverID)

guard !rootAccount.library.savedCurriculums.contains(where: { $0.id == curriculumID }) else {
log.info("\(curriculumID) is already saved.")
Expand Down Expand Up @@ -133,7 +133,7 @@ public class RootAccountManager {
return
}

let savedStory = SavedStory(id: storyID, rootOwnerUid: rootAccount.rootOwnerUid, caregiverID: caregiverID)
let savedStory = SavedStory(id: storyID, caregiverID: caregiverID)

guard !rootAccount.library.savedStories.contains(where: { $0.id == storyID }) else {
log.info("\(storyID) is already saved.")
Expand Down Expand Up @@ -167,7 +167,7 @@ public class RootAccountManager {
return
}

let savedGamepad = SavedGamepad(id: gamepadID, rootOwnerUid: rootAccount.rootOwnerUid, caregiverID: caregiverID)
let savedGamepad = SavedGamepad(id: gamepadID, caregiverID: caregiverID)

guard !rootAccount.library.savedGamepads.contains(where: { $0.id == gamepadID }) else {
log.info("\(gamepadID) is already saved.")
Expand Down

This file was deleted.

25 changes: 15 additions & 10 deletions Modules/AccountKit/Sources/Models/RootAccount/Library/Library.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@ import SwiftUI

// MARK: - SavedActivity

public struct Library: DatabaseDocument, Hashable {
// MARK: Public
public struct Library: Hashable {
// MARK: Lifecycle

public init(
savedActivities: [SavedActivity] = [],
savedCurriculums: [SavedCurriculum] = [],
savedStories: [SavedStory] = [],
savedGamepads: [SavedGamepad] = []
) {
self.savedActivities = savedActivities
self.savedCurriculums = savedCurriculums
self.savedStories = savedStories
self.savedGamepads = savedGamepads
}

@ServerTimestamp public var createdAt: Date?
@ServerTimestamp public var lastEditedAt: Date?
// MARK: Public

public var id: String?
public var rootOwnerUid: String
public var savedActivities: [SavedActivity]
public var savedCurriculums: [SavedCurriculum]
public var savedStories: [SavedStory]
Expand All @@ -23,13 +32,9 @@ public struct Library: DatabaseDocument, Hashable {
// MARK: Internal

enum CodingKeys: String, CodingKey {
case id = "uuid"
case rootOwnerUid = "root_owner_uid"
case savedActivities = "saved_activities"
case savedCurriculums = "saved_curriculums"
case savedStories = "saved_stories"
case savedGamepads = "saved_gamepads"
case createdAt = "created_at"
case lastEditedAt = "last_edited_at"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ import SwiftUI
public extension SavedActivity {
init(
id: String? = nil,
rootOwnerUid: String = "",
caregiverID: String
) {
self.id = id
self.rootOwnerUid = rootOwnerUid
self.caregiverID = caregiverID
self.createdAt = nil
self.lastEditedAt = nil
self.createdAt = Date()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,18 @@ import SwiftUI

// MARK: - SavedActivity

public struct SavedActivity: DatabaseDocument, Hashable {
public struct SavedActivity: Hashable {
// MARK: Public

@ServerTimestamp public var createdAt: Date?
@ServerTimestamp public var lastEditedAt: Date?

public var id: String?
public var rootOwnerUid: String
public var caregiverID: String
public var createdAt: Date

// MARK: Internal

enum CodingKeys: String, CodingKey {
case id = "uuid"
case rootOwnerUid = "root_owner_uid"
case caregiverID = "caregiver_id"
case createdAt = "created_at"
case lastEditedAt = "last_edited_at"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ import SwiftUI
public extension SavedCurriculum {
init(
id: String? = nil,
rootOwnerUid: String = "",
caregiverID: String
) {
self.id = id
self.rootOwnerUid = rootOwnerUid
self.caregiverID = caregiverID
self.createdAt = nil
self.lastEditedAt = nil
self.createdAt = Date()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,18 @@ import SwiftUI

// MARK: - SavedCurriculum

public struct SavedCurriculum: DatabaseDocument, Hashable {
public struct SavedCurriculum: Hashable {
// MARK: Public

@ServerTimestamp public var createdAt: Date?
@ServerTimestamp public var lastEditedAt: Date?

public var id: String?
public var rootOwnerUid: String
public var caregiverID: String
public var createdAt: Date

// MARK: Internal

enum CodingKeys: String, CodingKey {
case id = "uuid"
case rootOwnerUid = "root_owner_uid"
case caregiverID = "caregiver_id"
case createdAt = "created_at"
case lastEditedAt = "last_edited_at"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ import SwiftUI
public extension SavedGamepad {
init(
id: String? = nil,
rootOwnerUid: String = "",
caregiverID: String
) {
self.id = id
self.rootOwnerUid = rootOwnerUid
self.caregiverID = caregiverID
self.createdAt = nil
self.lastEditedAt = nil
self.createdAt = Date()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,18 @@ import SwiftUI

// MARK: - SavedGamepad

public struct SavedGamepad: DatabaseDocument, Hashable {
public struct SavedGamepad: Hashable {
// MARK: Public

@ServerTimestamp public var createdAt: Date?
@ServerTimestamp public var lastEditedAt: Date?

public var id: String?
public var rootOwnerUid: String
public var caregiverID: String
public var createdAt: Date

// MARK: Internal

enum CodingKeys: String, CodingKey {
case id = "uuid"
case rootOwnerUid = "root_owner_uid"
case caregiverID = "caregiver_id"
case createdAt = "created_at"
case lastEditedAt = "last_edited_at"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ import SwiftUI
public extension SavedStory {
init(
id: String? = nil,
rootOwnerUid: String = "",
caregiverID: String
) {
self.id = id
self.rootOwnerUid = rootOwnerUid
self.caregiverID = caregiverID
self.createdAt = nil
self.lastEditedAt = nil
self.createdAt = Date()
}
}
Loading
Loading