2021-02-03 06:01:50 +01:00
|
|
|
//
|
|
|
|
// TimelineLoaderTableViewCell.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by sxiaojian on 2021/2/3.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
|
|
|
|
class TimelineLoaderTableViewCell: UITableViewCell {
|
|
|
|
|
2021-03-16 12:28:52 +01:00
|
|
|
static let buttonHeight: CGFloat = 62
|
|
|
|
static let cellHeight: CGFloat = TimelineLoaderTableViewCell.buttonHeight + 17
|
|
|
|
static let extraTopPadding: CGFloat = 10
|
2021-03-17 09:16:55 +01:00
|
|
|
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-03-16 12:28:52 +01:00
|
|
|
var stateBindDispose: AnyCancellable?
|
|
|
|
|
2021-02-03 06:01:50 +01:00
|
|
|
let loadMoreButton: UIButton = {
|
2021-03-17 09:16:55 +01:00
|
|
|
let button = HighlightDimmableButton()
|
|
|
|
button.titleLabel?.font = TimelineLoaderTableViewCell.labelFont
|
|
|
|
button.backgroundColor = Asset.Colors.Background.secondaryGroupedSystemBackground.color
|
|
|
|
button.setTitleColor(Asset.Colors.Button.normal.color, for: .normal)
|
|
|
|
button.setTitle(L10n.Common.Controls.Timeline.Loader.loadMissingPosts, for: .normal)
|
|
|
|
button.setTitle("", for: .disabled)
|
2021-02-03 06:01:50 +01:00
|
|
|
return button
|
|
|
|
}()
|
|
|
|
|
2021-03-17 09:16:55 +01:00
|
|
|
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
|
|
|
|
}()
|
|
|
|
|
2021-03-17 09:16:55 +01:00
|
|
|
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
|
|
|
|
}()
|
|
|
|
|
|
|
|
override func prepareForReuse() {
|
|
|
|
super.prepareForReuse()
|
|
|
|
disposeBag.removeAll()
|
|
|
|
}
|
|
|
|
|
|
|
|
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
|
|
|
|
super.init(style: style, reuseIdentifier: reuseIdentifier)
|
|
|
|
_init()
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
super.init(coder: coder)
|
|
|
|
_init()
|
|
|
|
}
|
2021-03-17 09:16:55 +01:00
|
|
|
|
2021-03-16 12:28:52 +01:00
|
|
|
func startAnimating() {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func stopAnimating() {
|
|
|
|
activityIndicatorView.stopAnimating()
|
2021-03-17 09:16:55 +01:00
|
|
|
self.loadMoreButton.isEnabled = true
|
2021-03-16 12:28:52 +01:00
|
|
|
self.loadMoreLabel.textColor = Asset.Colors.buttonDefault.color
|
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
|
|
|
|
|
|
|
func _init() {
|
|
|
|
selectionStyle = .none
|
2021-03-17 09:16:55 +01:00
|
|
|
backgroundColor = Asset.Colors.Background.systemGroupedBackground.color
|
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-03-16 12:28:52 +01:00
|
|
|
loadMoreButton.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 7),
|
|
|
|
loadMoreButton.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
|
|
|
|
contentView.trailingAnchor.constraint(equalTo: loadMoreButton.trailingAnchor),
|
|
|
|
contentView.bottomAnchor.constraint(equalTo: loadMoreButton.bottomAnchor, constant: 14),
|
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-03-17 09:16:55 +01:00
|
|
|
// use stack view to alignlment content center
|
|
|
|
let stackView = UIStackView()
|
|
|
|
stackView.spacing = 4
|
|
|
|
stackView.axis = .horizontal
|
|
|
|
stackView.alignment = .center
|
|
|
|
stackView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
stackView.isUserInteractionEnabled = false
|
|
|
|
contentView.addSubview(stackView)
|
2021-03-16 12:28:52 +01:00
|
|
|
NSLayoutConstraint.activate([
|
2021-03-17 09:16:55 +01:00
|
|
|
stackView.topAnchor.constraint(equalTo: loadMoreButton.topAnchor),
|
|
|
|
stackView.leadingAnchor.constraint(equalTo: loadMoreButton.leadingAnchor),
|
|
|
|
stackView.trailingAnchor.constraint(equalTo: loadMoreButton.trailingAnchor),
|
|
|
|
stackView.bottomAnchor.constraint(equalTo: loadMoreButton.bottomAnchor),
|
2021-02-03 06:01:50 +01:00
|
|
|
])
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|