76 lines
2.8 KiB
Swift
Raw Normal View History

2024-05-05 19:31:28 +02:00
import DesignSystem
import Models
2024-05-06 08:38:37 +02:00
import Network
import SwiftUI
2024-05-05 19:31:28 +02:00
import Timeline
2024-05-06 08:38:37 +02:00
import WidgetKit
2024-05-05 19:31:28 +02:00
struct HashtagPostsWidgetProvider: AppIntentTimelineProvider {
2024-05-06 08:38:37 +02:00
func placeholder(in _: Context) -> PostsWidgetEntry {
2024-05-05 19:31:28 +02:00
.init(date: Date(),
2024-05-06 08:37:58 +02:00
title: "#Mastodon",
2024-05-05 19:31:28 +02:00
statuses: [.placeholder()],
images: [:])
}
2024-05-06 08:38:37 +02:00
2024-05-05 19:37:06 +02:00
func snapshot(for configuration: HashtagPostsWidgetConfiguration, in context: Context) async -> PostsWidgetEntry {
2024-05-05 19:31:28 +02:00
if let entry = await timeline(for: configuration, context: context).entries.first {
return entry
}
return .init(date: Date(),
2024-05-06 08:37:58 +02:00
title: "#Mastodon",
2024-05-05 19:31:28 +02:00
statuses: [],
images: [:])
}
2024-05-06 08:38:37 +02:00
2024-05-05 19:37:06 +02:00
func timeline(for configuration: HashtagPostsWidgetConfiguration, in context: Context) async -> Timeline<PostsWidgetEntry> {
2024-05-05 19:31:28 +02:00
await timeline(for: configuration, context: context)
}
2024-05-06 08:38:37 +02:00
2024-05-05 19:37:06 +02:00
private func timeline(for configuration: HashtagPostsWidgetConfiguration, context: Context) async -> Timeline<PostsWidgetEntry> {
2024-05-05 19:31:28 +02:00
do {
2024-05-06 08:37:58 +02:00
let timeline: TimelineFilter = .hashtag(tag: configuration.hashgtag, accountId: nil)
let statuses = await loadStatuses(for: timeline,
2024-05-05 19:41:04 +02:00
account: configuration.account,
2024-05-05 19:31:28 +02:00
widgetFamily: context.family)
2024-05-06 08:38:37 +02:00
let images = try await loadImages(urls: statuses.map { $0.account.avatar })
2024-05-05 19:31:28 +02:00
return Timeline(entries: [.init(date: Date(),
2024-05-06 08:38:37 +02:00
title: timeline.title,
statuses: statuses,
images: images)], policy: .atEnd)
2024-05-05 19:31:28 +02:00
} catch {
return Timeline(entries: [.init(date: Date(),
2024-05-06 08:37:58 +02:00
title: "#Mastodon",
2024-05-05 19:31:28 +02:00
statuses: [],
images: [:])],
2024-05-06 08:38:37 +02:00
policy: .atEnd)
2024-05-05 19:31:28 +02:00
}
}
}
struct HashtagPostsWidget: Widget {
let kind: String = "HashtagPostsWidget"
2024-05-06 08:38:37 +02:00
2024-05-05 19:31:28 +02:00
var body: some WidgetConfiguration {
AppIntentConfiguration(kind: kind,
intent: HashtagPostsWidgetConfiguration.self,
2024-05-06 08:38:37 +02:00
provider: HashtagPostsWidgetProvider())
{ entry in
2024-05-05 19:37:06 +02:00
PostsWidgetView(entry: entry)
2024-05-05 19:31:28 +02:00
.containerBackground(Color("WidgetBackground").gradient, for: .widget)
}
.configurationDisplayName("Hashtag timeline")
.description("Show the latest post for the selected hashtag")
.supportedFamilies([.systemSmall, .systemMedium, .systemLarge, .systemExtraLarge])
}
}
#Preview(as: .systemMedium) {
HashtagPostsWidget()
} timeline: {
2024-05-05 19:37:06 +02:00
PostsWidgetEntry(date: .now,
2024-05-06 08:38:37 +02:00
title: "#Mastodon",
statuses: [.placeholder(), .placeholder(), .placeholder(), .placeholder()],
images: [:])
2024-05-05 19:31:28 +02:00
}