From 1d966090032865362f1ec01442395889e26238ba Mon Sep 17 00:00:00 2001 From: CMK Date: Tue, 19 Apr 2022 20:57:21 +0800 Subject: [PATCH] fix: ProfileCardView layout initial setup failure issue --- .../xcschemes/xcschememanagement.plist | 6 +-- .../Discovery/DiscoverySection.swift | 7 +++- .../View/Content/ProfileCardView.swift | 39 ++++++++++++------- ...ofileCardTableViewCell+Configuration.swift | 30 ++++++++++++++ .../ProfileCardTableViewCell.swift | 10 +++-- 5 files changed, 71 insertions(+), 21 deletions(-) create mode 100644 MastodonSDK/Sources/MastodonUI/View/TableViewCell/ProfileCardTableViewCell+Configuration.swift diff --git a/Mastodon.xcodeproj/xcuserdata/mainasuk.xcuserdatad/xcschemes/xcschememanagement.plist b/Mastodon.xcodeproj/xcuserdata/mainasuk.xcuserdatad/xcschemes/xcschememanagement.plist index ae6e6040..55845d58 100644 --- a/Mastodon.xcodeproj/xcuserdata/mainasuk.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/Mastodon.xcodeproj/xcuserdata/mainasuk.xcuserdatad/xcschemes/xcschememanagement.plist @@ -109,7 +109,7 @@ MastodonIntent.xcscheme_^#shared#^_ orderHint - 33 + 23 MastodonIntents.xcscheme_^#shared#^_ @@ -124,12 +124,12 @@ NotificationService.xcscheme_^#shared#^_ orderHint - 32 + 22 ShareActionExtension.xcscheme_^#shared#^_ orderHint - 34 + 24 SuppressBuildableAutocreation diff --git a/Mastodon/Diffiable/Discovery/DiscoverySection.swift b/Mastodon/Diffiable/Discovery/DiscoverySection.swift index 76cc9c03..cab2eb82 100644 --- a/Mastodon/Diffiable/Discovery/DiscoverySection.swift +++ b/Mastodon/Diffiable/Discovery/DiscoverySection.swift @@ -52,13 +52,16 @@ extension DiscoverySection { let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: ProfileCardTableViewCell.self), for: indexPath) as! ProfileCardTableViewCell context.managedObjectContext.performAndWait { guard let user = record.object(in: context.managedObjectContext) else { return } - cell.profileCardView.configure(user: user) + cell.configure( + tableView: tableView, + user: user, + profileCardTableViewCellDelegate: configuration.profileCardTableViewCellDelegate + ) } context.authenticationService.activeMastodonAuthentication .map { $0?.user } .assign(to: \.me, on: cell.profileCardView.viewModel.relationshipViewModel) .store(in: &cell.disposeBag) - cell.delegate = configuration.profileCardTableViewCellDelegate return cell case .bottomLoader: let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: TimelineBottomLoaderTableViewCell.self), for: indexPath) as! TimelineBottomLoaderTableViewCell diff --git a/MastodonSDK/Sources/MastodonUI/View/Content/ProfileCardView.swift b/MastodonSDK/Sources/MastodonUI/View/Content/ProfileCardView.swift index 16351ebe..07f44150 100644 --- a/MastodonSDK/Sources/MastodonUI/View/Content/ProfileCardView.swift +++ b/MastodonSDK/Sources/MastodonUI/View/Content/ProfileCardView.swift @@ -46,6 +46,8 @@ public final class ProfileCardView: UIView { // author username public let authorUsernameLabel = MetaLabel(style: .profileCardUsername) + // bio + let bioMetaTextAdaptiveMarginContainerView = AdaptiveMarginContainerView() let bioMetaText: MetaText = { let metaText = MetaText() metaText.textView.backgroundColor = .clear @@ -78,6 +80,7 @@ public final class ProfileCardView: UIView { return metaText }() + let infoContainerAdaptiveMarginContainerView = AdaptiveMarginContainerView() let infoContainer = UIStackView() let statusDashboardView = ProfileStatusDashboardView() @@ -194,34 +197,36 @@ extension ProfileCardView { // authorInfoContainer: V - [ authorNameLabel | authorUsernameLabel ] let authorInfoContainer = UIStackView() authorInfoContainer.axis = .vertical - authorInfoContainer.spacing = 2 + // authorInfoContainer.spacing = 2 authorContainer.addArrangedSubview(authorInfoContainer) authorInfoContainer.addArrangedSubview(authorNameLabel) authorInfoContainer.addArrangedSubview(authorUsernameLabel) // bioMetaText - let bioMetaTextAdaptiveMarginContainerView = AdaptiveMarginContainerView() bioMetaTextAdaptiveMarginContainerView.contentView = bioMetaText.textView bioMetaTextAdaptiveMarginContainerView.margin = ProfileCardView.contentMargin bioMetaText.textView.setContentHuggingPriority(.required - 1, for: .vertical) bioMetaText.textView.setContentCompressionResistancePriority(.required - 1, for: .vertical) container.addArrangedSubview(bioMetaTextAdaptiveMarginContainerView) container.setCustomSpacing(16, after: bioMetaTextAdaptiveMarginContainerView) - + // infoContainer: H - [ statusDashboardView | (spacer) | relationshipActionButton ] infoContainer.axis = .horizontal infoContainer.spacing = 8 - let infoContainerAdaptiveMarginContainerView = AdaptiveMarginContainerView() infoContainerAdaptiveMarginContainerView.contentView = infoContainer infoContainerAdaptiveMarginContainerView.margin = ProfileCardView.contentMargin container.addArrangedSubview(infoContainerAdaptiveMarginContainerView) - infoContainer.addArrangedSubview(statusDashboardView) - infoContainer.addArrangedSubview(UIView()) - let relationshipActionButtonShadowContainer = ShadowBackgroundContainer() - infoContainer.addArrangedSubview(relationshipActionButtonShadowContainer) - updateInfoContainerLayout() + infoContainer.addArrangedSubview(statusDashboardView) + let infoContainerSpacer = UIView() + infoContainer.addArrangedSubview(UIView()) + infoContainerSpacer.setContentHuggingPriority(.defaultLow - 100, for: .vertical) + infoContainerSpacer.setContentHuggingPriority(.defaultLow - 100, for: .horizontal) + let relationshipActionButtonShadowContainer = ShadowBackgroundContainer() + relationshipActionButtonShadowContainer.translatesAutoresizingMaskIntoConstraints = false + infoContainer.addArrangedSubview(relationshipActionButtonShadowContainer) + relationshipActionButton.translatesAutoresizingMaskIntoConstraints = false relationshipActionButtonShadowContainer.addSubview(relationshipActionButton) NSLayoutConstraint.activate([ @@ -229,15 +234,15 @@ extension ProfileCardView { relationshipActionButton.leadingAnchor.constraint(equalTo: relationshipActionButtonShadowContainer.leadingAnchor), relationshipActionButton.trailingAnchor.constraint(equalTo: relationshipActionButtonShadowContainer.trailingAnchor), relationshipActionButton.bottomAnchor.constraint(equalTo: relationshipActionButtonShadowContainer.bottomAnchor), - relationshipActionButton.widthAnchor.constraint(greaterThanOrEqualToConstant: ProfileCardView.friendshipActionButtonSize.width).priority(.required - 1), - relationshipActionButton.heightAnchor.constraint(equalToConstant: ProfileCardView.friendshipActionButtonSize.height).priority(.required - 2), + relationshipActionButtonShadowContainer.widthAnchor.constraint(greaterThanOrEqualToConstant: ProfileCardView.friendshipActionButtonSize.width).priority(.required - 1), + relationshipActionButtonShadowContainer.heightAnchor.constraint(equalToConstant: ProfileCardView.friendshipActionButtonSize.height).priority(.required - 1), ]) - + let bottomPadding = UIView() bottomPadding.translatesAutoresizingMaskIntoConstraints = false container.addArrangedSubview(bottomPadding) NSLayoutConstraint.activate([ - bottomPadding.heightAnchor.constraint(equalToConstant: 16) + bottomPadding.heightAnchor.constraint(equalToConstant: 16).priority(.required - 10), ]) relationshipActionButton.addTarget(self, action: #selector(ProfileCardView.relationshipActionButtonDidPressed(_:)), for: .touchUpInside) @@ -257,6 +262,14 @@ extension ProfileCardView { } extension ProfileCardView { + public func setupLayoutFrame(_ rect: CGRect) { + frame.size.width = rect.width + bioMetaTextAdaptiveMarginContainerView.frame.size.width = frame.width + bioMetaTextAdaptiveMarginContainerView.contentView?.frame.size.width = frame.width - 2 * bioMetaTextAdaptiveMarginContainerView.margin + infoContainerAdaptiveMarginContainerView.frame.size.width = frame.width + infoContainerAdaptiveMarginContainerView.contentView?.frame.size.width = frame.width - 2 * infoContainerAdaptiveMarginContainerView.margin + } + private func updateInfoContainerLayout() { let isCompactAdaptive = bounds.width < 350 infoContainer.axis = isCompactAdaptive ? .vertical : .horizontal diff --git a/MastodonSDK/Sources/MastodonUI/View/TableViewCell/ProfileCardTableViewCell+Configuration.swift b/MastodonSDK/Sources/MastodonUI/View/TableViewCell/ProfileCardTableViewCell+Configuration.swift new file mode 100644 index 00000000..061af0f4 --- /dev/null +++ b/MastodonSDK/Sources/MastodonUI/View/TableViewCell/ProfileCardTableViewCell+Configuration.swift @@ -0,0 +1,30 @@ +// +// ProfileCardTableViewCell+Configuration.swift +// +// +// Created by MainasuK on 2022-4-19. +// + +import UIKit +import CoreDataStack + +extension ProfileCardTableViewCell { + + public func configure( + tableView: UITableView, + user: MastodonUser, + profileCardTableViewCellDelegate: ProfileCardTableViewCellDelegate? + ) { + if profileCardView.frame == .zero { + // set content view width + assert(layoutMarginsGuide.layoutFrame.width > .zero) + shadowBackgroundContainer.frame.size.width = layoutMarginsGuide.layoutFrame.width + profileCardView.setupLayoutFrame(layoutMarginsGuide.layoutFrame) + logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): did layout for new cell") + } + + profileCardView.configure(user: user) + delegate = profileCardTableViewCellDelegate + } + +} diff --git a/MastodonSDK/Sources/MastodonUI/View/TableViewCell/ProfileCardTableViewCell.swift b/MastodonSDK/Sources/MastodonUI/View/TableViewCell/ProfileCardTableViewCell.swift index 5961ad10..aff7b6fe 100644 --- a/MastodonSDK/Sources/MastodonUI/View/TableViewCell/ProfileCardTableViewCell.swift +++ b/MastodonSDK/Sources/MastodonUI/View/TableViewCell/ProfileCardTableViewCell.swift @@ -5,6 +5,7 @@ // Created by MainasuK on 2022-4-14. // +import os.log import UIKit import Combine @@ -14,9 +15,13 @@ public protocol ProfileCardTableViewCellDelegate: AnyObject { public final class ProfileCardTableViewCell: UITableViewCell { + let logger = Logger(subsystem: "ProfileCardTableViewCell", category: "Cell") + public weak var delegate: ProfileCardTableViewCellDelegate? public var disposeBag = Set() + public let shadowBackgroundContainer = ShadowBackgroundContainer() + public let profileCardView: ProfileCardView = { let profileCardView = ProfileCardView() profileCardView.layer.masksToBounds = true @@ -49,15 +54,14 @@ extension ProfileCardTableViewCell { private func _init() { selectionStyle = .none - let shadowBackgroundContainer = ShadowBackgroundContainer() shadowBackgroundContainer.cornerRadius = 6 shadowBackgroundContainer.translatesAutoresizingMaskIntoConstraints = false contentView.addSubview(shadowBackgroundContainer) NSLayoutConstraint.activate([ - shadowBackgroundContainer.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 10), + shadowBackgroundContainer.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 10).priority(.required - 1), shadowBackgroundContainer.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor), shadowBackgroundContainer.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor), - contentView.bottomAnchor.constraint(equalTo: shadowBackgroundContainer.bottomAnchor, constant: 10), + contentView.bottomAnchor.constraint(equalTo: shadowBackgroundContainer.bottomAnchor, constant: 10).priority(.required - 1), ]) profileCardView.translatesAutoresizingMaskIntoConstraints = false