From 5cc716733613905de7edf517ba5652f231ef8b9c Mon Sep 17 00:00:00 2001 From: Marcin Czachurski Date: Thu, 12 Oct 2023 12:06:04 +0200 Subject: [PATCH] Reduce number of request on home timeline and during application start --- Vernissage/Services/HomeTimelineService.swift | 12 +++++++++--- Vernissage/VernissageApp.swift | 4 ++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Vernissage/Services/HomeTimelineService.swift b/Vernissage/Services/HomeTimelineService.swift index ecdd45b..06b8b6e 100644 --- a/Vernissage/Services/HomeTimelineService.swift +++ b/Vernissage/Services/HomeTimelineService.swift @@ -20,6 +20,7 @@ public class HomeTimelineService { private init() { } private let defaultAmountOfDownloadedStatuses = 40 + private let maximumAmountOfDownloadedStatuses = 80 private let imagePrefetcher = ImagePrefetcher(destination: .diskCache) private let semaphore = AsyncSemaphore(value: 1) @@ -123,11 +124,11 @@ public class HomeTimelineService { var statuses: [Status] = [] var newestStatusId = newestStatus.id - // There can be more then 40 newest statuses, that's why we have to sometimes send more then one request. + // There can be more then 80 newest statuses, that's why we have to sometimes send more then one request. while true { do { let downloadedStatuses = try await client.getHomeTimeline(minId: newestStatusId, - limit: self.defaultAmountOfDownloadedStatuses, + limit: self.maximumAmountOfDownloadedStatuses, includeReblogs: includeReblogs) guard let firstStatus = downloadedStatuses.first else { @@ -383,7 +384,7 @@ public class HomeTimelineService { while true { let downloadedStatuses = try await client.getHomeTimeline(maxId: lastStatusId, - limit: self.defaultAmountOfDownloadedStatuses, + limit: self.maximumAmountOfDownloadedStatuses, includeReblogs: includeReblogs) // When there is not any older statuses we have to finish. @@ -395,6 +396,11 @@ public class HomeTimelineService { let statusesWithImagesOnly = downloadedStatuses.getStatusesWithImagesOnly() for status in statusesWithImagesOnly { + // When we process default amount of statuses to show we can stop adding another ones to the list. + if statuses.count == self.defaultAmountOfDownloadedStatuses { + break + } + // We have to hide statuses without ALT text. if hideStatusesWithoutAlt && status.statusContainsAltText() == false { continue diff --git a/Vernissage/VernissageApp.swift b/Vernissage/VernissageApp.swift index 4517ea8..ae64589 100644 --- a/Vernissage/VernissageApp.swift +++ b/Vernissage/VernissageApp.swift @@ -62,7 +62,7 @@ struct VernissageApp: App { } .navigationViewStyle(.stack) .onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in - DispatchQueue.main.asyncAfter(deadline: .now() + 5) { + DispatchQueue.main.asyncAfter(deadline: .now() + 2) { Task { // Refresh indicator of new photos when application is become active. await self.calculateNewPhotosInBackground() @@ -135,7 +135,7 @@ struct VernissageApp: App { return } - self.setApplicationState(accountModel: signedInAccountModel, checkNewPhotos: true) + self.setApplicationState(accountModel: signedInAccountModel) } }