Follow requests improvements (#679)

* fix: when accepting/rejecting followers, only disable the current request button

* fix: fetch followers request when updating notifications
This commit is contained in:
Jérôme Danthinne 2023-02-06 17:52:36 +01:00 committed by GitHub
parent d930871b04
commit 2f5e170983
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 6 deletions

View file

@ -35,7 +35,7 @@ public struct FollowRequestButtons: View {
}
}
.buttonStyle(.bordered)
.disabled(currentAccount.isUpdating)
.disabled(currentAccount.updatingFollowRequestAccountIds.contains(account.id))
.padding(.top, 4)
}
}

View file

@ -9,6 +9,7 @@ public class CurrentAccount: ObservableObject {
@Published public private(set) var tags: [Tag] = []
@Published public private(set) var followRequests: [Account] = []
@Published public private(set) var isUpdating: Bool = false
@Published public private(set) var updatingFollowRequestAccountIds = Set<String>()
@Published public private(set) var isLoadingAccount: Bool = false
private var client: Client?
@ -122,9 +123,9 @@ public class CurrentAccount: ObservableObject {
public func acceptFollowerRequest(id: String) async {
guard let client else { return }
do {
isUpdating = true
updatingFollowRequestAccountIds.insert(id)
defer {
isUpdating = false
updatingFollowRequestAccountIds.remove(id)
}
_ = try await client.post(endpoint: FollowRequests.accept(id: id))
await fetchFollowerRequests()
@ -134,9 +135,9 @@ public class CurrentAccount: ObservableObject {
public func rejectFollowerRequest(id: String) async {
guard let client else { return }
do {
isUpdating = true
updatingFollowRequestAccountIds.insert(id)
defer {
isUpdating = false
updatingFollowRequestAccountIds.remove(id)
}
_ = try await client.post(endpoint: FollowRequests.reject(id: id))
await fetchFollowerRequests()

View file

@ -52,6 +52,7 @@ public struct NotificationsListView: View {
.background(theme.primaryBackgroundColor)
.task {
viewModel.client = client
viewModel.currentAccount = account
if let lockedType {
viewModel.selectedType = lockedType
}

View file

@ -1,3 +1,4 @@
import Env
import Foundation
import Models
import Network
@ -27,6 +28,7 @@ class NotificationsViewModel: ObservableObject {
}
}
}
var currentAccount: CurrentAccount?
@Published var state: State = .loading
@Published var selectedType: Models.Notification.NotificationType? {
@ -52,7 +54,7 @@ class NotificationsViewModel: ObservableObject {
private var consolidatedNotifications: [ConsolidatedNotification] = []
func fetchNotifications() async {
guard let client else { return }
guard let client, let currentAccount else { return }
do {
var nextPageState: State.PagingState = .hasNextPage
if consolidatedNotifications.isEmpty {
@ -77,6 +79,9 @@ class NotificationsViewModel: ObservableObject {
at: 0
)
}
await currentAccount.fetchFollowerRequests()
withAnimation {
state = .display(notifications: consolidatedNotifications,
nextPageState: consolidatedNotifications.isEmpty ? .none : nextPageState)
@ -96,6 +101,7 @@ class NotificationsViewModel: ObservableObject {
maxId: lastId,
types: queryTypes))
consolidatedNotifications.append(contentsOf: newNotifications.consolidated(selectedType: selectedType))
await currentAccount?.fetchFollowerRequests()
state = .display(notifications: consolidatedNotifications, nextPageState: newNotifications.count < 15 ? .none : .hasNextPage)
} catch {
state = .error(error: error)
@ -136,6 +142,10 @@ class NotificationsViewModel: ObservableObject {
)
}
if event.notification.supportedType == .follow_request, let currentAccount {
await currentAccount.fetchFollowerRequests()
}
withAnimation {
state = .display(notifications: consolidatedNotifications, nextPageState: .hasNextPage)
}