2021-02-03 06:01:50 +01:00
|
|
|
//
|
|
|
|
// TimelineLoaderTableViewCell.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by sxiaojian on 2021/2/3.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
2022-01-27 14:23:39 +01:00
|
|
|
import MastodonAsset
|
2022-10-08 07:43:06 +02:00
|
|
|
import MastodonCore
|
2022-01-27 14:23:39 +01:00
|
|
|
import MastodonLocalization
|
2021-02-03 06:01:50 +01:00
|
|
|
|
2022-10-10 13:14:52 +02:00
|
|
|
open class TimelineLoaderTableViewCell: UITableViewCell {
|
2021-02-03 06:01:50 +01:00
|
|
|
|
2022-10-10 13:14:52 +02:00
|
|
|
public static let buttonHeight: CGFloat = 44
|
|
|
|
public static let buttonMargin: CGFloat = 12
|
|
|
|
public static let cellHeight: CGFloat = buttonHeight + 2 * buttonMargin
|
|
|
|
public static let labelFont = UIFontMetrics(forTextStyle: .body).scaledFont(for: .systemFont(ofSize: 17, weight: .medium))
|
2021-02-03 06:01:50 +01:00
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
2021-07-06 13:32:11 +02:00
|
|
|
|
|
|
|
private var _disposeBag = Set<AnyCancellable>()
|
2021-05-13 11:50:37 +02:00
|
|
|
|
2022-10-10 13:14:52 +02:00
|
|
|
public let stackView = UIStackView()
|
2021-05-13 11:50:37 +02:00
|
|
|
|
2022-10-10 13:14:52 +02:00
|
|
|
public let loadMoreButton: UIButton = {
|
2021-03-17 09:16:55 +01:00
|
|
|
let button = HighlightDimmableButton()
|
|
|
|
button.titleLabel?.font = TimelineLoaderTableViewCell.labelFont
|
2021-10-29 08:58:09 +02:00
|
|
|
button.setTitleColor(ThemeService.tintColor, for: .normal)
|
2021-03-17 09:16:55 +01:00
|
|
|
button.setTitle(L10n.Common.Controls.Timeline.Loader.loadMissingPosts, for: .normal)
|
|
|
|
button.setTitle("", for: .disabled)
|
2021-02-03 06:01:50 +01:00
|
|
|
return button
|
|
|
|
}()
|
|
|
|
|
2022-10-10 13:14:52 +02:00
|
|
|
public let loadMoreLabel: UILabel = {
|
2021-03-16 12:28:52 +01:00
|
|
|
let label = UILabel()
|
2021-03-17 09:16:55 +01:00
|
|
|
label.font = TimelineLoaderTableViewCell.labelFont
|
2021-03-16 12:28:52 +01:00
|
|
|
return label
|
|
|
|
}()
|
|
|
|
|
2022-10-10 13:14:52 +02:00
|
|
|
public let activityIndicatorView: UIActivityIndicatorView = {
|
2021-02-03 06:01:50 +01:00
|
|
|
let activityIndicatorView = UIActivityIndicatorView(style: .medium)
|
2021-03-17 09:16:55 +01:00
|
|
|
activityIndicatorView.tintColor = Asset.Colors.Label.secondary.color
|
2021-02-03 06:01:50 +01:00
|
|
|
activityIndicatorView.hidesWhenStopped = true
|
|
|
|
return activityIndicatorView
|
|
|
|
}()
|
|
|
|
|
2022-10-10 13:14:52 +02:00
|
|
|
public override func prepareForReuse() {
|
2021-02-03 06:01:50 +01:00
|
|
|
super.prepareForReuse()
|
|
|
|
disposeBag.removeAll()
|
|
|
|
}
|
|
|
|
|
2022-10-10 13:14:52 +02:00
|
|
|
public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
|
2021-02-03 06:01:50 +01:00
|
|
|
super.init(style: style, reuseIdentifier: reuseIdentifier)
|
|
|
|
_init()
|
|
|
|
}
|
|
|
|
|
2022-10-10 13:14:52 +02:00
|
|
|
public required init?(coder: NSCoder) {
|
2021-02-03 06:01:50 +01:00
|
|
|
super.init(coder: coder)
|
|
|
|
_init()
|
|
|
|
}
|
2021-03-17 09:16:55 +01:00
|
|
|
|
2022-10-10 13:14:52 +02:00
|
|
|
public func startAnimating() {
|
2021-03-16 12:28:52 +01:00
|
|
|
activityIndicatorView.startAnimating()
|
2021-03-17 09:16:55 +01:00
|
|
|
self.loadMoreButton.isEnabled = false
|
|
|
|
self.loadMoreLabel.textColor = Asset.Colors.Label.secondary.color
|
2021-03-16 12:28:52 +01:00
|
|
|
self.loadMoreLabel.text = L10n.Common.Controls.Timeline.Loader.loadingMissingPosts
|
|
|
|
}
|
|
|
|
|
2022-10-10 13:14:52 +02:00
|
|
|
public func stopAnimating() {
|
2021-03-16 12:28:52 +01:00
|
|
|
activityIndicatorView.stopAnimating()
|
2021-03-17 09:16:55 +01:00
|
|
|
self.loadMoreButton.isEnabled = true
|
2021-10-29 08:58:09 +02:00
|
|
|
self.loadMoreLabel.textColor = ThemeService.tintColor
|
2021-03-17 09:16:55 +01:00
|
|
|
self.loadMoreLabel.text = ""
|
2021-03-16 12:28:52 +01:00
|
|
|
}
|
2021-02-03 06:01:50 +01:00
|
|
|
|
2022-10-10 13:14:52 +02:00
|
|
|
open func _init() {
|
2021-02-03 06:01:50 +01:00
|
|
|
selectionStyle = .none
|
2021-04-14 13:45:09 +02:00
|
|
|
backgroundColor = .clear
|
2021-02-05 04:53:21 +01:00
|
|
|
|
2021-02-03 06:01:50 +01:00
|
|
|
loadMoreButton.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
contentView.addSubview(loadMoreButton)
|
|
|
|
NSLayoutConstraint.activate([
|
2021-04-13 13:46:42 +02:00
|
|
|
loadMoreButton.topAnchor.constraint(equalTo: contentView.topAnchor, constant: TimelineLoaderTableViewCell.buttonMargin),
|
2021-03-16 12:28:52 +01:00
|
|
|
loadMoreButton.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
|
|
|
|
contentView.trailingAnchor.constraint(equalTo: loadMoreButton.trailingAnchor),
|
2021-04-13 13:46:42 +02:00
|
|
|
contentView.bottomAnchor.constraint(equalTo: loadMoreButton.bottomAnchor, constant: TimelineLoaderTableViewCell.buttonMargin),
|
2021-03-17 09:16:55 +01:00
|
|
|
loadMoreButton.heightAnchor.constraint(equalToConstant: TimelineLoaderTableViewCell.buttonHeight).priority(.required - 1),
|
2021-03-16 12:28:52 +01:00
|
|
|
])
|
|
|
|
|
2021-07-08 11:07:53 +02:00
|
|
|
// use stack view to alignment content center
|
2021-03-17 09:16:55 +01:00
|
|
|
stackView.spacing = 4
|
|
|
|
stackView.axis = .horizontal
|
|
|
|
stackView.alignment = .center
|
|
|
|
stackView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
stackView.isUserInteractionEnabled = false
|
|
|
|
contentView.addSubview(stackView)
|
2022-11-17 17:45:27 +01:00
|
|
|
stackView.pinTo(to: loadMoreButton)
|
2021-03-17 09:16:55 +01:00
|
|
|
let leftPaddingView = UIView()
|
|
|
|
leftPaddingView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
stackView.addArrangedSubview(leftPaddingView)
|
|
|
|
stackView.addArrangedSubview(activityIndicatorView)
|
|
|
|
stackView.addArrangedSubview(loadMoreLabel)
|
|
|
|
let rightPaddingView = UIView()
|
|
|
|
rightPaddingView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
stackView.addArrangedSubview(rightPaddingView)
|
2021-02-03 06:01:50 +01:00
|
|
|
NSLayoutConstraint.activate([
|
2021-03-17 09:16:55 +01:00
|
|
|
leftPaddingView.widthAnchor.constraint(equalTo: rightPaddingView.widthAnchor, multiplier: 1.0),
|
2021-02-03 06:01:50 +01:00
|
|
|
])
|
|
|
|
|
2021-03-17 09:16:55 +01:00
|
|
|
// default set hidden and let subclass override it
|
|
|
|
loadMoreButton.isHidden = true
|
|
|
|
loadMoreLabel.isHidden = true
|
2021-02-03 06:01:50 +01:00
|
|
|
activityIndicatorView.isHidden = true
|
2021-07-06 13:32:11 +02:00
|
|
|
|
|
|
|
setupBackgroundColor(theme: ThemeService.shared.currentTheme.value)
|
|
|
|
ThemeService.shared.currentTheme
|
|
|
|
.receive(on: RunLoop.main)
|
|
|
|
.sink { [weak self] theme in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.setupBackgroundColor(theme: theme)
|
|
|
|
}
|
|
|
|
.store(in: &_disposeBag)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func setupBackgroundColor(theme: Theme) {
|
2021-07-08 11:07:53 +02:00
|
|
|
loadMoreButton.backgroundColor = theme.tableViewCellBackgroundColor
|
2021-02-03 06:01:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|