Skip to content
Open
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
6 changes: 6 additions & 0 deletions SwiftTweaks/TweakLibrary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public extension TweakLibraryType {
self.defaultStore.unbind(identifier)
}

/// Similar to `Combine.Subscribers.Assign`. returns nothing, since it doesn't strongly capture `on: OwnerObject`. example usage is:
/// `ExampleTweaks.bindWeakly(ExampleTweaks.colorTint, to: \.exampleButton.backgroundColor, on: self)`
static func bindWeakly<T, OwnerObject: AnyObject>(_ tweak: Tweak<T>, to keypath: ReferenceWritableKeyPath<OwnerObject, T>, on owner: OwnerObject) {
self.defaultStore.bindWeakly(tweak, to: keypath, on: owner)
}

// Accepts a collection of Tweaks, and immediately calls the updateHandler.
/// The updateHandler is then re-called each time any of the collection's tweaks change.
/// Inside the updateHandler, you'll need to use `assign` to get the tweaks' current values.
Expand Down
8 changes: 8 additions & 0 deletions SwiftTweaks/TweakStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ public final class TweakStore {
tweakBindings[identifier.tweak] = existingTweakBindings.filter { $0.identifier != identifier }
}

/// Similar to `Combine.Subscribers.Assign`. returns nothing, since it doesn't strongly capture `on: OwnerObject`. example usage is:
/// `ExampleTweaks.bindWeakly(ExampleTweaks.colorTint, to: \.exampleButton.backgroundColor, on: self)`
public func bindWeakly<T, OwnerObject: AnyObject>(_ tweak: Tweak<T>, to keypath: ReferenceWritableKeyPath<OwnerObject, T>, on owner: OwnerObject) {
_ = self.bind(tweak) { [weak owner] tweakValue in
owner?[keyPath: keypath] = tweakValue
}
}

public func bindMultiple(_ tweaks: [TweakType], binding: @escaping () -> Void) -> MultiTweakBindingIdentifier {
// Convert the array (which makes it easier to call a `bindTweakSet`) into a set (which makes it possible to cache the tweakSet)
let tweakSet = Set(tweaks.map(AnyTweak.init))
Expand Down