mastodon-app-ufficiale-ipho.../MastodonSDK/Sources/MastodonUI/View/TableViewCell/TimelineLoaderTableViewCell...

132 lines
5.0 KiB
Swift
Raw Normal View History

2021-02-03 06:01:50 +01:00
//
// TimelineLoaderTableViewCell.swift
// Mastodon
//
// Created by sxiaojian on 2021/2/3.
//
import UIKit
import Combine
import MastodonAsset
2022-10-08 07:43:06 +02:00
import MastodonCore
import MastodonLocalization
2021-02-03 06:01:50 +01:00
open class TimelineLoaderTableViewCell: UITableViewCell {
2021-02-03 06:01:50 +01: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>()
private var _disposeBag = Set<AnyCancellable>()
public let stackView = UIStackView()
public let loadMoreButton: UIButton = {
let button = HighlightDimmableButton()
button.titleLabel?.font = TimelineLoaderTableViewCell.labelFont
2021-10-29 08:58:09 +02:00
button.setTitleColor(ThemeService.tintColor, 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
}()
public let loadMoreLabel: UILabel = {
let label = UILabel()
label.font = TimelineLoaderTableViewCell.labelFont
return label
}()
public let activityIndicatorView: UIActivityIndicatorView = {
2021-02-03 06:01:50 +01:00
let activityIndicatorView = UIActivityIndicatorView(style: .medium)
activityIndicatorView.tintColor = Asset.Colors.Label.secondary.color
2021-02-03 06:01:50 +01:00
activityIndicatorView.hidesWhenStopped = true
return activityIndicatorView
}()
public override func prepareForReuse() {
2021-02-03 06:01:50 +01:00
super.prepareForReuse()
disposeBag.removeAll()
}
public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
2021-02-03 06:01:50 +01:00
super.init(style: style, reuseIdentifier: reuseIdentifier)
_init()
}
public required init?(coder: NSCoder) {
2021-02-03 06:01:50 +01:00
super.init(coder: coder)
_init()
}
public func startAnimating() {
activityIndicatorView.startAnimating()
self.loadMoreButton.isEnabled = false
self.loadMoreLabel.textColor = Asset.Colors.Label.secondary.color
self.loadMoreLabel.text = L10n.Common.Controls.Timeline.Loader.loadingMissingPosts
}
public func stopAnimating() {
activityIndicatorView.stopAnimating()
self.loadMoreButton.isEnabled = true
2021-10-29 08:58:09 +02:00
self.loadMoreLabel.textColor = ThemeService.tintColor
self.loadMoreLabel.text = ""
}
2021-02-03 06:01:50 +01:00
open func _init() {
2021-02-03 06:01:50 +01:00
selectionStyle = .none
backgroundColor = .clear
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),
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),
loadMoreButton.heightAnchor.constraint(equalToConstant: TimelineLoaderTableViewCell.buttonHeight).priority(.required - 1),
])
// use stack view to alignment content center
stackView.spacing = 4
stackView.axis = .horizontal
stackView.alignment = .center
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.isUserInteractionEnabled = false
contentView.addSubview(stackView)
stackView.pinTo(to: loadMoreButton)
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([
leftPaddingView.widthAnchor.constraint(equalTo: rightPaddingView.widthAnchor, multiplier: 1.0),
2021-02-03 06:01:50 +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
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) {
loadMoreButton.backgroundColor = theme.tableViewCellBackgroundColor
2021-02-03 06:01:50 +01:00
}
}