IceCubesApp/IceCubesAppWidgetsExtension/ListsWidget/ListsWidget.swift

106 lines
2.8 KiB
Swift
Raw Permalink Normal View History

2024-05-15 07:27:35 +00:00
import DesignSystem
import Models
import Network
import SwiftUI
import Timeline
import WidgetKit
struct ListsWidgetProvider: AppIntentTimelineProvider {
func placeholder(in _: Context) -> PostsWidgetEntry {
2024-10-28 09:57:48 +00:00
.init(
date: Date(),
title: "List name",
statuses: [.placeholder()],
images: [:])
2024-05-15 07:27:35 +00:00
}
2024-10-28 09:57:48 +00:00
func snapshot(for configuration: ListsWidgetConfiguration, in context: Context) async
-> PostsWidgetEntry
{
2024-05-15 07:27:35 +00:00
if let entry = await timeline(for: configuration, context: context).entries.first {
return entry
}
2024-10-28 09:57:48 +00:00
return .init(
date: Date(),
title: "List name",
statuses: [],
images: [:])
2024-05-15 07:27:35 +00:00
}
2024-10-28 09:57:48 +00:00
func timeline(for configuration: ListsWidgetConfiguration, in context: Context) async -> Timeline<
PostsWidgetEntry
> {
2024-05-15 07:27:35 +00:00
await timeline(for: configuration, context: context)
}
2024-10-28 09:57:48 +00:00
private func timeline(for configuration: ListsWidgetConfiguration, context: Context) async
-> Timeline<PostsWidgetEntry>
{
2024-05-15 07:27:35 +00:00
do {
guard let account = configuration.account, let timeline = configuration.timeline else {
2024-10-28 09:57:48 +00:00
return Timeline(
entries: [
.init(
date: Date(),
title: "List name",
statuses: [],
images: [:])
],
policy: .atEnd)
}
let filter: TimelineFilter = .list(list: timeline.list)
2024-10-28 09:57:48 +00:00
let statuses = await loadStatuses(
for: filter,
account: account,
widgetFamily: context.family)
2024-05-15 07:27:35 +00:00
let images = try await loadImages(urls: statuses.map { $0.account.avatar })
2024-10-28 09:57:48 +00:00
return Timeline(
entries: [
.init(
date: Date(),
title: filter.title,
statuses: statuses,
images: images)
], policy: .atEnd)
2024-05-15 07:27:35 +00:00
} catch {
2024-10-28 09:57:48 +00:00
return Timeline(
entries: [
.init(
date: Date(),
title: "List name",
statuses: [],
images: [:])
],
policy: .atEnd)
2024-05-15 07:27:35 +00:00
}
}
}
struct ListsPostWidget: Widget {
let kind: String = "ListsPostWidget"
var body: some WidgetConfiguration {
2024-10-28 09:57:48 +00:00
AppIntentConfiguration(
kind: kind,
intent: ListsWidgetConfiguration.self,
provider: ListsWidgetProvider()
) { entry in
2024-05-15 07:27:35 +00:00
PostsWidgetView(entry: entry)
.containerBackground(Color("WidgetBackground").gradient, for: .widget)
}
.configurationDisplayName("List timeline")
.description("Show the latest post for the selected list")
.supportedFamilies([.systemSmall, .systemMedium, .systemLarge, .systemExtraLarge])
}
}
#Preview(as: .systemMedium) {
ListsPostWidget()
} timeline: {
2024-10-28 09:57:48 +00:00
PostsWidgetEntry(
date: .now,
title: "List name",
statuses: [.placeholder(), .placeholder(), .placeholder(), .placeholder()],
images: [:])
2024-05-15 07:27:35 +00:00
}