Skip to content

Commit

Permalink
Merge pull request #115 from UbiqueInnovation/fix-on-preference-chang…
Browse files Browse the repository at this point in the history
…e-for-xcode-16-2

Fix onPreferenceChange modifier on Xcode 16.2
  • Loading branch information
maerki authored Jan 1, 2025
2 parents 7edaf15 + de420bc commit 9767a49
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct UBPopupPreferenceKey: PreferenceKey {
}
}

@MainActor
struct UBPopupPreference: Equatable {
let id = UUID()
let isPresented: Binding<Bool>
Expand All @@ -36,7 +37,7 @@ struct UBPopupPreference: Equatable {
self.content = content
}

static func == (lhs: UBPopupPreference, rhs: UBPopupPreference) -> Bool {
nonisolated static func == (lhs: UBPopupPreference, rhs: UBPopupPreference) -> Bool {
lhs.id == rhs.id
&& lhs.isPresented.wrappedValue == rhs.isPresented.wrappedValue
&& lhs.date == rhs.date
Expand Down
2 changes: 1 addition & 1 deletion Sources/UBUserInterface/SwiftUI/Popup/UBPopupStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import SwiftUI

public struct UBPopupStyle: Equatable {
public struct UBPopupStyle: Equatable, Sendable {
let extendsToEdges: Bool
let backgroundColor: Color
let backdropColor: Color
Expand Down
22 changes: 19 additions & 3 deletions Sources/UBUserInterface/SwiftUI/Popup/UBPopupWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#if arch(arm64) || arch(x86_64)

import Foundation
import UBFoundation
import SwiftUI

public struct UBPopupWrapper<V: View>: View {
Expand All @@ -25,13 +26,28 @@ public struct UBPopupWrapper<V: View>: View {
UBPopupWindowManager.shared.setupWindow()
}
.onPreferenceChange(UBPopupPreferenceKey.self) { popupPreference in
if let popupPreference, popupPreference.isPresented.wrappedValue {
UBPopupWindowManager.shared.showPopup(isPresented: popupPreference.isPresented, style: popupPreference.customStyle ?? style, content: popupPreference.content)
if Thread.isMainThread {
MainActor.assumeIsolated {
popupPreferenceChanged(popupPreference: popupPreference)
}
} else {
UBPopupWindowManager.shared.hideWindow()
Log.reportError("onPreferenceChange called on non-main thread")
DispatchQueue.main.async {
MainActor.assumeIsolated {
popupPreferenceChanged(popupPreference: popupPreference)
}
}
}
}
}

private func popupPreferenceChanged(popupPreference: UBPopupPreference?) {
if let popupPreference, popupPreference.isPresented.wrappedValue {
UBPopupWindowManager.shared.showPopup(isPresented: popupPreference.isPresented, style: popupPreference.customStyle ?? style, content: popupPreference.content)
} else {
UBPopupWindowManager.shared.hideWindow()
}
}
}

#endif

0 comments on commit 9767a49

Please sign in to comment.