IceCubesApp/IceCubesAppWidgetsExtension/Shared/SharedUtils.swift

61 lines
1.4 KiB
Swift
Raw Normal View History

2024-05-05 17:31:28 +00:00
import AppAccount
2024-05-06 06:38:37 +00:00
import Foundation
2024-05-05 17:31:28 +00:00
import Models
import Network
2024-05-06 06:38:37 +00:00
import StatusKit
import Timeline
import UIKit
import WidgetKit
2024-05-05 17:31:28 +00:00
2024-10-28 09:57:48 +00:00
func loadStatuses(
for timeline: TimelineFilter,
account: AppAccountEntity,
widgetFamily: WidgetFamily
) async -> [Status] {
2024-05-05 17:31:28 +00:00
let client = Client(server: account.account.server, oauthToken: account.account.oauthToken)
do {
2024-10-28 09:57:48 +00:00
var statuses: [Status] = try await client.get(
endpoint: timeline.endpoint(
sinceId: nil,
maxId: nil,
minId: nil,
offset: nil,
limit: 6))
2024-05-06 06:38:37 +00:00
statuses = statuses.filter { $0.reblog == nil && !$0.content.asRawText.isEmpty }
2024-05-05 17:31:28 +00:00
switch widgetFamily {
case .systemSmall, .systemMedium:
if statuses.count >= 1 {
2024-05-06 06:38:37 +00:00
statuses = statuses.prefix(upTo: 1).map { $0 }
2024-05-05 17:31:28 +00:00
}
case .systemLarge, .systemExtraLarge:
2024-05-05 18:03:12 +00:00
if statuses.count >= 5 {
2024-05-06 06:38:37 +00:00
statuses = statuses.prefix(upTo: 5).map { $0 }
2024-05-05 17:31:28 +00:00
}
default:
break
}
return statuses
} catch {
return []
}
}
2024-05-06 06:38:37 +00:00
2024-05-05 17:31:28 +00:00
func loadImages(urls: [URL]) async throws -> [URL: UIImage] {
try await withThrowingTaskGroup(of: (URL, UIImage?).self) { group in
for url in urls {
group.addTask {
let response = try await URLSession.shared.data(from: url)
return (url, UIImage(data: response.0))
}
}
2024-05-06 06:38:37 +00:00
2024-05-05 17:31:28 +00:00
var images: [URL: UIImage] = [:]
2024-05-06 06:38:37 +00:00
2024-05-05 17:31:28 +00:00
for try await (url, image) in group {
images[url] = image
}
2024-05-06 06:38:37 +00:00
2024-05-05 17:31:28 +00:00
return images
}
}