7da7995538
Add = false/true defaults to all non-optional Bool properties so SwiftData's lightweight migration knows what value to assign existing rows. Replace try! ModelContainer with crash-safe do-catch that resets the store on migration failure instead of crashing. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
40 lines
952 B
Swift
40 lines
952 B
Swift
import Foundation
|
|
import SwiftData
|
|
|
|
@Model
|
|
final class Dish {
|
|
@Attribute(.unique) var id: UUID
|
|
var name: String
|
|
var descriptionText: String?
|
|
var tagIds: [UUID]
|
|
var createdAt: Date
|
|
var isPriority: Bool = false
|
|
|
|
init(
|
|
id: UUID = UUID(),
|
|
name: String,
|
|
descriptionText: String? = nil,
|
|
tagIds: [UUID] = [],
|
|
createdAt: Date = Date(),
|
|
isPriority: Bool = false
|
|
) {
|
|
self.id = id
|
|
self.name = name
|
|
self.descriptionText = descriptionText
|
|
self.tagIds = tagIds
|
|
self.createdAt = createdAt
|
|
self.isPriority = isPriority
|
|
}
|
|
}
|
|
|
|
extension Dish {
|
|
static func stableSortedForDisplay(_ dishes: [Dish]) -> [Dish] {
|
|
dishes.sorted { lhs, rhs in
|
|
if lhs.createdAt != rhs.createdAt {
|
|
return lhs.createdAt > rhs.createdAt
|
|
}
|
|
return lhs.id.uuidString < rhs.id.uuidString
|
|
}
|
|
}
|
|
}
|