2024-05-06 06:37:58 +00:00
|
|
|
import DesignSystem
|
|
|
|
import Models
|
2024-05-06 06:38:37 +00:00
|
|
|
import Network
|
|
|
|
import SwiftUI
|
2024-05-06 06:37:58 +00:00
|
|
|
import Timeline
|
2024-05-06 06:38:37 +00:00
|
|
|
import WidgetKit
|
2024-05-06 06:37:58 +00:00
|
|
|
|
|
|
|
struct MentionsWidgetProvider: AppIntentTimelineProvider {
|
2024-05-06 06:38:37 +00:00
|
|
|
func placeholder(in _: Context) -> PostsWidgetEntry {
|
2024-05-06 06:37:58 +00:00
|
|
|
.init(date: Date(),
|
|
|
|
title: "Mentions",
|
|
|
|
statuses: [.placeholder()],
|
|
|
|
images: [:])
|
|
|
|
}
|
2024-05-06 06:38:37 +00:00
|
|
|
|
2024-05-06 06:37:58 +00: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 06:38:37 +00:00
|
|
|
|
2024-05-06 06:37:58 +00:00
|
|
|
func timeline(for configuration: MentionsWidgetConfiguration, in context: Context) async -> Timeline<PostsWidgetEntry> {
|
|
|
|
await timeline(for: configuration, context: context)
|
|
|
|
}
|
2024-05-06 06:38:37 +00:00
|
|
|
|
|
|
|
private func timeline(for configuration: MentionsWidgetConfiguration, context _: Context) async -> Timeline<PostsWidgetEntry> {
|
2024-05-06 06:37:58 +00:00
|
|
|
do {
|
2024-09-10 04:53:19 +00:00
|
|
|
guard let account = configuration.account else {
|
|
|
|
return Timeline(entries: [.init(date: Date(),
|
|
|
|
title: "Mentions",
|
|
|
|
statuses: [],
|
|
|
|
images: [:])],
|
|
|
|
policy: .atEnd)
|
|
|
|
}
|
|
|
|
let client = Client(server: account.account.server,
|
|
|
|
oauthToken: account.account.oauthToken)
|
2024-05-06 06:37:58 +00:00
|
|
|
var excludedTypes = Models.Notification.NotificationType.allCases
|
|
|
|
excludedTypes.removeAll(where: { $0 == .mention })
|
2024-05-14 17:36:25 +00:00
|
|
|
let notifications: [Models.Notification] =
|
2024-05-06 06:38:37 +00: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 06:37:58 +00:00
|
|
|
return Timeline(entries: [.init(date: Date(),
|
2024-05-06 06:38:37 +00:00
|
|
|
title: "Mentions",
|
|
|
|
statuses: statuses,
|
|
|
|
images: images)], policy: .atEnd)
|
2024-05-06 06:37:58 +00:00
|
|
|
} catch {
|
|
|
|
return Timeline(entries: [.init(date: Date(),
|
|
|
|
title: "Mentions",
|
|
|
|
statuses: [],
|
|
|
|
images: [:])],
|
2024-05-06 06:38:37 +00:00
|
|
|
policy: .atEnd)
|
2024-05-06 06:37:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MentionsWidget: Widget {
|
|
|
|
let kind: String = "MentionsWidget"
|
2024-05-06 06:38:37 +00:00
|
|
|
|
2024-05-06 06:37:58 +00:00
|
|
|
var body: some WidgetConfiguration {
|
|
|
|
AppIntentConfiguration(kind: kind,
|
|
|
|
intent: MentionsWidgetConfiguration.self,
|
2024-05-06 06:38:37 +00:00
|
|
|
provider: MentionsWidgetProvider())
|
|
|
|
{ entry in
|
2024-05-06 06:37:58 +00:00
|
|
|
PostsWidgetView(entry: entry)
|
|
|
|
.containerBackground(Color("WidgetBackground").gradient, for: .widget)
|
|
|
|
}
|
|
|
|
.configurationDisplayName("Mentions")
|
|
|
|
.description("Show the latest mentions for the selected account.")
|
2024-05-06 07:20:01 +00:00
|
|
|
.supportedFamilies([.systemLarge, .systemExtraLarge])
|
2024-05-06 06:37:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#Preview(as: .systemMedium) {
|
|
|
|
MentionsWidget()
|
|
|
|
} timeline: {
|
|
|
|
PostsWidgetEntry(date: .now,
|
|
|
|
title: "Mentions",
|
|
|
|
statuses: [.placeholder(), .placeholder(), .placeholder(), .placeholder()],
|
|
|
|
images: [:])
|
|
|
|
}
|