Reduce number of request on home timeline and during application start

This commit is contained in:
Marcin Czachurski 2023-10-12 12:06:04 +02:00
parent d7ad482e35
commit 5cc7167336
2 changed files with 11 additions and 5 deletions

View File

@ -20,6 +20,7 @@ public class HomeTimelineService {
private init() { } private init() { }
private let defaultAmountOfDownloadedStatuses = 40 private let defaultAmountOfDownloadedStatuses = 40
private let maximumAmountOfDownloadedStatuses = 80
private let imagePrefetcher = ImagePrefetcher(destination: .diskCache) private let imagePrefetcher = ImagePrefetcher(destination: .diskCache)
private let semaphore = AsyncSemaphore(value: 1) private let semaphore = AsyncSemaphore(value: 1)
@ -123,11 +124,11 @@ public class HomeTimelineService {
var statuses: [Status] = [] var statuses: [Status] = []
var newestStatusId = newestStatus.id 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 { while true {
do { do {
let downloadedStatuses = try await client.getHomeTimeline(minId: newestStatusId, let downloadedStatuses = try await client.getHomeTimeline(minId: newestStatusId,
limit: self.defaultAmountOfDownloadedStatuses, limit: self.maximumAmountOfDownloadedStatuses,
includeReblogs: includeReblogs) includeReblogs: includeReblogs)
guard let firstStatus = downloadedStatuses.first else { guard let firstStatus = downloadedStatuses.first else {
@ -383,7 +384,7 @@ public class HomeTimelineService {
while true { while true {
let downloadedStatuses = try await client.getHomeTimeline(maxId: lastStatusId, let downloadedStatuses = try await client.getHomeTimeline(maxId: lastStatusId,
limit: self.defaultAmountOfDownloadedStatuses, limit: self.maximumAmountOfDownloadedStatuses,
includeReblogs: includeReblogs) includeReblogs: includeReblogs)
// When there is not any older statuses we have to finish. // When there is not any older statuses we have to finish.
@ -395,6 +396,11 @@ public class HomeTimelineService {
let statusesWithImagesOnly = downloadedStatuses.getStatusesWithImagesOnly() let statusesWithImagesOnly = downloadedStatuses.getStatusesWithImagesOnly()
for status in statusesWithImagesOnly { 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. // We have to hide statuses without ALT text.
if hideStatusesWithoutAlt && status.statusContainsAltText() == false { if hideStatusesWithoutAlt && status.statusContainsAltText() == false {
continue continue

View File

@ -62,7 +62,7 @@ struct VernissageApp: App {
} }
.navigationViewStyle(.stack) .navigationViewStyle(.stack)
.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in .onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in
DispatchQueue.main.asyncAfter(deadline: .now() + 5) { DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
Task { Task {
// Refresh indicator of new photos when application is become active. // Refresh indicator of new photos when application is become active.
await self.calculateNewPhotosInBackground() await self.calculateNewPhotosInBackground()
@ -135,7 +135,7 @@ struct VernissageApp: App {
return return
} }
self.setApplicationState(accountModel: signedInAccountModel, checkNewPhotos: true) self.setApplicationState(accountModel: signedInAccountModel)
} }
} }