Skip to content

Commit

Permalink
🚧 (LekaApp): Check consent on app launch
Browse files Browse the repository at this point in the history
  • Loading branch information
macteuts committed Dec 13, 2024
1 parent a8d6b82 commit 495b7a7
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
1 change: 1 addition & 0 deletions Apps/LekaApp/Sources/MainApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ struct LekaApp: App {

@Environment(\.colorScheme) var colorScheme
@StateObject var updateManager: UpdateManager = .shared
@StateObject var rootAccountViewModel = RootAccountManagerViewModel()
@ObservedObject var styleManager: StyleManager = .shared

var body: some Scene {
Expand Down
1 change: 1 addition & 0 deletions Apps/LekaApp/Sources/Navigation/Navigation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ enum FullScreenCoverContent: Hashable, Identifiable {

enum SheetContent: Hashable, Identifiable {
case robotConnection
case consent
case createCaregiver
case editCaregiver
case caregiverPicker
Expand Down
28 changes: 28 additions & 0 deletions Apps/LekaApp/Sources/Views/MainView/MainView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,21 @@ struct MainView: View {
})
.logEventScreenView(screenName: "carereceiver_picker", context: .sheet)
.navigationBarTitleDisplayMode(.inline)

case .consent:
ConsentView(
onCancel: {
print("Consent was denied") // Add analytics
},
onAccept: {
self.rootAccountViewModel.updateConsentInfo(policyVersion: self.latestPolicyVersion)
if self.caregiverManager.currentCaregiver.value == nil {
self.navigation.sheetContent = .caregiverPicker
} else {
self.navigation.sheetContent = nil
}
}
)
}
}
}
Expand Down Expand Up @@ -375,6 +390,18 @@ struct MainView: View {
self.caregiverManager.setCurrentCaregiver(byID: storedCaregiverID)
}
}
.onReceive(self.rootAccountViewModel.$currentRootAccount) { rootAccount in
// DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
guard rootAccount != nil else { return }

guard self.rootAccountViewModel.needsConsentUpdate(
latestPolicyVersion: self.latestPolicyVersion
) else {
return
}
self.navigation.sheetContent = .consent
// }
}
.onChange(of: self.caregiverManagerViewModel.currentCaregiver) { currentCaregiver in
self.persistentDataManager.lastActiveCaregiverID = currentCaregiver?.id
self.persistentDataManager.updateLastActiveTimestamp()
Expand All @@ -401,6 +428,7 @@ struct MainView: View {
private var persistentDataManager: PersistentDataManager = .shared
private var caregiverManager: CaregiverManager = .shared
private var carereceiverManager: CarereceiverManager = .shared
private var latestPolicyVersion: String = "1.2.4" // fetch from Yaml
}

// swiftlint:enable type_body_length
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// SPDX-License-Identifier: Apache-2.0

import Combine
import Foundation

public class RootAccountManager {
// MARK: Lifecycle
Expand Down Expand Up @@ -57,6 +58,20 @@ public class RootAccountManager {
.store(in: &self.cancellables)
}

// MARK: ConsentInfo

public func appendConsentInfo(policyVersion: String) {
guard var rootAccount = self.currentRootAccount.value else {
self.fetchErrorSubject.send(DatabaseError.customError("RootAccount not found"))
return
}

let newConsentInfo = ConsentInfo(policyVersion: policyVersion, acceptedAt: Date())
rootAccount.consentInfo.append(newConsentInfo)

self.updateRootAccount(rootAccount: &rootAccount)
}

// MARK: Activities

public func addSavedActivity(activityID: String, caregiverID: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public class RootAccountManagerViewModel: ObservableObject {

// MARK: Public

@Published public var latestConsentInfo: ConsentInfo?
@Published public var currentRootAccount: RootAccount?
@Published public var currentConsentInfo: ConsentInfo? // Not published
@Published public var savedActivities: [SavedActivity] = []
@Published public var savedCurriculums: [SavedCurriculum] = []
@Published public var savedStories: [SavedStory] = []
Expand Down Expand Up @@ -84,6 +85,22 @@ public class RootAccountManagerViewModel: ObservableObject {
self.rootAccountManager.resetData()
}

// ConsentInfo

public func needsConsentUpdate(latestPolicyVersion: String) -> Bool {
guard let currentConsent = self.currentConsentInfo,
let currentVersion = Version(tolerant: currentConsent.policyVersion),
let latestVersion = Version(tolerant: latestPolicyVersion)
else {
return true
}
return currentVersion < latestVersion
}

public func updateConsentInfo(policyVersion: String) {
self.rootAccountManager.appendConsentInfo(policyVersion: policyVersion)
}

// MARK: Private

private var cancellables = Set<AnyCancellable>()
Expand All @@ -102,14 +119,15 @@ public class RootAccountManagerViewModel: ObservableObject {
.receive(on: DispatchQueue.main)
.sink(receiveValue: { [weak self] rootAccount in
guard let self, let rootAccount else { return }
self.currentRootAccount = rootAccount

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)
self.currentConsentInfo = self.getLatestConsentInfo(from: rootAccount.consentInfo)
})
.store(in: &self.cancellables)

Expand Down

0 comments on commit 495b7a7

Please sign in to comment.