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

Readme updated by adding SwiftUI usage. #2

Merged
merged 1 commit into from
Nov 21, 2023
Merged
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
88 changes: 85 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ Then, import `FeatureStore` dependency in your Swift files where you intend to u
import FeatureStore
```

## Usage
Let's consider a UIKit app:
## Usage | UIKit & SwiftUI
To get started, follow these basic steps:

### 1. Register a Feature
Expand All @@ -57,6 +56,7 @@ Here, `MyFeatureProtocol.self` is the type of your feature, and the closure is u
### 2. Resolve a Feature
To access a registered feature, use the `resolve` method:

#### UIKit
```swift
import FeatureStore
import UIKit
Expand All @@ -73,6 +73,23 @@ public extension FeatureStore {
}
```

#### SwiftUI
```swift
import FeatureStore
import SwiftUI

public protocol MyFeatureProtocol {
// Pass some parameters into the function if needed between features.
func build() -> AnyView
}

public extension FeatureStore {
var myFeature: MyFeatureProtocol {
resolve(MyFeatureProtocol.self)!
}
}
```

Make sure to replace `MyFeature` with the actual feature type you want to resolve.

### 3. Unregister a Feature
Expand All @@ -89,7 +106,8 @@ Firstly, we need to register these features:

```swift
import FeatureStore
import UIKit
import FeatureA
import FeatureB

FeatureStore.shared.register(FeatureAProtocol.self) {
FeatureA()
Expand All @@ -103,6 +121,7 @@ For each feature, create resolvers:

FeatureA:

#### UIKit
```swift
import FeatureStore
import UIKit
Expand All @@ -118,7 +137,25 @@ public extension FeatureStore {
}
```

#### SwiftUI
```swift
import FeatureStore
import SwiftUI

public protocol FeatureAProtocol {
func build() -> AnyView
}

public extension FeatureStore {
var featureA: FeatureAProtocol {
resolve(FeatureAProtocol.self)!
}
}
```

FeatureB:

#### UIKit
```swift
import FeatureStore
import UIKit
Expand All @@ -134,8 +171,25 @@ public extension FeatureStore {
}
```

#### SwiftUI
```swift
import FeatureStore
import SwiftUI

public protocol FeatureBProtocol {
func build() -> AnyView
}

public extension FeatureStore {
var featureB: FeatureBProtocol {
resolve(FeatureBProtocol.self)!
}
}
```

As we want to present `FeatureB` from `FeatureA`, so we will create a `public` builder in `FeatureB`:

#### UIKit
```swift
import FeatureStore
import UIKit
Expand All @@ -151,12 +205,40 @@ public struct FeatureBBuilder: FeatureBBuilderProtocol {
}
```

#### SwiftUI
```swift
import FeatureStore
import SwiftUI

public struct FeatureBBuilder: FeatureBBuilderProtocol {
public init() {}

public func build() -> AnyView {
let featureAView = FeatureASwiftUIView()
// Configure your controller as needed
return AnyView(featureAView)
}
}
```

Finally, we just need to build featureB from featureA and navigate to featureB:

#### UIKit
```swift
let controller = FeatureStore.shared.featureB.build()
present(controller, animated: true)
```

#### SwiftUI
```swift
var body: some View {
NavigationStack {
NavigationLink("Show FeatureB Screen") {
FeatureStore.shared.featureB.build()
}
}
}
```

## Support
If you have any questions, encounter issues, or want to contribute, please [create an issue](https://github.com/SerhanAksut/DependencyKit/issues) or [submit a pull request](https://github.com/SerhanAksut/DependencyKit/pulls) on GitHub.