/
StreamWrapper.swift
77 lines (72 loc) · 2.44 KB
/
StreamWrapper.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//
// StreamWrapper.swift
// ChatWithVideoUIKit
//
// Created by Martin Mitrevski on 22.11.22.
//
import Foundation
import StreamChat
import StreamVideo
import StreamVideoSwiftUI
import StreamVideoUIKit
import StreamChatUI
typealias StreamTokenProvider = (@escaping (Result<String, Error>) -> Void) -> Void
class StreamWrapper {
let chatClient: ChatClient
let streamVideo: StreamVideo
let streamVideoUI: StreamVideoUI
var tokenProvider: StreamTokenProvider
init(
apiKey: String,
userCredentials: UserCredentials,
tokenProvider: @escaping StreamTokenProvider
) {
Self.applyChatCustomizations()
chatClient = ChatClient(config: .init(apiKeyString: apiKey))
self.tokenProvider = tokenProvider
let token = userCredentials.videoToken
streamVideo = StreamVideo(
apiKey: apiKey,
user: userCredentials.user,
token: token,
videoConfig: VideoConfig(),
tokenProvider: { result in
tokenProvider { tokenResult in
switch tokenResult {
case .success(let rawValue):
let updatedToken = UserToken(rawValue: rawValue)
result(.success(updatedToken))
case .failure(let error):
result(.failure(error))
}
}
}
)
streamVideoUI = StreamVideoUI(streamVideo: streamVideo)
let userInfo = UserInfo.init(
id: userCredentials.user.id,
name: userCredentials.user.name,
imageURL: userCredentials.user.imageURL,
extraData: [:]
)
chatClient.connectUser(userInfo: userInfo) { result in
tokenProvider { tokenResult in
switch tokenResult {
case .success(let rawValue):
do {
let updatedToken = try Token(rawValue: rawValue)
result(.success(updatedToken))
} catch {
result(.failure(error))
}
case .failure(let error):
result(.failure(error))
}
}
}
}
static func applyChatCustomizations() {
Components.default.channelVC = ChatWithVideoViewController.self
Components.default.channelListRouter = ChannelListRouter.self
}
}