Skip to content

Commit 36947ff

Browse files
Implemented persisting login in the demo app
1 parent 7c13638 commit 36947ff

File tree

6 files changed

+89
-6
lines changed

6 files changed

+89
-6
lines changed

DemoAppSwiftUI/AppDelegate.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,23 @@ class AppDelegate: NSObject, UIApplicationDelegate {
6666
messageListConfig: MessageListConfig(dateIndicatorPlacement: .messageList)
6767
)
6868
streamChat = StreamChat(chatClient: chatClient, utils: utils)
69+
70+
let credentials = UnsecureRepository.shared.loadCurrentUser()
71+
if let credentials, let token = try? Token(rawValue: credentials.token) {
72+
chatClient.connectUser(
73+
userInfo: .init(
74+
id: credentials.id,
75+
name: credentials.name,
76+
imageURL: credentials.avatarURL
77+
),
78+
token: token
79+
)
80+
}
6981

70-
DispatchQueue.main.asyncAfter(deadline: .now() + 2.5) {
82+
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
7183
withAnimation {
72-
if AppState.shared.userState == .launchAnimation {
73-
AppState.shared.userState = .notLoggedIn
84+
if AppState.shared.userState == .launchAnimation {
85+
AppState.shared.userState = credentials == nil ? .notLoggedIn : .loggedIn
7486
}
7587
}
7688
}

DemoAppSwiftUI/CustomChannelHeader.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,12 @@ struct CustomChannelModifier: ChannelListHeaderViewModifier {
8686
message: Text("Are you sure you want to sign out?"),
8787
primaryButton: .destructive(Text("Sign out")) {
8888
withAnimation {
89-
chatClient.disconnect {}
90-
AppState.shared.userState = .notLoggedIn
89+
chatClient.disconnect {
90+
UnsecureRepository.shared.removeCurrentUser()
91+
DispatchQueue.main.async {
92+
AppState.shared.userState = .notLoggedIn
93+
}
94+
}
9195
}
9296
},
9397
secondaryButton: .cancel()

DemoAppSwiftUI/DemoUser.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public let apiKeyString = "zcgvnykxsfm8"
88
public let applicationGroupIdentifier = "group.io.getstream.iOS.ChatDemoAppSwiftUI"
99
public let currentUserIdRegisteredForPush = "currentUserIdRegisteredForPush"
1010

11-
public struct UserCredentials {
11+
public struct UserCredentials: Codable {
1212
public let id: String
1313
public let name: String
1414
public let avatarURL: URL

DemoAppSwiftUI/LoginViewModel.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class LoginViewModel: ObservableObject {
3434
DispatchQueue.main.async { [weak self] in
3535
withAnimation {
3636
self?.loading = false
37+
UnsecureRepository.shared.save(user: credentials)
3738
AppState.shared.userState = .loggedIn
3839
}
3940
}

DemoAppSwiftUI/UserRepository.swift

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//
2+
// Copyright © 2023 Stream.io Inc. All rights reserved.
3+
//
4+
5+
import StreamChat
6+
import Foundation
7+
8+
protocol UserRepository {
9+
10+
func save(user: UserCredentials)
11+
12+
func loadCurrentUser() -> UserCredentials?
13+
14+
func removeCurrentUser()
15+
}
16+
17+
//NOTE: This is just for simplicity. User data shouldn't be kept in `UserDefaults`.
18+
final class UnsecureRepository: UserRepository {
19+
enum Key: String, CaseIterable {
20+
case user = "stream.chat.user"
21+
}
22+
23+
private let defaults: UserDefaults
24+
25+
private init(defaults: UserDefaults = UserDefaults.standard) {
26+
self.defaults = defaults
27+
}
28+
29+
private func set(_ value: Any?, for key: Key) {
30+
defaults.set(value, forKey: key.rawValue)
31+
}
32+
33+
private func get<T>(for key: Key) -> T? {
34+
defaults.object(forKey: key.rawValue) as? T
35+
}
36+
37+
static let shared = UnsecureRepository()
38+
39+
func save(user: UserCredentials) {
40+
let encoder = JSONEncoder()
41+
if let encoded = try? encoder.encode(user) {
42+
set(encoded, for: .user)
43+
}
44+
}
45+
46+
func loadCurrentUser() -> UserCredentials? {
47+
if let savedUser: Data = get(for: .user) {
48+
let decoder = JSONDecoder()
49+
do {
50+
let loadedUser = try decoder.decode(UserCredentials.self, from: savedUser)
51+
return loadedUser
52+
} catch {
53+
log.error("Error while decoding user")
54+
}
55+
}
56+
return nil
57+
}
58+
59+
func removeCurrentUser() {
60+
defaults.set(nil, forKey: Key.user.rawValue)
61+
}
62+
}

StreamChatSwiftUI.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
8402EAD4282BFCCA00CCA696 /* UserCredentials.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8402EAD3282BFCCA00CCA696 /* UserCredentials.swift */; };
3333
840A3F3828193AB20084E9CC /* ChatChannelInfoView_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 840A3F3728193AB20084E9CC /* ChatChannelInfoView_Tests.swift */; };
3434
8413D90227A9654600A89432 /* SearchResultsView_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8413D90127A9654600A89432 /* SearchResultsView_Tests.swift */; };
35+
8417AE922ADEDB6400445021 /* UserRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8417AE912ADEDB6400445021 /* UserRepository.swift */; };
3536
841B2EF4278DB9E500ED619E /* MessageListHelperViews.swift in Sources */ = {isa = PBXBuildFile; fileRef = 841B2EF3278DB9E500ED619E /* MessageListHelperViews.swift */; };
3637
841B2EF6278F108700ED619E /* MessageReadIndicatorView_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 841B2EF5278F108700ED619E /* MessageReadIndicatorView_Tests.swift */; };
3738
841B64C427744DB60016FF3B /* ComposerModels.swift in Sources */ = {isa = PBXBuildFile; fileRef = 841B64C327744DB60016FF3B /* ComposerModels.swift */; };
@@ -445,6 +446,7 @@
445446
8402EAD3282BFCCA00CCA696 /* UserCredentials.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserCredentials.swift; sourceTree = "<group>"; };
446447
840A3F3728193AB20084E9CC /* ChatChannelInfoView_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatChannelInfoView_Tests.swift; sourceTree = "<group>"; };
447448
8413D90127A9654600A89432 /* SearchResultsView_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchResultsView_Tests.swift; sourceTree = "<group>"; };
449+
8417AE912ADEDB6400445021 /* UserRepository.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserRepository.swift; sourceTree = "<group>"; };
448450
841B2EF3278DB9E500ED619E /* MessageListHelperViews.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageListHelperViews.swift; sourceTree = "<group>"; };
449451
841B2EF5278F108700ED619E /* MessageReadIndicatorView_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageReadIndicatorView_Tests.swift; sourceTree = "<group>"; };
450452
841B64C327744DB60016FF3B /* ComposerModels.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComposerModels.swift; sourceTree = "<group>"; };
@@ -1010,6 +1012,7 @@
10101012
8465FDDC2747A14700AF091E /* CustomComposerAttachmentView.swift */,
10111013
84335013274BAB15007A1B81 /* ViewFactoryExamples.swift */,
10121014
84FF723E2782FB2E006E26C8 /* iMessagePocView.swift */,
1015+
8417AE912ADEDB6400445021 /* UserRepository.swift */,
10131016
84EDBC36274FE5CD0057218D /* Localizable.strings */,
10141017
8465FCCA27468B7500AF091E /* Info.plist */,
10151018
8465FCC227468B6A00AF091E /* Assets.xcassets */,
@@ -2181,6 +2184,7 @@
21812184
8465FCDA274694D200AF091E /* AppDelegate.swift in Sources */,
21822185
8465FCD8274694D200AF091E /* LaunchScreen.swift in Sources */,
21832186
84B288CF274C545900DD090B /* CreateGroupViewModel.swift in Sources */,
2187+
8417AE922ADEDB6400445021 /* UserRepository.swift in Sources */,
21842188
84FF723F2782FB2E006E26C8 /* iMessagePocView.swift in Sources */,
21852189
);
21862190
runOnlyForDeploymentPostprocessing = 0;

0 commit comments

Comments
 (0)