Skip to content

Commit

Permalink
perf(ThreadListViewModel): Sort in the background
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippeWeidmann committed Mar 30, 2023
1 parent 6f2c9b2 commit 7561c74
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions Mail/Views/Thread List/ThreadListViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class DateSection: Identifiable {

private var observationThreadToken: NotificationToken?
private var observationLastUpdateToken: NotificationToken?
private let observeQueue = DispatchQueue(label: "com.infomaniak.thread-results", qos: .userInteractive)

@Published var filter = Filter.all {
didSet {
Expand Down Expand Up @@ -218,22 +219,30 @@ class DateSection: Identifiable {
threadResults = folder.threads.sorted(by: \.date, ascending: false)
}

observationThreadToken = threadResults.observe(on: .main) { [weak self] changes in
observationThreadToken = threadResults.observe(on: observeQueue) { [weak self] changes in
switch changes {
case .initial(let results):
let filteredThreads = Array(results.freezeIfNeeded())
self?.filteredThreads = filteredThreads
withAnimation(animateInitialThreadChanges ? .default : nil) {
self?.sortThreadsIntoSections(threads: filteredThreads)
guard let newSections = self?.sortThreadsIntoSections(threads: filteredThreads) else { return }

DispatchQueue.main.sync {
self?.filteredThreads = filteredThreads
withAnimation(animateInitialThreadChanges ? .default : nil) {
self?.sections = newSections
}
}
case .update(let results, _, _, _):
let filteredThreads = Array(results.freezeIfNeeded())
self?.filteredThreads = filteredThreads
if self?.filter != .all && results.count == 1 && self?.filter.accepts(thread: results[0]) != true {
self?.filter = .all
}
withAnimation {
self?.sortThreadsIntoSections(threads: filteredThreads)
guard let newSections = self?.sortThreadsIntoSections(threads: filteredThreads) else { return }

DispatchQueue.main.sync {
self?.filteredThreads = filteredThreads
if self?.filter != .all && results.count == 1 && self?.filter.accepts(thread: results[0]) != true {
self?.filter = .all
}
withAnimation {
self?.sections = newSections
}
}
case .error:
break
Expand All @@ -251,12 +260,15 @@ class DateSection: Identifiable {
}
}

func sortThreadsIntoSections(threads: [Thread]) {
private func sortThreadsIntoSections(threads: [Thread]) -> [DateSection]? {
var newSections = [DateSection]()

var currentSection: DateSection?
if threads.isEmpty && filterUnreadOn {
filterUnreadOn.toggle()
DispatchQueue.main.sync {
filterUnreadOn.toggle()
}
return nil
} else {
for thread in threads {
if currentSection?.threadBelongsToSection(thread: thread) != true {
Expand All @@ -266,7 +278,7 @@ class DateSection: Identifiable {
currentSection?.threads.append(thread)
}

sections = newSections
return newSections
}
}

Expand Down

0 comments on commit 7561c74

Please sign in to comment.