mastodon-ios/Mastodon/Scene/SuggestionAccount/TableViewCell/SuggestionAccountTableViewC...

175 lines
6.5 KiB
Swift
Raw Normal View History

2021-04-21 08:46:31 +02:00
//
// SuggestionAccountTableViewCell.swift
// Mastodon
//
// Created by sxiaojian on 2021/4/21.
//
2022-02-16 10:25:55 +01:00
import os.log
2021-04-21 08:46:31 +02:00
import Combine
import CoreData
import CoreDataStack
import Foundation
import MastodonSDK
import UIKit
2021-07-23 13:10:27 +02:00
import MetaTextKit
import MastodonMeta
import MastodonAsset
import MastodonLocalization
2022-02-16 10:25:55 +01:00
import MastodonUI
2021-04-21 08:46:31 +02:00
protocol SuggestionAccountTableViewCellDelegate: AnyObject {
2022-02-16 10:25:55 +01:00
func suggestionAccountTableViewCell(_ cell: SuggestionAccountTableViewCell, friendshipDidPressed button: UIButton)
2021-04-21 08:46:31 +02:00
}
final class SuggestionAccountTableViewCell: UITableViewCell {
2022-02-16 10:25:55 +01:00
let logger = Logger(subsystem: "SuggestionAccountTableViewCell", category: "View")
2021-04-21 08:46:31 +02:00
var disposeBag = Set<AnyCancellable>()
2022-02-16 10:25:55 +01:00
2021-04-21 08:46:31 +02:00
weak var delegate: SuggestionAccountTableViewCellDelegate?
2022-02-16 10:25:55 +01:00
public private(set) lazy var viewModel: ViewModel = {
let viewModel = ViewModel()
viewModel.bind(cell: self)
return viewModel
2021-04-21 08:46:31 +02:00
}()
2022-02-16 10:25:55 +01:00
let avatarButton = AvatarButton()
2021-07-23 13:10:27 +02:00
let titleLabel = MetaLabel(style: .statusName)
2021-04-21 08:46:31 +02:00
let subTitleLabel: UILabel = {
let label = UILabel()
label.textColor = Asset.Colors.Label.secondary.color
label.font = .preferredFont(forTextStyle: .body)
return label
}()
let buttonContainer: UIView = {
let view = UIView()
view.backgroundColor = .clear
return view
}()
let button: HighlightDimmableButton = {
2021-04-21 08:46:31 +02:00
let button = HighlightDimmableButton(type: .custom)
2022-02-16 10:25:55 +01:00
let image = UIImage(systemName: "plus.circle", withConfiguration: UIImage.SymbolConfiguration(pointSize: 22, weight: .regular))
button.setImage(image, for: .normal)
2021-04-21 08:46:31 +02:00
return button
}()
let activityIndicatorView: UIActivityIndicatorView = {
let activityIndicatorView = UIActivityIndicatorView(style: .medium)
activityIndicatorView.hidesWhenStopped = true
return activityIndicatorView
}()
2021-04-21 11:58:56 +02:00
2021-04-21 08:46:31 +02:00
override func prepareForReuse() {
super.prepareForReuse()
2022-02-16 10:25:55 +01:00
2021-04-21 08:46:31 +02:00
disposeBag.removeAll()
2022-02-16 10:25:55 +01:00
avatarButton.avatarImageView.prepareForReuse()
viewModel.prepareForReuse()
2021-04-21 08:46:31 +02:00
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
configure()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
configure()
}
2022-02-16 10:25:55 +01:00
2021-04-21 08:46:31 +02:00
}
extension SuggestionAccountTableViewCell {
2022-02-16 10:25:55 +01:00
2021-04-21 08:46:31 +02:00
private func configure() {
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)
NSLayoutConstraint.activate([
containerStackView.topAnchor.constraint(equalTo: contentView.topAnchor),
containerStackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
containerStackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
containerStackView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
2021-04-21 08:46:31 +02:00
])
2022-02-16 10:25:55 +01:00
avatarButton.translatesAutoresizingMaskIntoConstraints = false
containerStackView.addArrangedSubview(avatarButton)
2021-04-21 08:46:31 +02:00
NSLayoutConstraint.activate([
2022-02-16 10:25:55 +01:00
avatarButton.widthAnchor.constraint(equalToConstant: 42).priority(.required - 1),
avatarButton.heightAnchor.constraint(equalToConstant: 42).priority(.required - 1),
2021-04-21 08:46:31 +02:00
])
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
2021-04-21 08:46:31 +02:00
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),
])
2022-02-16 10:25:55 +01:00
button.addTarget(self, action: #selector(SuggestionAccountTableViewCell.buttonDidPressed(_:)), for: .touchUpInside)
2021-04-21 08:46:31 +02:00
}
2022-02-16 10:25:55 +01:00
}
extension SuggestionAccountTableViewCell {
@objc private func buttonDidPressed(_ sender: UIButton) {
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public)")
delegate?.suggestionAccountTableViewCell(self, friendshipDidPressed: sender)
}
2022-02-16 10:25:55 +01:00
}
extension SuggestionAccountTableViewCell {
func startAnimating() {
activityIndicatorView.isHidden = false
activityIndicatorView.startAnimating()
2022-02-16 10:25:55 +01:00
button.isHidden = true
}
func stopAnimating() {
activityIndicatorView.stopAnimating()
activityIndicatorView.isHidden = true
2022-02-16 10:25:55 +01:00
button.isHidden = false
2021-04-21 08:46:31 +02:00
}
2022-02-16 10:25:55 +01:00
2021-04-21 08:46:31 +02:00
}