|
5 | 5 | import StreamChat |
6 | 6 | import SwiftUI |
7 | 7 |
|
8 | | -/// View for the add users popup. |
| 8 | +/// Full-sheet view for adding members to a channel. |
| 9 | +/// Supports search, pagination, and multi-select with a batch confirm action. |
9 | 10 | public struct AddUsersView<Factory: ViewFactory>: View { |
10 | | - @Injected(\.fonts) private var fonts |
11 | 11 | @Injected(\.colors) private var colors |
12 | 12 |
|
13 | | - private let columns = Array( |
14 | | - repeating: |
15 | | - GridItem( |
16 | | - .adaptive(minimum: 64), |
17 | | - alignment: .top |
18 | | - ), |
19 | | - count: 4 |
20 | | - ) |
21 | | - |
22 | | - private let factory: Factory |
| 13 | + @Environment(\.presentationMode) private var presentationMode |
23 | 14 |
|
| 15 | + private let factory: Factory |
24 | 16 | @StateObject private var viewModel: AddUsersViewModel |
25 | | - var onUserTap: (ChatUser) -> Void |
| 17 | + var onConfirm: @MainActor ([ChatUser]) -> Void |
26 | 18 |
|
27 | 19 | public init( |
28 | 20 | factory: Factory = DefaultViewFactory.shared, |
29 | 21 | loadedUserIds: [String], |
30 | | - onUserTap: @escaping (ChatUser) -> Void |
| 22 | + onConfirm: @escaping @MainActor ([ChatUser]) -> Void |
31 | 23 | ) { |
32 | 24 | _viewModel = StateObject( |
33 | 25 | wrappedValue: AddUsersViewModel(loadedUserIds: loadedUserIds) |
34 | 26 | ) |
35 | | - self.onUserTap = onUserTap |
| 27 | + self.onConfirm = onConfirm |
36 | 28 | self.factory = factory |
37 | 29 | } |
38 | 30 |
|
39 | 31 | init( |
40 | 32 | factory: Factory = DefaultViewFactory.shared, |
41 | 33 | viewModel: AddUsersViewModel, |
42 | | - onUserTap: @escaping (ChatUser) -> Void |
| 34 | + onConfirm: @escaping @MainActor ([ChatUser]) -> Void |
43 | 35 | ) { |
44 | | - _viewModel = StateObject( |
45 | | - wrappedValue: viewModel |
46 | | - ) |
47 | | - self.onUserTap = onUserTap |
| 36 | + _viewModel = StateObject(wrappedValue: viewModel) |
| 37 | + self.onConfirm = onConfirm |
48 | 38 | self.factory = factory |
49 | 39 | } |
50 | 40 |
|
51 | 41 | public var body: some View { |
52 | | - VStack { |
53 | | - SearchBar(text: $viewModel.searchText) |
54 | | - |
55 | | - ScrollView { |
56 | | - LazyVGrid(columns: columns, alignment: .center, spacing: 0) { |
57 | | - ForEach(viewModel.users) { user in |
58 | | - Button { |
59 | | - onUserTap(user) |
60 | | - } label: { |
61 | | - VStack { |
62 | | - let itemSize: CGFloat = 64 |
63 | | - factory.makeUserAvatarView( |
64 | | - options: UserAvatarViewOptions( |
65 | | - user: user, |
66 | | - size: itemSize, |
67 | | - showsIndicator: false |
68 | | - ) |
69 | | - ) |
70 | | - |
71 | | - Text(user.name ?? user.id) |
72 | | - .multilineTextAlignment(.center) |
73 | | - .lineLimit(2) |
74 | | - .font(fonts.footnoteBold) |
75 | | - .frame(width: itemSize) |
76 | | - .foregroundColor(Color(colors.text)) |
| 42 | + NavigationView { |
| 43 | + VStack(spacing: 0) { |
| 44 | + SearchBar(text: $viewModel.searchText) |
| 45 | + |
| 46 | + ScrollView { |
| 47 | + LazyVStack(spacing: 0) { |
| 48 | + ForEach(viewModel.users) { user in |
| 49 | + AddMembersUserRow( |
| 50 | + factory: factory, |
| 51 | + user: user, |
| 52 | + isSelected: viewModel.isSelected(user), |
| 53 | + isAlreadyMember: viewModel.isAlreadyMember(user) |
| 54 | + ) { |
| 55 | + viewModel.toggleUser(user) |
| 56 | + } |
| 57 | + .onAppear { |
| 58 | + viewModel.onUserAppear(user) |
77 | 59 | } |
78 | | - .padding(.all, 8) |
79 | | - } |
80 | | - .onAppear { |
81 | | - viewModel.onUserAppear(user) |
82 | 60 | } |
83 | 61 | } |
84 | 62 | } |
85 | 63 | } |
86 | | - .frame(maxHeight: 240) |
| 64 | + .background(Color(colors.backgroundCoreApp).edgesIgnoringSafeArea(.all)) |
| 65 | + .modifier( |
| 66 | + AddMembersToolbarModifier( |
| 67 | + viewModel: viewModel, |
| 68 | + onConfirm: { onConfirm(viewModel.selectedUsers) }, |
| 69 | + onDismiss: { presentationMode.wrappedValue.dismiss() } |
| 70 | + ) |
| 71 | + ) |
| 72 | + .navigationBarTitleDisplayMode(.inline) |
87 | 73 | } |
88 | | - .standardPadding() |
89 | | - .background(Color(colors.background)) |
90 | | - .cornerRadius(16) |
91 | | - .padding() |
92 | 74 | } |
93 | 75 | } |
94 | 76 |
|
95 | 77 | /// Options used in the add users view. |
96 | 78 | public final class AddUsersOptions: Sendable { |
97 | | - public let loadedUsers: [ChatUser] |
98 | | - |
99 | | - public init(loadedUsers: [ChatUser]) { |
100 | | - self.loadedUsers = loadedUsers |
| 79 | + public let loadedUserIds: [String] |
| 80 | + |
| 81 | + public init(loadedUserIds: [String]) { |
| 82 | + self.loadedUserIds = loadedUserIds |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// MARK: - User Row |
| 87 | + |
| 88 | +private struct AddMembersUserRow<Factory: ViewFactory>: View { |
| 89 | + @Injected(\.colors) private var colors |
| 90 | + @Injected(\.fonts) private var fonts |
| 91 | + @Injected(\.tokens) private var tokens |
| 92 | + @Injected(\.images) private var images |
| 93 | + |
| 94 | + let factory: Factory |
| 95 | + let user: ChatUser |
| 96 | + let isSelected: Bool |
| 97 | + let isAlreadyMember: Bool |
| 98 | + let onTap: () -> Void |
| 99 | + |
| 100 | + var body: some View { |
| 101 | + Button(action: { if !isAlreadyMember { onTap() } }) { |
| 102 | + HStack(spacing: tokens.spacingSm) { |
| 103 | + factory.makeUserAvatarView( |
| 104 | + options: UserAvatarViewOptions( |
| 105 | + user: user, |
| 106 | + size: AvatarSize.large, |
| 107 | + showsIndicator: false |
| 108 | + ) |
| 109 | + ) |
| 110 | + |
| 111 | + VStack(alignment: .leading, spacing: tokens.spacingXxxs) { |
| 112 | + Text(user.name ?? user.id) |
| 113 | + .font(fonts.body) |
| 114 | + .foregroundColor(Color(colors.textPrimary)) |
| 115 | + .lineLimit(1) |
| 116 | + |
| 117 | + if isAlreadyMember { |
| 118 | + Text(L10n.ChatInfo.Members.alreadyMember) |
| 119 | + .font(fonts.footnote) |
| 120 | + .foregroundColor(Color(colors.textTertiary)) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + Spacer() |
| 125 | + |
| 126 | + if !isAlreadyMember { |
| 127 | + selectionIndicator |
| 128 | + } |
| 129 | + } |
| 130 | + .padding(.horizontal, tokens.spacingMd) |
| 131 | + .padding(.vertical, tokens.spacingXs) |
| 132 | + .background(Color(colors.backgroundCoreApp)) |
| 133 | + .contentShape(.rect) |
| 134 | + } |
| 135 | + .buttonStyle(.plain) |
| 136 | + } |
| 137 | + |
| 138 | + private var selectionIndicator: some View { |
| 139 | + ZStack { |
| 140 | + if isSelected { |
| 141 | + Circle() |
| 142 | + .fill(Color(colors.accentPrimary)) |
| 143 | + .frame(width: 24, height: 24) |
| 144 | + Image(uiImage: images.selectionBadgeIcon) |
| 145 | + .customizable() |
| 146 | + .frame(width: tokens.iconSizeXs) |
| 147 | + .foregroundColor(Color(colors.buttonPrimaryTextOnAccent)) |
| 148 | + } else { |
| 149 | + Circle() |
| 150 | + .strokeBorder(Color(colors.borderCoreSubtle), lineWidth: 1.5) |
| 151 | + .frame(width: 24, height: 24) |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +// MARK: - Toolbar |
| 158 | + |
| 159 | +private struct AddMembersToolbarModifier: ViewModifier { |
| 160 | + @Injected(\.colors) private var colors |
| 161 | + @Injected(\.fonts) private var fonts |
| 162 | + @Injected(\.images) private var images |
| 163 | + @Injected(\.tokens) private var tokens |
| 164 | + |
| 165 | + @ObservedObject var viewModel: AddUsersViewModel |
| 166 | + let onConfirm: () -> Void |
| 167 | + let onDismiss: () -> Void |
| 168 | + |
| 169 | + func body(content: Content) -> some View { |
| 170 | + if #available(iOS 26.0, *) { |
| 171 | + content |
| 172 | + .toolbarThemed { |
| 173 | + toolbarContent() |
| 174 | + #if compiler(>=6.2) |
| 175 | + .sharedBackgroundVisibility(.hidden) |
| 176 | + #endif |
| 177 | + } |
| 178 | + } else { |
| 179 | + content |
| 180 | + .toolbarThemed { |
| 181 | + toolbarContent() |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + @ToolbarContentBuilder private func toolbarContent() -> some ToolbarContent { |
| 187 | + ToolbarItem(placement: .principal) { |
| 188 | + Text(L10n.ChatInfo.Members.addMembersTitle) |
| 189 | + .font(fonts.bodyBold) |
| 190 | + .foregroundColor(Color(colors.navigationBarTitle)) |
| 191 | + } |
| 192 | + ToolbarItem(placement: .navigationBarLeading) { |
| 193 | + Button(action: onDismiss) { |
| 194 | + Image(uiImage: images.close) |
| 195 | + .foregroundColor(Color(colors.textSecondary)) |
| 196 | + } |
| 197 | + } |
| 198 | + ToolbarItem(placement: .navigationBarTrailing) { |
| 199 | + Button(action: onConfirm) { |
| 200 | + Image(uiImage: images.selectionBadgeIcon) |
| 201 | + .customizable() |
| 202 | + .frame(width: tokens.iconSizeSm) |
| 203 | + .foregroundColor(Color(colors.buttonPrimaryTextOnAccent)) |
| 204 | + } |
| 205 | + .frame(width: tokens.buttonVisualHeightMd) |
| 206 | + .background(Circle().fill(Color(colors.accentPrimary))) |
| 207 | + } |
101 | 208 | } |
102 | 209 | } |
0 commit comments