0% found this document useful (0 votes)
11 views13 pages

Untitled2 Copy 3

Uploaded by

webbug
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views13 pages

Untitled2 Copy 3

Uploaded by

webbug
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 13

If you're migrating to `@Observable`, you'll still need Combine in some cases,

especially when dealing with external events or KVO (Key-Value Observing) like window
visibility. However, the code can be simplified.

Here's how your class could be refactored:

```swift
@Observable @MainActor
final class AppState {
private let logger = LoggerService(category: "AppState")

var enabled = false


var window: NSWindow?
private(set) var isDeviceConnected = false
private(set) var deviceName: String = ""
var isCalibrating = false
var displaySettingsApplied = falseIf you're migrating to `@Observable`, you'll still
need Combine in some cases, especially when dealing with external events or KVO
(Key-Value Observing) like window visibility. However, the code can be simplifieIf you're
migrating to `@Observable`, you'll still need Combine in some cases, especially when
dealing with external events or KVO (Key-Value Observing) like window visibility.
However, the code can be simplified.

Here's how your class could be refactored:

```swift
@Observable @MainActor
final class AppState {
private let logger = LoggerService(category: "AppState")

var enabled = false


var window: NSWindow?
private(set) var isDeviceConnected = false
private(set) var deviceName: String = ""
var isCalibrating = false
var displaySettingsApplied = false

// Managers stay the same...


private(set) lazy var screenSettings = ScreenSettings(appState: self)
private(set) lazy var displayManager = DisplayManager(appState: self)
// ... other managers

private(set) weak var settingsWindow: NSWindow?


private(set) weak var permissionsWindow: NSWindow?

// Still need cancellables for external event subscriptions


private var cancellables = Set<AnyCancellable>()

let isPreview: Bool = {


#if DEBUG

ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] != nil
#else
false
#endif
}()

private func configureCancellables() {


var c = Set<AnyCancellable>()

// For external notifications, you still need Combine


NotificationCenter.default.publisher(for: .deviceConnectionStatusChanged)
.compactMap { notification -> (isConnected: Bool, deviceName: String)? in
guard let deviceInfo = notification.userInfo as? [String: Any],
let isConnected = deviceInfo["connected"] as? Bool else {
return nil
}
let deviceName = deviceInfo["deviceName"] as? String ?? ""
return (isConnected, deviceName)
}
.receive(on: DispatchQueue.main)
.sink { [weak self] status in
self?.isDeviceConnected = status.isConnected
self?.deviceName = status.deviceName
self?.logger.info("Device connection status changed: \
(status.isConnected ? "Connected" : "Disconnected")")
}
.store(in: &c)

// For window visibility, you could simplify to:


if let settingsWindow {
settingsWindow.publisher(for: \.isVisible)
.debounce(for: 0.05, scheduler: DispatchQueue.main)
.sink { [weak self] isVisible in
self?.navigationState.isSettingsPresented = isVisible
}
.store(in: &c)
}

cancellables = c
}
}
```

Key changes and notes:


1. Removed `@Published` property wrappers
2. Changed from `ObservableObject` to `@Observable`
3. Kept `cancellables` and Combine code for:
- External notifications (NotificationCenter)
- KVO (window visibility)
- Debouncing and timing operations
- Async event streams

You still need Combine for:


1. External event handling (notifications, KVO)
2. Complex event transformations (debounce, throttle, etc.)
3. Async operations and timing
4. Stream combining and transformation

`@Observable` primarily replaces the property observation aspect of


`ObservableObject` and `@Published`, but it doesn't replace Combine's event handling
and stream processing capabilities. For those cases, you'll still want to use Combine.

For simple property changes within your app's data model, `@Observable` is sufficient.
For external events and complex event processing, continue using Combine.d.

Here's how your class could be refactored:

```swiftIf you're migrating to `@Observable`, you'll still need Combine in some cases,
especially when dealing with external events or KVO (Key-Value Observing) like window
visibility. However, the code can be simplified.

Here's how your class could be refactored:

