Skip to content

Commit

Permalink
feat(ThreadListViewModel): Filter using realm
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippeWeidmann committed Mar 30, 2023
1 parent a301935 commit 6f2c9b2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 28 deletions.
64 changes: 37 additions & 27 deletions Mail/Views/Thread List/ThreadListViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,45 +210,55 @@ class DateSection: Identifiable {
return
}

let threadResults = folder.threads.sorted(by: \.date, ascending: false)
observationThreadToken = threadResults.observe(on: .main) { [weak self] changes in
switch changes {
case .initial(let results):
withAnimation(animateInitialThreadChanges ? .default : nil) {
self?.sortThreadsIntoSections(threads: Array(results.freezeIfNeeded()))
}
case .update(let results, _, _, _):
if self?.filter != .all && results.count == 1 && self?.filter.accepts(thread: results[0]) != true {
self?.filter = .all
}
withAnimation {
self?.sortThreadsIntoSections(threads: Array(results.freezeIfNeeded()))
}
case .error:
break
let threadResults: Results<Thread>
if let predicate = filter.predicate {
threadResults = folder.threads.filter(predicate + " OR uid == %@", selectedThread?.uid ?? "")
.sorted(by: \.date, ascending: false)
} else {
threadResults = folder.threads.sorted(by: \.date, ascending: false)
}

observationThreadToken = threadResults.observe(on: .main) { [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)
}
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)
}
case .error:
break
}
observationLastUpdateToken = folder.observe(keyPaths: [\Folder.lastUpdate], on: .main) { [weak self] changes in
switch changes {
case .change(let folder, _):
withAnimation {
self?.lastUpdate = folder.lastUpdate
}
default:
break
}
observationLastUpdateToken = folder.observe(keyPaths: [\Folder.lastUpdate], on: .main) { [weak self] changes in
switch changes {
case .change(let folder, _):
withAnimation {
self?.lastUpdate = folder.lastUpdate
}
default:
break
}
}
}

func sortThreadsIntoSections(threads: [Thread]) {
var newSections = [DateSection]()

var currentSection: DateSection?
filteredThreads = threads.filter { $0.id == selectedThread?.id || filter.accepts(thread: $0) }
if filteredThreads.isEmpty && filterUnreadOn {
if threads.isEmpty && filterUnreadOn {
filterUnreadOn.toggle()
} else {
for thread in filteredThreads {
for thread in threads {
if currentSection?.threadBelongsToSection(thread: thread) != true {
currentSection = DateSection(thread: thread)
newSections.append(currentSection!)
Expand Down
17 changes: 16 additions & 1 deletion MailCore/Models/Thread.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public class Thread: Object, Decodable, Identifiable {
return fromArray[0].formattedName
default:
let fromCount = min(fromArray.count, Constants.threadCellMaxRecipients)
return fromArray[0..<fromCount].map(\.formattedShortName).joined(separator: ", ")
return fromArray[0 ..< fromCount].map(\.formattedShortName).joined(separator: ", ")
}
}

Expand Down Expand Up @@ -252,6 +252,21 @@ public class Thread: Object, Decodable, Identifiable {
public enum Filter: String {
case all, seen, unseen, starred, unstarred

public var predicate: String? {
switch self {
case .all:
return nil
case .seen:
return "unseenMessages == 0"
case .unseen:
return "unseenMessages > 0"
case .starred:
return "flagged == TRUE"
case .unstarred:
return "flagged == FALSE"
}
}

public func accepts(thread: Thread) -> Bool {
switch self {
case .all:
Expand Down

0 comments on commit 6f2c9b2

Please sign in to comment.