Skip to content

Commit d2fd04a

Browse files
cubit
1 parent a7f3457 commit d2fd04a

File tree

13 files changed

+266
-75
lines changed

13 files changed

+266
-75
lines changed

Bloc.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//
2+
// Bloc.swift
3+
// Pods-SwiftBloc_Example
4+
//
5+
// Created by Kachalov, Victor on 27.02.21.
6+
//
7+
8+
import Foundation
9+
10+
class Bloc {
11+
static let observer = BlocObserver()
12+
}

BlocObserver.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// BlocObserver.swift
3+
// Pods-SwiftBloc_Example
4+
//
5+
// Created by Kachalov, Victor on 27.02.21.
6+
//
7+
8+
import Foundation
9+
10+
class BlocObserver {
11+
12+
func onCreate<State>(cubit: Cubit<State>) {}
13+
14+
func onEvent(bloc: Bloc, event: Any) {}
15+
16+
func onChange<State>(cubit: Cubit<State>, change: Change<State>) {}
17+
18+
func onTransition<Event, State>(bloc: Bloc, transition: Transition<Event, State>) {}
19+
20+
func onError<State>(cubit: Cubit<State>, error: Any) {}
21+
22+
func onClose<State>(cubit: Cubit<State>) {}
23+
}

Change.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// Change.swift
3+
// Pods-SwiftBloc_Example
4+
//
5+
// Created by Kachalov, Victor on 27.02.21.
6+
//
7+
8+
import Foundation
9+
10+
class Change<State> {
11+
let currentState: State
12+
13+
let nextState: State
14+
15+
var description: String {
16+
return "Change currentState: \(currentState), nextState: \(nextState) }"
17+
}
18+
19+
init(currentState: State, nextState: State) {
20+
self.currentState = currentState
21+
self.nextState = nextState
22+
}
23+
24+
}
25+
// MARK: - Equatable
26+
extension Change: Equatable where State: Equatable {
27+
static func == (lhs: Change<State>, rhs: Change<State>) -> Bool {
28+
lhs.currentState == rhs.currentState && lhs.nextState == rhs.nextState
29+
}
30+
}

Example/Pods/Pods.xcodeproj/project.pbxproj

Lines changed: 30 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/SwiftBloc.xcodeproj/project.pbxproj

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
047EC4D025EAEE860004C90C /* CounterCubit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 047EC4CF25EAEE860004C90C /* CounterCubit.swift */; };
1011
04EB15FB25EAC7E8008E0030 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04EB15FA25EAC7E8008E0030 /* SceneDelegate.swift */; };
1112
08611E94466D97B3F329DCE3 /* Pods_SwiftBloc_Tests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 82EC3EC02FB63CEFC4EA0403 /* Pods_SwiftBloc_Tests.framework */; };
1213
2EF31873A1F3203A48F66733 /* Pods_SwiftBloc_Example.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0D9208F1CC2C0172BFA8D1E4 /* Pods_SwiftBloc_Example.framework */; };
1314
607FACD61AFB9204008FA782 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 607FACD51AFB9204008FA782 /* AppDelegate.swift */; };
14-
607FACD81AFB9204008FA782 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 607FACD71AFB9204008FA782 /* ViewController.swift */; };
15-
607FACDB1AFB9204008FA782 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 607FACD91AFB9204008FA782 /* Main.storyboard */; };
15+
607FACD81AFB9204008FA782 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 607FACD71AFB9204008FA782 /* ContentView.swift */; };
1616
607FACDD1AFB9204008FA782 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 607FACDC1AFB9204008FA782 /* Images.xcassets */; };
1717
607FACE01AFB9204008FA782 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 607FACDE1AFB9204008FA782 /* LaunchScreen.xib */; };
1818
607FACEC1AFB9204008FA782 /* Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 607FACEB1AFB9204008FA782 /* Tests.swift */; };
@@ -30,6 +30,7 @@
3030

