IceCubesApp/IceCubesAppWidgetsExtension/MentionWidget/MentionWidget.swift

114 lines
3.1 KiB
Swift
Raw Normal View History

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-10-28 09:57:48 +00:00
.init(
date: Date(),
title: "Mentions",
statuses: [.placeholder()],
images: [:])
2024-05-06 06:37:58 +00:00
}
2024-05-06 06:38:37 +00:00
2024-10-28 09:57:48 +00:00
func snapshot(for configuration: MentionsWidgetConfiguration, in context: Context) async
-> PostsWidgetEntry
{
2024-05-06 06:37:58 +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: "Mentions",
statuses: [],
images: [:])
2024-05-06 06:37:58 +00:00
}
2024-05-06 06:38:37 +00:00
2024-10-28 09:57:48 +00:00
func timeline(for configuration: MentionsWidgetConfiguration, in context: Context) async
-> Timeline<PostsWidgetEntry>
{
2024-05-06 06:37:58 +00:00
await timeline(for: configuration, context: context)
}
2024-05-06 06:38:37 +00:00
2024-10-28 09:57:48 +00:00
private func timeline(for configuration: MentionsWidgetConfiguration, context _: Context) async
-> Timeline<PostsWidgetEntry>
{
2024-05-06 06:37:58 +00:00
do {
guard let account = configuration.account else {
2024-10-28 09:57:48 +00:00
return Timeline(
entries: [
.init(
date: Date(),
title: "Mentions",
statuses: [],
images: [:])
],
policy: .atEnd)
}
2024-10-28 09:57:48 +00:00
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 })
let notifications: [Models.Notification] =
2024-10-28 09:57:48 +00:00
try await client.get(
endpoint: Notifications.notifications(
minId: nil,
maxId: nil,
types: excludedTypes.map(\.rawValue),
limit: 5))
2024-05-06 06:38:37 +00:00
let statuses = notifications.compactMap { $0.status }
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: "Mentions",
statuses: statuses,
images: images)
], policy: .atEnd)
2024-05-06 06:37:58 +00:00
} catch {
2024-10-28 09:57:48 +00:00
return Timeline(
entries: [
.init(
date: Date(),
title: "Mentions",
statuses: [],
images: [:])
],
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 {
2024-10-28 09:57:48 +00:00
AppIntentConfiguration(
kind: kind,
intent: MentionsWidgetConfiguration.self,
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: {
2024-10-28 09:57:48 +00:00
PostsWidgetEntry(
date: .now,
title: "Mentions",
statuses: [.placeholder(), .placeholder(), .placeholder(), .placeholder()],
images: [:])
2024-05-06 06:37:58 +00:00
}