2021-03-05 15:50:20 +01:00
|
|
|
//
|
|
|
|
// PickServerSection.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by Cirno MainasuK on 2021/3/5.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import MastodonSDK
|
|
|
|
import Kanna
|
2021-03-06 05:55:52 +01:00
|
|
|
import AlamofireImage
|
2021-03-05 15:50:20 +01:00
|
|
|
|
|
|
|
enum PickServerSection: Equatable, Hashable {
|
|
|
|
case header
|
|
|
|
case servers
|
|
|
|
}
|
|
|
|
|
|
|
|
extension PickServerSection {
|
|
|
|
static func tableViewDiffableDataSource(
|
|
|
|
for tableView: UITableView,
|
|
|
|
dependency: NeedsDependency,
|
|
|
|
pickServerCellDelegate: PickServerCellDelegate
|
|
|
|
) -> UITableViewDiffableDataSource<PickServerSection, PickServerItem> {
|
2022-02-08 05:36:06 +01:00
|
|
|
tableView.register(OnboardingHeadlineTableViewCell.self, forCellReuseIdentifier: String(describing: OnboardingHeadlineTableViewCell.self))
|
|
|
|
tableView.register(PickServerCell.self, forCellReuseIdentifier: String(describing: PickServerCell.self))
|
|
|
|
tableView.register(PickServerLoaderTableViewCell.self, forCellReuseIdentifier: String(describing: PickServerLoaderTableViewCell.self))
|
|
|
|
|
|
|
|
return UITableViewDiffableDataSource(tableView: tableView) { [
|
2021-04-09 05:05:10 +02:00
|
|
|
weak dependency,
|
|
|
|
weak pickServerCellDelegate
|
|
|
|
] tableView, indexPath, item -> UITableViewCell? in
|
|
|
|
guard let dependency = dependency else { return nil }
|
2021-03-05 15:50:20 +01:00
|
|
|
switch item {
|
|
|
|
case .header:
|
2022-01-05 08:11:35 +01:00
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: OnboardingHeadlineTableViewCell.self), for: indexPath) as! OnboardingHeadlineTableViewCell
|
2021-03-05 15:50:20 +01:00
|
|
|
return cell
|
|
|
|
case .server(let server, let attribute):
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: PickServerCell.self), for: indexPath) as! PickServerCell
|
|
|
|
PickServerSection.configure(cell: cell, server: server, attribute: attribute)
|
|
|
|
cell.delegate = pickServerCellDelegate
|
|
|
|
return cell
|
2021-05-13 11:50:37 +02:00
|
|
|
case .loader(let attribute):
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: PickServerLoaderTableViewCell.self), for: indexPath) as! PickServerLoaderTableViewCell
|
|
|
|
PickServerSection.configure(cell: cell, attribute: attribute)
|
|
|
|
return cell
|
2021-03-05 15:50:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension PickServerSection {
|
|
|
|
|
|
|
|
static func configure(cell: PickServerCell, server: Mastodon.Entity.Server, attribute: PickServerItem.ServerItemAttribute) {
|
|
|
|
cell.domainLabel.text = server.domain
|
2022-01-04 11:30:21 +01:00
|
|
|
cell.descriptionLabel.attributedText = {
|
|
|
|
let content: String = {
|
|
|
|
guard let html = try? HTML(html: server.description, encoding: .utf8) else {
|
|
|
|
return server.description
|
|
|
|
}
|
|
|
|
return html.text ?? server.description
|
|
|
|
}()
|
|
|
|
|
|
|
|
let paragraphStyle = NSMutableParagraphStyle()
|
|
|
|
paragraphStyle.lineHeightMultiple = 1.16
|
|
|
|
|
|
|
|
return NSAttributedString(
|
|
|
|
string: content,
|
|
|
|
attributes: [
|
|
|
|
.paragraphStyle: paragraphStyle
|
|
|
|
]
|
|
|
|
)
|
|
|
|
}()
|
|
|
|
cell.usersValueLabel.attributedText = {
|
|
|
|
let attributedString = NSMutableAttributedString()
|
|
|
|
let attachment = NSTextAttachment(image: UIImage(systemName: "person.2.fill")!)
|
|
|
|
let attachmentAttributedString = NSAttributedString(attachment: attachment)
|
|
|
|
attributedString.append(attachmentAttributedString)
|
|
|
|
attributedString.append(NSAttributedString(string: " "))
|
|
|
|
|
|
|
|
let paragraphStyle = NSMutableParagraphStyle()
|
|
|
|
paragraphStyle.lineHeightMultiple = 1.12
|
|
|
|
let valueAttributedString = NSAttributedString(
|
|
|
|
string: parseUsersCount(server.totalUsers),
|
|
|
|
attributes: [
|
|
|
|
.paragraphStyle: paragraphStyle
|
|
|
|
]
|
|
|
|
)
|
|
|
|
attributedString.append(valueAttributedString)
|
2021-03-05 15:50:20 +01:00
|
|
|
|
2022-01-04 11:30:21 +01:00
|
|
|
return attributedString
|
2021-03-05 15:50:20 +01:00
|
|
|
}()
|
2022-01-04 11:30:21 +01:00
|
|
|
cell.langValueLabel.attributedText = {
|
|
|
|
let attributedString = NSMutableAttributedString()
|
|
|
|
let attachment = NSTextAttachment(image: UIImage(systemName: "text.bubble.fill")!)
|
|
|
|
let attachmentAttributedString = NSAttributedString(attachment: attachment)
|
|
|
|
attributedString.append(attachmentAttributedString)
|
|
|
|
attributedString.append(NSAttributedString(string: " "))
|
|
|
|
|
|
|
|
let paragraphStyle = NSMutableParagraphStyle()
|
|
|
|
paragraphStyle.lineHeightMultiple = 1.12
|
|
|
|
let valueAttributedString = NSAttributedString(
|
|
|
|
string: server.language.uppercased(),
|
|
|
|
attributes: [
|
|
|
|
.paragraphStyle: paragraphStyle
|
|
|
|
]
|
|
|
|
)
|
|
|
|
attributedString.append(valueAttributedString)
|
|
|
|
|
|
|
|
return attributedString
|
|
|
|
}()
|
|
|
|
|
2021-05-19 11:40:09 +02:00
|
|
|
attribute.isLast
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak cell] isLast in
|
|
|
|
guard let cell = cell else { return }
|
|
|
|
if isLast {
|
|
|
|
cell.containerView.layer.maskedCorners = [
|
|
|
|
.layerMinXMaxYCorner,
|
|
|
|
.layerMaxXMaxYCorner
|
|
|
|
]
|
|
|
|
cell.containerView.layer.cornerCurve = .continuous
|
|
|
|
cell.containerView.layer.cornerRadius = MastodonPickServerAppearance.tableViewCornerRadius
|
|
|
|
cell.containerView.layer.masksToBounds = true
|
|
|
|
} else {
|
|
|
|
cell.containerView.layer.cornerRadius = 0
|
|
|
|
cell.containerView.layer.masksToBounds = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &cell.disposeBag)
|
2021-03-05 15:50:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static func parseUsersCount(_ usersCount: Int) -> String {
|
|
|
|
switch usersCount {
|
|
|
|
case 0..<1000:
|
|
|
|
return "\(usersCount)"
|
|
|
|
default:
|
|
|
|
let usersCountInThousand = Float(usersCount) / 1000.0
|
|
|
|
return String(format: "%.1fK", usersCountInThousand)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-05-13 11:50:37 +02:00
|
|
|
|
|
|
|
extension PickServerSection {
|
|
|
|
|
|
|
|
static func configure(cell: PickServerLoaderTableViewCell, attribute: PickServerItem.LoaderItemAttribute) {
|
|
|
|
if attribute.isLast {
|
|
|
|
cell.containerView.layer.maskedCorners = [
|
|
|
|
.layerMinXMaxYCorner,
|
|
|
|
.layerMaxXMaxYCorner
|
|
|
|
]
|
|
|
|
cell.containerView.layer.cornerCurve = .continuous
|
|
|
|
cell.containerView.layer.cornerRadius = MastodonPickServerAppearance.tableViewCornerRadius
|
2021-05-19 11:40:09 +02:00
|
|
|
cell.containerView.layer.masksToBounds = true
|
2021-05-13 11:50:37 +02:00
|
|
|
} else {
|
|
|
|
cell.containerView.layer.cornerRadius = 0
|
2021-05-19 11:40:09 +02:00
|
|
|
cell.containerView.layer.masksToBounds = false
|
2021-05-13 11:50:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
attribute.isNoResult ? cell.stopAnimating() : cell.startAnimating()
|
|
|
|
cell.emptyStatusLabel.isHidden = !attribute.isNoResult
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|