```swift
@Observable @MainActor
final class AppState {
private let logger = LoggerService(category: "AppState")

var enabled = false


var window: NSWindow?
private(set) var isDeviceConnected = false
private(set) var deviceName: String = ""
var isCalibrating = false
var displaySettingsApplied = false

// Managers stay the same...


private(set) lazy var screenSettings = ScreenSettings(appState: self)
private(set) lazy var displayManager = DisplayManager(appState: self)
// ... other managers

private(set) weak var settingsWindow: NSWindow?


private(set) weak var permissionsWindow: NSWindow?

// Still need cancellables for external event subscriptions


private var cancellables = Set<AnyCancellable>()

let isPreview: Bool = {


#if DEBUG

ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] != nil
#else
false
#endif
}()

private func configureCancellables() {


var c = Set<AnyCancellable>()

// For external notifications, you still need Combine


NotificationCenter.default.publisher(for: .deviceConnectionStatusChanged)
.compactMap { notification -> (isConnected: Bool, deviceName: String)? in
guard let deviceInfo = notification.userInfo as? [String: Any],
let isConnected = deviceInfo["connected"] as? Bool else {
return nil
}
let deviceName = deviceInfo["deviceName"] as? String ?? ""
return (isConnected, deviceName)
}
.receive(on: DispatchQueue.main)
.sink { [weak self] status in
self?.isDeviceConnected = status.isConnected
self?.deviceName = status.deviceName
self?.logger.info("Device connection status changed: \
(status.isConnected ? "Connected" : "Disconnected")")
}
.store(in: &c)

// For window visibility, you could simplify to:


if let settingsWindow {
settingsWindow.publisher(for: \.isVisible)
.debounce(for: 0.05, scheduler: DispatchQueue.main)
.sink { [weak self] isVisible in
self?.navigationState.isSettingsPresented = isVisible
}
.store(in: &c)
}

cancellables = c
}
}
```

Key changes and notes:


1. Removed `@Published` property wrappers
2. Changed from `ObservableObject` to `@Observable`
3. Kept `cancellables` and Combine code for:
- External notifications (NotificationCenter)
- KVO (window visibility)
- Debouncing and timing operations
- Async event streams

You still need Combine for:


1. External event handling (notifications, KVO)
2. Complex event transformations (debounce, throttle, etc.)
3. Async operations and timing
4. Stream combining and transformation

`@Observable` primarily replaces the property observation aspect of


`ObservableObject` and `@Published`, but it doesn't replace Combine's event handling
and stream processing capabilities. For those cases, you'll still want to use Combine.

For simple property changes within your app's data model, `@Observable` is sufficient.
For external events and complex event processing, continue using Combine.
@Observable @MainActorIf you're migrating to `@Observable`, you'll still need
Combine in some cases, especially when dealing with external events or KVO (Key-
Value Observing) like window visibility. However, the code can be simplified.

Here's how your class could be refactored:

```swift
@Observable @MainActor
final class AppState {
private let logger = LoggerService(category: "AppState")

var enabled = false


var window: NSWindow?
private(set) var isDeviceConnected = false
private(set) var deviceName: String = ""
var isCalibrating = false
var displaySettingsApplied = false

// Managers stay the same...


private(set) lazy var screenSettings = ScreenSettings(appState: self)
private(set) lazy var displayManager = DisplayManager(appState: self)
// ... other managers

private(set) weak var settingsWindow: NSWindow?


private(set) weak var permissionsWindow: NSWindow?

// Still need cancellables for external event subscriptions


private var cancellables = Set<AnyCancellable>()

let isPreview: Bool = {


#if DEBUG

ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] != nil
#else
false
#endif
}()

private func configureCancellables() {


var c = Set<AnyCancellable>()

// For external notifications, you still need Combine


NotificationCenter.default.publisher(for: .deviceConnectionStatusChanged)
.compactMap { notification -> (isConnected: Bool, deviceName: String)? in
guard let deviceInfo = notification.userInfo as? [String: Any],
let isConnected = deviceInfo["connected"] as? Bool else {
return nil
}
let deviceName = deviceInfo["deviceName"] as? String ?? ""
return (isConnected, deviceName)
}
.receive(on: DispatchQueue.main)
.sink { [weak self] status in
self?.isDeviceConnected = status.isConnected
self?.deviceName = status.deviceName
self?.logger.info("Device connection status changed: \
(status.isConnected ? "Connected" : "Disconnected")")
}
.store(in: &c)

// For window visibility, you could simplify to:


if let settingsWindow {
settingsWindow.publisher(for: \.isVisible)
.debounce(for: 0.05, scheduler: DispatchQueue.main)
.sink { [weak self] isVisible in
self?.navigationState.isSettingsPresented = isVisible
}
.store(in: &c)
}

cancellables = c
}
}
```

