2023-01-31 14:37:49 +01:00
|
|
|
// Copyright © 2023 Mastodon gGmbH. All rights reserved.
|
|
|
|
|
|
|
|
import WidgetKit
|
|
|
|
import SwiftUI
|
|
|
|
import Intents
|
|
|
|
import MastodonSDK
|
|
|
|
|
|
|
|
struct MultiFollowersCountWidgetProvider: IntentTimelineProvider {
|
|
|
|
func placeholder(in context: Context) -> MultiFollowersCountEntry {
|
|
|
|
.placeholder
|
|
|
|
}
|
|
|
|
|
2023-01-31 14:55:59 +01:00
|
|
|
func getSnapshot(for configuration: MultiFollowersCountIntent, in context: Context, completion: @escaping (MultiFollowersCountEntry) -> ()) {
|
2023-01-31 14:37:49 +01:00
|
|
|
loadCurrentEntry(for: configuration, in: context, completion: completion)
|
|
|
|
}
|
|
|
|
|
2023-01-31 14:55:59 +01:00
|
|
|
func getTimeline(for configuration: MultiFollowersCountIntent, in context: Context, completion: @escaping (Timeline<MultiFollowersCountEntry>) -> ()) {
|
2023-01-31 14:37:49 +01:00
|
|
|
loadCurrentEntry(for: configuration, in: context) { entry in
|
|
|
|
completion(Timeline(entries: [entry], policy: .after(.now)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MultiFollowersCountEntry: TimelineEntry {
|
|
|
|
let date: Date
|
2023-02-06 13:51:58 +01:00
|
|
|
let accounts: [MultiFollowersEntryAccountable]?
|
2023-01-31 14:55:59 +01:00
|
|
|
let configuration: MultiFollowersCountIntent
|
2023-01-31 14:37:49 +01:00
|
|
|
|
|
|
|
static var placeholder: Self {
|
|
|
|
MultiFollowersCountEntry(
|
|
|
|
date: .now,
|
|
|
|
accounts: [
|
2023-02-06 13:51:58 +01:00
|
|
|
MultiFollowersEntryAccount(
|
2023-01-31 14:37:49 +01:00
|
|
|
followersCount: 99_900,
|
|
|
|
displayNameWithFallback: "Mastodon",
|
|
|
|
acct: "mastodon",
|
|
|
|
avatarImage: UIImage(named: "missingAvatar")!,
|
|
|
|
domain: "mastodon"
|
|
|
|
)
|
|
|
|
],
|
2023-01-31 14:55:59 +01:00
|
|
|
configuration: MultiFollowersCountIntent()
|
2023-01-31 14:37:49 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
static var unconfigured: Self {
|
|
|
|
MultiFollowersCountEntry(
|
|
|
|
date: .now,
|
2023-02-06 13:51:58 +01:00
|
|
|
accounts: nil,
|
2023-01-31 14:55:59 +01:00
|
|
|
configuration: MultiFollowersCountIntent()
|
2023-01-31 14:37:49 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MultiFollowersCountWidget: Widget {
|
|
|
|
private var availableFamilies: [WidgetFamily] {
|
2023-01-31 14:54:05 +01:00
|
|
|
return [.systemSmall, .systemMedium]
|
2023-01-31 14:37:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var body: some WidgetConfiguration {
|
2023-01-31 14:55:59 +01:00
|
|
|
IntentConfiguration(kind: "Multiple followers", intent: MultiFollowersCountIntent.self, provider: MultiFollowersCountWidgetProvider()) { entry in
|
2023-01-31 14:37:49 +01:00
|
|
|
MultiFollowersCountWidgetView(entry: entry)
|
|
|
|
}
|
|
|
|
.configurationDisplayName("Multiple followers")
|
|
|
|
.description("Show number of followers for multiple accounts.")
|
|
|
|
.supportedFamilies(availableFamilies)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension MultiFollowersCountWidgetProvider {
|
2023-01-31 14:55:59 +01:00
|
|
|
func loadCurrentEntry(for configuration: MultiFollowersCountIntent, in context: Context, completion: @escaping (MultiFollowersCountEntry) -> Void) {
|
2023-01-31 14:37:49 +01:00
|
|
|
Task {
|
|
|
|
guard
|
|
|
|
let authBox = WidgetExtension.appContext
|
|
|
|
.authenticationService
|
|
|
|
.mastodonAuthenticationBoxes
|
|
|
|
.first
|
|
|
|
else {
|
2023-02-06 11:46:46 +01:00
|
|
|
guard !context.isPreview else {
|
|
|
|
return completion(.placeholder)
|
|
|
|
}
|
2023-01-31 14:37:49 +01:00
|
|
|
return completion(.unconfigured)
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let desiredAccounts: [String] = {
|
|
|
|
guard let account = configuration.accounts?.compactMap({ $0 }) else {
|
|
|
|
if let acct = authBox.authenticationRecord.object(in: WidgetExtension.appContext.managedObjectContext)?.user.acct {
|
|
|
|
return [acct]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return account
|
|
|
|
}() else {
|
|
|
|
return completion(.unconfigured)
|
|
|
|
}
|
|
|
|
|
2023-02-06 13:51:58 +01:00
|
|
|
var accounts = [MultiFollowersEntryAccountable]()
|
2023-01-31 14:37:49 +01:00
|
|
|
|
|
|
|
for desiredAccount in desiredAccounts {
|
|
|
|
let resultingAccount = try await WidgetExtension.appContext
|
|
|
|
.apiService
|
|
|
|
.search(query: .init(q: desiredAccount, type: .accounts), authenticationBox: authBox)
|
|
|
|
.value
|
|
|
|
.accounts
|
|
|
|
.first!
|
|
|
|
|
|
|
|
let imageData = try await URLSession.shared.data(from: resultingAccount.avatarImageURLWithFallback(domain: authBox.domain)).0
|
|
|
|
|
2023-02-06 13:51:58 +01:00
|
|
|
accounts.append(MultiFollowersEntryAccount.from(
|
2023-01-31 14:37:49 +01:00
|
|
|
mastodonAccount: resultingAccount,
|
|
|
|
domain: authBox.domain,
|
|
|
|
avatarImage: UIImage(data: imageData) ?? UIImage(named: "missingAvatar")!
|
|
|
|
))
|
|
|
|
}
|
2023-02-06 13:51:58 +01:00
|
|
|
|
|
|
|
if context.isPreview {
|
|
|
|
accounts.append(
|
|
|
|
MultiFollowersEntryAccount(
|
|
|
|
followersCount: 1_200,
|
|
|
|
displayNameWithFallback: "Another follower",
|
|
|
|
acct: "@another@follower.social",
|
|
|
|
avatarImage: UIImage(named: "missingAvatar")!,
|
|
|
|
domain: authBox.domain
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
2023-01-31 14:37:49 +01:00
|
|
|
|
|
|
|
let entry = MultiFollowersCountEntry(
|
|
|
|
date: Date(),
|
|
|
|
accounts: accounts,
|
|
|
|
configuration: configuration
|
|
|
|
)
|
|
|
|
|
|
|
|
completion(entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protocol MultiFollowersEntryAccountable {
|
|
|
|
var followersCount: Int { get }
|
|
|
|
var displayNameWithFallback: String { get }
|
|
|
|
var acct: String { get }
|
|
|
|
var avatarImage: UIImage { get }
|
|
|
|
var domain: String { get }
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MultiFollowersEntryAccount: MultiFollowersEntryAccountable {
|
|
|
|
let followersCount: Int
|
|
|
|
let displayNameWithFallback: String
|
|
|
|
let acct: String
|
|
|
|
let avatarImage: UIImage
|
|
|
|
let domain: String
|
|
|
|
|
|
|
|
static func from(mastodonAccount: Mastodon.Entity.Account, domain: String, avatarImage: UIImage) -> Self {
|
|
|
|
MultiFollowersEntryAccount(
|
|
|
|
followersCount: mastodonAccount.followersCount,
|
|
|
|
displayNameWithFallback: mastodonAccount.displayNameWithFallback,
|
|
|
|
acct: mastodonAccount.acct,
|
|
|
|
avatarImage: avatarImage,
|
|
|
|
domain: domain
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|