Skip to content

Commit 9e64be0

Browse files
committed
Update README.md
1 parent c663983 commit 9e64be0

File tree

1 file changed

+22
-58
lines changed

1 file changed

+22
-58
lines changed

README.md

Lines changed: 22 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -51,63 +51,31 @@ class CounterCubit: Cubit<Int> {
5151

5252
Next you need to use it in your views.
5353

54-
Take a look on the next example without wrapping in a **BlocView**.
55-
56-
```swift
57-
struct CubitContentView: View {
58-
@ObservedObject var cubit = CounterCubit()
59-
60-
var body: some View {
61-
VStack {
62-
Button(action: {
63-
self.cubit.increment()
64-
}, label: {
65-
Text("Increment")
66-
})
67-
Button(action: {
68-
self.cubit.decrement()
69-
}, label: {
70-
Text("Decrement")
71-
})
72-
Text("Count: \(cubit.state)")
73-
}
74-
}
75-
}
76-
77-
struct CubitContentView_Previews: PreviewProvider {
78-
static var previews: some View {
79-
CubitContentView()
80-
}
81-
}
82-
```
83-
84-
It is important the the **cubit** instance is wrapped inside **@ObservedObject** property wrapper because the **Cubit** class is monitoring the changes of the **state** property via **@PublishedSubject** property wrapper.
85-
8654
Since our **State**generic state is **Int** we can directly access to an integer getter **state** and display in in the text.
8755

88-
If you want to wrap your **view** in your **body** in a **BlocView** it is done like this:
56+
Next, inside your **body** property create a **BlocView** with some content:
8957

9058
```swift
9159
var body: some View {
92-
BlocView(builder: { (state) in
60+
BlocView(builder: { (cubit) in
9361
VStack {
9462
Button(action: {
95-
self.cubit.increment()
63+
cubit.increment()
9664
}, label: {
9765
Text("Increment")
9866
})
9967
Button(action: {
100-
self.cubit.decrement()
68+
cubit.decrement()
10169
}, label: {
10270
Text("Decrement")
10371
})
104-
Text("Count: \(state)")
72+
Text("Count: \(cubit.state)")
10573
}
106-
}, cubit: cubit)
74+
}, cubit: CounterCubit())
10775
}
10876
```
10977

110-
In this case you may use **state** directly from a **builder** in order to redruce a getter call from **cubit**. Moreover the benefit of this approach will let to use your **cubit** instance as an **@EnvironmentObject** so every child **view** inside your **builder** function will get the instance of **cubit** without a need "to drill" through the whole view tree.
78+
In this case you may use **cubit** directly from a **builder**. Moreover this approach will let to use your **cubit** instance as an **@EnvironmentObject** so every child **view** inside your **builder** function will get the instance of **cubit** without a need "to drill" through the whole view tree.
11179

11280
### Bloc
11381

@@ -120,6 +88,8 @@ The difference between **Cubit** and **Bloc** although the **Bloc** is a child c
12088
As a simple example the **CounterBloc**
12189

12290
```swift
91+
import SwiftBloc
92+
12393
enum CounterEvent {
12494
case increment
12595
case decrement
@@ -173,33 +143,33 @@ Now let's see what is the view looks like:
173143
import SwiftBloc
174144

175145
struct BlocContentView: View {
176-
@ObservedObject var bloc = CounterBloc()
177-
178146
@State var isAlertCalled = false
179147

180148
var body: some View {
181-
BlocView(builder: { (state) in
149+
BlocView(builder: { (bloc) in
182150
VStack {
183-
if state.count > 5 {
151+
if bloc.state.count > 5 {
184152
LimitView()
185153
} else {
186154
OperationView()
187155
}
188156
}
189157
.alert(isPresented: self.$isAlertCalled) {
190-
Alert(title: Text("Hi"), message: Text("Message"), dismissButton: .cancel({
191-
self.bloc.add(event: .increment)
192-
self.bloc.add(event: .increment)
193-
}))
158+
Alert(
159+
title: Text("Hi"),
160+
message: Text("Message"),
161+
dismissButton: .cancel {
162+
bloc.add(event: .increment)
163+
}
164+
)
194165
}
195-
}, action: { (state) in
196-
print(state.count)
197-
if state.count < -1 {
166+
}, action: { (bloc) in
167+
if bloc.state.count < -1 {
198168
DispatchQueue.main.async {
199169
self.isAlertCalled = true
200170
}
201171
}
202-
}, cubit: bloc)
172+
}, cubit: CounterBloc())
203173
}
204174
}
205175

@@ -242,15 +212,9 @@ struct OperationView: View {
242212
}
243213
}
244214
}
245-
246-
struct BlocContentView_Previews: PreviewProvider {
247-
static var previews: some View {
248-
BlocContentView()
249-
}
250-
}
251215
```
252216

253-
Again it is important the the **bloc** instance is wrapped inside **@ObservedObject** property wrapper because the **Bloc** class is monitoring the changes of the **event** property via **@PublishedSubject** property wrapper. This **bloc** instance is set by default as **@EnvironmentObject** and will be available for all child views if needed.
217+
The **Bloc** class is monitoring the changes of the **event** property via **@PublishedSubject** property wrapper. This **bloc** instance is set by default as **@EnvironmentObject** and will be available for all child views if needed.
254218

255219
## Example
256220

0 commit comments

Comments
 (0)