You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jun 13, 2024. It is now read-only.
// Build our component, and make the property injector
105
-
let propertyInjector =try!Component().build()
106
-
107
-
// Now inject the properties into ourselves
108
-
propertyInjector.injectProperties(into: self)
112
+
The parameters and return types are confusing right now, but will make more sense as we go along.
109
113
110
-
window!.makeKeyAndVisible()
111
-
112
-
returntrue
113
-
}
114
-
115
-
Now, if we ran the app as is, it would blow up. We haven't told cleanse how to make a `PropertyInjector<AppDelegate>`,
116
-
so let's do that. For the simplest app delegates, we need to populate just one property:
114
+
The first function is required of any `Component` since it tells Cleanse how to construct the root object. Since we have to use property injection, we will fill out its contents with the following:
return bind.propertyInjector(configuredWith: { bind in
120
+
bind.to(injector: AppDelegate.injectProperties)
121
+
})
122
+
}
123
+
124
+
**Note**: Even though we can configure property injection with closures, it is generally cleaner to make a method that sets the
125
+
properties like we did with `AppDelegate.injectProperties`:
121
126
122
-
Even though we can configure property injection with closures, it is generally cleaner to make a method that sets the
123
-
properties. Let's define a method like:
124
127
125
-
.. code-block:: swift
128
+
Now, in our App Delegate let's add the new function we referenced when configuring our root object.
129
+
This tells Cleanse to use the ``injectProperties`` function when a ``PropertyInjector<AppDelegate>`` is
130
+
requested.
126
131
127
-
extensionAppDelegate {
128
-
/// Requests the main window and sets it
129
-
funcinjectProperties(window: UIWindow) {
130
-
self.window= window
131
-
}
132
+
.. code-block::swift
133
+
func injectProperties(_ window: UIWindow) {
134
+
self.window = window
132
135
}
133
136
134
-
And add the following to ``AppDelegate.Component.configure``
137
+
We've successfully wired up our root component! Our root object `PropertyInjector<AppDelegate>` is configured properly, so in our App Delegate we can now `build` the component (and graph) to use.
// Build our component, and make the property injector
143
+
let propertyInjector =try! ComponentFactory.of(AppDelegate.Component.self).build(())
143
144
144
-
This tells Cleanse to use the ``AppDelegate.injectProperties()`` function when a ``PropertyInjector<AppDelegate>`` is
145
-
requested.
145
+
// Now inject the properties into ourselves
146
+
propertyInjector.injectProperties(into: self)
147
+
148
+
window!.makeKeyAndVisible()
146
149
150
+
returntrue
151
+
}
147
152
148
153
Satisfying Dependencies
149
154
~~~~~~~~~~~~~~~~~~~~~~~
150
155
151
-
Running the app now, would yield a new error saying a provider for ``UIWindow`` is missing. That's because we haven't
152
-
configured it.
156
+
Running the app now will yield a new error saying a provider for ``UIWindow`` is missing. That's because we referenced it from our `injectProperties` function, but Cleanse didn't find a binding for the ``UIWindow`` type. So let's create one!
153
157
154
158
A ``Module`` in Cleanse is similar to a ``Component`` but doesn't define a root object, ``Component``\ s can *install*
155
159
``Module``\ s and ``Modules``\ s can install other ``Modules`` using ``binder.install(module:)``.
156
160
157
-
Let's define a module that creates our main window. The following will declare `UIWindow` as a singleton.
161
+
Let's define a module that creates our main window. The following will declare `UIWindow` as a singleton. We can do this by changing the parameter `Binder<Unscoped>` to `Binder<Singleton>`. You can learn more about scopes in the `Scope Step`_ section.
158
162
159
163
.. code-block:: swift
160
164
161
165
extensionUIWindow {
162
166
structModule : Cleanse.Module {
163
-
publicfuncconfigure<B : Binder>(binderbinder: B) {
167
+
publicfuncconfigure(binder: Binder<Singleton>) {
164
168
binder
165
169
.bind(UIWindow.self)
166
-
.asSingleton()
170
+
.sharedInScope()
167
171
.to { (rootViewController: TaggedProvider<UIViewController.Root>) in
168
172
let window =UIWindow(frame: UIScreen.mainScreen().bounds)
(note: One can omit the `RootViewController.self` from the binding builder as it's not required, but we recommend you include it anyways to make it easier to discover where specific types are constructed in Cleanse.)
248
+
243
249
and in our ``AppDelegate.Component.configure`` method we want to install this module by adding
0 commit comments