1
0
mirror of https://github.com/mastodon/mastodon-ios.git synced 2025-02-02 18:36:44 +01:00

120 lines
4.5 KiB
Swift
Raw Normal View History

2021-02-03 13:01:50 +08:00
//
// TimelineLoaderTableViewCell.swift
// Mastodon
//
// Created by sxiaojian on 2021/2/3.
//
import UIKit
import Combine
import MastodonAsset
2022-10-08 13:43:06 +08:00
import MastodonCore
import MastodonLocalization
2021-02-03 13:01:50 +08:00
open class TimelineLoaderTableViewCell: UITableViewCell {
2021-02-03 13:01:50 +08: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 13:01:50 +08: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
2023-09-27 15:08:12 +02:00
button.setTitleColor(SystemTheme.tintColor, for: .normal)
button.setTitle(L10n.Common.Controls.Timeline.Loader.loadMissingPosts, for: .normal)
button.setTitle("", for: .disabled)
2021-02-03 13:01:50 +08:00
return button
}()
public let loadMoreLabel: UILabel = {
let label = UILabel()
label.font = TimelineLoaderTableViewCell.labelFont
return label
}()
public let activityIndicatorView: UIActivityIndicatorView = {
2021-02-03 13:01:50 +08:00
let activityIndicatorView = UIActivityIndicatorView(style: .medium)
activityIndicatorView.tintColor = Asset.Colors.Label.secondary.color
2021-02-03 13:01:50 +08:00
activityIndicatorView.hidesWhenStopped = true
return activityIndicatorView
}()
public override func prepareForReuse() {
2021-02-03 13:01:50 +08:00
super.prepareForReuse()
disposeBag.removeAll()
}
public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
2021-02-03 13:01:50 +08:00
super.init(style: style, reuseIdentifier: reuseIdentifier)
_init()
}
public required init?(coder: NSCoder) {
2021-02-03 13:01:50 +08: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
2023-09-27 15:08:12 +02:00
self.loadMoreLabel.textColor = SystemTheme.tintColor
self.loadMoreLabel.text = ""
}
2021-02-03 13:01:50 +08:00
open func _init() {
2021-02-03 13:01:50 +08:00
selectionStyle = .none
backgroundColor = .clear
2021-02-03 13:01:50 +08:00
loadMoreButton.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(loadMoreButton)
NSLayoutConstraint.activate([
2021-04-13 19:46:42 +08: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 19:46:42 +08: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 13:01:50 +08:00
NSLayoutConstraint.activate([
leftPaddingView.widthAnchor.constraint(equalTo: rightPaddingView.widthAnchor, multiplier: 1.0),
2021-02-03 13:01:50 +08:00
])
// default set hidden and let subclass override it
loadMoreButton.isHidden = true
loadMoreLabel.isHidden = true
2021-02-03 13:01:50 +08:00
activityIndicatorView.isHidden = true
2023-09-27 15:08:12 +02:00
loadMoreButton.backgroundColor = SystemTheme.tableViewCellBackgroundColor
}
2021-02-03 13:01:50 +08:00
}