From 0e303418d7f5b602f1973f598e8eb7e3552d1b40 Mon Sep 17 00:00:00 2001 From: Marcus Kida Date: Tue, 7 Feb 2023 09:07:37 +0100 Subject: [PATCH] chore(Widget): Fix picked account not desired account --- .../FollowersCount/FollowersCountWidget.swift | 27 +++++++------- .../MultiFollowersCountWidget.swift | 36 ++++++++++--------- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/WidgetExtension/Variants/FollowersCount/FollowersCountWidget.swift b/WidgetExtension/Variants/FollowersCount/FollowersCountWidget.swift index 51baf30f0..6f9a6e81c 100644 --- a/WidgetExtension/Variants/FollowersCount/FollowersCountWidget.swift +++ b/WidgetExtension/Variants/FollowersCount/FollowersCountWidget.swift @@ -84,21 +84,24 @@ private extension FollowersCountWidgetProvider { return completion(.unconfigured) } - guard let desiredAccount: String = { - guard let account = configuration.account else { - return authBox.authenticationRecord.object(in: WidgetExtension.appContext.managedObjectContext)?.user.acct - } - return account - }() else { + guard + let desiredAccount = configuration.account ?? authBox.authenticationRecord.object( + in: WidgetExtension.appContext.managedObjectContext + )?.user.acct + else { return completion(.unconfigured) } - let resultingAccount = try await WidgetExtension.appContext - .apiService - .search(query: .init(q: desiredAccount, type: .accounts), authenticationBox: authBox) - .value - .accounts - .first! + guard + let resultingAccount = try await WidgetExtension.appContext + .apiService + .search(query: .init(q: desiredAccount, type: .accounts), authenticationBox: authBox) + .value + .accounts + .first(where: { $0.acct == desiredAccount }) + else { + return completion(.unconfigured) + } let imageData = try await URLSession.shared.data(from: resultingAccount.avatarImageURLWithFallback(domain: authBox.domain)).0 diff --git a/WidgetExtension/Variants/MultiFollowersCount/MultiFollowersCountWidget.swift b/WidgetExtension/Variants/MultiFollowersCount/MultiFollowersCountWidget.swift index d5398b684..f39e548c4 100644 --- a/WidgetExtension/Variants/MultiFollowersCount/MultiFollowersCountWidget.swift +++ b/WidgetExtension/Variants/MultiFollowersCount/MultiFollowersCountWidget.swift @@ -82,27 +82,31 @@ private extension MultiFollowersCountWidgetProvider { 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 { + let desiredAccounts: [String] + + if let configuredAccounts = configuration.accounts?.compactMap({ $0 }) { + desiredAccounts = configuredAccounts + } else if let currentlyLoggedInAccount = authBox.authenticationRecord.object( + in: WidgetExtension.appContext.managedObjectContext + )?.user.acct { + desiredAccounts = [currentlyLoggedInAccount] + } else { return completion(.unconfigured) } - + var accounts = [MultiFollowersEntryAccountable]() for desiredAccount in desiredAccounts { - let resultingAccount = try await WidgetExtension.appContext - .apiService - .search(query: .init(q: desiredAccount, type: .accounts), authenticationBox: authBox) - .value - .accounts - .first! + guard + let resultingAccount = try await WidgetExtension.appContext + .apiService + .search(query: .init(q: desiredAccount, type: .accounts), authenticationBox: authBox) + .value + .accounts + .first(where: { $0.acct == desiredAccount }) + else { + continue + } let imageData = try await URLSession.shared.data(from: resultingAccount.avatarImageURLWithFallback(domain: authBox.domain)).0