Key changes and notes:


1. Removed `@Published` property wrappers
2. Changed from `ObservableObject` to `@Observable`
3. Kept `cancellables` and Combine code for:
- External notifications (NotificationCenter)
- KVO (window visibility)
- Debouncing and timing operations
- Async event streams

You still need Combine for:


1. External event handling (notifications, KVO)
2. Complex event transformations (debounce, throttle, etc.)
3. Async operations and timing
4. Stream combining and transformation

`@Observable` primarily replaces the property observation aspect of


`ObservableObject` and `@Published`, but it doesn't replace Combine's event handling
and stream processing capabilities. For those cases, you'll still want to use Combine.

For simple property changes within your app's data model, `@Observable` is sufficient.
For external events and complex event processing, continue using Combine.
final class AppState {
private let logger = LoggerService(category: "AppState")

var enabled = false


var window: NSWindow?
private(set) var isDeviceConnected = false
private(set) var deviceName: String = ""
var isCalibrating = false
var displaySettingsApplied = false

// Managers stay the same...


private(set) lazy var screenSettings = ScreenSettings(appState: self)
private(set) lazy var displayManager = DisplayManager(appState: self)
// ... other managers

private(set) weak var settingsWindow: NSWindow?


private(set) weak var permissionsWindow: NSWindow?

// Still need cancellables for external event subscriptions


private var cancellables = Set<AnyCancellable>()

let isPreview: Bool = {


#if DEBUG

ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] != nil
#else
false
#endif
}()

private func configureCancellables() {


var c = Set<AnyCancellable>()

// For external notifications, you still need Combine


NotificationCenter.default.publisher(for: .deviceConnectionStatusChanged)
.compactMap { notification -> (isConnected: Bool, deviceName: String)? in
guard let deviceInfo = notification.userInfo as? [String: Any],
let isConnected = deviceInfo["connected"] as? Bool else {
return nil
}
let deviceName = deviceInfo["deviceName"] as? String ?? ""
return (isConnected, deviceName)
}
.receive(on: DispatchQueue.main)
.sink { [weak self] status in
self?.isDeviceConnected = status.isConnected
self?.deviceName = status.deviceName
self?.logger.info("Device connection status changed: \
(status.isConnected ? "Connected" : "Disconnected")")
}
.store(in: &c)

// For window visibility, you could simplify to:


if let settingsWindow {
settingsWindow.publisher(for: \.isVisible)
.debounce(for: 0.05, scheduler: DispatchQueue.main)
.sink { [weak self] isVisible in
self?.navigationState.isSettingsPresented = isVisible
}
.store(in: &c)
}

cancellables = c
}
}
```

Key changes and notes:


1. Removed `@Published` property wrappers
2. Changed from `ObservableObject` to `@Observable`
3. Kept `cancellables` and Combine code for:
- External notifications (NotificationCenter)
- KVO (window visibility)
- Debouncing and timing operations
- Async event streams

You still need Combine for:


1. External event handling (notifications, KVO)
2. Complex event transformations (debounce, throttle, etc.)
3. Async operations and timing
4. Stream combining and transformation

`@Observable` primarily replaces the property observation aspect of


`ObservableObject` and `@Published`, but it doesn't replace Combine's event handling
and stream processing capabilities. For those cases, you'll still want to use Combine.

For simple property changes within your app's data model, `@Observable` is sufficient.
For external events and complex event processing, continue using Combine.

// Managers stay the same...


private(set) lazy var screenSettings = ScreenSettings(appState: self)
private(set) lazy var displayManager = DisplayManager(appState: self)
// ... other managers

private(set) weak var settingsWindow: NSWindow?


private(set) weak var permissionsWindow: NSWindow?

// Still need cancellables for external event subscriptions


private var cancellables = Set<AnyCancellable>()

let isPreview: Bool = {


#if DEBUG

ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] != nil
#else
false
#endif
}()

private func configureCancellables() {


var c = Set<AnyCancellable>()

// For external notifications, you still need Combine


NotificationCenter.default.publisher(for: .deviceConnectionStatusChanged)
.compactMap { notification -> (isConnected: Bool, deviceName: String)? in
guard let deviceInfo = notification.userInfo as? [String: Any],
let isConnected = deviceInfo["connected"] as? Bool else {
return nil
}
let deviceName = deviceInfo["deviceName"] as? String ?? ""
return (isConnected, deviceName)
}
.receive(on: DispatchQueue.main)
.sink { [weak self] status in
self?.isDeviceConnected = status.isConnected
self?.deviceName = status.deviceName
self?.logger.info("Device connection status changed: \
(status.isConnected ? "Connected" : "Disconnected")")
}
.store(in: &c)

// For window visibility, you could simplify to:


if let settingsWindow {
settingsWindow.publisher(for: \.isVisible)
.debounce(for: 0.05, scheduler: DispatchQueue.main)
.sink { [weak self] isVisible in
self?.navigationState.isSettingsPresented = isVisible
}
.store(in: &c)
}

cancellables = c
}
}
```

