-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppObserverView.swift
More file actions
44 lines (42 loc) · 1.44 KB
/
AppObserverView.swift
File metadata and controls
44 lines (42 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//
// AppObserverView.swift
// NewsFlowAI
//
// Created by Akalpit Dawkhar on 3/7/25.
//
import SwiftUI
import os.log
struct AppObserverView: View {
@EnvironmentObject var newsViewModel: NewsViewModel
private let logger = Logger(subsystem: "com.newsflowai.app", category: "AppObserverView")
var body: some View {
Color.clear // A transparent view
.onAppear {
logger.info("AppObserverView appeared – setting up NotificationCenter observers.")
NotificationCenter.default.addObserver(
forName: NSNotification.Name("UserLoggedIn"),
object: nil,
queue: .main
) { notification in
if let userId = notification.userInfo?["userId"] as? String {
Task {
await MainActor.run {
newsViewModel.setUserId(userId)
}
}
}
}
NotificationCenter.default.addObserver(
forName: NSNotification.Name("UserLoggedOut"),
object: nil,
queue: .main
) { _ in
Task {
await MainActor.run {
newsViewModel.clearUserId()
}
}
}
}
}
}