From d3c7ba2c7cd24e7e5f82b20552e557e9fbbb7d61 Mon Sep 17 00:00:00 2001 From: Nathan Mattes Date: Fri, 8 Dec 2023 15:43:06 +0100 Subject: [PATCH] Show header-meta-information about account (IOS-192) --- .../Header/ProfileHeaderViewController.swift | 15 ++---- .../Header/ProfileHeaderViewModel.swift | 7 +-- .../ProfileHeaderView+Configuration.swift | 47 ++++--------------- .../Scene/Profile/ProfileViewController.swift | 4 +- .../Entity/Mastodon+Entity+Account.swift | 5 ++ 5 files changed, 25 insertions(+), 53 deletions(-) diff --git a/Mastodon/Scene/Profile/Header/ProfileHeaderViewController.swift b/Mastodon/Scene/Profile/Header/ProfileHeaderViewController.swift index aba92c9c8..8b5a0c8b8 100644 --- a/Mastodon/Scene/Profile/Header/ProfileHeaderViewController.swift +++ b/Mastodon/Scene/Profile/Header/ProfileHeaderViewController.swift @@ -88,7 +88,11 @@ extension ProfileHeaderViewController { override func viewDidLoad() { super.viewDidLoad() - + + profileHeaderView.prepareForReuse() + profileHeaderView.configuration(account: viewModel.account) + + view.setContentHuggingPriority(.required - 1, for: .vertical) view.backgroundColor = .systemBackground @@ -128,15 +132,6 @@ extension ProfileHeaderViewController { self.titleView.subtitleLabel.alpha = isTitleViewContentOffsetDidSet ? 1 : 0 } .store(in: &disposeBag) - viewModel.$account - .receive(on: DispatchQueue.main) - .sink { [weak self] account in - guard let self, let account else { return } - - self.profileHeaderView.prepareForReuse() - self.profileHeaderView.configuration(account: account) - } - .store(in: &disposeBag) viewModel.$relationship .assign(to: \.relationship, on: profileHeaderView.viewModel) .store(in: &disposeBag) diff --git a/Mastodon/Scene/Profile/Header/ProfileHeaderViewModel.swift b/Mastodon/Scene/Profile/Header/ProfileHeaderViewModel.swift index 726ddf715..f8db96e06 100644 --- a/Mastodon/Scene/Profile/Header/ProfileHeaderViewModel.swift +++ b/Mastodon/Scene/Profile/Header/ProfileHeaderViewModel.swift @@ -26,7 +26,7 @@ final class ProfileHeaderViewModel { let context: AppContext let authContext: AuthContext - @Published var account: Mastodon.Entity.Account? + @Published var account: Mastodon.Entity.Account @Published var relationship: Mastodon.Entity.Relationship? @Published var isMyself = false @@ -44,10 +44,11 @@ final class ProfileHeaderViewModel { @Published var isTitleViewDisplaying = false @Published var isTitleViewContentOffsetSet = false - init(context: AppContext, authContext: AuthContext) { + init(context: AppContext, authContext: AuthContext, account: Mastodon.Entity.Account) { self.context = context self.authContext = authContext - + self.account = account + $accountForEdit .receive(on: DispatchQueue.main) .sink { [weak self] account in diff --git a/Mastodon/Scene/Profile/Header/View/ProfileHeaderView+Configuration.swift b/Mastodon/Scene/Profile/Header/View/ProfileHeaderView+Configuration.swift index b044b0603..2d8a6cdd0 100644 --- a/Mastodon/Scene/Profile/Header/View/ProfileHeaderView+Configuration.swift +++ b/Mastodon/Scene/Profile/Header/View/ProfileHeaderView+Configuration.swift @@ -11,44 +11,15 @@ import MastodonSDK extension ProfileHeaderView { func configuration(account: Mastodon.Entity.Account) { - #warning("TODO: Implement") -// // header -// account.header.publisher -// .assign(to: \.headerImageURL, on: viewModel) -// .store(in: &disposeBag) -// // avatar -// account.avatar.publisher -// .assign(to: \.avatarImageURL, on: viewModel) -// .store(in: &disposeBag) -// // emojiMeta -// account.emojis.publisher -// .map { $0.asDictionary } -// .assign(to: \.emojiMeta, on: viewModel) -// .store(in: &disposeBag) -// // name -// account.publisher(for: \.displayName) -// .map { _ in account.displayNameWithFallback } -// .assign(to: \.name, on: viewModel) -// .store(in: &disposeBag) -// // username -// viewModel.acct = account.acctWithDomain -// // bio -// account.publisher(for: \.note) -// .assign(to: \.note, on: viewModel) -// .store(in: &disposeBag) -// // dashboard -// account.publisher(for: \.statusesCount) -// .map { Int($0) } -// .assign(to: \.statusesCount, on: viewModel) -// .store(in: &disposeBag) -// account.publisher(for: \.followingCount) -// .map { Int($0) } -// .assign(to: \.followingCount, on: viewModel) -// .store(in: &disposeBag) -// account.publisher(for: \.followersCount) -// .map { Int($0) } -// .assign(to: \.followersCount, on: viewModel) -// .store(in: &disposeBag) + viewModel.headerImageURL = account.headerImageURL() + viewModel.avatarImageURL = account.avatarImageURL() + viewModel.emojiMeta = account.emojiMeta + viewModel.name = account.displayNameWithFallback + viewModel.acct = account.acctWithDomain + viewModel.note = account.note + viewModel.statusesCount = account.statusesCount + viewModel.followingCount = account.followingCount + viewModel.followersCount = account.followersCount } } diff --git a/Mastodon/Scene/Profile/ProfileViewController.swift b/Mastodon/Scene/Profile/ProfileViewController.swift index f9b6c7ec4..b43694483 100644 --- a/Mastodon/Scene/Profile/ProfileViewController.swift +++ b/Mastodon/Scene/Profile/ProfileViewController.swift @@ -122,7 +122,7 @@ final class ProfileViewController: UIViewController, NeedsDependency, MediaPrevi let viewController = ProfileHeaderViewController() viewController.context = context viewController.coordinator = coordinator - viewController.viewModel = ProfileHeaderViewModel(context: context, authContext: viewModel.authContext) + viewController.viewModel = ProfileHeaderViewModel(context: context, authContext: viewModel.authContext, account: viewModel.account) return viewController }() @@ -378,7 +378,7 @@ extension ProfileViewController { profileHeaderViewController.profileHeaderView.viewModel.viewDidAppear ) .sink { [weak self] (user, _) in - guard let self, let user else { return } + guard let self else { return } Task { _ = try await self.context.apiService.fetchUser( username: user.username, diff --git a/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Account.swift b/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Account.swift index 88f05e3fc..2f5b40acd 100644 --- a/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Account.swift +++ b/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Account.swift @@ -216,6 +216,11 @@ extension Mastodon.Entity.Account { return components.host } + public func headerImageURL() -> URL? { + let string = UserDefaults.shared.preferredStaticAvatar ? headerStatic ?? header : header + return URL(string: string) + } + public func avatarImageURL() -> URL? { let string = UserDefaults.shared.preferredStaticAvatar ? avatarStatic ?? avatar : avatar return URL(string: string)