How to set default items in EntityQuery in Widget's settings selection?

201 views Asked by At

I'm trying to do it like an Apple widget with a preset clock. When we add them, by default there is some set of hours already set. And we go into the settings and there, too, some hours are selected by default

enter image description here

enter image description here

Please tell me how to do the same? I try using Entity Query, only nothing is displayed by default, but offers to choose some option

enter image description here

My code here:

@available(iOSApplicationExtension 17.0, *)
struct MyEntity: AppEntity {
    
    var id: String
    var title: String
    
    static var defaultQuery = MyQuery()
    
    static var typeDisplayRepresentation: TypeDisplayRepresentation = "Action"
    
    var displayRepresentation: DisplayRepresentation {
        DisplayRepresentation(title: "\(self.id)")
    }
    
    static var all: [MyEntity] = [
        MyEntity(id: "123", title: "123"),
        MyEntity(id: "111", title: "111"),
        MyEntity(id: "222", title: "222"),
        MyEntity(id: "333", title: "333")
    ]
}

@available(iOSApplicationExtension 17.0, *)
struct MyQuery: EntityQuery {
    
    func entities(for identifiers: [MyEntity.ID]) async throws -> [MyEntity] {
        MyEntity.all.filter {
            identifiers.contains($0.id)
        }
    }
    
    func suggestedEntities() async throws -> [MyEntity] {
        MyEntity.all
    }
    
    func defaultResult() async -> MyEntity? {
        do {
            return try await self.suggestedEntities().first
        } catch {
            return nil
        }
    }
    
}
0

There are 0 answers