3131
/* Begin PBXFileReference section */
3232
03ED00AF75438BCB8C629F90 /* LICENSE */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = LICENSE; path = ../LICENSE; sourceTree = "<group>"; };
33+
047EC4CF25EAEE860004C90C /* CounterCubit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CounterCubit.swift; sourceTree = "<group>"; };
3334
04EB15FA25EAC7E8008E0030 /* SceneDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
3435
0D9208F1CC2C0172BFA8D1E4 /* Pods_SwiftBloc_Example.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SwiftBloc_Example.framework; sourceTree = BUILT_PRODUCTS_DIR; };
3536
0DD499F2FF4B6CDD3A8F0679 /* README.md */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
@@ -41,8 +42,7 @@
4142
607FACD01AFB9204008FA782 /* SwiftBloc_Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwiftBloc_Example.app; sourceTree = BUILT_PRODUCTS_DIR; };
4243
607FACD41AFB9204008FA782 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
4344
607FACD51AFB9204008FA782 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
44-
607FACD71AFB9204008FA782 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
45-
607FACDA1AFB9204008FA782 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
45+
607FACD71AFB9204008FA782 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
4646
607FACDC1AFB9204008FA782 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };
4747
607FACDF1AFB9204008FA782 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = "<group>"; };
4848
607FACE51AFB9204008FA782 /* SwiftBloc_Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SwiftBloc_Tests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -95,10 +95,10 @@
9595
607FACD21AFB9204008FA782 /* Example for SwiftBloc */ = {
9696
isa = PBXGroup;
9797
children = (
98+
047EC4CF25EAEE860004C90C /* CounterCubit.swift */,
9899
607FACD51AFB9204008FA782 /* AppDelegate.swift */,
99100
04EB15FA25EAC7E8008E0030 /* SceneDelegate.swift */,
100-
607FACD71AFB9204008FA782 /* ViewController.swift */,
101-
607FACD91AFB9204008FA782 /* Main.storyboard */,
101+
607FACD71AFB9204008FA782 /* ContentView.swift */,
102102
607FACDC1AFB9204008FA782 /* Images.xcassets */,
103103
607FACDE1AFB9204008FA782 /* LaunchScreen.xib */,
104104
607FACD31AFB9204008FA782 /* Supporting Files */,
@@ -249,7 +249,6 @@
249249
isa = PBXResourcesBuildPhase;
250250
buildActionMask = 2147483647;
251251
files = (
252-
607FACDB1AFB9204008FA782 /* Main.storyboard in Resources */,
253252
607FACE01AFB9204008FA782 /* LaunchScreen.xib in Resources */,
254253
607FACDD1AFB9204008FA782 /* Images.xcassets in Resources */,
255254
);
@@ -334,7 +333,8 @@
334333
isa = PBXSourcesBuildPhase;
335334
buildActionMask = 2147483647;
336335
files = (
337-
607FACD81AFB9204008FA782 /* ViewController.swift in Sources */,
336+
047EC4D025EAEE860004C90C /* CounterCubit.swift in Sources */,
337+
607FACD81AFB9204008FA782 /* ContentView.swift in Sources */,
338338
607FACD61AFB9204008FA782 /* AppDelegate.swift in Sources */,
339339
04EB15FB25EAC7E8008E0030 /* SceneDelegate.swift in Sources */,
340340
);
@@ -359,14 +359,6 @@
359359
/* End PBXTargetDependency section */
360360

361361
/* Begin PBXVariantGroup section */
362-
607FACD91AFB9204008FA782 /* Main.storyboard */ = {
363-
isa = PBXVariantGroup;
364-
children = (
365-
607FACDA1AFB9204008FA782 /* Base */,
366-
);
367-
name = Main.storyboard;
368-
sourceTree = "<group>";
369-
};
370362
607FACDE1AFB9204008FA782 /* LaunchScreen.xib */ = {
371363
isa = PBXVariantGroup;
372364
children = (

Example/SwiftBloc/Base.lproj/Main.storyboard

Lines changed: 0 additions & 30 deletions
This file was deleted.

Example/SwiftBloc/ContentView.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// ViewController.swift
3+
// SwiftBloc
4+
//
5+
// Created by v.kachalov on 02/27/2021.
6+
// Copyright (c) 2021 v.kachalov. All rights reserved.
7+
//
8+
9+
import UIKit
10+
import SwiftBloc
11+
import SwiftUI
12+
13+
struct ContentView: View {
14+
private let cubit = CounterCubit()
15+
16+
var body: some View {
17+
VStack {
18+
Button(action: {
19+
self.cubit.increment()
20+
}) {
21+
Text("Increment")
22+
}
23+
Button(action: {
24+
self.cubit.close()
25+
}) {
26+
Text("Cancel")
27+
}
28+
}
29+
}
30+
}
31+
32+
struct ContentView_Previews: PreviewProvider {
33+
static var previews: some View {
34+
ContentView()
35+
}
36+
}

Example/SwiftBloc/CounterCubit.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// CounterCubit.swift
3+
// SwiftBloc_Example
4+
//
5+
// Created by Kachalov, Victor on 27.02.21.
6+
// Copyright © 2021 CocoaPods. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import SwiftBloc
11+
12+
class CounterCubit: Cubit<Int> {
13+
init() {
14+
super.init(state: 0, onCompletion: { (completed) in
15+
print("Completed")
16+
}) { (newValue) in
17+
print("new value: \(newValue)")
18+
}
19+
}
20+
21+
func increment() {
22+
emit(state: state + 1)
23+
}
24+
}

0 commit comments

Comments
 (0)