Skip to content

Commit 36dcb70

Browse files
added possibility to logout from the sample app
1 parent 5a572fc commit 36dcb70

File tree

5 files changed

+50
-7
lines changed

5 files changed

+50
-7
lines changed

DemoAppSwiftUI/AppDelegate.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ class AppDelegate: NSObject, UIApplicationDelegate {
5454
streamChat = StreamChat(chatClient: chatClient)
5555

5656
DispatchQueue.main.asyncAfter(deadline: .now() + 2.5) {
57-
AppState.shared.userState = .notLoggedIn
57+
withAnimation {
58+
AppState.shared.userState = .notLoggedIn
59+
}
5860
}
5961

6062
return true

DemoAppSwiftUI/CustomChannelHeader.swift

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@
44
//
55

66
import SwiftUI
7+
import StreamChat
78
import StreamChatSwiftUI
9+
import NukeUI
810

911
public struct CustomChannelHeader: ToolbarContent {
10-
12+
1113
@Injected(\.fonts) var fonts
1214
@Injected(\.images) var images
1315

14-
public var title: String
16+
var title: String
17+
var currentUserController: CurrentChatUserController
1518
@Binding var isNewChatShown: Bool
19+
@Binding var logoutAlertShown: Bool
1620

1721
public var body: some ToolbarContent {
1822
ToolbarItem(placement: .principal) {
@@ -27,24 +31,54 @@ public struct CustomChannelHeader: ToolbarContent {
2731
.resizable()
2832
}
2933
}
34+
ToolbarItem(placement: .navigationBarLeading) {
35+
Button {
36+
logoutAlertShown = true
37+
} label: {
38+
LazyImage(source: currentUserController.currentUser?.imageURL)
39+
.onDisappear(.reset)
40+
.clipShape(Circle())
41+
.frame(
42+
width: 30,
43+
height: 30
44+
)
45+
}
46+
47+
}
3048
}
3149

3250
}
3351

3452
struct CustomChannelModifier: ChannelListHeaderViewModifier {
3553

54+
@Injected(\.chatClient) var chatClient
55+
3656
var title: String
3757

3858
@State var isNewChatShown = false
59+
@State var logoutAlertShown = false
3960

4061
func body(content: Content) -> some View {
4162
ZStack {
4263
content.toolbar {
4364
CustomChannelHeader(
4465
title: title,
45-
isNewChatShown: $isNewChatShown
66+
currentUserController: chatClient.currentUserController(),
67+
isNewChatShown: $isNewChatShown,
68+
logoutAlertShown: $logoutAlertShown
4669
)
4770
}
71+
.alert(isPresented: $logoutAlertShown) {
72+
Alert(
73+
title: Text("Sign out"),
74+
message: Text("Are you sure you want to sign out?"),
75+
primaryButton: .destructive(Text("Sign out")) {
76+
withAnimation {
77+
AppState.shared.userState = .notLoggedIn
78+
}
79+
},
80+
secondaryButton: .cancel())
81+
}
4882

4983
NavigationLink(isActive: $isNewChatShown) {
5084
NewChatView(isNewChatShown: $isNewChatShown)

DemoAppSwiftUI/LoginView.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ struct LoginView: View {
1212

1313
var body: some View {
1414
VStack {
15-
1615
Image("STREAMMARK")
1716
.resizable()
1817
.aspectRatio(contentMode: .fit)
@@ -40,6 +39,9 @@ struct LoginView: View {
4039

4140
Spacer()
4241
}
42+
.overlay(
43+
viewModel.loading ? ProgressView() : nil
44+
)
4345
}
4446

4547
}

DemoAppSwiftUI/LoginViewModel.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import StreamChatSwiftUI
99
class LoginViewModel: ObservableObject {
1010

1111
@Published var demoUsers = UserCredentials.builtInUsers
12+
@Published var loading = false
1213

1314
@Injected(\.chatClient) var chatClient
1415

@@ -17,6 +18,7 @@ class LoginViewModel: ObservableObject {
1718
}
1819

1920
private func connectUser(withCredentials credentials: UserCredentials) {
21+
loading = true
2022
let token = try! Token(rawValue: credentials.token)
2123
LogConfig.level = .warning
2224

@@ -29,8 +31,11 @@ class LoginViewModel: ObservableObject {
2931
return
3032
}
3133

32-
DispatchQueue.main.async {
33-
AppState.shared.userState = .loggedIn
34+
DispatchQueue.main.async { [weak self] in
35+
withAnimation {
36+
self?.loading = false
37+
AppState.shared.userState = .loggedIn
38+
}
3439
}
3540
}
3641
}

0 commit comments

Comments
 (0)