Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UIBinding._printChanges() #214

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions Sources/SwiftNavigation/UIBinding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,25 @@ public struct UIBinding<Value>: Sendable {
binding.transaction = transaction
return binding
}

public func _printChanges(
_ prefix: String = "",
fileID: StaticString = #fileID,
line: UInt = #line
) -> Self {
func open(_ location: some _UIBinding<Value>) -> Self {
Self(
location: _UIBindingPrintChanges(
base: location,
prefix: prefix,
fileID: fileID,
line: line
),
transaction: transaction
)
}
return open(location)
}
}

extension UIBinding: Identifiable where Value: Identifiable {
Expand Down Expand Up @@ -793,3 +812,38 @@ private final class _UIBindingOptionalEnumToCase<
hasher.combine(keyPath)
}
}

private final class _UIBindingPrintChanges<Base: _UIBinding>: _UIBinding, @unchecked Sendable {
let base: Base
let prefix: String
let fileID: StaticString
let line: UInt
init(base: Base, prefix: String, fileID: StaticString, line: UInt) {
self.base = base
self.prefix = prefix
self.fileID = fileID
self.line = line
}
var wrappedValue: Base.Value {
get { base.wrappedValue }
set {
var oldDescription = ""
debugPrint(base.wrappedValue, terminator: "", to: &oldDescription)
var newDescription = ""
debugPrint(newValue, terminator: "", to: &newDescription)
print(
"\(prefix.isEmpty ? "UIBinding<\(Value.self)>@\(fileID):\(line)" : prefix):",
oldDescription,
"→",
newDescription
)
base.wrappedValue = newValue
}
}
static func == (lhs: _UIBindingPrintChanges, rhs: _UIBindingPrintChanges) -> Bool {
lhs.base == rhs.base
}
func hash(into hasher: inout Hasher) {
hasher.combine(base)
}
}
13 changes: 11 additions & 2 deletions Sources/SwiftUINavigation/Binding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,24 @@
}

extension Binding where Value: Sendable {
public func _printChanges(_ prefix: String = "") -> Self {
public func _printChanges(
_ prefix: String = "",
fileID: StaticString = #fileID,
line: UInt = #line
) -> Self {
Self(
get: { self.wrappedValue },
set: { newValue, transaction in
var oldDescription = ""
debugPrint(self.wrappedValue, terminator: "", to: &oldDescription)
var newDescription = ""
debugPrint(newValue, terminator: "", to: &newDescription)
print("\(prefix.isEmpty ? "\(Self.self)" : prefix):", oldDescription, "=", newDescription)
print(
"\(prefix.isEmpty ? "\(Self.self)@\(fileID):\(line)" : prefix):",
oldDescription,
"→",
newDescription
)
self.transaction(transaction).wrappedValue = newValue
}
)
Expand Down