Key changes and notes:


1. Removed `@Published` property wrappers
2. Changed from `ObservableObject` to `@Observable`
3. Kept `cancellables` and Combine code for:
- External notifications (NotificationCenter)
- KVO (window visibility)
- Debouncing and timing operations
- Async event streams

You still need Combine for:


1. External event handling (notifications, KVO)
2. Complex event transformations (debounce, throttle, etc.)
3. Async operations and timing
4. Stream combining and transformationIf you're migrating to `@Observable`, you'll still
need Combine in some cases, especially when dealing with external events or KVO
(Key-Value Observing) like window visibility. However, the code can be simplified.

Here's how your class could be refactored:

```swift
@Observable @MainActor
final class AppState {
private let logger = LoggerService(category: "AppState")

var enabled = false


var window: NSWindow?
private(set) var isDeviceConnected = false
private(set) var deviceName: String = ""
var isCalibrating = false
var displaySettingsApplied = false

// Managers stay the same...


private(set) lazy var screenSettings = ScreenSettings(appState: self)
private(set) lazy var displayManager = DisplayManager(appState: self)
// ... other managers

private(set) weak var settingsWindow: NSWindow?


private(set) weak var permissionsWindow: NSWindow?

// Still need cancellables for external event subscriptions


private var cancellables = Set<AnyCancellable>()

let isPreview: Bool = {


#if DEBUG

ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] != nil
#else
false
#endif
}()
private func configureCancellables() {
var c = Set<AnyCancellable>()

// For external notifications, you still need Combine


NotificationCenter.default.publisher(for: .deviceConnectionStatusChanged)
.compactMap { notification -> (isConnected: Bool, deviceName: String)? in
guard let deviceInfo = notification.userInfo as? [String: Any],
let isConnected = deviceInfo["connected"] as? Bool else {
return nil
}
let deviceName = deviceInfo["deviceName"] as? String ?? ""
return (isConnected, deviceName)
}
.receive(on: DispatchQueue.main)
.sink { [weak self] status in
self?.isDeviceConnected = status.isConnected
self?.deviceName = status.deviceName
self?.logger.info("Device connection status changed: \
(status.isConnected ? "Connected" : "Disconnected")")
}
.store(in: &c)

// For window visibility, you could simplify to:


if let settingsWindow {
settingsWindow.publisher(for: \.isVisible)
.debounce(for: 0.05, scheduler: DispatchQueue.main)
.sink { [weak self] isVisible in
self?.navigationState.isSettingsPresented = isVisible
}
.store(in: &c)
}

cancellables = c
}
}
```

Key changes and notes:


1. Removed `@Published` property wrappers
2. Changed from `ObservableObject` to `@Observable`
3. Kept `cancellables` and Combine code for:
- External notifications (NotificationCenter)
- KVO (window visibility)
- Debouncing and timing operations
- Async event streams
You still need Combine for:
1. External event handling (notifications, KVO)
2. Complex event transformations (debounce, throttle, etc.)
3. Async operations and timing
4. Stream combining and transformation

`@Observable` primarily replaces the property observation aspect of


`ObservableObject` and `@Published`, but it doesn't replace Combine's event handling
and stream processing capabilities. For those cases, you'll still want to use Combine.

For simple property changes within your app's data model, `@Observable` is sufficient.
For external events and complex event processing, continue using Combine.

`@Observable` primarily replaces the property observation aspect of


`ObservableObject` and `@Published`, but it doesn't replace Combine's event handling
and stream processing capabilities. For those cases, you'll still want to use Combine.

For simple property changes within your app's data model, `@Observable` is sufficient.
For external events and complex event processing, continue using Combine.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy