diff --git a/DemoAppSwiftUI/DemoUser.swift b/DemoAppSwiftUI/DemoUser.swift index 54a62cb8e..12d53b5ac 100644 --- a/DemoAppSwiftUI/DemoUser.swift +++ b/DemoAppSwiftUI/DemoUser.swift @@ -11,9 +11,23 @@ public let currentUserIdRegisteredForPush = "currentUserIdRegisteredForPush" public struct UserCredentials: Codable { public let id: String public let name: String - public let avatarURL: URL + public let avatarURL: URL? public let token: String public let birthLand: String + + var isGuest: Bool { + id == "guest" + } + + static var guestUser: UserCredentials { + UserCredentials( + id: "guest", + name: "Guest", + avatarURL: nil, + token: "", + birthLand: "" + ) + } } extension UserCredentials: Identifiable { @@ -135,8 +149,7 @@ extension UserCredentials: Identifiable { "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiZ2VuZXJhbF9ncmlldm91cyJ9.g2UUZdENuacFIxhYCylBuDJZUZ2x59MTWaSpndWGCTU", "Qymaen jai Sheelal" ) - ].map { UserCredentials(id: $0.0, name: $0.1, avatarURL: URL(string: $0.2)!, token: $0.3, birthLand: $0.4) - } + } + [UserCredentials.guestUser] } diff --git a/DemoAppSwiftUI/LoginView.swift b/DemoAppSwiftUI/LoginView.swift index 9a5c41f10..a2ebf8360 100644 --- a/DemoAppSwiftUI/LoginView.swift +++ b/DemoAppSwiftUI/LoginView.swift @@ -56,15 +56,25 @@ struct DemoUserView: View { var body: some View { HStack { - StreamLazyImage( - url: user.avatarURL, - size: CGSize(width: imageSize, height: imageSize) - ) + if user.isGuest { + Image(systemName: "person.fill") + .resizable() + .foregroundColor(colors.tintColor) + .frame(width: imageSize, height: imageSize) + .aspectRatio(contentMode: .fit) + .background(Color(colors.background6)) + .clipShape(Circle()) + } else { + StreamLazyImage( + url: user.avatarURL, + size: CGSize(width: imageSize, height: imageSize) + ) + } VStack(alignment: .leading, spacing: 4) { Text(user.name) .font(fonts.bodyBold) - Text("Stream test account") + Text(user.isGuest ? "Login as Guest" : "Stream test account") .font(fonts.footnote) .foregroundColor(Color(colors.textLowEmphasis)) } diff --git a/DemoAppSwiftUI/LoginViewModel.swift b/DemoAppSwiftUI/LoginViewModel.swift index 673dec322..e69070177 100644 --- a/DemoAppSwiftUI/LoginViewModel.swift +++ b/DemoAppSwiftUI/LoginViewModel.swift @@ -14,6 +14,11 @@ class LoginViewModel: ObservableObject { @Injected(\.chatClient) var chatClient func demoUserTapped(_ user: UserCredentials) { + if user.isGuest { + connectGuestUser(withCredentials: user) + return + } + connectUser(withCredentials: user) } @@ -25,7 +30,7 @@ class LoginViewModel: ObservableObject { chatClient.connectUser( userInfo: .init(id: credentials.id, name: credentials.name, imageURL: credentials.avatarURL), token: token - ) { error in + ) { [weak self] error in if let error = error { log.error("connecting the user failed \(error)") return @@ -40,4 +45,25 @@ class LoginViewModel: ObservableObject { } } } + + private func connectGuestUser(withCredentials credentials: UserCredentials) { + loading = true + LogConfig.level = .warning + + chatClient.connectGuestUser( + userInfo: .init(id: credentials.id, name: credentials.name) + ) { [weak self] error in + if let error = error { + log.error("connecting the user failed \(error)") + return + } + + DispatchQueue.main.async { [weak self] in + withAnimation { + self?.loading = false + AppState.shared.userState = .loggedIn + } + } + } + } }