-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added blocked users example in the demo app (#571)
- Loading branch information
1 parent
42f5672
commit 4e13e10
Showing
5 changed files
with
134 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// | ||
// Copyright © 2024 Stream.io Inc. All rights reserved. | ||
// | ||
|
||
import StreamChatSwiftUI | ||
import SwiftUI | ||
|
||
struct BlockedUsersView: View { | ||
|
||
@StateObject var viewModel = BlockedUsersViewModel() | ||
|
||
var body: some View { | ||
ZStack { | ||
if !viewModel.blockedUsers.isEmpty { | ||
List { | ||
ForEach(viewModel.blockedUsers) { blockedUser in | ||
HStack { | ||
MessageAvatarView(avatarURL: blockedUser.imageURL, size: .init(width: 48, height: 48)) | ||
Text(blockedUser.name ?? blockedUser.id) | ||
.font(.headline) | ||
Spacer() | ||
} | ||
.listRowSeparator(.hidden) | ||
} | ||
.onDelete(perform: delete) | ||
} | ||
.toolbar { | ||
EditButton() | ||
} | ||
.listStyle(.plain) | ||
} else { | ||
VStack { | ||
Text("There are currently no blocked users.") | ||
.padding() | ||
Spacer() | ||
} | ||
} | ||
} | ||
.onAppear { | ||
viewModel.loadBlockedUsers() | ||
} | ||
.navigationTitle("Blocked Users") | ||
} | ||
|
||
func delete(at offsets: IndexSet) { | ||
if let first = offsets.first, first < viewModel.blockedUsers.count { | ||
viewModel.unblock(user: viewModel.blockedUsers[first]) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// Copyright © 2024 Stream.io Inc. All rights reserved. | ||
// | ||
|
||
import StreamChat | ||
import StreamChatSwiftUI | ||
import SwiftUI | ||
|
||
class BlockedUsersViewModel: ObservableObject { | ||
|
||
@Injected(\.chatClient) var chatClient | ||
|
||
@Published var blockedUsers = [ChatUser]() | ||
|
||
private let currentUserController: CurrentChatUserController | ||
|
||
init() { | ||
currentUserController = InjectedValues[\.chatClient].currentUserController() | ||
currentUserController.synchronize() | ||
} | ||
|
||
func loadBlockedUsers() { | ||
let blockedUserIds = currentUserController.currentUser?.blockedUserIds ?? [] | ||
for blockedUserId in blockedUserIds { | ||
if let user = currentUserController.dataStore.user(id: blockedUserId) { | ||
blockedUsers.append(user) | ||
} else { | ||
let controller = chatClient.userController(userId: blockedUserId) | ||
controller.synchronize { [weak self] _ in | ||
if let user = controller.user { | ||
self?.blockedUsers.append(user) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
func unblock(user: ChatUser) { | ||
let unblockController = chatClient.userController(userId: user.id) | ||
unblockController.unblock { [weak self] error in | ||
if error == nil { | ||
self?.blockedUsers.removeAll { blocked in | ||
blocked.id == user.id | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters