Simplify Timeline refresh

This commit is contained in:
Thomas Ricouard 2023-02-25 10:10:27 +01:00
parent 250730be42
commit b036e90ce4
7 changed files with 26 additions and 26 deletions

View file

@ -87,7 +87,7 @@ public struct AccountDetailView: View {
group.addTask { await viewModel.fetchAccount() }
group.addTask {
if await viewModel.statuses.isEmpty {
await viewModel.fetchStatuses()
await viewModel.fetchNewestStatuses()
}
}
if !viewModel.isCurrentUser {
@ -99,7 +99,7 @@ public struct AccountDetailView: View {
.refreshable {
Task {
await viewModel.fetchAccount()
await viewModel.fetchStatuses()
await viewModel.fetchNewestStatuses()
}
}
.onChange(of: watcher.latestEvent?.id) { _ in

View file

@ -75,7 +75,7 @@ class AccountDetailViewModel: ObservableObject, StatusesFetcher {
case .statuses, .postsAndReplies, .media:
tabTask?.cancel()
tabTask = Task {
await fetchStatuses()
await fetchNewestStatuses()
}
default:
reloadTabState()
@ -146,7 +146,7 @@ class AccountDetailViewModel: ObservableObject, StatusesFetcher {
self.familiarFollowers = familiarFollowers?.first?.accounts ?? []
}
func fetchStatuses() async {
func fetchNewestStatuses() async {
guard let client else { return }
do {
tabState = .statuses(statusesState: .loading)

View file

@ -14,7 +14,7 @@ public enum StatusesState {
@MainActor
public protocol StatusesFetcher: ObservableObject {
var statusesState: StatusesState { get }
func fetchStatuses() async
func fetchNewestStatuses() async
func fetchNextPage() async
func statusDidAppear(status: Status)
func statusDidDisappear(status: Status)

View file

@ -32,7 +32,7 @@ public struct StatusesListView<Fetcher>: View where Fetcher: StatusesFetcher {
message: "status.error.loading.message",
buttonTitle: "action.retry") {
Task {
await fetcher.fetchStatuses()
await fetcher.fetchNewestStatuses()
}
}
.listRowBackground(theme.primaryBackgroundColor)

View file

@ -46,6 +46,17 @@ public enum TimelineFilter: Hashable, Equatable {
}
return [.home, .local, .federated, .trending]
}
public var supportNewestPagination: Bool {
switch self {
case .trending:
return false
case let .remoteLocal(_, filter):
return filter != .trending
default:
return true
}
}
public var title: String {
switch self {

View file

@ -103,24 +103,15 @@ public struct TimelineView: View {
.navigationBarTitleDisplayMode(.inline)
.onAppear {
viewModel.isTimelineVisible = true
if viewModel.client == nil {
viewModel.client = client
viewModel.timeline = timeline
} else {
Task {
await viewModel.fetchStatuses()
}
}
viewModel.client = client
viewModel.timeline = timeline
}
.onDisappear {
viewModel.isTimelineVisible = false
}
.refreshable {
if timeline == .trending {
await viewModel.reset()
}
HapticManager.shared.fireHaptic(of: .dataRefresh(intensity: 0.3))
await viewModel.fetchStatuses()
await viewModel.fetchNewestStatuses()
HapticManager.shared.fireHaptic(of: .dataRefresh(intensity: 0.7))
}
.onChange(of: watcher.latestEvent?.id) { _ in
@ -146,7 +137,7 @@ public struct TimelineView: View {
if wasBackgrounded {
wasBackgrounded = false
Task {
await viewModel.fetchStatuses()
await viewModel.fetchNewestStatuses()
}
}
case .background:

View file

@ -24,7 +24,7 @@ class TimelineViewModel: ObservableObject {
guard !Task.isCancelled else {
return
}
await fetchStatuses()
await fetchNewestStatuses()
switch timeline {
case let .hashtag(tag, _):
await fetchTag(id: tag)
@ -156,13 +156,11 @@ extension TimelineViewModel {
// MARK: - StatusesFetcher
extension TimelineViewModel: StatusesFetcher {
func fetchStatuses() async {
func fetchNewestStatuses() async {
guard let client else { return }
do {
if await datasource.isEmpty || timeline == .trending {
if await !datasource.isEmpty && timeline == .trending {
return
}
if await datasource.isEmpty || !timeline.supportNewestPagination {
await datasource.reset()
try await fetchFirstPage(client: client)
} else if let latest = await datasource.get().first {
try await fetchNewPagesFrom(latestStatus: latest, client: client)
@ -205,7 +203,7 @@ extension TimelineViewModel: StatusesFetcher {
}
}
// And then we fetch statuses again toget newest statuses from there.
await fetchStatuses()
await fetchNewestStatuses()
} else {
var statuses: [Status] = try await client.get(endpoint: timeline.endpoint(sinceId: nil,
maxId: nil,