From ee398407134dbdf7d78cf3cc8198f6e9278a69a0 Mon Sep 17 00:00:00 2001 From: Thomas Ricouard Date: Tue, 27 Dec 2022 13:49:54 +0100 Subject: [PATCH] Account: Internal isCurrentUser --- IceCubesApp/App/Tabs/AccountTab.swift | 2 +- .../Account/Sources/Account/AccountDetailView.swift | 12 +++++------- .../Sources/Account/AccountDetailViewModel.swift | 5 ++--- .../Sources/Timeline/TimelineViewModel.swift | 3 ++- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/IceCubesApp/App/Tabs/AccountTab.swift b/IceCubesApp/App/Tabs/AccountTab.swift index e92d7e77..62ef62fe 100644 --- a/IceCubesApp/App/Tabs/AccountTab.swift +++ b/IceCubesApp/App/Tabs/AccountTab.swift @@ -14,7 +14,7 @@ struct AccountTab: View { var body: some View { NavigationStack(path: $routeurPath.path) { if let account = currentAccount.account { - AccountDetailView(account: account, isCurrentUser: true) + AccountDetailView(account: account) .withAppRouteur() .withSheetDestinations(sheetDestinations: $routeurPath.presentedSheet) } else { diff --git a/Packages/Account/Sources/Account/AccountDetailView.swift b/Packages/Account/Sources/Account/AccountDetailView.swift index 1c930a0e..a9892a89 100644 --- a/Packages/Account/Sources/Account/AccountDetailView.swift +++ b/Packages/Account/Sources/Account/AccountDetailView.swift @@ -17,20 +17,16 @@ public struct AccountDetailView: View { @StateObject private var viewModel: AccountDetailViewModel @State private var scrollOffset: CGFloat = 0 @State private var isFieldsSheetDisplayed: Bool = false - - private let isCurrentUser: Bool + @State private var isCurrentUser: Bool = false /// When coming from a URL like a mention tap in a status. public init(accountId: String) { _viewModel = StateObject(wrappedValue: .init(accountId: accountId)) - isCurrentUser = false } /// When the account is already fetched by the parent caller. - public init(account: Account, isCurrentUser: Bool = false) { - _viewModel = StateObject(wrappedValue: .init(account: account, - isCurrentUser: isCurrentUser)) - self.isCurrentUser = isCurrentUser + public init(account: Account) { + _viewModel = StateObject(wrappedValue: .init(account: account)) } public var body: some View { @@ -72,6 +68,8 @@ public struct AccountDetailView: View { } .task { guard reasons != .placeholder else { return } + isCurrentUser = currentAccount.account?.id == viewModel.accountId + viewModel.isCurrentUser = isCurrentUser viewModel.client = client await viewModel.fetchAccount() if viewModel.statuses.isEmpty { diff --git a/Packages/Account/Sources/Account/AccountDetailViewModel.swift b/Packages/Account/Sources/Account/AccountDetailViewModel.swift index a6ef3f15..1ab732ab 100644 --- a/Packages/Account/Sources/Account/AccountDetailViewModel.swift +++ b/Packages/Account/Sources/Account/AccountDetailViewModel.swift @@ -8,6 +8,7 @@ import Env class AccountDetailViewModel: ObservableObject, StatusesFetcher { let accountId: String var client: Client? + var isCurrentUser: Bool = false enum AccountState { case loading, data(account: Account), error(error: Error) @@ -62,7 +63,6 @@ class AccountDetailViewModel: ObservableObject, StatusesFetcher { private var account: Account? private(set) var statuses: [Status] = [] - private let isCurrentUser: Bool /// When coming from a URL like a mention tap in a status. init(accountId: String) { @@ -71,10 +71,9 @@ class AccountDetailViewModel: ObservableObject, StatusesFetcher { } /// When the account is already fetched by the parent caller. - init(account: Account, isCurrentUser: Bool) { + init(account: Account) { self.accountId = account.id self.accountState = .data(account: account) - self.isCurrentUser = isCurrentUser } func fetchAccount() async { diff --git a/Packages/Timeline/Sources/Timeline/TimelineViewModel.swift b/Packages/Timeline/Sources/Timeline/TimelineViewModel.swift index 1f54e6ea..c653c038 100644 --- a/Packages/Timeline/Sources/Timeline/TimelineViewModel.swift +++ b/Packages/Timeline/Sources/Timeline/TimelineViewModel.swift @@ -128,7 +128,8 @@ class TimelineViewModel: ObservableObject, StatusesFetcher { statuses.insert(event.status, at: 0) statusesState = .display(statuses: statuses, nextPageState: .hasNextPage) } else if pendingStatusesEnabled, - !statuses.contains(where: { $0.id == event.status.id }) { + !statuses.contains(where: { $0.id == event.status.id }), + !pendingStatuses.contains(where: { $0.id == event.status.id }){ pendingStatuses.insert(event.status, at: 0) pendingStatusesState = .stream }