Skip to content

Instantly share code, notes, and snippets.

@yccheok
Created March 27, 2025 12:46
Show Gist options
  • Save yccheok/5af29dcb6a23d2fdb96d695169e80fe9 to your computer and use it in GitHub Desktop.
Save yccheok/5af29dcb6a23d2fdb96d695169e80fe9 to your computer and use it in GitHub Desktop.
@globalActor actor MyGlobalActor : GlobalActor {
static let shared = MyGlobalActor()
}
extension MoveNoteViewController: UISearchBarDelegate {
@MyGlobalActor
private func filterNotes(_ text: String) async -> [Note] {
if text.isEmpty {
return await notes
} else {
let notes = await self.notes!
let idToFolderMap = await self.idToFolderMap!
return notes.filter {
let emoji = $0.emoji
let title = $0.title
var folderName: String? = nil
if let folderId = $0.folderId {
folderName = idToFolderMap[folderId]?.name ?? ""
}
return
emoji.localizedCaseInsensitiveContains(text) ||
title.localizedCaseInsensitiveContains(text) ||
(folderName?.localizedCaseInsensitiveContains(text) ?? false)
}
}
}
@MainActor
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
let text = searchText.trim()
if text.isEmpty {
applySnapshot(snapshot: getSnapshot(notes: notes))
} else {
Task {
let filteredNotes = await filterNotes(text)
if searchBar.text?.trim() == text {
applySnapshot(snapshot: getSnapshot(notes: filteredNotes))
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment