2021-04-02 10:24:00 +02:00
|
|
|
//
|
|
|
|
// SearchingTableViewCell.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by sxiaojian on 2021/4/2.
|
|
|
|
//
|
|
|
|
|
2021-04-07 13:49:33 +02:00
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
2021-04-02 10:24:00 +02:00
|
|
|
import Foundation
|
|
|
|
import MastodonSDK
|
2021-04-06 09:25:04 +02:00
|
|
|
import UIKit
|
2021-04-02 10:24:00 +02:00
|
|
|
|
|
|
|
final class SearchingTableViewCell: UITableViewCell {
|
|
|
|
let _imageView: UIImageView = {
|
|
|
|
let imageView = UIImageView()
|
2021-04-07 13:49:33 +02:00
|
|
|
imageView.tintColor = Asset.Colors.Label.primary.color
|
2021-04-12 06:49:01 +02:00
|
|
|
imageView.layer.cornerRadius = 4
|
|
|
|
imageView.clipsToBounds = true
|
2021-04-02 10:24:00 +02:00
|
|
|
return imageView
|
|
|
|
}()
|
|
|
|
|
|
|
|
let _titleLabel: UILabel = {
|
|
|
|
let label = UILabel()
|
2021-04-07 15:42:43 +02:00
|
|
|
label.textColor = Asset.Colors.brandBlue.color
|
2021-04-02 10:24:00 +02:00
|
|
|
label.font = .systemFont(ofSize: 17, weight: .semibold)
|
|
|
|
label.lineBreakMode = .byTruncatingTail
|
|
|
|
return label
|
|
|
|
}()
|
|
|
|
|
|
|
|
let _subTitleLabel: UILabel = {
|
|
|
|
let label = UILabel()
|
|
|
|
label.textColor = Asset.Colors.Label.secondary.color
|
|
|
|
label.font = .preferredFont(forTextStyle: .body)
|
|
|
|
return label
|
|
|
|
}()
|
|
|
|
|
|
|
|
override func prepareForReuse() {
|
|
|
|
super.prepareForReuse()
|
|
|
|
_imageView.af.cancelImageRequest()
|
|
|
|
_imageView.image = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
|
|
|
|
super.init(style: style, reuseIdentifier: reuseIdentifier)
|
|
|
|
configure()
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
super.init(coder: coder)
|
|
|
|
configure()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension SearchingTableViewCell {
|
|
|
|
private func configure() {
|
2021-04-07 13:49:33 +02:00
|
|
|
backgroundColor = .clear
|
2021-04-06 09:25:04 +02:00
|
|
|
selectionStyle = .none
|
2021-04-02 10:24:00 +02:00
|
|
|
contentView.addSubview(_imageView)
|
|
|
|
_imageView.pin(toSize: CGSize(width: 42, height: 42))
|
|
|
|
_imageView.constrain([
|
|
|
|
_imageView.constraint(.leading, toView: contentView, constant: 21),
|
|
|
|
_imageView.constraint(.centerY, toView: contentView)
|
|
|
|
])
|
|
|
|
|
|
|
|
contentView.addSubview(_titleLabel)
|
|
|
|
_titleLabel.pin(top: 12, left: 75, bottom: nil, right: 0)
|
|
|
|
|
|
|
|
contentView.addSubview(_subTitleLabel)
|
|
|
|
_subTitleLabel.pin(top: 34, left: 75, bottom: nil, right: 0)
|
|
|
|
}
|
|
|
|
|
2021-04-06 09:25:04 +02:00
|
|
|
func config(with account: Mastodon.Entity.Account) {
|
|
|
|
_imageView.af.setImage(
|
2021-04-02 10:24:00 +02:00
|
|
|
withURL: URL(string: account.avatar)!,
|
|
|
|
placeholderImage: UIImage.placeholder(color: .systemFill),
|
|
|
|
imageTransition: .crossDissolve(0.2)
|
|
|
|
)
|
2021-04-06 09:25:04 +02:00
|
|
|
_titleLabel.text = account.displayName.isEmpty ? account.username : account.displayName
|
|
|
|
_subTitleLabel.text = account.acct
|
2021-04-02 10:24:00 +02:00
|
|
|
}
|
|
|
|
|
2021-04-07 13:49:33 +02:00
|
|
|
func config(with account: MastodonUser) {
|
|
|
|
_imageView.af.setImage(
|
|
|
|
withURL: URL(string: account.avatar)!,
|
|
|
|
placeholderImage: UIImage.placeholder(color: .systemFill),
|
|
|
|
imageTransition: .crossDissolve(0.2)
|
|
|
|
)
|
|
|
|
_titleLabel.text = account.displayName.isEmpty ? account.username : account.displayName
|
|
|
|
_subTitleLabel.text = account.acct
|
|
|
|
}
|
|
|
|
|
2021-04-06 09:25:04 +02:00
|
|
|
func config(with tag: Mastodon.Entity.Tag) {
|
2021-04-02 10:24:00 +02:00
|
|
|
let image = UIImage(systemName: "number.circle.fill", withConfiguration: UIImage.SymbolConfiguration(pointSize: 34, weight: .regular))!.withRenderingMode(.alwaysTemplate)
|
2021-04-06 09:25:04 +02:00
|
|
|
_imageView.image = image
|
|
|
|
_titleLabel.text = "# " + tag.name
|
2021-04-02 10:24:00 +02:00
|
|
|
guard let historys = tag.history else {
|
2021-04-06 09:25:04 +02:00
|
|
|
_subTitleLabel.text = ""
|
2021-04-02 10:24:00 +02:00
|
|
|
return
|
|
|
|
}
|
2021-04-08 06:22:05 +02:00
|
|
|
let recentHistory = historys.prefix(2)
|
2021-04-06 09:25:04 +02:00
|
|
|
let peopleAreTalking = recentHistory.compactMap { Int($0.accounts) }.reduce(0, +)
|
2021-04-07 13:49:33 +02:00
|
|
|
let string = L10n.Scene.Search.Recommend.HashTag.peopleTalking(String(peopleAreTalking))
|
|
|
|
_subTitleLabel.text = string
|
|
|
|
}
|
|
|
|
|
|
|
|
func config(with tag: Tag) {
|
|
|
|
let image = UIImage(systemName: "number.circle.fill", withConfiguration: UIImage.SymbolConfiguration(pointSize: 34, weight: .regular))!.withRenderingMode(.alwaysTemplate)
|
|
|
|
_imageView.image = image
|
|
|
|
_titleLabel.text = "# " + tag.name
|
|
|
|
guard let historys = tag.histories?.sorted(by: {
|
|
|
|
$0.createAt.compare($1.createAt) == .orderedAscending
|
|
|
|
}) else {
|
|
|
|
_subTitleLabel.text = ""
|
|
|
|
return
|
|
|
|
}
|
2021-04-08 06:22:05 +02:00
|
|
|
let recentHistory = historys.prefix(2)
|
2021-04-07 13:49:33 +02:00
|
|
|
let peopleAreTalking = recentHistory.compactMap { Int($0.accounts) }.reduce(0, +)
|
2021-04-02 10:24:00 +02:00
|
|
|
let string = L10n.Scene.Search.Recommend.HashTag.peopleTalking(String(peopleAreTalking))
|
2021-04-06 09:25:04 +02:00
|
|
|
_subTitleLabel.text = string
|
2021-04-02 10:24:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if canImport(SwiftUI) && DEBUG
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct SearchingTableViewCell_Previews: PreviewProvider {
|
|
|
|
static var controls: some View {
|
|
|
|
Group {
|
|
|
|
UIViewPreview {
|
|
|
|
let cell = SearchingTableViewCell()
|
|
|
|
cell.backgroundColor = .white
|
|
|
|
cell._imageView.image = UIImage(systemName: "number.circle.fill")
|
|
|
|
cell._titleLabel.text = "Electronic Frontier Foundation"
|
|
|
|
cell._subTitleLabel.text = "@eff@mastodon.social"
|
|
|
|
return cell
|
|
|
|
}
|
|
|
|
.previewLayout(.fixed(width: 228, height: 130))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static var previews: some View {
|
|
|
|
Group {
|
|
|
|
controls.colorScheme(.light)
|
|
|
|
controls.colorScheme(.dark)
|
|
|
|
}
|
|
|
|
.background(Color.gray)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|