Skip to content

Instantly share code, notes, and snippets.

@fatbobman
Created March 16, 2024 13:40
Show Gist options
  • Save fatbobman/6dc873ae18bb28cd1ccc521b3f56cefb to your computer and use it in GitHub Desktop.
Save fatbobman/6dc873ae18bb28cd1ccc521b3f56cefb to your computer and use it in GitHub Desktop.
ModelExtensionDemo
import Foundation
import SwiftData
extension Note {
/// 根据给定的筛选条件生成相应的谓词,用于筛选笔记。
public static func predicateFor(_ filter: NotePredicate) -> Predicate<Note>? {
var result: Predicate<Note>?
switch filter {
case let .filterRootNoteByName(name):
result = #Predicate<Note> { $0.name == name && $0.parent == nil }
case let .filterGroupByName(groupName, rootNoteID):
result = #Predicate<Note> {
$0.parent?.persistentModelID == rootNoteID && $0.name == groupName
}
case .filterByParentIsNil:
result = #Predicate<Note> { $0.parent == nil }
case let .filterByParentByID(parentID):
result = #Predicate<Note> { $0.parent?.persistentModelID == parentID }
}
return result
}
}
/// 定义笔记筛选条件的枚举,用于构造筛选笔记的谓词。
public enum NotePredicate: Sendable {
/// 根据笔记名称筛选根笔记。
case filterRootNoteByName(noteName: String)
/// 根据子笔记名称和根笔记的ID筛选子笔记。
case filterGroupByName(groupName: String, rootNoteID: PersistentIdentifier)
/// 筛选全部的根笔记。
case filterByParentIsNil
/// 根据父笔记的ID筛选其包含的全部子笔记。
case filterByParentByID(PersistentIdentifier)
}
extension Note {
/// 根据给定的查询和排序条件构造抓取描述符,用于从数据存储中抓取符合条件的笔记。
public static func fetchDescriptorForQuery(_ query: NoteQuery) -> FetchDescriptor<Note> {
var fetchDescriptor = FetchDescriptor<Note>()
switch query {
case let .getRootNoteByName(name, _):
fetchDescriptor = FetchDescriptor<Note>(predicate: Self.predicateFor(.filterRootNoteByName(noteName: name)))
case let .getGroupByName(groupName, rootNoteID, _):
fetchDescriptor = FetchDescriptor<Note>(predicate: Self.predicateFor(.filterGroupByName(groupName: groupName, rootNoteID: rootNoteID)))
case let .allRootNotes(order):
fetchDescriptor = FetchDescriptor<Note>(
predicate: Self.predicateFor(.filterByParentIsNil),
sortBy: Self.sortDescriptorsFor(order)
)
case let .allGroupOfNoteID(parentID, order):
fetchDescriptor = FetchDescriptor<Note>(
predicate: Note.predicateFor(.filterByParentByID(parentID)),
sortBy: Self.sortDescriptorsFor(order)
)
}
return fetchDescriptor
}
}
/// 定义笔记查询条件的枚举,用于构造数据抓取请求。
public enum NoteQuery {
/// 根据给定的笔记名称和排序条件,查询根笔记。
case getRootNoteByName(noteName: String, order: NoteSortingOption)
/// 根据给定的子笔记名称、根笔记ID和排序条件,查询子笔记。
case getGroupByName(groupName: String, rootNoteID: PersistentIdentifier, order: NoteSortingOption)
/// 依照排序条件,查询全部的根笔记。
case allRootNotes(order: NoteSortingOption)
/// 根据给定的父笔记ID和排序条件,查询其包含的全部子笔记。
case allGroupOfNoteID(modelID: PersistentIdentifier, order: NoteSortingOption)
}
/// 定义笔记排序选项的枚举,用于指定笔记列表的排序方式。
public enum NoteSortingOption {
/// 根据索引值排序
case orderByIndex
/// 根据名称排序
case orderByName
/// 根据创建时间排序
case orderByCreateTimestamp
/// 不用排序
case empty
}
extension Note {
/// 根据排序选项生成排序描述符数组,用于对笔记进行排序。锁定了调整排序方向
public static func sortDescriptorsFor(_ option: NoteSortingOption) -> [SortDescriptor<Note>] {
switch option {
/// 根据笔记的排序编号降序,序号越大,位置越靠前。
case .orderByIndex:
[
SortDescriptor(\Note.index, order: .reverse),
SortDescriptor(\Note.createTimestamp, order: .forward),
SortDescriptor(\Note.name, order: .forward),
]
/// 根据笔记的创建时间升序。
case .orderByCreateTimestamp:
[
SortDescriptor(\Note.createTimestamp, order: .forward),
SortDescriptor(\Note.name, order: .forward),
SortDescriptor(\Note.index, order: .reverse),
]
/// 根据笔记的名称升序。
case .orderByName:
[
SortDescriptor(\Note.name, order: .forward),
SortDescriptor(\Note.createTimestamp, order: .forward),
SortDescriptor(\Note.index, order: .reverse),
]
case .empty:
[]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment