-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCreateGroupChannelView.swift
137 lines (116 loc) · 4.57 KB
/
CreateGroupChannelView.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//
// CreateGroupChannelView.swift
// SendbirdSwiftUI
//
// Copyright © 2024 Sendbird, Inc. All rights reserved.
//
import SwiftUI
import SendbirdChatSDK
/// A view that displays a CreateGroupChannel
public struct CreateGroupChannelView: View {
// MARK: - Properties
@Environment(\.dismiss)
private var dismiss
var configurations: [(SBUCreateChannelViewController) -> Void] = []
@ObservedObject private var provider: CreateGroupChannelViewProvider
public init() {
self.provider = CreateGroupChannelViewProvider()
}
public var body: some View {
SBUViewControllerSet.CreateChannelViewController
.swiftUI {
createViewController()
}
.injectData { viewController in
if self.shouldUpdateData(viewController: viewController) {
// Inject data into view model and load
viewController.viewModel?.initializeAndLoad(
users: self.provider.customUsers
)
}
}
.configure { viewController in
viewController.dismissAction = {
dismiss()
}
// Used in SubViewBuilder
configurations.forEach { config in
config(viewController)
}
// Apply viewUpdateHandlers recursively
viewConverter.applyViewUpdates(to: viewController)
}
.switchUIKitNavigationBar()
.onDisappear {
if GlobalOptions.enableViewAdaptorAutoReset {
SBViewConverterSet.CreateGroupChannel = CreateGroupChannelViewConverter()
}
}
}
// MARK: - Methods
private func createViewController() -> SBUCreateChannelViewController {
let viewController = SBUViewControllerSet.CreateChannelViewController.init(
users: self.provider.customUsers,
type: self.provider.type
)
self.provider.bind(viewController: viewController)
return viewController
}
private func shouldUpdateData(viewController: SBUCreateChannelViewController) -> Bool {
let shouldUpdateCustomUsers = viewController.viewModel?.customizedUsers == nil && self.provider.customUsers != nil
return shouldUpdateCustomUsers
}
}
// MARK: - Initializers
/// CreateGroupChannelView initializers
public extension CreateGroupChannelView {
// MARK: - typealias
typealias ListContent = CreateGroupChannelViewConverter.List
init(
provider: CreateGroupChannelViewProvider? = nil,
headerItem: (() -> CreateGroupChannelType.HeaderItem)? = nil,
listItem: (() -> CreateGroupChannelType.ListItem)? = nil
) {
self.provider = provider ?? CreateGroupChannelViewProvider()
if let headerItem { _ = headerItem() }
if let listItem { _ = listItem() }
// Apply view converter in viewConverterSet.
self.applyViewConverterSet()
}
// TODO: After entire content is implemented
internal init<Content: View>(
provider: CreateGroupChannelViewProvider? = nil,
headerItem: (() -> CreateGroupChannelType.HeaderItem)? = nil,
list: @escaping (ListContent.TableView.ViewConfig) -> Content
) {
self.provider = provider ?? CreateGroupChannelViewProvider()
typealias ViewConverterType = ViewConverter<ListContent.TableView.ViewConfig>
let listViewConverter: ViewConverterType = ViewConverter { listConfig in
UIHostingController(rootView: list(listConfig)).view
}
self.viewConverter.list.tableView.entireContent = listViewConverter
self.viewConverter.list.tableView.viewUpdateHandlers[.entireContent] = { viewController in
viewController.listComponent?.reloadTableView()
}
// Apply view converter in viewConverterSet.
self.applyViewConverterSet()
}
}
// MARK: Event handler interfaces
public extension CreateGroupChannelView {
func onSendbirdSelectRow(_ selectRowHandler: @escaping ((_ indexPath: IndexPath) -> Void)) -> Self {
let copy = self
copy.provider.eventHandlers.selectRowHandler = selectRowHandler
return copy
}
func onSendbirdError(_ errorHandler: @escaping ((_ error: SBError?) -> Void)) -> Self {
let copy = self
copy.provider.eventHandlers.errorHandler = errorHandler
return copy
}
}
#Preview {
NavigationView {
CreateGroupChannelView()
}
}