2021-04-21 08:46:31 +02:00
|
|
|
//
|
|
|
|
// SuggestionAccountTableViewCell.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by sxiaojian on 2021/4/21.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Combine
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
import Foundation
|
|
|
|
import MastodonSDK
|
|
|
|
import UIKit
|
2021-07-23 13:10:27 +02:00
|
|
|
import MetaTextKit
|
|
|
|
import MastodonMeta
|
2022-01-27 14:23:39 +01:00
|
|
|
import MastodonAsset
|
|
|
|
import MastodonLocalization
|
2022-02-16 10:25:55 +01:00
|
|
|
import MastodonUI
|
2023-05-22 11:41:42 +02:00
|
|
|
import MastodonCore
|
2021-04-21 08:46:31 +02:00
|
|
|
|
2023-05-22 11:41:42 +02:00
|
|
|
protocol SuggestionAccountTableViewCellDelegate: AnyObject, UserViewDelegate {}
|
2021-04-21 08:46:31 +02:00
|
|
|
|
|
|
|
final class SuggestionAccountTableViewCell: UITableViewCell {
|
2023-05-12 22:06:44 +02:00
|
|
|
|
2023-05-19 14:51:22 +02:00
|
|
|
static let reuseIdentifier = "SuggestionAccountTableViewCell"
|
|
|
|
|
2021-04-21 08:46:31 +02:00
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
weak var delegate: SuggestionAccountTableViewCellDelegate?
|
|
|
|
|
2023-05-17 17:23:19 +02:00
|
|
|
let userView: UserView
|
|
|
|
let bioMetaLabel: MetaLabel
|
|
|
|
private let contentStackView: UIStackView
|
2021-04-21 11:58:56 +02:00
|
|
|
|
2021-04-21 08:46:31 +02:00
|
|
|
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
|
2022-02-16 10:25:55 +01:00
|
|
|
|
2023-05-17 17:23:19 +02:00
|
|
|
userView = UserView()
|
|
|
|
bioMetaLabel = MetaLabel()
|
|
|
|
bioMetaLabel.numberOfLines = 0
|
|
|
|
bioMetaLabel.textAttributes = [
|
|
|
|
.font: UIFontMetrics(forTextStyle: .body).scaledFont(for: .systemFont(ofSize: 15, weight: .regular)),
|
2023-05-19 16:34:21 +02:00
|
|
|
.foregroundColor: UIColor.label
|
2023-05-17 17:23:19 +02:00
|
|
|
]
|
|
|
|
bioMetaLabel.linkAttributes = [
|
|
|
|
.font: UIFontMetrics(forTextStyle: .body).scaledFont(for: .systemFont(ofSize: 15, weight: .semibold)),
|
|
|
|
.foregroundColor: Asset.Colors.brand.color
|
|
|
|
]
|
|
|
|
bioMetaLabel.isUserInteractionEnabled = false
|
2021-04-21 08:46:31 +02:00
|
|
|
|
2023-05-17 17:23:19 +02:00
|
|
|
contentStackView = UIStackView(arrangedSubviews: [userView, bioMetaLabel])
|
|
|
|
contentStackView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
contentStackView.alignment = .leading
|
|
|
|
contentStackView.axis = .vertical
|
|
|
|
|
|
|
|
super.init(style: style, reuseIdentifier: reuseIdentifier)
|
|
|
|
|
|
|
|
contentView.addSubview(contentStackView)
|
2023-05-12 22:06:44 +02:00
|
|
|
|
|
|
|
backgroundColor = .systemBackground
|
2023-05-17 17:23:19 +02:00
|
|
|
|
|
|
|
setupConstraints()
|
2021-04-21 08:46:31 +02:00
|
|
|
}
|
|
|
|
|
2023-05-17 17:23:19 +02:00
|
|
|
required init?(coder: NSCoder) { fatalError("We don't support ancient technology like Storyboards") }
|
2022-02-16 10:25:55 +01:00
|
|
|
|
2023-05-17 17:23:19 +02:00
|
|
|
private func setupConstraints() {
|
|
|
|
let constraints = [
|
|
|
|
contentStackView.topAnchor.constraint(equalTo: contentView.topAnchor),
|
|
|
|
contentStackView.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor),
|
|
|
|
contentView.layoutMarginsGuide.trailingAnchor.constraint(equalTo: contentStackView.trailingAnchor),
|
|
|
|
contentView.bottomAnchor.constraint(equalTo: contentStackView.bottomAnchor, constant: 16),
|
|
|
|
]
|
|
|
|
|
|
|
|
NSLayoutConstraint.activate(constraints)
|
2021-04-22 13:58:42 +02:00
|
|
|
}
|
2022-02-16 10:25:55 +01:00
|
|
|
|
2023-05-17 17:23:19 +02:00
|
|
|
override func prepareForReuse() {
|
|
|
|
super.prepareForReuse()
|
|
|
|
|
|
|
|
disposeBag.removeAll()
|
2021-04-22 13:58:42 +02:00
|
|
|
}
|
|
|
|
|
2023-05-22 13:30:05 +02:00
|
|
|
func configure(viewModel: SuggestionAccountTableViewCell.ViewModel) {
|
|
|
|
userView.configure(user: viewModel.user, delegate: delegate)
|
|
|
|
|
2023-05-22 16:14:06 +02:00
|
|
|
if viewModel.blockedUsers.contains(viewModel.user.id) {
|
|
|
|
self.userView.setButtonState(.blocked)
|
|
|
|
} else if viewModel.followedUsers.contains(viewModel.user.id) {
|
|
|
|
self.userView.setButtonState(.unfollow)
|
|
|
|
} else if viewModel.followRequestedUsers.contains(viewModel.user.id) {
|
|
|
|
self.userView.setButtonState(.pending)
|
|
|
|
} else if viewModel.user.locked {
|
|
|
|
self.userView.setButtonState(.request)
|
|
|
|
} else {
|
|
|
|
self.userView.setButtonState(.follow)
|
2023-05-22 13:30:05 +02:00
|
|
|
}
|
2023-05-22 16:14:06 +02:00
|
|
|
|
2023-05-22 11:41:42 +02:00
|
|
|
let metaContent: MetaContent = {
|
|
|
|
do {
|
2023-05-22 14:29:50 +02:00
|
|
|
let mastodonContent = MastodonContent(content: viewModel.user.note ?? "", emojis: viewModel.user.emojis.asDictionary)
|
2023-05-22 11:41:42 +02:00
|
|
|
return try MastodonMetaContent.convert(document: mastodonContent)
|
|
|
|
} catch {
|
|
|
|
assertionFailure()
|
2023-05-22 13:30:05 +02:00
|
|
|
return PlaintextMetaContent(string: viewModel.user.note ?? "")
|
2023-05-22 11:41:42 +02:00
|
|
|
}
|
2023-05-22 13:30:05 +02:00
|
|
|
}()
|
2023-05-22 11:41:42 +02:00
|
|
|
|
|
|
|
bioMetaLabel.configure(content: metaContent)
|
2021-04-21 08:46:31 +02:00
|
|
|
}
|
|
|
|
}
|