Skip to content

Commit 2a43774

Browse files
authored
Add Guest Login to the Demo App (#634)
1 parent 5fea4bc commit 2a43774

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed

DemoAppSwiftUI/DemoUser.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,23 @@ public let currentUserIdRegisteredForPush = "currentUserIdRegisteredForPush"
1111
public struct UserCredentials: Codable {
1212
public let id: String
1313
public let name: String
14-
public let avatarURL: URL
14+
public let avatarURL: URL?
1515
public let token: String
1616
public let birthLand: String
17+
18+
var isGuest: Bool {
19+
id == "guest"
20+
}
21+
22+
static var guestUser: UserCredentials {
23+
UserCredentials(
24+
id: "guest",
25+
name: "Guest",
26+
avatarURL: nil,
27+
token: "",
28+
birthLand: ""
29+
)
30+
}
1731
}
1832

1933
extension UserCredentials: Identifiable {
@@ -135,8 +149,7 @@ extension UserCredentials: Identifiable {
135149
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiZ2VuZXJhbF9ncmlldm91cyJ9.g2UUZdENuacFIxhYCylBuDJZUZ2x59MTWaSpndWGCTU",
136150
"Qymaen jai Sheelal"
137151
)
138-
139152
].map {
140153
UserCredentials(id: $0.0, name: $0.1, avatarURL: URL(string: $0.2)!, token: $0.3, birthLand: $0.4)
141-
}
154+
} + [UserCredentials.guestUser]
142155
}

DemoAppSwiftUI/LoginView.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,25 @@ struct DemoUserView: View {
5656

5757
var body: some View {
5858
HStack {
59-
StreamLazyImage(
60-
url: user.avatarURL,
61-
size: CGSize(width: imageSize, height: imageSize)
62-
)
59+
if user.isGuest {
60+
Image(systemName: "person.fill")
61+
.resizable()
62+
.foregroundColor(colors.tintColor)
63+
.frame(width: imageSize, height: imageSize)
64+
.aspectRatio(contentMode: .fit)
65+
.background(Color(colors.background6))
66+
.clipShape(Circle())
67+
} else {
68+
StreamLazyImage(
69+
url: user.avatarURL,
70+
size: CGSize(width: imageSize, height: imageSize)
71+
)
72+
}
6373

6474
VStack(alignment: .leading, spacing: 4) {
6575
Text(user.name)
6676
.font(fonts.bodyBold)
67-
Text("Stream test account")
77+
Text(user.isGuest ? "Login as Guest" : "Stream test account")
6878
.font(fonts.footnote)
6979
.foregroundColor(Color(colors.textLowEmphasis))
7080
}

DemoAppSwiftUI/LoginViewModel.swift

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ class LoginViewModel: ObservableObject {
1414
@Injected(\.chatClient) var chatClient
1515

1616
func demoUserTapped(_ user: UserCredentials) {
17+
if user.isGuest {
18+
connectGuestUser(withCredentials: user)
19+
return
20+
}
21+
1722
connectUser(withCredentials: user)
1823
}
1924

@@ -25,7 +30,7 @@ class LoginViewModel: ObservableObject {
2530
chatClient.connectUser(
2631
userInfo: .init(id: credentials.id, name: credentials.name, imageURL: credentials.avatarURL),
2732
token: token
28-
) { error in
33+
) { [weak self] error in
2934
if let error = error {
3035
log.error("connecting the user failed \(error)")
3136
return
@@ -40,4 +45,25 @@ class LoginViewModel: ObservableObject {
4045
}
4146
}
4247
}
48+
49+
private func connectGuestUser(withCredentials credentials: UserCredentials) {
50+
loading = true
51+
LogConfig.level = .warning
52+
53+
chatClient.connectGuestUser(
54+
userInfo: .init(id: credentials.id, name: credentials.name)
55+
) { [weak self] error in
56+
if let error = error {
57+
log.error("connecting the user failed \(error)")
58+
return
59+
}
60+
61+
DispatchQueue.main.async { [weak self] in
62+
withAnimation {
63+
self?.loading = false
64+
AppState.shared.userState = .loggedIn
65+
}
66+
}
67+
}
68+
}
4369
}

0 commit comments

Comments
 (0)