Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions DemoAppSwiftUI/DemoUser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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]
}
20 changes: 15 additions & 5 deletions DemoAppSwiftUI/LoginView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
28 changes: 27 additions & 1 deletion DemoAppSwiftUI/LoginViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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
Expand All @@ -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
}
}
}
}
}
Loading