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

View file

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

View file

@ -14,7 +14,7 @@ public enum StatusesState {
@MainActor @MainActor
public protocol StatusesFetcher: ObservableObject { public protocol StatusesFetcher: ObservableObject {
var statusesState: StatusesState { get } var statusesState: StatusesState { get }
func fetchStatuses() async func fetchNewestStatuses() async
func fetchNextPage() async func fetchNextPage() async
func statusDidAppear(status: Status) func statusDidAppear(status: Status)
func statusDidDisappear(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", message: "status.error.loading.message",
buttonTitle: "action.retry") { buttonTitle: "action.retry") {
Task { Task {
await fetcher.fetchStatuses() await fetcher.fetchNewestStatuses()
} }
} }
.listRowBackground(theme.primaryBackgroundColor) .listRowBackground(theme.primaryBackgroundColor)

View file

@ -46,6 +46,17 @@ public enum TimelineFilter: Hashable, Equatable {
} }
return [.home, .local, .federated, .trending] 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 { public var title: String {
switch self { switch self {

View file

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

View file

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