diff --git a/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel+LoadLatestState.swift b/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel+LoadLatestState.swift index 846b87d62..3c8f15969 100644 --- a/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel+LoadLatestState.swift +++ b/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel+LoadLatestState.swift @@ -115,7 +115,7 @@ extension HomeTimelineViewModel.LoadLatestState { } do { - await AuthenticationServiceProvider.shared.fetchAccounts() + await AuthenticationServiceProvider.shared.fetchAccounts(onlyIfItHasBeenAwhile: true) let response: Mastodon.Response.Content<[Mastodon.Entity.Status]> /// To find out wether or not we need to show the "Load More" button diff --git a/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel+LoadOldestState.swift b/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel+LoadOldestState.swift index fae432104..3cceef8ba 100644 --- a/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel+LoadOldestState.swift +++ b/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel+LoadOldestState.swift @@ -61,7 +61,7 @@ extension HomeTimelineViewModel.LoadOldestState { } do { - await AuthenticationServiceProvider.shared.fetchAccounts() + await AuthenticationServiceProvider.shared.fetchAccounts(onlyIfItHasBeenAwhile: true) let response: Mastodon.Response.Content<[Mastodon.Entity.Status]> diff --git a/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel.swift b/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel.swift index cd6b2f8b6..69d674b6c 100644 --- a/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel.swift +++ b/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel.swift @@ -166,7 +166,7 @@ extension HomeTimelineViewModel { guard let status = record.status else { return } record.isLoadingMore = true - await AuthenticationServiceProvider.shared.fetchAccounts() + await AuthenticationServiceProvider.shared.fetchAccounts(onlyIfItHasBeenAwhile: true) // fetch data let response: Mastodon.Response.Content<[Mastodon.Entity.Status]>? diff --git a/MastodonSDK/Sources/MastodonCore/AuthenticationServiceProvider.swift b/MastodonSDK/Sources/MastodonCore/AuthenticationServiceProvider.swift index db4bf11d4..82fa92bdb 100644 --- a/MastodonSDK/Sources/MastodonCore/AuthenticationServiceProvider.swift +++ b/MastodonSDK/Sources/MastodonCore/AuthenticationServiceProvider.swift @@ -10,6 +10,9 @@ import os.log @MainActor public class AuthenticationServiceProvider: ObservableObject { + + private(set) var lastFetchOfAllAccounts: Date? + private let logger = Logger(subsystem: "AuthenticationServiceProvider", category: "Authentication") public static let shared = AuthenticationServiceProvider() @@ -238,11 +241,25 @@ public extension AuthenticationServiceProvider { userDefaults.didMigrateAuthentications == false } - func fetchAccounts() async { + func fetchAccounts(onlyIfItHasBeenAwhile: Bool) async { // FIXME: This is a dirty hack to make the performance-stuff work. // Problem is, that we don't persist the user on disk anymore. So we have to fetch // it when we need it to display on the home timeline. // We need this (also) for the Account-list, but it might be the wrong place. App Startup might be more appropriate + + let minTimeBetweenAutomaticAccountFetches = TimeInterval( 60 * 60 * 24) // one day + let itHasBeenAwhile: Bool + + if let lastFetch = lastFetchOfAllAccounts { + itHasBeenAwhile = lastFetch.distance(to: Date.now) > minTimeBetweenAutomaticAccountFetches + } else { + itHasBeenAwhile = true + } + + guard itHasBeenAwhile else { return } + + lastFetchOfAllAccounts = Date.now + for authentication in authentications { guard let account = try? await APIService.shared.accountInfo(MastodonAuthenticationBox(authentication: authentication)) else { continue } } diff --git a/MastodonSDK/Sources/MastodonCore/DataController/FeedDataController.swift b/MastodonSDK/Sources/MastodonCore/DataController/FeedDataController.swift index 6ace64d5c..501a60bbc 100644 --- a/MastodonSDK/Sources/MastodonCore/DataController/FeedDataController.swift +++ b/MastodonSDK/Sources/MastodonCore/DataController/FeedDataController.swift @@ -205,10 +205,11 @@ final public class FeedDataController { } private extension FeedDataController { + func load(kind: MastodonFeed.Kind, maxID: MastodonStatus.ID?) async throws -> [MastodonFeed] { switch kind { case .home(let timeline): - await AuthenticationServiceProvider.shared.fetchAccounts() + await AuthenticationServiceProvider.shared.fetchAccounts(onlyIfItHasBeenAwhile: true) let response: Mastodon.Response.Content<[Mastodon.Entity.Status]>