From e0c25751f03da97fafcee742aad748397139518a Mon Sep 17 00:00:00 2001 From: "mathieu J." Date: Tue, 10 Dec 2024 11:34:55 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20(AccountKit):=20Get=20latest=20Cons?= =?UTF-8?q?entInfo=20from=20currentRootAccount?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RootAccountManagerViewModel.swift | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/Modules/AccountKit/Sources/Managers/RootAccounts/RootAccountManagerViewModel.swift b/Modules/AccountKit/Sources/Managers/RootAccounts/RootAccountManagerViewModel.swift index 006c130c8..0dcc4dbab 100644 --- a/Modules/AccountKit/Sources/Managers/RootAccounts/RootAccountManagerViewModel.swift +++ b/Modules/AccountKit/Sources/Managers/RootAccounts/RootAccountManagerViewModel.swift @@ -4,6 +4,7 @@ import Combine import SwiftUI +import Version public class RootAccountManagerViewModel: ObservableObject { // MARK: Lifecycle @@ -14,6 +15,7 @@ public class RootAccountManagerViewModel: ObservableObject { // MARK: Public + @Published public var latestConsentInfo: ConsentInfo? @Published public var savedActivities: [SavedActivity] = [] @Published public var savedCurriculums: [SavedCurriculum] = [] @Published public var savedStories: [SavedStory] = [] @@ -87,15 +89,27 @@ public class RootAccountManagerViewModel: ObservableObject { private var cancellables = Set() private let rootAccountManager = RootAccountManager.shared + private func getLatestConsentInfo(from consentInfo: [ConsentInfo]) -> ConsentInfo? { + consentInfo.max(by: { consent1, consent2 in + let version1 = Version(tolerant: consent1.policyVersion) ?? Version("0.0.0")! + let version2 = Version(tolerant: consent2.policyVersion) ?? Version("0.0.0")! + return version1 < version2 + }) + } + private func subscribeToManager() { self.rootAccountManager.currentRootAccountPublisher .receive(on: DispatchQueue.main) .sink(receiveValue: { [weak self] rootAccount in - guard let library = rootAccount?.library else { return } - self?.savedActivities = library.savedActivities - self?.savedCurriculums = library.savedCurriculums - self?.savedStories = library.savedStories - self?.savedGamepads = library.savedGamepads + guard let self, let rootAccount else { return } + + let library = rootAccount.library + self.savedActivities = library.savedActivities + self.savedCurriculums = library.savedCurriculums + self.savedStories = library.savedStories + self.savedGamepads = library.savedGamepads + + self.latestConsentInfo = self.getLatestConsentInfo(from: rootAccount.consentInfo) }) .store(in: &self.cancellables)