2023-01-24 11:14:50 +01:00
|
|
|
// Copyright © 2023 Mastodon gGmbH. All rights reserved.
|
|
|
|
|
|
|
|
import WidgetKit
|
|
|
|
import SwiftUI
|
|
|
|
import Intents
|
2023-01-24 12:47:48 +01:00
|
|
|
import MastodonSDK
|
2023-01-24 11:14:50 +01:00
|
|
|
|
|
|
|
struct FollowersProvider: IntentTimelineProvider {
|
2023-01-30 15:28:16 +01:00
|
|
|
private let followersHistory = FollowersCountHistory.shared
|
|
|
|
|
2023-01-24 11:14:50 +01:00
|
|
|
func placeholder(in context: Context) -> FollowersEntry {
|
2023-01-24 15:23:25 +01:00
|
|
|
.placeholder
|
2023-01-24 11:14:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func getSnapshot(for configuration: FollowersCountIntent, in context: Context, completion: @escaping (FollowersEntry) -> ()) {
|
2023-01-24 15:23:25 +01:00
|
|
|
guard !context.isPreview else {
|
|
|
|
return completion(.placeholder)
|
|
|
|
}
|
2023-01-24 12:47:48 +01:00
|
|
|
loadCurrentEntry(for: configuration, in: context, completion: completion)
|
2023-01-24 11:14:50 +01:00
|
|
|
}
|
|
|
|
|
2023-01-26 16:22:18 +01:00
|
|
|
func getTimeline(for configuration: FollowersCountIntent, in context: Context, completion: @escaping (Timeline<FollowersEntry>) -> ()) {
|
2023-01-24 12:47:48 +01:00
|
|
|
loadCurrentEntry(for: configuration, in: context) { entry in
|
|
|
|
completion(Timeline(entries: [entry], policy: .after(.now)))
|
2023-01-24 11:14:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct FollowersEntry: TimelineEntry {
|
|
|
|
let date: Date
|
2023-01-24 15:23:25 +01:00
|
|
|
let account: FollowersEntryAccountable?
|
2023-01-24 11:14:50 +01:00
|
|
|
let configuration: FollowersCountIntent
|
2023-01-24 12:47:48 +01:00
|
|
|
|
2023-01-24 15:23:25 +01:00
|
|
|
static var placeholder: Self {
|
|
|
|
FollowersEntry(
|
|
|
|
date: .now,
|
|
|
|
account: FollowersEntryAccount(
|
|
|
|
followersCount: 99_900,
|
|
|
|
displayNameWithFallback: "Mastodon",
|
|
|
|
acct: "mastodon",
|
2023-01-30 15:28:16 +01:00
|
|
|
avatarImage: UIImage(named: "missingAvatar")!,
|
|
|
|
domain: "mastodon"
|
2023-01-24 15:23:25 +01:00
|
|
|
),
|
|
|
|
configuration: FollowersCountIntent()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
static var unconfigured: Self {
|
|
|
|
FollowersEntry(
|
|
|
|
date: .now,
|
|
|
|
account: nil,
|
|
|
|
configuration: FollowersCountIntent()
|
|
|
|
)
|
2023-01-24 12:47:48 +01:00
|
|
|
}
|
2023-01-24 11:14:50 +01:00
|
|
|
}
|
|
|
|
|
2023-01-26 16:22:18 +01:00
|
|
|
struct FollowersWidgetExtension: Widget {
|
|
|
|
private var availableFamilies: [WidgetFamily] {
|
|
|
|
if #available(iOS 16, *) {
|
|
|
|
return [.systemSmall, .accessoryRectangular, .accessoryCircular]
|
2023-01-24 12:47:48 +01:00
|
|
|
}
|
2023-01-26 16:22:18 +01:00
|
|
|
return [.systemSmall]
|
2023-01-24 11:14:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var body: some WidgetConfiguration {
|
|
|
|
IntentConfiguration(kind: "Followers", intent: FollowersCountIntent.self, provider: FollowersProvider()) { entry in
|
2023-01-26 16:22:18 +01:00
|
|
|
FollowCountWidgetView(entry: entry)
|
2023-01-24 11:14:50 +01:00
|
|
|
}
|
|
|
|
.configurationDisplayName("Followers")
|
|
|
|
.description("Show number of followers.")
|
2023-01-26 16:22:18 +01:00
|
|
|
.supportedFamilies(availableFamilies)
|
2023-01-24 12:47:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension FollowersProvider {
|
|
|
|
func loadCurrentEntry(for configuration: FollowersCountIntent, in context: Context, completion: @escaping (FollowersEntry) -> Void) {
|
|
|
|
Task {
|
|
|
|
guard
|
|
|
|
let authBox = WidgetExtension.appContext
|
|
|
|
.authenticationService
|
|
|
|
.mastodonAuthenticationBoxes
|
2023-01-26 15:15:59 +01:00
|
|
|
.first
|
2023-01-24 12:47:48 +01:00
|
|
|
else {
|
2023-01-24 15:23:25 +01:00
|
|
|
return completion(.unconfigured)
|
2023-01-24 12:47:48 +01:00
|
|
|
}
|
2023-01-26 15:15:59 +01:00
|
|
|
|
|
|
|
guard let desiredAccount: String = {
|
|
|
|
guard let account = configuration.account else {
|
|
|
|
return authBox.authenticationRecord.object(in: WidgetExtension.appContext.managedObjectContext)?.user.acct
|
|
|
|
}
|
|
|
|
return account
|
|
|
|
}() else {
|
|
|
|
return completion(.unconfigured)
|
|
|
|
}
|
|
|
|
|
2023-01-24 12:47:48 +01:00
|
|
|
let resultingAccount = try await WidgetExtension.appContext
|
|
|
|
.apiService
|
2023-01-26 15:15:59 +01:00
|
|
|
.search(query: .init(q: desiredAccount, type: .accounts), authenticationBox: authBox)
|
2023-01-24 12:47:48 +01:00
|
|
|
.value
|
|
|
|
.accounts
|
2023-01-24 15:23:25 +01:00
|
|
|
.first!
|
2023-01-24 12:47:48 +01:00
|
|
|
|
2023-01-24 15:23:25 +01:00
|
|
|
let imageData = try await URLSession.shared.data(from: resultingAccount.avatarImageURLWithFallback(domain: authBox.domain)).0
|
2023-01-24 12:47:48 +01:00
|
|
|
|
|
|
|
let entry = FollowersEntry(
|
|
|
|
date: Date(),
|
2023-01-24 15:23:25 +01:00
|
|
|
account: FollowersEntryAccount.from(
|
|
|
|
mastodonAccount: resultingAccount,
|
2023-01-30 15:28:16 +01:00
|
|
|
domain: authBox.domain,
|
2023-01-24 15:23:25 +01:00
|
|
|
avatarImage: UIImage(data: imageData) ?? UIImage(named: "missingAvatar")!
|
|
|
|
),
|
2023-01-24 12:47:48 +01:00
|
|
|
configuration: configuration
|
|
|
|
)
|
2023-01-30 15:28:16 +01:00
|
|
|
|
|
|
|
followersHistory.updateFollowersTodayCount(
|
|
|
|
account: entry.account!,
|
|
|
|
count: resultingAccount.followersCount
|
|
|
|
)
|
|
|
|
|
2023-01-24 12:47:48 +01:00
|
|
|
completion(entry)
|
|
|
|
}
|
2023-01-24 11:14:50 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-24 15:23:25 +01:00
|
|
|
|
|
|
|
protocol FollowersEntryAccountable {
|
|
|
|
var followersCount: Int { get }
|
|
|
|
var displayNameWithFallback: String { get }
|
|
|
|
var acct: String { get }
|
|
|
|
var avatarImage: UIImage { get }
|
2023-01-30 15:28:16 +01:00
|
|
|
var domain: String { get }
|
2023-01-24 15:23:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct FollowersEntryAccount: FollowersEntryAccountable {
|
|
|
|
let followersCount: Int
|
|
|
|
let displayNameWithFallback: String
|
|
|
|
let acct: String
|
|
|
|
let avatarImage: UIImage
|
2023-01-30 15:28:16 +01:00
|
|
|
let domain: String
|
2023-01-24 15:23:25 +01:00
|
|
|
|
2023-01-30 15:28:16 +01:00
|
|
|
static func from(mastodonAccount: Mastodon.Entity.Account, domain: String, avatarImage: UIImage) -> Self {
|
2023-01-24 15:23:25 +01:00
|
|
|
FollowersEntryAccount(
|
|
|
|
followersCount: mastodonAccount.followersCount,
|
|
|
|
displayNameWithFallback: mastodonAccount.displayNameWithFallback,
|
|
|
|
acct: mastodonAccount.acct,
|
2023-01-30 15:28:16 +01:00
|
|
|
avatarImage: avatarImage,
|
|
|
|
domain: domain
|
2023-01-24 15:23:25 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|