├── .gitignore ├── LICENSE ├── Package.swift ├── README.md ├── Sources └── AnyObservableObject │ ├── AnyObservableObject.swift │ ├── Binding.swift │ ├── Input.swift │ └── Output.swift └── Tests └── AnyObservableObjectTests └── AnyObservableObjectTests.swift /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | 3 | # Xcode 4 | # 5 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 6 | 7 | ## User settings 8 | xcuserdata/ 9 | 10 | ## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) 11 | *.xcscmblueprint 12 | *.xccheckout 13 | 14 | ## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) 15 | build/ 16 | DerivedData/ 17 | *.moved-aside 18 | *.pbxuser 19 | !default.pbxuser 20 | *.mode1v3 21 | !default.mode1v3 22 | *.mode2v3 23 | !default.mode2v3 24 | *.perspectivev3 25 | !default.perspectivev3 26 | 27 | ## Obj-C/Swift specific 28 | *.hmap 29 | 30 | ## App packaging 31 | *.ipa 32 | *.dSYM.zip 33 | *.dSYM 34 | 35 | ## Playgrounds 36 | timeline.xctimeline 37 | playground.xcworkspace 38 | 39 | # Swift Package Manager 40 | # 41 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 42 | Packages/ 43 | # Package.pins 44 | # Package.resolved 45 | *.xcodeproj 46 | # 47 | # Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata 48 | # hence it is not needed unless you have added a package configuration file to your project 49 | .swiftpm 50 | 51 | .build/ 52 | 53 | # CocoaPods 54 | # 55 | # We recommend against adding the Pods directory to your .gitignore. However 56 | # you should judge for yourself, the pros and cons are mentioned at: 57 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 58 | # 59 | # Pods/ 60 | # 61 | # Add this line if you want to avoid checking in source code from the Xcode workspace 62 | # *.xcworkspace 63 | 64 | # Carthage 65 | # 66 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 67 | # Carthage/Checkouts 68 | 69 | Carthage/Build/ 70 | 71 | # Accio dependency management 72 | Dependencies/ 73 | .accio/ 74 | 75 | # fastlane 76 | # 77 | # It is recommended to not store the screenshots in the git repo. 78 | # Instead, use fastlane to re-generate the screenshots whenever they are needed. 79 | # For more information about the recommended setup visit: 80 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 81 | 82 | fastlane/report.xml 83 | fastlane/Preview.html 84 | fastlane/screenshots/**/*.png 85 | fastlane/test_output 86 | 87 | # Code Injection 88 | # 89 | # After new code Injection tools there's a generated folder /iOSInjectionProject 90 | # https://github.com/johnno1962/injectionforxcode 91 | 92 | iOSInjectionProject/ 93 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Taiki Suzuki 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version: 5.6 2 | // The swift-tools-version declares the minimum version of Swift required to build this package. 3 | 4 | import PackageDescription 5 | 6 | let package = Package( 7 | name: "AnyObservableObject", 8 | platforms: [ 9 | .iOS(.v13), 10 | ], 11 | products: [ 12 | // Products define the executables and libraries a package produces, and make them visible to other packages. 13 | .library( 14 | name: "AnyObservableObject", 15 | targets: ["AnyObservableObject"]), 16 | ], 17 | dependencies: [ 18 | // Dependencies declare other packages that this package depends on. 19 | // .package(url: /* package url */, from: "1.0.0"), 20 | ], 21 | targets: [ 22 | // Targets are the basic building blocks of a package. A target can define a module or a test suite. 23 | // Targets can depend on other targets in this package, and on products in packages this package depends on. 24 | .target( 25 | name: "AnyObservableObject", 26 | dependencies: []), 27 | .testTarget( 28 | name: "AnyObservableObjectTests", 29 | dependencies: ["AnyObservableObject"]), 30 | ] 31 | ) 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AnyObservableObject 2 | 3 | ## Usage 4 | 5 | ```swift 6 | // views 7 | 8 | @main 9 | struct SampleApp: App { 10 | var body: some Scene { 11 | WindowGroup { 12 | ContentView( 13 | observableObject: SampleObservableObjectImpl().eraseToAnyObservableObject() 14 | ) 15 | } 16 | } 17 | } 18 | 19 | struct ContentView: View { 20 | @ObservedObject var observableObject: AnySampleObservableObject 21 | 22 | var body: some View { 23 | Text(observableObject.output.text) 24 | .onTapGesture { 25 | observableObject.input.onTap() 26 | } 27 | .sheet(isPresented: observableObject.$binding.isPresenting) { 28 | Text("Hello, AnyObservableObject!") 29 | } 30 | .padding() 31 | } 32 | } 33 | 34 | struct ContentView_Preview: PreviewProvider { 35 | static var previews: some View { 36 | ContentView( 37 | observableObject: FakeObject().eraseToAnyObservableObject() 38 | ) 39 | } 40 | 41 | private final class FakeObject: SampleObservableObject { 42 | let input = SampleInput().toWrapper() 43 | let binding = SampleBinging().toWrapper() 44 | let output: OutputWrapper 45 | 46 | init() { 47 | let output = SampleOutput() 48 | output.text = "Preview Title" 49 | self.output = output.toWrapper() 50 | } 51 | } 52 | } 53 | ``` 54 | 55 | ```swift 56 | // definitions 57 | final class SampleInput: InputType { 58 | let onTap = PassthroughSubject() 59 | } 60 | 61 | final class SampleBinging: BindingType { 62 | @Published var isPresenting = false 63 | } 64 | 65 | final class SampleOutput: OutputType { 66 | @Published var text = "Sheet has not presented, yet\nPlease tap!" 67 | } 68 | 69 | protocol SampleObservableObject: ObservableObjectType { 70 | associatedtype Input = SampleInput 71 | associatedtype Binding = SampleBinging 72 | associatedtype Output = SampleOutput 73 | } 74 | 75 | typealias AnySampleObservableObject = AnyObservableObject< 76 | SampleInput, 77 | SampleBinging, 78 | SampleOutput 79 | > 80 | ``` 81 | 82 | ```swift 83 | // implementations 84 | final class SampleObservableObjectImpl: SampleObservableObject { 85 | let input: InputWrapper 86 | let binding: BindingWrapper 87 | let output: OutputWrapper 88 | 89 | private var cancellables = Set() 90 | 91 | init() { 92 | let input = SampleInput() 93 | let binding = SampleBinging() 94 | let output = SampleOutput() 95 | self.input = input.toWrapper() 96 | self.binding = binding.toWrapper() 97 | self.output = output.toWrapper() 98 | 99 | input.onTap 100 | .map { true } 101 | .prefix(1) 102 | .assign(to: \.isPresenting, on: binding) 103 | .store(in: &cancellables) 104 | 105 | binding.$isPresenting 106 | .filter { $0 } 107 | .prefix(1) 108 | .map { _ in "Sheet has presented" } 109 | .assign(to: \.text, on: output) 110 | .store(in: &cancellables) 111 | } 112 | } 113 | ``` 114 | -------------------------------------------------------------------------------- /Sources/AnyObservableObject/AnyObservableObject.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AnyObservableObject.swift 3 | // AnyObservableObject 4 | // 5 | // Created by marty-suzuki on 2022/04/03. 6 | // Copyright © 2022 marty-suzuki. All rights reserved. 7 | // 8 | 9 | import Combine 10 | 11 | public protocol ObservableObjectType: AnyObject { 12 | associatedtype Input: InputType 13 | associatedtype Binding: BindingType 14 | associatedtype Output: OutputType 15 | var input: InputWrapper { get } 16 | var binding: BindingWrapper { get } 17 | var output: OutputWrapper { get } 18 | } 19 | 20 | extension ObservableObjectType { 21 | public func eraseToAnyObservableObject() -> AnyObservableObject { 22 | AnyObservableObject(self) 23 | } 24 | } 25 | 26 | public final class AnyObservableObject< 27 | Input: InputType, 28 | Binding: BindingType, 29 | Output: OutputType 30 | >: ObservableObjectType, ObservableObject { 31 | public let input: InputWrapper 32 | public let output: OutputWrapper 33 | 34 | @Bindable 35 | public var binding: BindingWrapper 36 | 37 | public var objectWillChange: AnyPublisher { 38 | let p1 = binding.objectWillChange.map { _ in } 39 | let p2 = output.objectWillChange.map { _ in } 40 | return p1.merge(with: p2).eraseToAnyPublisher() 41 | } 42 | 43 | private let observableObject: AnyObject 44 | 45 | public init( 46 | _ observableObject: Object 47 | ) where Input == Object.Input, Binding == Object.Binding, Output == Object.Output { 48 | self.input = observableObject.input 49 | self._binding = Bindable(wrappedValue: observableObject.binding) 50 | self.output = observableObject.output 51 | self.observableObject = observableObject 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Sources/AnyObservableObject/Binding.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Binding.swift 3 | // AnyObservableObject 4 | // 5 | // Created by marty-suzuki on 2022/04/03. 6 | // Copyright © 2022 marty-suzuki. All rights reserved. 7 | // 8 | 9 | import SwiftUI 10 | 11 | public protocol BindingType: ObservableObject {} 12 | 13 | extension BindingType { 14 | public func toWrapper() -> BindingWrapper { 15 | BindingWrapper(self) 16 | } 17 | } 18 | 19 | @dynamicMemberLookup 20 | public final class BindingWrapper: ObservableObject { 21 | 22 | public var objectWillChange: T.ObjectWillChangePublisher { 23 | object.objectWillChange 24 | } 25 | 26 | fileprivate let object: T 27 | 28 | public init(_ object: T) { 29 | self.object = object 30 | } 31 | 32 | public subscript(dynamicMember keyPath: KeyPath) -> U { 33 | object[keyPath: keyPath] 34 | } 35 | } 36 | 37 | @propertyWrapper 38 | public struct Bindable { 39 | 40 | public let wrappedValue: BindingWrapper 41 | public var projectedValue: Wrapper { 42 | Wrapper(binding: wrappedValue) 43 | } 44 | 45 | public init( 46 | wrappedValue: BindingWrapper 47 | ) { 48 | self.wrappedValue = wrappedValue 49 | } 50 | } 51 | 52 | extension Bindable { 53 | 54 | @dynamicMemberLookup 55 | public struct Wrapper { 56 | fileprivate let binding: BindingWrapper 57 | 58 | public subscript(dynamicMember keyPath: ReferenceWritableKeyPath) -> Binding { 59 | return .init( 60 | get: { self.binding.object[keyPath: keyPath] }, 61 | set: { self.binding.object[keyPath: keyPath] = $0 } 62 | ) 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /Sources/AnyObservableObject/Input.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Input.swift 3 | // AnyObservableObject 4 | // 5 | // Created by marty-suzuki on 2022/04/03. 6 | // Copyright © 2022 marty-suzuki. All rights reserved. 7 | // 8 | 9 | import Combine 10 | 11 | public protocol InputType: AnyObject {} 12 | 13 | extension InputType { 14 | public func toWrapper() -> InputWrapper { 15 | InputWrapper(self) 16 | } 17 | } 18 | 19 | @dynamicMemberLookup 20 | public final class InputWrapper { 21 | fileprivate let object: T 22 | 23 | public init(_ object: T) { 24 | self.object = object 25 | } 26 | 27 | public subscript(dynamicMember keyPath: KeyPath>) -> (U) -> Void { 28 | let subject = object[keyPath: keyPath] 29 | return { subject.send($0) } 30 | } 31 | 32 | public subscript(dynamicMember keyPath: KeyPath>) -> () -> Void { 33 | let subject = object[keyPath: keyPath] 34 | return { subject.send() } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Sources/AnyObservableObject/Output.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Output.swift 3 | // AnyObservableObject 4 | // 5 | // Created by marty-suzuki on 2022/04/03. 6 | // Copyright © 2022 marty-suzuki. All rights reserved. 7 | // 8 | 9 | import Combine 10 | 11 | public protocol OutputType: ObservableObject {} 12 | 13 | extension OutputType { 14 | public func toWrapper() -> OutputWrapper { 15 | OutputWrapper(self) 16 | } 17 | } 18 | 19 | @dynamicMemberLookup 20 | public final class OutputWrapper: ObservableObject { 21 | 22 | public var objectWillChange: T.ObjectWillChangePublisher { 23 | object.objectWillChange 24 | } 25 | 26 | private let object: T 27 | 28 | public init(_ object: T) { 29 | self.object = object 30 | } 31 | 32 | public subscript(dynamicMember keyPath: KeyPath) -> U { 33 | object[keyPath: keyPath] 34 | } 35 | 36 | public subscript(dynamicMember keyPath: KeyPath>) -> AnyPublisher { 37 | object[keyPath: keyPath].eraseToAnyPublisher() 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Tests/AnyObservableObjectTests/AnyObservableObjectTests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | @testable import AnyObservableObject 3 | 4 | final class AnyObservableObjectTests: XCTestCase { 5 | func testExample() throws { 6 | // This is an example of a functional test case. 7 | // Use XCTAssert and related functions to verify your tests produce the correct 8 | // results. 9 | } 10 | } 11 | --------------------------------------------------------------------------------