StatusDataController: update to fresh statuses data on user profile

This commit is contained in:
Thomas Ricouard 2023-03-01 19:27:56 +01:00
parent 43a4551d9b
commit a9e935016f
4 changed files with 38 additions and 18 deletions

View file

@ -65,7 +65,9 @@ public struct AccountDetailView: View {
if viewModel.selectedTab == .statuses {
pinnedPostsView
}
StatusesListView(fetcher: viewModel, client: client, routerPath: routerPath)
StatusesListView(fetcher: viewModel,
client: client,
routerPath: routerPath)
case .followedTags:
tagsListView
case .lists:

View file

@ -157,6 +157,7 @@ class AccountDetailViewModel: ObservableObject, StatusesFetcher {
onlyMedia: selectedTab == .media ? true : nil,
excludeReplies: selectedTab == .statuses && !isCurrentUser ? true : nil,
pinned: nil))
StatusDataControllerProvider.shared.updateDataControllers(for: statuses, client: client)
if selectedTab == .statuses {
pinned =
try await client.get(endpoint: Accounts.statuses(id: accountId,
@ -165,10 +166,13 @@ class AccountDetailViewModel: ObservableObject, StatusesFetcher {
onlyMedia: nil,
excludeReplies: nil,
pinned: true))
StatusDataControllerProvider.shared.updateDataControllers(for: pinned, client: client)
}
if isCurrentUser {
(favorites, favoritesNextPage) = try await client.getWithLink(endpoint: Accounts.favorites(sinceId: nil))
(bookmarks, bookmarksNextPage) = try await client.getWithLink(endpoint: Accounts.bookmarks(sinceId: nil))
StatusDataControllerProvider.shared.updateDataControllers(for: favorites, client: client)
StatusDataControllerProvider.shared.updateDataControllers(for: bookmarks, client: client)
}
reloadTabState()
} catch {
@ -191,6 +195,7 @@ class AccountDetailViewModel: ObservableObject, StatusesFetcher {
excludeReplies: selectedTab == .statuses && !isCurrentUser ? true : nil,
pinned: nil))
statuses.append(contentsOf: newStatuses)
StatusDataControllerProvider.shared.updateDataControllers(for: newStatuses, client: client)
tabState = .statuses(statusesState: .display(statuses: statuses,
nextPageState: newStatuses.count < 20 ? .none : .hasNextPage))
case .favorites:
@ -198,11 +203,13 @@ class AccountDetailViewModel: ObservableObject, StatusesFetcher {
let newFavorites: [Status]
(newFavorites, favoritesNextPage) = try await client.getWithLink(endpoint: Accounts.favorites(sinceId: nextPageId))
favorites.append(contentsOf: newFavorites)
StatusDataControllerProvider.shared.updateDataControllers(for: newFavorites, client: client)
tabState = .statuses(statusesState: .display(statuses: favorites, nextPageState: .hasNextPage))
case .bookmarks:
guard let nextPageId = bookmarksNextPage?.maxId else { return }
let newBookmarks: [Status]
(newBookmarks, bookmarksNextPage) = try await client.getWithLink(endpoint: Accounts.bookmarks(sinceId: nextPageId))
StatusDataControllerProvider.shared.updateDataControllers(for: newBookmarks, client: client)
bookmarks.append(contentsOf: newBookmarks)
tabState = .statuses(statusesState: .display(statuses: bookmarks, nextPageState: .hasNextPage))
case .followedTags, .lists:

View file

@ -38,6 +38,14 @@ public final class StatusDataControllerProvider {
cache[key] = controller
return controller
}
public func updateDataControllers(for statuses: [Status], client: Client) {
for status in statuses {
let realStatus: AnyStatus = status.reblog ?? status
let controller = dataController(for: realStatus, client: client)
controller.updateFrom(status: realStatus, publishUpdate: false)
}
}
}
@MainActor

View file

@ -7,19 +7,22 @@ import SwiftUI
public struct StatusesListView<Fetcher>: View where Fetcher: StatusesFetcher {
@EnvironmentObject private var theme: Theme
@ObservedObject private var fetcher: Fetcher
private let isRemote: Bool
private let routerPath: RouterPath
private let client: Client
public init(fetcher: Fetcher, client: Client, routerPath: RouterPath, isRemote: Bool = false) {
public init(fetcher: Fetcher,
client: Client,
routerPath: RouterPath,
isRemote: Bool = false) {
self.fetcher = fetcher
self.isRemote = isRemote
self.client = client
self.routerPath = routerPath
}
public var body: some View {
switch fetcher.statusesState {
case .loading:
@ -35,24 +38,24 @@ public struct StatusesListView<Fetcher>: View where Fetcher: StatusesFetcher {
await fetcher.fetchNewestStatuses()
}
}
.listRowBackground(theme.primaryBackgroundColor)
.listRowSeparator(.hidden)
.listRowBackground(theme.primaryBackgroundColor)
.listRowSeparator(.hidden)
case let .display(statuses, nextPageState):
ForEach(statuses, id: \.viewId) { status in
StatusRowView(viewModel: { StatusRowViewModel(status: status,
client: client,
routerPath: routerPath,
isRemote: isRemote)
})
.id(status.id)
.onAppear {
fetcher.statusDidAppear(status: status)
}
.onDisappear {
fetcher.statusDidDisappear(status: status)
}
})
.id(status.id)
.onAppear {
fetcher.statusDidAppear(status: status)
}
.onDisappear {
fetcher.statusDidDisappear(status: status)
}
}
switch nextPageState {
case .hasNextPage:
@ -69,7 +72,7 @@ public struct StatusesListView<Fetcher>: View where Fetcher: StatusesFetcher {
}
}
}
private var loadingRow: some View {
HStack {
Spacer()