Improve refresh timeline
This commit is contained in:
parent
77e6f26cfb
commit
2c855eaf16
|
@ -120,20 +120,6 @@ class StatusDataHandler {
|
|||
}
|
||||
}
|
||||
|
||||
func remove(accountId: String, statuses: [StatusData], viewContext: NSManagedObjectContext? = nil) {
|
||||
let context = viewContext ?? CoreDataHandler.shared.container.viewContext
|
||||
|
||||
for status in statuses {
|
||||
context.delete(status)
|
||||
}
|
||||
|
||||
do {
|
||||
try context.save()
|
||||
} catch {
|
||||
CoreDataError.shared.handle(error, message: "Error during deleting status (remove).")
|
||||
}
|
||||
}
|
||||
|
||||
func setFavourited(accountId: String, statusId: String) {
|
||||
let backgroundContext = CoreDataHandler.shared.newBackgroundContext()
|
||||
|
||||
|
|
|
@ -45,7 +45,7 @@ public class HomeTimelineService {
|
|||
}
|
||||
|
||||
@MainActor
|
||||
public func refreshTimeline(for account: AccountModel) async throws -> String? {
|
||||
public func refreshTimeline(for account: AccountModel, updateLastSeenStatus: Bool = false) async throws -> String? {
|
||||
// Load data from API and operate on CoreData on background context.
|
||||
let backgroundContext = CoreDataHandler.shared.newBackgroundContext()
|
||||
|
||||
|
@ -57,29 +57,28 @@ public class HomeTimelineService {
|
|||
// When Apple introduce good way to show new items without scroll to top then we can change that method.
|
||||
let allStatusesFromApi = try await self.refresh(for: account, on: backgroundContext)
|
||||
|
||||
// Save data into database.
|
||||
CoreDataHandler.shared.save(viewContext: backgroundContext)
|
||||
|
||||
// Update last seen status.
|
||||
if let lastSeenStatusId, updateLastSeenStatus == true {
|
||||
try self.update(lastSeenStatusId: lastSeenStatusId, for: account, on: backgroundContext)
|
||||
}
|
||||
|
||||
// Start prefetching images.
|
||||
self.prefetch(statuses: allStatusesFromApi)
|
||||
|
||||
// Save data into database.
|
||||
CoreDataHandler.shared.save(viewContext: backgroundContext)
|
||||
|
||||
// Return id of last seen status.
|
||||
return lastSeenStatusId
|
||||
}
|
||||
|
||||
public func save(lastSeenStatusId: String, for account: AccountModel) async throws {
|
||||
// Load data from API and operate on CoreData on background context.
|
||||
let backgroundContext = CoreDataHandler.shared.newBackgroundContext()
|
||||
|
||||
private func update(lastSeenStatusId: String, for account: AccountModel, on backgroundContext: NSManagedObjectContext) throws {
|
||||
// Save information about last seen status.
|
||||
guard let accountDataFromDb = AccountDataHandler.shared.getAccountData(accountId: account.id, viewContext: backgroundContext) else {
|
||||
throw DatabaseError.cannotDownloadAccount
|
||||
}
|
||||
|
||||
accountDataFromDb.lastSeenStatusId = lastSeenStatusId
|
||||
|
||||
// Save data into database.
|
||||
CoreDataHandler.shared.save(viewContext: backgroundContext)
|
||||
}
|
||||
|
||||
public func update(status statusData: StatusData, basedOn status: Status, for account: AccountModel) async throws -> StatusData? {
|
||||
|
@ -193,7 +192,9 @@ public class HomeTimelineService {
|
|||
|
||||
// Delete statuses from database.
|
||||
if !dbStatusesToRemove.isEmpty {
|
||||
StatusDataHandler.shared.remove(accountId: account.id, statuses: dbStatusesToRemove, viewContext: backgroundContext)
|
||||
for dbStatusToRemove in dbStatusesToRemove {
|
||||
backgroundContext.delete(dbStatusToRemove)
|
||||
}
|
||||
}
|
||||
|
||||
// Save statuses in database.
|
||||
|
|
|
@ -101,12 +101,10 @@ struct HomeFeedView: View {
|
|||
private func refreshData() async {
|
||||
do {
|
||||
if let account = self.applicationState.account {
|
||||
if let lastSeenStatusId = try await HomeTimelineService.shared.refreshTimeline(for: account) {
|
||||
try await HomeTimelineService.shared.save(lastSeenStatusId: lastSeenStatusId, for: account)
|
||||
self.applicationState.lastSeenStatusId = lastSeenStatusId
|
||||
}
|
||||
let lastSeenStatusId = try await HomeTimelineService.shared.refreshTimeline(for: account, updateLastSeenStatus: true)
|
||||
|
||||
asyncAfter(0.35) {
|
||||
self.applicationState.lastSeenStatusId = lastSeenStatusId
|
||||
self.applicationState.amountOfNewStatuses = 0
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue