// // SPPlayHistoryCell.swift // Thimra // // Created by Overseas on 2025/4/23. // import UIKit class SPPlayHistoryCell: SPCollectionViewCell { var model: SPShortModel? { didSet { coverImageView.sp_setImage(url: model?.image_url) titleLabel.text = model?.name let episode = String(format: "EP.%@".localized, model?.current_episode ?? "") let totalEpisode = "/\(model?.episode_total ?? 0)" let range = NSRange(location: episode.length(), length: totalEpisode.length()) let episodeString = NSMutableAttributedString(string: episode + totalEpisode) episodeString.color = .colorEC3324() episodeString.setColor(.color6D7A8F(), range: range) episodeLabel.attributedText = episodeString collectButton.isSelected = model?.is_collect ?? false } } private lazy var coverImageView: SPImageView = { let imageView = SPImageView() imageView.layer.cornerRadius = 6 imageView.layer.masksToBounds = true imageView.layer.borderColor = UIColor.colorFFFFFF(alpha: 0.15).cgColor imageView.layer.borderWidth = 1 imageView.isUserInteractionEnabled = true return imageView }() private lazy var titleLabel: UILabel = { let label = UILabel() label.font = .fontMedium(ofSize: 16) label.textColor = .colorFFFFFF() label.numberOfLines = 2 return label }() private lazy var episodeLabel: UILabel = { let label = UILabel() label.font = .fontMedium(ofSize: 12) return label }() private lazy var collectButton: UIButton = { let button = UIButton(type: .custom) button.setImage(UIImage(named: "collect_icon_02"), for: .normal) button.setImage(UIImage(named: "collect_icon_02_selected"), for: .selected) button.setImage(UIImage(named: "collect_icon_02_selected"), for: [.selected, .highlighted]) button.addTarget(self, action: #selector(handleCollectButton), for: .touchUpInside) return button }() override init(frame: CGRect) { super.init(frame: frame) NotificationCenter.default.addObserver(self, selector: #selector(updateShortCollectStateNotification), name: SPVideoAPI.updateShortCollectStateNotification, object: nil) _setupUI() } @MainActor required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } @objc private func handleCollectButton() { let isCollect = !(self.model?.is_collect ?? false) guard let shortPlayId = self.model?.short_play_id else { return } SPVideoAPI.requestCollectShort(isCollect: isCollect, shortPlayId: shortPlayId, videoId: nil) { } } @objc private func updateShortCollectStateNotification(sender: Notification) { guard let userInfo = sender.userInfo else { return } guard let shortPlayId = userInfo["id"] as? String else { return } guard let isCollect = userInfo["state"] as? Bool else { return } guard shortPlayId == self.model?.short_play_id else { return } self.model?.is_collect = isCollect collectButton.isSelected = isCollect } } extension SPPlayHistoryCell { private func _setupUI() { contentView.addSubview(coverImageView) contentView.addSubview(titleLabel) contentView.addSubview(episodeLabel) contentView.addSubview(collectButton) coverImageView.snp.makeConstraints { make in make.left.top.bottom.equalToSuperview() make.width.equalTo(109) } titleLabel.snp.makeConstraints { make in make.left.equalTo(coverImageView.snp.right).offset(14) make.top.equalToSuperview().offset(3) make.right.lessThanOrEqualToSuperview().offset(-40) } episodeLabel.snp.makeConstraints { make in make.left.equalTo(titleLabel) make.top.equalTo(titleLabel.snp.bottom).offset(12) } collectButton.snp.makeConstraints { make in make.right.equalToSuperview() make.top.equalToSuperview().offset(3) } } }