2023-10-04 10:03:06 +02:00
|
|
|
// Copyright © 2023 Mastodon gGmbH. All rights reserved.
|
|
|
|
|
|
|
|
import UIKit
|
2023-10-04 17:37:33 +02:00
|
|
|
import MastodonSDK
|
2023-10-04 10:03:06 +02:00
|
|
|
|
|
|
|
protocol InstanceRulesViewControllerDelegate: AnyObject {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class InstanceRulesViewController: UIViewController {
|
2023-10-04 18:20:42 +02:00
|
|
|
|
2023-10-04 10:03:06 +02:00
|
|
|
weak var delegate: InstanceRulesViewControllerDelegate?
|
2023-10-04 18:20:42 +02:00
|
|
|
let tableView: UITableView
|
|
|
|
var dataSource: UITableViewDiffableDataSource<ServerRuleSection, ServerRuleItem>?
|
|
|
|
|
|
|
|
var sections: [ServerRuleSection] = []
|
2023-10-04 10:03:06 +02:00
|
|
|
|
|
|
|
init() {
|
2023-10-04 18:20:42 +02:00
|
|
|
tableView = UITableView(frame: .zero, style: .insetGrouped)
|
|
|
|
tableView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
tableView.register(ServerRulesTableViewCell.self, forCellReuseIdentifier: ServerRulesTableViewCell.reuseIdentifier)
|
|
|
|
|
2023-10-04 10:03:06 +02:00
|
|
|
super.init(nibName: nil, bundle: nil)
|
2023-10-04 18:20:42 +02:00
|
|
|
view.addSubview(tableView)
|
2023-10-04 17:37:33 +02:00
|
|
|
|
2023-10-04 18:20:42 +02:00
|
|
|
let dataSource = ServerRuleSection.tableViewDiffableDataSource(tableView: tableView)
|
|
|
|
|
|
|
|
tableView.dataSource = dataSource
|
|
|
|
self.dataSource = dataSource
|
|
|
|
|
|
|
|
setupConstraints()
|
2023-10-04 10:03:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
|
2023-10-04 17:37:33 +02:00
|
|
|
|
2023-10-04 18:20:42 +02:00
|
|
|
private func setupConstraints() {
|
|
|
|
let constraints = [
|
|
|
|
tableView.topAnchor.constraint(equalTo: view.topAnchor),
|
|
|
|
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
|
|
view.trailingAnchor.constraint(equalTo: tableView.trailingAnchor),
|
|
|
|
view.bottomAnchor.constraint(equalTo: tableView.bottomAnchor),
|
|
|
|
]
|
|
|
|
|
|
|
|
NSLayoutConstraint.activate(constraints)
|
|
|
|
}
|
|
|
|
|
2023-10-04 17:37:33 +02:00
|
|
|
func update(with instance: Mastodon.Entity.V2.Instance) {
|
2023-10-04 18:20:42 +02:00
|
|
|
guard let dataSource, let rules = instance.rules, rules.isNotEmpty else { return }
|
|
|
|
|
|
|
|
var snapshot = NSDiffableDataSourceSnapshot<ServerRuleSection, ServerRuleItem>()
|
|
|
|
|
|
|
|
snapshot.appendSections([.rules])
|
|
|
|
let ruleItems = rules.enumerated().compactMap { index, rule in ServerRuleItem.rule(index: index, rule: rule) }
|
|
|
|
snapshot.appendItems(ruleItems, toSection: .rules)
|
|
|
|
|
|
|
|
dataSource.apply(snapshot)
|
2023-10-04 17:37:33 +02:00
|
|
|
}
|
2023-10-04 10:03:06 +02:00
|
|
|
}
|