Add a flag to disable hidden profiles for ignored users. (#2892)

This commit is contained in:
Doug
2024-05-31 13:37:59 +01:00
committed by GitHub
parent ea08fad530
commit 6deedb593b
8 changed files with 99 additions and 40 deletions
@@ -20,27 +20,30 @@ import SwiftUI
typealias BlockedUsersScreenViewModelType = StateStoreViewModel<BlockedUsersScreenViewState, BlockedUsersScreenViewAction>
class BlockedUsersScreenViewModel: BlockedUsersScreenViewModelType, BlockedUsersScreenViewModelProtocol {
let hideProfiles: Bool
let clientProxy: ClientProxyProtocol
let userIndicatorController: UserIndicatorControllerProtocol
init(clientProxy: ClientProxyProtocol,
init(hideProfiles: Bool,
clientProxy: ClientProxyProtocol,
mediaProvider: MediaProviderProtocol,
userIndicatorController: UserIndicatorControllerProtocol) {
self.hideProfiles = hideProfiles
self.clientProxy = clientProxy
self.userIndicatorController = userIndicatorController
super.init(initialViewState: BlockedUsersScreenViewState(blockedUsers: clientProxy.ignoredUsersPublisher.value ?? []))
let ignoredUsers = clientProxy.ignoredUsersPublisher.value?.map { UserProfileProxy(userID: $0) }
super.init(initialViewState: BlockedUsersScreenViewState(blockedUsers: ignoredUsers ?? []),
imageProvider: mediaProvider)
showLoadingIndicator()
clientProxy.ignoredUsersPublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] blockedUsers in
guard let self else { return }
if let blockedUsers {
hideLoadingIndicator()
state.blockedUsers = blockedUsers
}
guard let blockedUsers else { return }
Task { await self?.updateUsers(blockedUsers) }
}
.store(in: &cancellables)
}
@@ -49,12 +52,12 @@ class BlockedUsersScreenViewModel: BlockedUsersScreenViewModelType, BlockedUsers
override func process(viewAction: BlockedUsersScreenViewAction) {
switch viewAction {
case .unblockUser(let userID):
case .unblockUser(let user):
state.bindings.alertInfo = .init(id: .unblock,
title: L10n.screenBlockedUsersUnblockAlertTitle,
message: L10n.screenBlockedUsersUnblockAlertDescription,
primaryButton: .init(title: L10n.screenBlockedUsersUnblockAlertAction, role: .destructive) { [weak self] in
self?.unblockUser(userID)
self?.unblockUser(user)
},
secondaryButton: .init(title: L10n.actionCancel, role: .cancel, action: nil))
}
@@ -66,12 +69,35 @@ class BlockedUsersScreenViewModel: BlockedUsersScreenViewModelType, BlockedUsers
// MARK: - Private
private func unblockUser(_ userID: String) {
private func updateUsers(_ blockedUsers: [String]) async {
defer { hideLoadingIndicator() }
if hideProfiles {
state.blockedUsers = blockedUsers.map { UserProfileProxy(userID: $0) }
} else {
state.blockedUsers = await withTaskGroup(of: UserProfileProxy.self) { group in
for userID in blockedUsers {
group.addTask {
switch await self.clientProxy.profile(for: userID) {
case .success(let profile): profile
case .failure: UserProfileProxy(userID: userID)
}
}
}
return await group.reduce(into: []) { partialResult, profile in
partialResult.append(profile)
}
}
}
}
private func unblockUser(_ user: UserProfileProxy) {
showLoadingIndicator()
state.processingUserID = userID
state.processingUserID = user.userID
Task {
if case .failure = await clientProxy.unignoreUser(userID) {
if case .failure = await clientProxy.unignoreUser(user.userID) {
state.bindings.alertInfo = .init(id: .error)
}