@@ -124,7 +124,7 @@ struct CounterState: Equatable {
124124
125125class CounterBloc : Bloc<CounterEvent, CounterState> {
126126 init () {
127- super .init (intialState : CounterState (count : 0 ))
127+ super .init (initialState : CounterState (count : 0 ))
128128 }
129129
130130 override func mapEventToState (event : CounterEvent) -> CounterState {
@@ -162,33 +162,39 @@ Now let's see what is the view looks like:
162162import SwiftBloc
163163
164164struct BlocContentView : View {
165- @State var isAlertCalled = false
166-
167165 var body: some View {
168- BlocView ( builder : { (bloc) in
169- VStack {
170- if bloc.state.count > 5 {
171- LimitView ()
172- } else {
173- OperationView ()
174- }
175- }
176- . alert ( isPresented : self .$isAlertCalled) {
177- Alert (
178- title : Text ( " Hi " ),
179- message : Text ( " Message " ),
180- dismissButton : . cancel {
181- bloc. add ( event : . increment )
166+ NavigationView {
167+ BlocView ( builder : { (bloc) in
168+ let isPresented = Binding. constant ( bloc.state .count < -6 )
169+ CounterView ()
170+ . alert ( isPresented : isPresented) {
171+ Alert (
172+ title : Text ( " Hi " ),
173+ message : Text ( " Message " ),
174+ dismissButton : . cancel {
175+ for _ in 0 ..< 6 {
176+ bloc. add ( event : . increment )
177+ }
178+ }
179+ )
182180 }
183- )
184- }
185- }, action : { (bloc) in
186- if bloc.state.count < -1 {
187- DispatchQueue.main .async {
188- self .isAlertCalled = true
189- }
190- }
191- }, cubit : CounterBloc ())
181+ }, action : { (bloc) in
182+ print (bloc.state .count )
183+ }, base : CounterBloc ())
184+ .navigationBarTitle (Text (" Bloc" ), displayMode : .inline )
185+ }
186+ }
187+ }
188+
189+ struct CounterView : View {
190+ @EnvironmentObject var bloc: CounterBloc
191+
192+ var body: some View {
193+ if bloc.state.count > 5 {
194+ LimitView ()
195+ } else {
196+ OperationView ()
197+ }
192198 }
193199}
194200
@@ -199,12 +205,9 @@ struct LimitView: View {
199205 VStack {
200206 Text (" Hooora" )
201207 Button (action : {
202- self .bloc .add (event : .decrement )
203- self .bloc .add (event : .decrement )
204- self .bloc .add (event : .decrement )
205- self .bloc .add (event : .decrement )
206- self .bloc .add (event : .decrement )
207- self .bloc .add (event : .decrement )
208+ for _ in 0 ..< 6 {
209+ bloc.add (event : .decrement )
210+ }
208211 }, label : {
209212 Text (" Reset" )
210213 })
@@ -218,12 +221,12 @@ struct OperationView: View {
218221 var body: some View {
219222 VStack {
220223 Button (action : {
221- self . bloc .add (event : .increment )
224+ bloc.add (event : .increment )
222225 }, label : {
223226 Text (" Send Increment event" )
224227 })
225228 Button (action : {
226- self . bloc .add (event : .decrement )
229+ bloc.add (event : .decrement )
227230 }, label : {
228231 Text (" Send Decrement event" )
229232 })
0 commit comments