Extending dependencies with Combine publishers (or any functionality) outside of this repository #93
acosmicflamingo
started this conversation in
Ideas
Replies: 1 comment
-
Might have found a clean approach for the Combine publishers use-case. Just create a static var that stores the instance. Since import AccessibilityDependency
import Combine
import UIKit
extension Accessibility {
private static var _publisher = Publisher()
public struct Publisher {
@Dependency(\.notificationCenter) var notificationCenter
public var isBoldTextEnabled: AnyPublisher<Bool, Never> {
let condition = { UIAccessibility.isBoldTextEnabled }
return Just(condition())
.merge(
with: notificationCenter.publisher(
for: UIAccessibility.boldTextStatusDidChangeNotification
)
.map { _ in condition() }
).removeDuplicates()
.eraseToAnyPublisher()
}
}
public var publisher: Publisher {
get { Self._publisher }
set { Self._publisher = newValue }
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
There are some dependencies with great functionality (e.d. AccessibilityDependency), but they only give me 90% of what I want. Because of that, I've created my own dependencies that essentially wrap around these dependencies (e.g. I've created a
A11yClient
dependency that is 90% a carbon copy of AccessibilityDependency and 10% extra useful functionality like Combine publisher generators). However, it seems pretty silly that I have to maintain a codebase that is 90% the same as another well-maintained one. This was especially apparent when I was trying to overridea11yClient.$isVoiceOverEnabled
and additionally had to overrideaccessibility.$isVoiceOverEnabled
to get my UIView to work in Xcode Previews.One workaround I came up with is extending a dependency in this manner:
That allows me to subscribe to the interfaceOrientation publisher, and gives the impression that
deviceClient
does in fact have a publisher property:While it doesn't conform to
ConfigurableProxy
protocol and is much harder to test, that seems like a decent tradeoff since it's rather straightforward nature and doesn't really need to be tested so thoroughly. Even then, it does seem a bit hacky to create a brand newDeviceDependency.Publisher
instance every timepublisher
is accessed (while_implementation
is only instantiated once in DependenciesAdditions's dependencies that conform toConfigurableProxy
protocol).Is there a better way for me to be able to extend API of these powerful dependencies while retaining clean API and ability to test in the process?
Beta Was this translation helpful? Give feedback.
All reactions