IceCubes/IceCubesAppWidgetsExtension/MentionWidget/MentionWidget.swift

82 lines
3.1 KiB
Swift
Raw Normal View History

2024-05-06 08:37:58 +02:00
import DesignSystem
import Models
2024-05-06 08:38:37 +02:00
import Network
import SwiftUI
2024-05-06 08:37:58 +02:00
import Timeline
2024-05-06 08:38:37 +02:00
import WidgetKit
2024-05-06 08:37:58 +02:00
struct MentionsWidgetProvider: AppIntentTimelineProvider {
2024-05-06 08:38:37 +02:00
func placeholder(in _: Context) -> PostsWidgetEntry {
2024-05-06 08:37:58 +02:00
.init(date: Date(),
title: "Mentions",
statuses: [.placeholder()],
images: [:])
}
2024-05-06 08:38:37 +02:00
2024-05-06 08:37:58 +02:00
func snapshot(for configuration: MentionsWidgetConfiguration, in context: Context) async -> PostsWidgetEntry {
if let entry = await timeline(for: configuration, context: context).entries.first {
return entry
}
return .init(date: Date(),
title: "Mentions",
statuses: [],
images: [:])
}
2024-05-06 08:38:37 +02:00
2024-05-06 08:37:58 +02:00
func timeline(for configuration: MentionsWidgetConfiguration, in context: Context) async -> Timeline<PostsWidgetEntry> {
await timeline(for: configuration, context: context)
}
2024-05-06 08:38:37 +02:00
private func timeline(for configuration: MentionsWidgetConfiguration, context _: Context) async -> Timeline<PostsWidgetEntry> {
2024-05-06 08:37:58 +02:00
do {
let client = Client(server: configuration.account.account.server,
oauthToken: configuration.account.account.oauthToken)
var excludedTypes = Models.Notification.NotificationType.allCases
excludedTypes.removeAll(where: { $0 == .mention })
let notifications: [Models.Notification] =
2024-05-06 08:38:37 +02:00
try await client.get(endpoint: Notifications.notifications(minId: nil,
maxId: nil,
types: excludedTypes.map(\.rawValue),
limit: 5))
let statuses = notifications.compactMap { $0.status }
let images = try await loadImages(urls: statuses.map { $0.account.avatar })
2024-05-06 08:37:58 +02:00
return Timeline(entries: [.init(date: Date(),
2024-05-06 08:38:37 +02:00
title: "Mentions",
statuses: statuses,
images: images)], policy: .atEnd)
2024-05-06 08:37:58 +02:00
} catch {
return Timeline(entries: [.init(date: Date(),
title: "Mentions",
statuses: [],
images: [:])],
2024-05-06 08:38:37 +02:00
policy: .atEnd)
2024-05-06 08:37:58 +02:00
}
}
}
struct MentionsWidget: Widget {
let kind: String = "MentionsWidget"
2024-05-06 08:38:37 +02:00
2024-05-06 08:37:58 +02:00
var body: some WidgetConfiguration {
AppIntentConfiguration(kind: kind,
intent: MentionsWidgetConfiguration.self,
2024-05-06 08:38:37 +02:00
provider: MentionsWidgetProvider())
{ entry in
2024-05-06 08:37:58 +02:00
PostsWidgetView(entry: entry)
.containerBackground(Color("WidgetBackground").gradient, for: .widget)
}
.configurationDisplayName("Mentions")
.description("Show the latest mentions for the selected account.")
2024-05-06 09:20:01 +02:00
.supportedFamilies([.systemLarge, .systemExtraLarge])
2024-05-06 08:37:58 +02:00
}
}
#Preview(as: .systemMedium) {
MentionsWidget()
} timeline: {
PostsWidgetEntry(date: .now,
title: "Mentions",
statuses: [.placeholder(), .placeholder(), .placeholder(), .placeholder()],
images: [:])
}