From 24504e3bc2d5594e4ce1db3e9134486699279f33 Mon Sep 17 00:00:00 2001 From: Thomas Ricouard Date: Mon, 30 Jan 2023 19:14:43 +0100 Subject: [PATCH] Various optimizations to speed things up --- IceCubesApp/App/IceCubesApp.swift | 2 +- IceCubesApp/App/Tabs/MessagesTab.swift | 2 +- IceCubesApp/App/Tabs/NotificationTab.swift | 14 +++++++++++++- IceCubesApp/App/Tabs/ProfileTab.swift | 4 ++-- IceCubesApp/App/Tabs/Timeline/TimelineTab.swift | 4 ++-- .../Sources/Account/AccountDetailView.swift | 1 - .../AppAccount/AppAccountsSelectorView.swift | 2 +- .../Detail/ConversationDetailView.swift | 1 - .../Detail/ConversationMessageView.swift | 1 - .../Conversations/List/ConversationsListView.swift | 1 - Packages/Env/Sources/Env/CurrentAccount.swift | 5 ++++- Packages/Explore/Sources/Explore/ExploreView.swift | 1 - Packages/Network/Sources/Network/Client.swift | 6 +++++- .../Notifications/NotificationsListView.swift | 2 +- .../Sources/Status/Detail/StatusDetailView.swift | 1 - .../Sources/Status/List/StatusesListView.swift | 1 - 16 files changed, 30 insertions(+), 18 deletions(-) diff --git a/IceCubesApp/App/IceCubesApp.swift b/IceCubesApp/App/IceCubesApp.swift index 7281cf76..4c358648 100644 --- a/IceCubesApp/App/IceCubesApp.swift +++ b/IceCubesApp/App/IceCubesApp.swift @@ -116,7 +116,7 @@ struct IceCubesApp: App { } } if proxy.frame(in: .global).width > (.maxColumnWidth + .secondaryColumnWidth), - currentAccount.account?.id != nil + appAccountsManager.currentClient.isAuth { Divider().edgesIgnoringSafeArea(.all) NotificationsTab(popToRootTab: $popToRootTab, lockedType: nil) diff --git a/IceCubesApp/App/Tabs/MessagesTab.swift b/IceCubesApp/App/Tabs/MessagesTab.swift index 80b65fee..52702a92 100644 --- a/IceCubesApp/App/Tabs/MessagesTab.swift +++ b/IceCubesApp/App/Tabs/MessagesTab.swift @@ -30,7 +30,7 @@ struct MessagesTab: View { } } .toolbarBackground(theme.primaryBackgroundColor.opacity(0.50), for: .navigationBar) - .id(currentAccount.account?.id) + .id(client.id) } .onChange(of: $popToRootTab.wrappedValue) { popToRootTab in if popToRootTab == .messages { diff --git a/IceCubesApp/App/Tabs/NotificationTab.swift b/IceCubesApp/App/Tabs/NotificationTab.swift index a6afe276..d775d837 100644 --- a/IceCubesApp/App/Tabs/NotificationTab.swift +++ b/IceCubesApp/App/Tabs/NotificationTab.swift @@ -9,6 +9,7 @@ import Timeline struct NotificationsTab: View { @Environment(\.isSecondaryColumn) private var isSecondaryColumn: Bool + @Environment(\.scenePhase) private var scenePhase @EnvironmentObject private var theme: Theme @EnvironmentObject private var client: Client @@ -38,7 +39,7 @@ struct NotificationsTab: View { } } .toolbarBackground(theme.primaryBackgroundColor.opacity(0.50), for: .navigationBar) - .id(currentAccount.account?.id) + .id(client.id) } .onAppear { routerPath.client = client @@ -52,6 +53,17 @@ struct NotificationsTab: View { routerPath.path = [] } } + .onChange(of: scenePhase, perform: { scenePhase in + switch scenePhase { + case .active: + if isSecondaryColumn { + watcher.unreadNotificationsCount = 0 + userPreferences.pushNotificationsCount = 0 + } + default: + break + } + }) .onChange(of: currentAccount.account?.id) { _ in routerPath.path = [] } diff --git a/IceCubesApp/App/Tabs/ProfileTab.swift b/IceCubesApp/App/Tabs/ProfileTab.swift index 1fa7d0d3..3ca2007c 100644 --- a/IceCubesApp/App/Tabs/ProfileTab.swift +++ b/IceCubesApp/App/Tabs/ProfileTab.swift @@ -9,6 +9,7 @@ import Shimmer import SwiftUI struct ProfileTab: View { + @EnvironmentObject private var appAccount: AppAccountsManager @EnvironmentObject private var theme: Theme @EnvironmentObject private var client: Client @EnvironmentObject private var currentAccount: CurrentAccount @@ -22,11 +23,10 @@ struct ProfileTab: View { .withAppRouter() .withSheetDestinations(sheetDestinations: $routerPath.presentedSheet) .toolbarBackground(theme.primaryBackgroundColor.opacity(0.50), for: .navigationBar) - .id(currentAccount.account?.id) + .id(client.id) } else { AccountDetailView(account: .placeholder()) .redacted(reason: .placeholder) - .shimmering() } } .onChange(of: $popToRootTab.wrappedValue) { popToRootTab in diff --git a/IceCubesApp/App/Tabs/Timeline/TimelineTab.swift b/IceCubesApp/App/Tabs/Timeline/TimelineTab.swift index 434693b9..432f9e01 100644 --- a/IceCubesApp/App/Tabs/Timeline/TimelineTab.swift +++ b/IceCubesApp/App/Tabs/Timeline/TimelineTab.swift @@ -37,7 +37,7 @@ struct TimelineTab: View { toolbarView } .toolbarBackground(theme.primaryBackgroundColor.opacity(0.50), for: .navigationBar) - .id(currentAccount.account?.id) + .id(client.id) } .onAppear { routerPath.client = client @@ -144,7 +144,7 @@ struct TimelineTab: View { if UIDevice.current.userInterfaceIdiom != .pad { ToolbarItem(placement: .navigationBarLeading) { AppAccountsSelectorView(routerPath: routerPath) - .id(currentAccount.account?.id) + .id(client.id) } } statusEditorToolbarItem(routerPath: routerPath, diff --git a/Packages/Account/Sources/Account/AccountDetailView.swift b/Packages/Account/Sources/Account/AccountDetailView.swift index 1f70919a..2d59a47a 100644 --- a/Packages/Account/Sources/Account/AccountDetailView.swift +++ b/Packages/Account/Sources/Account/AccountDetailView.swift @@ -143,7 +143,6 @@ public struct AccountDetailView: View { scrollViewProxy: proxy, scrollOffset: $scrollOffset) .redacted(reason: .placeholder) - .shimmering() case let .data(account): AccountDetailHeaderView(viewModel: viewModel, account: account, diff --git a/Packages/AppAccount/Sources/AppAccount/AppAccountsSelectorView.swift b/Packages/AppAccount/Sources/AppAccount/AppAccountsSelectorView.swift index 758cc146..b9fa086f 100644 --- a/Packages/AppAccount/Sources/AppAccount/AppAccountsSelectorView.swift +++ b/Packages/AppAccount/Sources/AppAccount/AppAccountsSelectorView.swift @@ -55,7 +55,7 @@ public struct AppAccountsSelectorView: View { @ViewBuilder private var labelView: some View { Group { - if let avatar = currentAccount.account?.avatar { + if let avatar = currentAccount.account?.avatar, !currentAccount.isLoadingAccount { AvatarView(url: avatar, size: avatarSize) } else { ProgressView() diff --git a/Packages/Conversations/Sources/Conversations/Detail/ConversationDetailView.swift b/Packages/Conversations/Sources/Conversations/Detail/ConversationDetailView.swift index c2af7961..b26a4b80 100644 --- a/Packages/Conversations/Sources/Conversations/Detail/ConversationDetailView.swift +++ b/Packages/Conversations/Sources/Conversations/Detail/ConversationDetailView.swift @@ -99,7 +99,6 @@ public struct ConversationDetailView: View { ForEach(Status.placeholders()) { message in ConversationMessageView(message: message, conversation: viewModel.conversation) .redacted(reason: .placeholder) - .shimmering() .padding(.vertical, 4) } } diff --git a/Packages/Conversations/Sources/Conversations/Detail/ConversationMessageView.swift b/Packages/Conversations/Sources/Conversations/Detail/ConversationMessageView.swift index 64b6fcf6..e901620b 100644 --- a/Packages/Conversations/Sources/Conversations/Detail/ConversationMessageView.swift +++ b/Packages/Conversations/Sources/Conversations/Detail/ConversationMessageView.swift @@ -135,7 +135,6 @@ struct ConversationMessageView: View { RoundedRectangle(cornerRadius: 8) .fill(Color.gray) .frame(height: 200) - .shimmering() } } .frame(height: 200) diff --git a/Packages/Conversations/Sources/Conversations/List/ConversationsListView.swift b/Packages/Conversations/Sources/Conversations/List/ConversationsListView.swift index 3e14f5e1..7be178d1 100644 --- a/Packages/Conversations/Sources/Conversations/List/ConversationsListView.swift +++ b/Packages/Conversations/Sources/Conversations/List/ConversationsListView.swift @@ -32,7 +32,6 @@ public struct ConversationsListView: View { ConversationsListRow(conversation: conversation, viewModel: viewModel) .padding(.horizontal, .layoutPadding) .redacted(reason: .placeholder) - .shimmering() } else { ConversationsListRow(conversation: conversation, viewModel: viewModel) .padding(.horizontal, .layoutPadding) diff --git a/Packages/Env/Sources/Env/CurrentAccount.swift b/Packages/Env/Sources/Env/CurrentAccount.swift index b0ec9aba..d6abceea 100644 --- a/Packages/Env/Sources/Env/CurrentAccount.swift +++ b/Packages/Env/Sources/Env/CurrentAccount.swift @@ -9,6 +9,7 @@ public class CurrentAccount: ObservableObject { @Published public private(set) var tags: [Tag] = [] @Published public private(set) var followRequests: [Account] = [] @Published public private(set) var isUpdating: Bool = false + @Published public private(set) var isLoadingAccount: Bool = false private var client: Client? @@ -26,8 +27,8 @@ public class CurrentAccount: ObservableObject { private func fetchUserData() async { await withTaskGroup(of: Void.self) { group in - group.addTask { await self.fetchConnections() } group.addTask { await self.fetchCurrentAccount() } + group.addTask { await self.fetchConnections() } group.addTask { await self.fetchLists() } group.addTask { await self.fetchFollowedTags() } group.addTask { await self.fetchFollowerRequests() } @@ -47,7 +48,9 @@ public class CurrentAccount: ObservableObject { account = nil return } + isLoadingAccount = true account = try? await client.get(endpoint: Accounts.verifyCredentials) + isLoadingAccount = false } public func fetchLists() async { diff --git a/Packages/Explore/Sources/Explore/ExploreView.swift b/Packages/Explore/Sources/Explore/ExploreView.swift index eccf2eb0..df590d85 100644 --- a/Packages/Explore/Sources/Explore/ExploreView.swift +++ b/Packages/Explore/Sources/Explore/ExploreView.swift @@ -84,7 +84,6 @@ public struct ExploreView: View { StatusRowView(viewModel: .init(status: status, isCompact: false)) .padding(.vertical, 8) .redacted(reason: .placeholder) - .shimmering() .listRowBackground(theme.primaryBackgroundColor) } } diff --git a/Packages/Network/Sources/Network/Client.swift b/Packages/Network/Sources/Network/Client.swift index 8883c365..5521d802 100644 --- a/Packages/Network/Sources/Network/Client.swift +++ b/Packages/Network/Sources/Network/Client.swift @@ -2,7 +2,7 @@ import Foundation import Models import SwiftUI -public class Client: ObservableObject, Equatable { +public class Client: ObservableObject, Equatable, Identifiable { public static func == (lhs: Client, rhs: Client) -> Bool { lhs.isAuth == rhs.isAuth && lhs.server == rhs.server && @@ -17,6 +17,10 @@ public class Client: ObservableObject, Equatable { case missingApp case invalidRedirectURL } + + public var id: String { + "\(isAuth)\(server)\(oauthToken?.accessToken ?? "")" + } public var server: String public let version: Version diff --git a/Packages/Notifications/Sources/Notifications/NotificationsListView.swift b/Packages/Notifications/Sources/Notifications/NotificationsListView.swift index d976ccb9..97f54df8 100644 --- a/Packages/Notifications/Sources/Notifications/NotificationsListView.swift +++ b/Packages/Notifications/Sources/Notifications/NotificationsListView.swift @@ -87,7 +87,7 @@ public struct NotificationsListView: View { bottom: 12, trailing: .layoutPadding)) .listRowBackground(theme.primaryBackgroundColor) - .shimmering() + .redacted(reason: .placeholder) } case let .display(notifications, nextPageState): diff --git a/Packages/Status/Sources/Status/Detail/StatusDetailView.swift b/Packages/Status/Sources/Status/Detail/StatusDetailView.swift index edf0ce73..b07261c5 100644 --- a/Packages/Status/Sources/Status/Detail/StatusDetailView.swift +++ b/Packages/Status/Sources/Status/Detail/StatusDetailView.swift @@ -35,7 +35,6 @@ public struct StatusDetailView: View { StatusRowView(viewModel: .init(status: status, isCompact: false)) .padding(.horizontal, .layoutPadding) .redacted(reason: .placeholder) - .shimmering() Divider() .padding(.vertical, .dividerPadding) } diff --git a/Packages/Status/Sources/Status/List/StatusesListView.swift b/Packages/Status/Sources/Status/List/StatusesListView.swift index 8d9fc44f..69785575 100644 --- a/Packages/Status/Sources/Status/List/StatusesListView.swift +++ b/Packages/Status/Sources/Status/List/StatusesListView.swift @@ -20,7 +20,6 @@ public struct StatusesListView: View where Fetcher: StatusesFetcher { StatusRowView(viewModel: .init(status: status, isCompact: false)) .redacted(reason: .placeholder) .padding(.horizontal, .layoutPadding) - .shimmering() Divider() .padding(.vertical, .dividerPadding) }