Use UserView etc. to present suggested people to follow (IOS-157)

This commit is contained in:
Nathan Mattes 2023-05-17 17:23:19 +02:00
parent 44f6fc9a5c
commit a30c77c783
4 changed files with 60 additions and 243 deletions

View File

@ -40,7 +40,6 @@ extension RecommendAccountSection {
cell.configure(user: user) cell.configure(user: user)
} }
cell.viewModel.userIdentifier = configuration.authContext.mastodonAuthenticationBox
cell.delegate = configuration.suggestionAccountTableViewCellDelegate cell.delegate = configuration.suggestionAccountTableViewCellDelegate
} }
return cell return cell

View File

@ -31,7 +31,6 @@ class SuggestionAccountViewController: UIViewController, NeedsDependency {
}() }()
//TODO: Add "follow all"-footer-cell //TODO: Add "follow all"-footer-cell
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
@ -106,7 +105,6 @@ extension SuggestionAccountViewController: SuggestionAccountTableViewCellDelegat
switch item { switch item {
case .account(let user): case .account(let user):
Task { @MainActor in Task { @MainActor in
cell.startAnimating()
do { do {
try await DataSourceFacade.responseToUserFollowAction( try await DataSourceFacade.responseToUserFollowAction(
dependency: self, dependency: self,
@ -115,8 +113,7 @@ extension SuggestionAccountViewController: SuggestionAccountTableViewCellDelegat
} catch { } catch {
// do noting // do noting
} }
cell.stopAnimating() }
} // end Task
} }
} }
} }

View File

@ -17,119 +17,21 @@ import Meta
extension SuggestionAccountTableViewCell { extension SuggestionAccountTableViewCell {
func configure(user: MastodonUser) { func configure(user: MastodonUser) {
// author avatar //TODO: Set Delegate
Publishers.CombineLatest( userView.configure(user: user, delegate: nil)
user.publisher(for: \.avatar), //TODO: Fix Button State
UserDefaults.shared.publisher(for: \.preferredStaticAvatar) userView.setButtonState(.follow)
)
.map { _ in user.avatarImageURL() }
.assign(to: \.avatarImageURL, on: viewModel)
.store(in: &disposeBag)
// author name
Publishers.CombineLatest(
user.publisher(for: \.displayName),
user.publisher(for: \.emojis)
)
.map { _, emojis in
do {
let content = MastodonContent(content: user.displayNameWithFallback, emojis: emojis.asDictionary)
let metaContent = try MastodonMetaContent.convert(document: content)
return metaContent
} catch {
assertionFailure(error.localizedDescription)
return PlaintextMetaContent(string: user.displayNameWithFallback)
}
}
.assign(to: \.authorName, on: viewModel)
.store(in: &disposeBag)
// author username
user.publisher(for: \.acct)
.map { $0 as String? }
.assign(to: \.authorUsername, on: viewModel)
.store(in: &disposeBag)
// isFollowing
Publishers.CombineLatest(
viewModel.$userIdentifier,
user.publisher(for: \.followingBy)
)
.map { userIdentifier, followingBy in
guard let userIdentifier = userIdentifier else { return false }
return followingBy.contains(where: {
$0.id == userIdentifier.userID && $0.domain == userIdentifier.domain
})
}
.assign(to: \.isFollowing, on: viewModel)
.store(in: &disposeBag)
// isPending
Publishers.CombineLatest(
viewModel.$userIdentifier,
user.publisher(for: \.followRequestedBy)
)
.map { userIdentifier, followRequestedBy in
guard let userIdentifier = userIdentifier else { return false }
return followRequestedBy.contains(where: {
$0.id == userIdentifier.userID && $0.domain == userIdentifier.domain
})
}
.assign(to: \.isPending, on: viewModel)
.store(in: &disposeBag)
}
class ViewModel { let metaContent: MetaContent = {
var disposeBag = Set<AnyCancellable>() do {
let mastodonContent = MastodonContent(content: user.note ?? "", emojis: [:])
@Published public var userIdentifier: UserIdentifier? // me return try MastodonMetaContent.convert(document: mastodonContent)
} catch {
@Published var avatarImageURL: URL? assertionFailure()
@Published public var authorName: MetaContent? return PlaintextMetaContent(string: user.note ?? "")
@Published public var authorUsername: String?
@Published var isFollowing = false
@Published var isPending = false
func prepareForReuse() {
isFollowing = false
isPending = false
}
func bind(cell: SuggestionAccountTableViewCell) {
// avatar
$avatarImageURL.removeDuplicates()
.sink { url in
let configuration = AvatarImageView.Configuration(url: url)
cell.avatarButton.avatarImageView.configure(configuration: configuration)
cell.avatarButton.avatarImageView.configure(cornerConfiguration: .init(corner: .fixed(radius: 12)))
}
.store(in: &disposeBag)
// name
$authorName
.sink { metaContent in
let metaContent = metaContent ?? PlaintextMetaContent(string: " ")
cell.titleLabel.configure(content: metaContent)
}
.store(in: &disposeBag)
// username
$authorUsername
.map { text -> String in
guard let text = text else { return "" }
return "@\(text)"
}
.sink { username in
cell.subTitleLabel.text = username
}
.store(in: &disposeBag)
// button
Publishers.CombineLatest(
$isFollowing,
$isPending
)
.sink { isFollowing, isPending in
let isFollowState = isFollowing || isPending
let imageName = isFollowState ? "minus.circle.fill" : "plus.circle"
let image = UIImage(systemName: imageName, withConfiguration: UIImage.SymbolConfiguration(pointSize: 22, weight: .regular))
cell.button.setImage(image, for: .normal)
cell.button.tintColor = isFollowState ? Asset.Colors.danger.color : Asset.Colors.Label.secondary.color
} }
.store(in: &disposeBag) } ()
}
bioMetaLabel.configure(content: metaContent)
} }
} }

