-
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.
✨ (AccountKit): Add LibraryManagerViewModel
- Loading branch information
Showing
1 changed file
with
150 additions
and
0 deletions.
There are no files selected for viewing
150 changes: 150 additions & 0 deletions
150
Modules/AccountKit/Sources/Managers/Library/LibraryManagerViewModel.swift
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,150 @@ | ||
// Leka - iOS Monorepo | ||
// Copyright APF France handicap | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import Combine | ||
import SwiftUI | ||
|
||
public class LibraryManagerViewModel: ObservableObject { | ||
// MARK: Lifecycle | ||
|
||
public init() { | ||
self.subscribeToManager() | ||
} | ||
|
||
// MARK: Public | ||
|
||
@Published public var currentLibrary: Library? | ||
@Published public var savedActivities: [SavedActivity] = [] | ||
@Published public var savedCurriculums: [SavedCurriculum] = [] | ||
@Published public var savedStories: [SavedStory] = [] | ||
@Published public var savedGamepads: [SavedGamepad] = [] | ||
|
||
@Published public var errorMessage: String = "" | ||
@Published public var showErrorAlert: Bool = false | ||
@Published public var isLoading: Bool = false | ||
|
||
// MARK: - Activities | ||
|
||
public func addSavedActivity(activityID: String, caregiverID: String) { | ||
self.libraryManager.addSavedActivity(activityID: activityID, caregiverID: caregiverID) | ||
} | ||
|
||
public func removeSavedActivity(activityID: String) { | ||
self.libraryManager.removeSavedActivity(activityID: activityID) | ||
} | ||
|
||
public func isActivitySaved(activityID: String) -> Bool { | ||
self.savedActivities.contains(where: { $0.id == activityID }) | ||
} | ||
|
||
// MARK: - Curriculums | ||
|
||
public func addSavedCurriculum(curriculumID: String, caregiverID: String) { | ||
self.libraryManager.addSavedCurriculum(curriculumID: curriculumID, caregiverID: caregiverID) | ||
} | ||
|
||
public func removeSavedCurriculum(curriculumID: String) { | ||
self.libraryManager.removeSavedCurriculum(curriculumID: curriculumID) | ||
} | ||
|
||
public func isCurriculumSaved(curriculumID: String) -> Bool { | ||
self.savedCurriculums.contains(where: { $0.id == curriculumID }) | ||
} | ||
|
||
// MARK: - Stories | ||
|
||
public func addSavedStory(storyID: String, caregiverID: String) { | ||
self.libraryManager.addSavedStory(storyID: storyID, caregiverID: caregiverID) | ||
} | ||
|
||
public func removeSavedStory(storyID: String) { | ||
self.libraryManager.removeSavedStory(storyID: storyID) | ||
} | ||
|
||
public func isStorySaved(storyID: String) -> Bool { | ||
self.savedStories.contains(where: { $0.id == storyID }) | ||
} | ||
|
||
// MARK: - Gamepads | ||
|
||
public func addSavedGamepad(gamepadID: String, caregiverID: String) { | ||
self.libraryManager.addSavedGamepad(gamepadID: gamepadID, caregiverID: caregiverID) | ||
} | ||
|
||
public func removeSavedGamepad(gamepadID: String) { | ||
self.libraryManager.removeSavedGamepad(gamepadID: gamepadID) | ||
} | ||
|
||
public func isGamepadSaved(gamepadID: String) -> Bool { | ||
self.savedGamepads.contains(where: { $0.id == gamepadID }) | ||
} | ||
|
||
// MARK: - Reset Data | ||
|
||
public func resetData() { | ||
self.libraryManager.resetData() | ||
} | ||
|
||
// MARK: Private | ||
|
||
private var cancellables = Set<AnyCancellable>() | ||
private let libraryManager = LibraryManager.shared | ||
|
||
private func subscribeToManager() { | ||
self.libraryManager.currentLibrary | ||
.receive(on: DispatchQueue.main) | ||
.sink(receiveValue: { [weak self] library in | ||
self?.currentLibrary = library | ||
self?.updateCurrentLibraryContent(from: library) | ||
}) | ||
.store(in: &self.cancellables) | ||
|
||
self.libraryManager.isLoading | ||
.receive(on: DispatchQueue.main) | ||
.sink { [weak self] isLoading in | ||
self?.isLoading = isLoading | ||
} | ||
.store(in: &self.cancellables) | ||
|
||
self.libraryManager.fetchError | ||
.receive(on: DispatchQueue.main) | ||
.sink(receiveValue: { [weak self] error in | ||
self?.handleError(error) | ||
}) | ||
.store(in: &self.cancellables) | ||
} | ||
|
||
private func updateCurrentLibraryContent(from library: Library?) { | ||
guard let library else { | ||
self.savedActivities = [] | ||
self.savedCurriculums = [] | ||
self.savedStories = [] | ||
self.savedGamepads = [] | ||
return | ||
} | ||
|
||
self.savedActivities = library.savedActivities | ||
self.savedCurriculums = library.savedCurriculums | ||
self.savedStories = library.savedStories | ||
self.savedGamepads = library.savedGamepads | ||
} | ||
|
||
private func handleError(_ error: Error) { | ||
if let databaseError = error as? DatabaseError { | ||
switch databaseError { | ||
case let .customError(message): | ||
self.errorMessage = message | ||
case .documentNotFound: | ||
self.errorMessage = "The requested library could not be found. Consider creating one." | ||
case .decodeError: | ||
self.errorMessage = "There was an error decoding the library data." | ||
case .encodeError: | ||
self.errorMessage = "There was an error encoding the library data." | ||
} | ||
} else { | ||
self.errorMessage = "An unknown error occurred: \(error.localizedDescription)" | ||
} | ||
self.showErrorAlert = true | ||
} | ||
} |