IceCubesApp/Packages/Account/Sources/Account/AccountDetailViewModel.swift

295 lines
11 KiB
Swift
Raw Normal View History

2023-01-17 10:36:01 +00:00
import Env
2022-11-29 11:18:06 +00:00
import Models
2023-01-17 10:36:01 +00:00
import Network
import Observation
2024-01-06 18:27:26 +00:00
import StatusKit
2023-01-17 10:36:01 +00:00
import SwiftUI
2022-11-29 11:18:06 +00:00
@MainActor
@Observable class AccountDetailViewModel: StatusesFetcher {
2022-11-29 11:18:06 +00:00
let accountId: String
2022-12-19 11:28:55 +00:00
var client: Client?
2022-12-27 12:49:54 +00:00
var isCurrentUser: Bool = false
2023-01-17 10:36:01 +00:00
enum AccountState {
2022-12-01 08:05:26 +00:00
case loading, data(account: Account), error(error: Error)
2022-11-29 11:18:06 +00:00
}
2023-01-17 10:36:01 +00:00
enum Tab: Int {
2024-01-08 17:22:44 +00:00
case statuses, favorites, bookmarks, replies, boosts, media
2023-01-17 10:36:01 +00:00
static var currentAccountTabs: [Tab] {
2024-01-08 17:22:44 +00:00
[.statuses, .replies, .boosts, .favorites, .bookmarks]
}
2023-01-17 10:36:01 +00:00
static var accountTabs: [Tab] {
2024-01-08 17:22:44 +00:00
[.statuses, .replies, .boosts, .media]
}
2023-01-17 10:36:01 +00:00
2023-01-09 18:26:56 +00:00
var iconName: String {
switch self {
2023-09-16 12:15:03 +00:00
case .statuses: "bubble.right"
case .favorites: "star"
case .bookmarks: "bookmark"
2024-01-08 17:22:44 +00:00
case .replies: "bubble.left.and.bubble.right"
case .boosts: ""
2023-09-16 12:15:03 +00:00
case .media: "photo.on.rectangle.angled"
}
}
Profile tab accessibility uplift (#1274) * Combine `joinedAtView` into one accessibility element Previously, the calendar image was visible with a nonsensical label. We use the `.combine` operator here to maintain the proper string formatting of the date. * Improve the accessibility of the AccountDetailHeaderView Previously, this image had no description and no indication that it had an associated interaction. Now, we wrap it in a button that performs the tap gesture action, and remove the element altogether if there is no avatar image set. This commit also handles the checkmark for supporter users * Tweak accessibility of Profile CustomInfoLabels This commit: - Reverses the order of title and value - Sets the value as an `accessibilityValue` - Adds a hint indicating what the button does, as they perform slightly different actions * Make Profile tab header image into a Button This element has an action associated with it (quicklook), so it makes more sense to have it as a button, and hide it if the user does not have an image set. Without the action it would have been considered decorative and should be hidden. * Change accessibilityLabel of Profile tab nav bar item to ‘Options’ “More” is considered overly generic. This commit also adds two additional user input label options * Add accessibility labels for the Profile tab `Picker` Previously, these labels were the default accessibility label provided by the SF symbol, that almost, but not quite, made sense * Remove StatusRowView swipe actions if VoiceOver is running These swipe actions are automagically added to the accessibility element’s custom actions, in addition to the ones already there, which means that there is a significant (and confusing) amount of doubling up going on. * Fix typo in StatusRowView.accessibilityActions * Add accessibilityLabels to all StatusRowActionsView actions * Provide explicit combined accessibility label for unfocused StatusRowView Previously, this was a synthesized label, which read the elements in their traversal order, and didn’t provide any context for which of the three numbers corresponded to replies, boosts or favourites. Now, we create an explicit combined label when the post isn’t being viewed by itself. * Improve accessibility of StatusRow(Reblog|Reply)View They are now combined elements and don’t vend the icon as its own element. * Add missing punctuation to accessibility hints * Remove interaction from Profile tab @username and profile note elements These elements open the profile photo url, which is already provided explicitly through the profile photo * Prefer spoiler warning for StatusRowView accessibility label …but place the full, unredacted content in an `AccessibilityCustomContent` field for easy access. Additionally, if VoiceOver is running, an action to expand the warning is also available. * Represent `FollowButton` elements as Toggles to accessibility Since these buttons have two states (though arguable in the case of following, but handled here by not changing the representation if a request is pending), it makes sense to handle them as toggles, so they will be read as “Following, On, <Trait>” * Remove errant comment * Add “Verified” accessibilityValue to profile fields * Fix bug StatusRowView default action bug affecting VoiceOver users Previously, the default (‘Activate’) action for VoiceOver users would be to share a link to the toot, rather than navigate to its detail. It’s hard to say exactly what caused this, but the root was the inclusion of the `contextMenu` in the `accessibilityActions`. Now, double-tapping on a a non-focused `StatusRowView` will take you to the toot detail. * Add header trait to Profile tab display name and familiar followers These stand out as being header-like in presentation and represent the beginning of specific parts of the screen. * Add conditional accessibility modifier to Profile tab user-defined fields that opens the correct link * Add accessibility container that contextualises the user-defined fields When VoiceOver users first enter a user-defined field, the container label will be read out before the element’s spoken description. * Improve StatusRowView combined accessibility label It will now start with: “X boosted Y”, “X replied to @Y”, or “X…” depending on the context of the toot. * Change familiar follows thumbnail to a Button; add display name as accessibility label Previously, this button had no context, and would just be a series of images with nothing to allow users to disambiguate them. * Revert changes from ZStack with tap gesture to Button Using a Button for this purpose caused high weirdness in tap zones. Basically everything down to the familiar followers triggered both image buttons. * Add image alt text to StatusRowView and StatusRowMediaPreviewView Previously, there was no way for the intended audience for the alt text to find said text. There is a tap gesture on each image in the focused status row, but this is not advertised to the user. Now, the first image’s alt text is read as part of the non-focused, combined representation, and each image has its own alt text attributed in the focused representation. * Add Profile tab accessibility labels to indicate private/bot/muted/blocked accounts Previously, the icon did not have any accessible representation (an empty text string). * Add header trait to Profile “pinned post” * Use the Account.Field.name for the user input label * Replace spaces with commas in StatusRowView.combinedAccessibilityLabel
2023-03-19 15:27:18 +00:00
var accessibilityLabel: LocalizedStringKey {
switch self {
2023-09-16 12:15:03 +00:00
case .statuses: "accessibility.tabs.profile.picker.statuses"
case .favorites: "accessibility.tabs.profile.picker.favorites"
case .bookmarks: "accessibility.tabs.profile.picker.bookmarks"
2024-01-08 17:22:44 +00:00
case .replies: "accessibility.tabs.profile.picker.posts-and-replies"
case .boosts: "accessibility.tabs.profile.picker.boosts"
2023-09-16 12:15:03 +00:00
case .media: "accessibility.tabs.profile.picker.media"
Profile tab accessibility uplift (#1274) * Combine `joinedAtView` into one accessibility element Previously, the calendar image was visible with a nonsensical label. We use the `.combine` operator here to maintain the proper string formatting of the date. * Improve the accessibility of the AccountDetailHeaderView Previously, this image had no description and no indication that it had an associated interaction. Now, we wrap it in a button that performs the tap gesture action, and remove the element altogether if there is no avatar image set. This commit also handles the checkmark for supporter users * Tweak accessibility of Profile CustomInfoLabels This commit: - Reverses the order of title and value - Sets the value as an `accessibilityValue` - Adds a hint indicating what the button does, as they perform slightly different actions * Make Profile tab header image into a Button This element has an action associated with it (quicklook), so it makes more sense to have it as a button, and hide it if the user does not have an image set. Without the action it would have been considered decorative and should be hidden. * Change accessibilityLabel of Profile tab nav bar item to ‘Options’ “More” is considered overly generic. This commit also adds two additional user input label options * Add accessibility labels for the Profile tab `Picker` Previously, these labels were the default accessibility label provided by the SF symbol, that almost, but not quite, made sense * Remove StatusRowView swipe actions if VoiceOver is running These swipe actions are automagically added to the accessibility element’s custom actions, in addition to the ones already there, which means that there is a significant (and confusing) amount of doubling up going on. * Fix typo in StatusRowView.accessibilityActions * Add accessibilityLabels to all StatusRowActionsView actions * Provide explicit combined accessibility label for unfocused StatusRowView Previously, this was a synthesized label, which read the elements in their traversal order, and didn’t provide any context for which of the three numbers corresponded to replies, boosts or favourites. Now, we create an explicit combined label when the post isn’t being viewed by itself. * Improve accessibility of StatusRow(Reblog|Reply)View They are now combined elements and don’t vend the icon as its own element. * Add missing punctuation to accessibility hints * Remove interaction from Profile tab @username and profile note elements These elements open the profile photo url, which is already provided explicitly through the profile photo * Prefer spoiler warning for StatusRowView accessibility label …but place the full, unredacted content in an `AccessibilityCustomContent` field for easy access. Additionally, if VoiceOver is running, an action to expand the warning is also available. * Represent `FollowButton` elements as Toggles to accessibility Since these buttons have two states (though arguable in the case of following, but handled here by not changing the representation if a request is pending), it makes sense to handle them as toggles, so they will be read as “Following, On, <Trait>” * Remove errant comment * Add “Verified” accessibilityValue to profile fields * Fix bug StatusRowView default action bug affecting VoiceOver users Previously, the default (‘Activate’) action for VoiceOver users would be to share a link to the toot, rather than navigate to its detail. It’s hard to say exactly what caused this, but the root was the inclusion of the `contextMenu` in the `accessibilityActions`. Now, double-tapping on a a non-focused `StatusRowView` will take you to the toot detail. * Add header trait to Profile tab display name and familiar followers These stand out as being header-like in presentation and represent the beginning of specific parts of the screen. * Add conditional accessibility modifier to Profile tab user-defined fields that opens the correct link * Add accessibility container that contextualises the user-defined fields When VoiceOver users first enter a user-defined field, the container label will be read out before the element’s spoken description. * Improve StatusRowView combined accessibility label It will now start with: “X boosted Y”, “X replied to @Y”, or “X…” depending on the context of the toot. * Change familiar follows thumbnail to a Button; add display name as accessibility label Previously, this button had no context, and would just be a series of images with nothing to allow users to disambiguate them. * Revert changes from ZStack with tap gesture to Button Using a Button for this purpose caused high weirdness in tap zones. Basically everything down to the familiar followers triggered both image buttons. * Add image alt text to StatusRowView and StatusRowMediaPreviewView Previously, there was no way for the intended audience for the alt text to find said text. There is a tap gesture on each image in the focused status row, but this is not advertised to the user. Now, the first image’s alt text is read as part of the non-focused, combined representation, and each image has its own alt text attributed in the focused representation. * Add Profile tab accessibility labels to indicate private/bot/muted/blocked accounts Previously, the icon did not have any accessible representation (an empty text string). * Add header trait to Profile “pinned post” * Use the Account.Field.name for the user input label * Replace spaces with commas in StatusRowView.combinedAccessibilityLabel
2023-03-19 15:27:18 +00:00
}
}
}
2023-01-17 10:36:01 +00:00
var accountState: AccountState = .loading
var statusesState: StatusesState = .loading
2023-01-17 10:36:01 +00:00
var relationship: Relationship?
var pinned: [Status] = []
var favorites: [Status] = []
var bookmarks: [Status] = []
private var favoritesNextPage: LinkHandler?
2023-01-09 18:26:56 +00:00
private var bookmarksNextPage: LinkHandler?
var featuredTags: [FeaturedTag] = []
var fields: [Account.Field] = []
var familiarFollowers: [Account] = []
var selectedTab = Tab.statuses {
didSet {
switch selectedTab {
2024-01-08 17:22:44 +00:00
case .statuses, .replies, .boosts, .media:
tabTask?.cancel()
tabTask = Task {
await fetchNewestStatuses(pullToRefresh: false)
}
default:
reloadTabState()
}
}
}
2023-01-17 10:36:01 +00:00
var scrollToTopVisible: Bool = false
var translation: Translation?
var isLoadingTranslation = false
private(set) var account: Account?
private var tabTask: Task<Void, Never>?
2023-01-17 10:36:01 +00:00
2022-12-20 15:08:09 +00:00
private(set) var statuses: [Status] = []
2024-02-14 11:48:14 +00:00
2024-01-08 17:22:44 +00:00
var boosts: [Status] = []
2023-01-17 10:36:01 +00:00
/// When coming from a URL like a mention tap in a status.
2022-11-29 11:18:06 +00:00
init(accountId: String) {
self.accountId = accountId
2023-01-17 10:36:01 +00:00
isCurrentUser = false
2022-11-29 11:18:06 +00:00
}
2023-01-17 10:36:01 +00:00
/// When the account is already fetched by the parent caller.
2022-12-27 12:49:54 +00:00
init(account: Account) {
2023-01-17 10:36:01 +00:00
accountId = account.id
self.account = account
2023-01-17 10:36:01 +00:00
accountState = .data(account: account)
2022-12-17 12:37:46 +00:00
}
2023-01-17 10:36:01 +00:00
2023-01-10 20:09:20 +00:00
struct AccountData {
let account: Account
let featuredTags: [FeaturedTag]
let relationships: [Relationship]
2023-01-10 20:09:20 +00:00
}
2023-01-17 10:36:01 +00:00
2022-11-29 11:18:06 +00:00
func fetchAccount() async {
2022-12-19 11:28:55 +00:00
guard let client else { return }
2022-11-29 11:18:06 +00:00
do {
2023-01-10 20:09:20 +00:00
let data = try await fetchAccountData(accountId: accountId, client: client)
accountState = .data(account: data.account)
2023-01-17 10:36:01 +00:00
2023-01-10 20:09:20 +00:00
account = data.account
fields = data.account.fields
featuredTags = data.featuredTags
featuredTags.sort { $0.statusesCountInt > $1.statusesCountInt }
relationship = data.relationships.first
2022-11-29 11:18:06 +00:00
} catch {
2022-12-30 07:36:22 +00:00
if let account {
accountState = .data(account: account)
} else {
accountState = .error(error: error)
}
2022-11-29 11:18:06 +00:00
}
}
2023-01-17 10:36:01 +00:00
private func fetchAccountData(accountId: String, client: Client) async throws -> AccountData {
2023-01-10 20:09:20 +00:00
async let account: Account = client.get(endpoint: Accounts.accounts(id: accountId))
async let featuredTags: [FeaturedTag] = client.get(endpoint: Accounts.featuredTags(id: accountId))
2023-09-16 12:15:03 +00:00
if client.isAuth, !isCurrentUser {
async let relationships: [Relationship] = client.get(endpoint: Accounts.relationships(ids: [accountId]))
do {
return try await .init(account: account,
featuredTags: featuredTags,
relationships: relationships)
} catch {
2023-03-13 12:38:28 +00:00
return try await .init(account: account,
featuredTags: [],
relationships: relationships)
}
2023-01-10 20:09:20 +00:00
}
return try await .init(account: account,
featuredTags: featuredTags,
relationships: [])
}
2023-01-22 05:38:30 +00:00
func fetchFamilliarFollowers() async {
let familiarFollowers: [FamiliarAccounts]? = try? await client?.get(endpoint: Accounts.familiarFollowers(withAccount: accountId))
self.familiarFollowers = familiarFollowers?.first?.accounts ?? []
2023-01-10 20:09:20 +00:00
}
2023-01-17 10:36:01 +00:00
2024-02-14 11:48:14 +00:00
func fetchNewestStatuses(pullToRefresh _: Bool) async {
2022-12-19 11:28:55 +00:00
guard let client else { return }
2022-12-18 19:30:19 +00:00
do {
2024-01-08 17:22:44 +00:00
statusesState = .loading
boosts = []
statuses =
2023-01-03 17:22:08 +00:00
try await client.get(endpoint: Accounts.statuses(id: accountId,
sinceId: nil,
tag: nil,
2024-01-08 17:22:44 +00:00
onlyMedia: selectedTab == .media,
excludeReplies: selectedTab != .replies,
excludeReblogs: selectedTab != .boosts,
2023-01-17 10:36:01 +00:00
pinned: nil))
StatusDataControllerProvider.shared.updateDataControllers(for: statuses, client: client)
2024-01-08 17:22:44 +00:00
if selectedTab == .boosts {
2024-02-14 11:48:14 +00:00
boosts = statuses.filter { $0.reblog != nil }
2024-01-08 17:22:44 +00:00
}
2023-01-17 10:36:01 +00:00
if selectedTab == .statuses {
pinned =
try await client.get(endpoint: Accounts.statuses(id: accountId,
sinceId: nil,
tag: nil,
2024-01-08 17:22:44 +00:00
onlyMedia: false,
excludeReplies: false,
excludeReblogs: false,
2023-01-17 10:36:01 +00:00
pinned: true))
StatusDataControllerProvider.shared.updateDataControllers(for: pinned, client: client)
2023-01-03 17:22:08 +00:00
}
if isCurrentUser {
(favorites, favoritesNextPage) = try await client.getWithLink(endpoint: Accounts.favorites(sinceId: nil))
2023-01-09 18:26:56 +00:00
(bookmarks, bookmarksNextPage) = try await client.getWithLink(endpoint: Accounts.bookmarks(sinceId: nil))
StatusDataControllerProvider.shared.updateDataControllers(for: favorites, client: client)
StatusDataControllerProvider.shared.updateDataControllers(for: bookmarks, client: client)
}
2022-12-21 11:39:29 +00:00
reloadTabState()
2022-12-18 19:30:19 +00:00
} catch {
2024-01-08 17:22:44 +00:00
statusesState = .error(error: error)
2022-12-18 19:30:19 +00:00
}
}
2023-01-17 10:36:01 +00:00
func fetchNextPage() async throws {
2022-12-19 11:28:55 +00:00
guard let client else { return }
switch selectedTab {
case .statuses, .replies, .boosts, .media:
guard let lastId = statuses.last?.id else { return }
let newStatuses: [Status] =
2024-02-14 11:48:14 +00:00
try await client.get(endpoint: Accounts.statuses(id: accountId,
sinceId: lastId,
tag: nil,
onlyMedia: selectedTab == .media,
excludeReplies: selectedTab != .replies,
excludeReblogs: selectedTab != .boosts,
pinned: nil))
statuses.append(contentsOf: newStatuses)
if selectedTab == .boosts {
2024-02-14 11:48:14 +00:00
let newBoosts = statuses.filter { $0.reblog != nil }
boosts.append(contentsOf: newBoosts)
}
StatusDataControllerProvider.shared.updateDataControllers(for: newStatuses, client: client)
if selectedTab == .boosts {
statusesState = .display(statuses: boosts,
nextPageState: newStatuses.count < 20 ? .none : .hasNextPage)
} else {
statusesState = .display(statuses: statuses,
nextPageState: newStatuses.count < 20 ? .none : .hasNextPage)
}
case .favorites:
guard let nextPageId = favoritesNextPage?.maxId else { return }
let newFavorites: [Status]
(newFavorites, favoritesNextPage) = try await client.getWithLink(endpoint: Accounts.favorites(sinceId: nextPageId))
favorites.append(contentsOf: newFavorites)
StatusDataControllerProvider.shared.updateDataControllers(for: newFavorites, client: client)
statusesState = .display(statuses: favorites, nextPageState: .hasNextPage)
case .bookmarks:
guard let nextPageId = bookmarksNextPage?.maxId else { return }
let newBookmarks: [Status]
(newBookmarks, bookmarksNextPage) = try await client.getWithLink(endpoint: Accounts.bookmarks(sinceId: nextPageId))
StatusDataControllerProvider.shared.updateDataControllers(for: newBookmarks, client: client)
bookmarks.append(contentsOf: newBookmarks)
statusesState = .display(statuses: bookmarks, nextPageState: .hasNextPage)
2022-12-18 19:30:19 +00:00
}
}
2023-01-17 10:36:01 +00:00
2022-12-21 11:39:29 +00:00
private func reloadTabState() {
switch selectedTab {
2024-01-08 17:22:44 +00:00
case .statuses, .replies, .media:
statusesState = .display(statuses: statuses, nextPageState: statuses.count < 20 ? .none : .hasNextPage)
case .boosts:
statusesState = .display(statuses: boosts, nextPageState: statuses.count < 20 ? .none : .hasNextPage)
case .favorites:
2024-01-08 17:22:44 +00:00
statusesState = .display(statuses: favorites,
nextPageState: favoritesNextPage != nil ? .hasNextPage : .none)
2023-01-09 18:26:56 +00:00
case .bookmarks:
2024-01-08 17:22:44 +00:00
statusesState = .display(statuses: bookmarks,
nextPageState: bookmarksNextPage != nil ? .hasNextPage : .none)
2022-12-21 11:39:29 +00:00
}
}
2023-01-17 10:36:01 +00:00
func handleEvent(event: any StreamEvent, currentAccount: CurrentAccount) {
if let event = event as? StreamEventUpdate {
2024-02-11 17:52:58 +00:00
if event.status.account.id == currentAccount.account?.id {
if (event.status.inReplyToId == nil && selectedTab == .statuses) ||
2024-02-14 11:48:14 +00:00
(event.status.inReplyToId != nil && selectedTab == .replies)
{
2024-02-11 17:52:58 +00:00
statuses.insert(event.status, at: 0)
statusesState = .display(statuses: statuses, nextPageState: .hasNextPage)
}
}
} else if let event = event as? StreamEventDelete {
statuses.removeAll(where: { $0.id == event.status })
statusesState = .display(statuses: statuses, nextPageState: .hasNextPage)
} else if let event = event as? StreamEventStatusUpdate {
if let originalIndex = statuses.firstIndex(where: { $0.id == event.status.id }) {
statuses[originalIndex] = event.status
statusesState = .display(statuses: statuses, nextPageState: .hasNextPage)
}
}
}
2023-02-01 11:49:59 +00:00
func statusDidAppear(status _: Models.Status) {}
func statusDidDisappear(status _: Status) {}
func translate(userLang: String) async {
guard let account else { return }
withAnimation {
isLoadingTranslation = true
}
let userAPIKey = DeepLUserAPIHandler.readIfAllowed()
let userAPIFree = UserPreferences.shared.userDeeplAPIFree
let deeplClient = DeepLClient(userAPIKey: userAPIKey, userAPIFree: userAPIFree)
let translation = try? await deeplClient.request(target: userLang, text: account.note.asRawText)
withAnimation {
self.translation = translation
isLoadingTranslation = false
}
}
2022-11-29 11:18:06 +00:00
}