Files
iOS/Sources/Extensions/Widgets/OpenPage/WidgetOpenPageProvider.swift
Bruno Pantaleão Gonçalves cc59047b46 Fix widget corner radius when size style compressed and preview them as tile card (#3211)
<!-- Thank you for submitting a Pull Request and helping to improve Home
Assistant. Please complete the following sections to help the processing
and review of your changes. Please do not delete anything from this
template. -->

## Summary
<!-- Provide a brief summary of the changes you have made and most
importantly what they aim to achieve -->

## Screenshots
<!-- If this is a user-facing change not in the frontend, please include
screenshots in light and dark mode. -->

## Link to pull request in Documentation repository
<!-- Pull requests that add, change or remove functionality must have a
corresponding pull request in the Companion App Documentation repository
(https://github.com/home-assistant/companion.home-assistant). Please add
the number of this pull request after the "#" -->
Documentation: home-assistant/companion.home-assistant#

## Any other notes
<!-- If there is any other information of note, like if this Pull
Request is part of a bigger change, please include it here. -->
2024-11-28 12:06:02 +01:00

69 lines
2.4 KiB
Swift

import PromiseKit
import Shared
import SwiftUI
import WidgetKit
struct WidgetOpenPageEntry: TimelineEntry {
var date = Date()
var pages: [IntentPanel] = []
}
struct WidgetOpenPageProvider: IntentTimelineProvider {
typealias Intent = WidgetOpenPageIntent
typealias Entry = WidgetOpenPageEntry
func placeholder(in context: Context) -> WidgetOpenPageEntry {
let count = WidgetFamilySizes.size(for: context.family)
let pages = stride(from: 0, to: count, by: 1).map { idx in
with(IntentPanel(identifier: "redacted\(idx)", display: "Redacted Text")) {
$0.icon = MaterialDesignIcons.bedEmptyIcon.name
}
}
return .init(pages: pages)
}
private func panels(
for context: Context,
updating existing: [IntentPanel],
timeout: Measurement<UnitDuration> = .init(value: 10.0, unit: .seconds),
completion: @escaping ([IntentPanel]) -> Void
) {
OpenPageIntentHandler.panels { panels in
var intentsToDisplay = panels
if !existing.isEmpty {
intentsToDisplay = existing.compactMap { existingValue in
intentsToDisplay.first {
$0.identifier == existingValue.identifier &&
$0.server == existingValue.server
}
}
}
completion(Array(intentsToDisplay.prefix(WidgetFamilySizes.size(for: context.family))))
}
}
func getSnapshot(for configuration: Intent, in context: Context, completion: @escaping (Entry) -> Void) {
panels(for: context, updating: configuration.pages ?? []) { panels in
completion(Entry(pages: Array(panels.prefix(WidgetFamilySizes.sizeForPreview(for: context.family)))))
}
}
private static var expiration: Measurement<UnitDuration> {
.init(value: 24, unit: .hours)
}
func getTimeline(for configuration: Intent, in context: Context, completion: @escaping (Timeline<Entry>) -> Void) {
func timeline(for pages: [IntentPanel]) -> Timeline<Entry> {
.init(
entries: [.init(pages: pages)],
policy: .after(Current.date().addingTimeInterval(Self.expiration.converted(to: .seconds).value))
)
}
panels(for: context, updating: configuration.pages ?? []) { panels in
completion(timeline(for: panels))
}
}
}