View File

@ -24,144 +24,63 @@ protocol SuggestionAccountTableViewCellDelegate: AnyObject {
final class SuggestionAccountTableViewCell: UITableViewCell { final class SuggestionAccountTableViewCell: UITableViewCell {
var disposeBag = Set<AnyCancellable>() var disposeBag = Set<AnyCancellable>()
weak var delegate: SuggestionAccountTableViewCellDelegate? weak var delegate: SuggestionAccountTableViewCellDelegate?
public private(set) lazy var viewModel: ViewModel = { let userView: UserView
let viewModel = ViewModel() let bioMetaLabel: MetaLabel
viewModel.bind(cell: self) private let contentStackView: UIStackView
return viewModel
}()
//TODO: Replace this with user view override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
let avatarButton = AvatarButton()
userView = UserView()
bioMetaLabel = MetaLabel()
bioMetaLabel.numberOfLines = 0
bioMetaLabel.textAttributes = [
.font: UIFontMetrics(forTextStyle: .body).scaledFont(for: .systemFont(ofSize: 15, weight: .regular)),
.foregroundColor: UIColor.secondaryLabel
]
bioMetaLabel.linkAttributes = [
.font: UIFontMetrics(forTextStyle: .body).scaledFont(for: .systemFont(ofSize: 15, weight: .semibold)),
.foregroundColor: Asset.Colors.brand.color
]
bioMetaLabel.isUserInteractionEnabled = false
contentStackView = UIStackView(arrangedSubviews: [userView, bioMetaLabel])
contentStackView.translatesAutoresizingMaskIntoConstraints = false
contentStackView.alignment = .leading
contentStackView.axis = .vertical
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.addSubview(contentStackView)
backgroundColor = .systemBackground
setupConstraints()
}
let titleLabel = MetaLabel(style: .statusName) required init?(coder: NSCoder) { fatalError("We don't support ancient technology like Storyboards") }
let subTitleLabel: UILabel = { private func setupConstraints() {
let label = UILabel() let constraints = [
label.textColor = Asset.Colors.Label.secondary.color contentStackView.topAnchor.constraint(equalTo: contentView.topAnchor),
label.font = .preferredFont(forTextStyle: .body) contentStackView.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor),
return label contentView.layoutMarginsGuide.trailingAnchor.constraint(equalTo: contentStackView.trailingAnchor),
}() contentView.bottomAnchor.constraint(equalTo: contentStackView.bottomAnchor, constant: 16),
]
let buttonContainer: UIView = {
let view = UIView() NSLayoutConstraint.activate(constraints)
return view }
}()
let button: HighlightDimmableButton = {
let button = HighlightDimmableButton(type: .custom)
let image = UIImage(systemName: "plus.circle", withConfiguration: UIImage.SymbolConfiguration(pointSize: 22, weight: .regular))
button.setImage(image, for: .normal)
return button
}()
let activityIndicatorView: UIActivityIndicatorView = {
let activityIndicatorView = UIActivityIndicatorView(style: .medium)
activityIndicatorView.hidesWhenStopped = true
return activityIndicatorView
}()
override func prepareForReuse() { override func prepareForReuse() {
super.prepareForReuse() super.prepareForReuse()
disposeBag.removeAll() disposeBag.removeAll()
avatarButton.avatarImageView.prepareForReuse()
viewModel.prepareForReuse()
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
configure()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
configure()
} }
} //MARK: - Action
extension SuggestionAccountTableViewCell {
private func configure() {
backgroundColor = .systemBackground
let containerStackView = UIStackView()
containerStackView.axis = .horizontal
containerStackView.distribution = .fill
containerStackView.spacing = 12
containerStackView.layoutMargins = UIEdgeInsets(top: 12, left: 21, bottom: 12, right: 12)
containerStackView.isLayoutMarginsRelativeArrangement = true
containerStackView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(containerStackView)
containerStackView.pinToParent()
avatarButton.translatesAutoresizingMaskIntoConstraints = false
containerStackView.addArrangedSubview(avatarButton)
NSLayoutConstraint.activate([
avatarButton.widthAnchor.constraint(equalToConstant: 42).priority(.required - 1),
avatarButton.heightAnchor.constraint(equalToConstant: 42).priority(.required - 1),
])
let textStackView = UIStackView()
textStackView.axis = .vertical
textStackView.distribution = .fill
textStackView.alignment = .leading
textStackView.translatesAutoresizingMaskIntoConstraints = false
titleLabel.translatesAutoresizingMaskIntoConstraints = false
textStackView.addArrangedSubview(titleLabel)
subTitleLabel.translatesAutoresizingMaskIntoConstraints = false
textStackView.addArrangedSubview(subTitleLabel)
subTitleLabel.setContentHuggingPriority(.defaultLow - 1, for: .vertical)
containerStackView.addArrangedSubview(textStackView)
textStackView.setContentHuggingPriority(.defaultLow - 1, for: .horizontal)
buttonContainer.translatesAutoresizingMaskIntoConstraints = false
containerStackView.addArrangedSubview(buttonContainer)
NSLayoutConstraint.activate([
buttonContainer.widthAnchor.constraint(equalToConstant: 24).priority(.required - 1),
buttonContainer.heightAnchor.constraint(equalToConstant: 42).priority(.required - 1),
])
buttonContainer.setContentHuggingPriority(.required - 1, for: .horizontal)
activityIndicatorView.translatesAutoresizingMaskIntoConstraints = false
button.translatesAutoresizingMaskIntoConstraints = false
activityIndicatorView.translatesAutoresizingMaskIntoConstraints = false
buttonContainer.addSubview(button)
buttonContainer.addSubview(activityIndicatorView)
NSLayoutConstraint.activate([
buttonContainer.centerXAnchor.constraint(equalTo: activityIndicatorView.centerXAnchor),
buttonContainer.centerYAnchor.constraint(equalTo: activityIndicatorView.centerYAnchor),
buttonContainer.centerXAnchor.constraint(equalTo: button.centerXAnchor),
buttonContainer.centerYAnchor.constraint(equalTo: button.centerYAnchor),
])
button.addTarget(self, action: #selector(SuggestionAccountTableViewCell.buttonDidPressed(_:)), for: .touchUpInside)
}
}
extension SuggestionAccountTableViewCell {
@objc private func buttonDidPressed(_ sender: UIButton) { @objc private func buttonDidPressed(_ sender: UIButton) {
delegate?.suggestionAccountTableViewCell(self, friendshipDidPressed: sender) delegate?.suggestionAccountTableViewCell(self, friendshipDidPressed: sender)
} }
} }
extension SuggestionAccountTableViewCell {
func startAnimating() {
activityIndicatorView.isHidden = false
activityIndicatorView.startAnimating()
button.isHidden = true
}
func stopAnimating() {
activityIndicatorView.stopAnimating()
activityIndicatorView.isHidden = true
button.isHidden = false
}
}