7
7
//
8
8
9
9
import SwiftUI
10
+ import Combine
10
11
import Quickblox
11
12
12
13
enum ConnectState {
@@ -19,16 +20,22 @@ enum ConnectState {
19
20
20
21
enum AuthState {
21
22
case authorized
23
+ case authorization
22
24
case unAuthorized
23
25
}
24
26
25
27
class Connect : ObservableObject {
26
- @Published var authState : AuthState = . unAuthorized
27
- @Published var state : ConnectState = . waiting
28
+
29
+ public let objectWillChange = PassthroughSubject < AuthState , Never > ( )
30
+
31
+ @Published var authState : AuthState = . unAuthorized {
32
+ didSet {
33
+ objectWillChange. send ( authState)
34
+ }
35
+ }
28
36
@Published var isConnected : Bool = false
29
37
30
38
init ( state: ConnectState = . disconnected) {
31
- self . state = state
32
39
33
40
Quickblox . initWithApplicationId ( 0 ,
34
41
authKey: " " ,
@@ -40,32 +47,35 @@ class Connect: ObservableObject {
40
47
}
41
48
42
49
func login( withLogin login: String , password: String ) {
43
- state = . waiting
50
+ authState = . authorization
44
51
QBRequest . logIn ( withUserLogin: login. trimmingCharacters ( in: . whitespacesAndNewlines) ,
45
52
password: password. trimmingCharacters ( in: . whitespacesAndNewlines) ) { [ weak self] response, user in
46
53
guard QBSession . current. sessionDetails? . token != nil else {
47
54
print ( " Login Error: \( response) " )
48
- self ? . state = . disconnected
49
- self ? . authState = . unAuthorized
55
+ DispatchQueue . main. async {
56
+ self ? . authState = . unAuthorized
57
+ }
50
58
return
51
59
}
52
- self ? . authState = . authorized
60
+ DispatchQueue . main. async {
61
+ self ? . authState = . authorized
62
+ }
53
63
} errorBlock: { [ weak self] response in
54
64
if response. status == QBResponseStatusCode . unAuthorized {
55
65
// The user with existent login was created earlier
56
- self ? . state = . unAuthorized
57
66
self ? . authState = . unAuthorized
58
67
return
59
68
}
60
69
print ( " Login Error: \( response) " )
61
- self ? . authState = . unAuthorized
62
- self ? . state = . disconnected
70
+ DispatchQueue . main. async {
71
+ self ? . authState = . unAuthorized
72
+ }
63
73
return
64
74
}
65
75
}
66
76
67
77
func signUp( withLogin login: String , displayName: String , password: String ) {
68
- state = . waiting
78
+ authState = . authorization
69
79
let newUser = QBUUser ( )
70
80
newUser. login = login. trimmingCharacters ( in: . whitespacesAndNewlines)
71
81
newUser. fullName = displayName
@@ -76,35 +86,23 @@ class Connect: ObservableObject {
76
86
if response. status == QBResponseStatusCode . validationFailed {
77
87
// The user with existent login was created earlier
78
88
self ? . login ( withLogin: login, password: password)
79
- self ? . state = . waiting
80
89
return
81
90
}
82
91
print ( " Login Error: \( response) " )
83
- self ? . state = . disconnected
92
+ DispatchQueue . main. async {
93
+ self ? . authState = . unAuthorized
94
+ }
84
95
return
85
96
} )
86
97
}
87
98
88
- func connect( withUserID userId: UInt ) {
89
- state = . waiting
90
- guard let token = QBSession . current. sessionDetails? . token else {
91
- self . state = . disconnected
92
- return
93
- }
94
- QBChat . instance. connect ( withUserID: userId, password: token) { [ weak self] _ in
95
- self ? . isConnected = true
96
- self ? . state = . connected
97
- print ( " Success connect " )
98
- }
99
- }
100
-
101
99
func disconnect( ) {
102
- state = . waiting
103
100
QBChat . instance. disconnect ( ) { _ in
104
101
self . isConnected = false
105
- self . state = . disconnected
106
102
QBRequest . logOut { [ weak self] response in
107
- self ? . authState = . unAuthorized
103
+ DispatchQueue . main. async {
104
+ self ? . authState = . unAuthorized
105
+ }
108
106
print ( " Success disconnect " )
109
107
}
110
108
}
0 commit comments