IceCubesApp/IceCubesAppWidgetsExtension/Shared/SharedUtils.swift

58 lines
1.7 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
func loadStatuses(for timeline: TimelineFilter,
account: AppAccountEntity,
2024-05-06 06:38:37 +00:00
widgetFamily: WidgetFamily) async -> [Status]
{
2024-05-05 17:31:28 +00:00
let client = Client(server: account.account.server, oauthToken: account.account.oauthToken)
do {
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
}
}