2021-05-18 08:25:32 +02:00
|
|
|
//
|
2022-10-10 13:14:52 +02:00
|
|
|
// AutoCompleteSection+Diffable.swift
|
|
|
|
//
|
2021-05-18 08:25:32 +02:00
|
|
|
//
|
2022-10-10 13:14:52 +02:00
|
|
|
// Created by MainasuK on 22/10/10.
|
2021-05-18 08:25:32 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2022-10-10 13:14:52 +02:00
|
|
|
import MastodonCore
|
2021-05-18 08:25:32 +02:00
|
|
|
import MastodonSDK
|
2022-01-27 14:23:39 +01:00
|
|
|
import MastodonLocalization
|
2022-10-10 13:14:52 +02:00
|
|
|
import MastodonMeta
|
2021-05-18 08:25:32 +02:00
|
|
|
|
|
|
|
extension AutoCompleteSection {
|
|
|
|
|
2022-10-10 13:14:52 +02:00
|
|
|
public static func tableViewDiffableDataSource(
|
|
|
|
tableView: UITableView
|
2021-05-18 08:25:32 +02:00
|
|
|
) -> UITableViewDiffableDataSource<AutoCompleteSection, AutoCompleteItem> {
|
|
|
|
UITableViewDiffableDataSource(tableView: tableView) { tableView, indexPath, item in
|
|
|
|
switch item {
|
|
|
|
case .hashtag(let hashtag):
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: AutoCompleteTableViewCell.self), for: indexPath) as! AutoCompleteTableViewCell
|
|
|
|
configureHashtag(cell: cell, hashtag: hashtag)
|
|
|
|
return cell
|
|
|
|
case .hashtagV1(let hashtagName):
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: AutoCompleteTableViewCell.self), for: indexPath) as! AutoCompleteTableViewCell
|
|
|
|
configureHashtag(cell: cell, hashtagName: hashtagName)
|
|
|
|
return cell
|
|
|
|
case .account(let account):
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: AutoCompleteTableViewCell.self), for: indexPath) as! AutoCompleteTableViewCell
|
|
|
|
configureAccount(cell: cell, account: account)
|
|
|
|
return cell
|
|
|
|
case .emoji(let emoji):
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: AutoCompleteTableViewCell.self), for: indexPath) as! AutoCompleteTableViewCell
|
2021-06-21 12:30:24 +02:00
|
|
|
configureEmoji(cell: cell, emoji: emoji, isFirst: indexPath.row == 0)
|
2021-05-18 08:25:32 +02:00
|
|
|
return cell
|
|
|
|
case .bottomLoader:
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: TimelineBottomLoaderTableViewCell.self), for: indexPath) as! TimelineBottomLoaderTableViewCell
|
2021-05-18 09:06:00 +02:00
|
|
|
cell.startAnimating()
|
2021-05-18 08:25:32 +02:00
|
|
|
return cell
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension AutoCompleteSection {
|
|
|
|
|
|
|
|
private static func configureHashtag(cell: AutoCompleteTableViewCell, hashtag: Mastodon.Entity.Tag) {
|
2021-08-04 09:47:29 +02:00
|
|
|
let metaContent = PlaintextMetaContent(string: "#" + hashtag.name)
|
|
|
|
cell.titleLabel.configure(content: metaContent)
|
2021-05-18 08:25:32 +02:00
|
|
|
cell.subtitleLabel.text = {
|
|
|
|
let count = (hashtag.history ?? [])
|
|
|
|
.sorted(by: { $0.day > $1.day })
|
|
|
|
.prefix(2)
|
|
|
|
.compactMap { Int($0.accounts) }
|
|
|
|
.reduce(0, +)
|
2021-07-06 08:55:24 +02:00
|
|
|
return L10n.Plural.peopleTalking(count)
|
2021-05-18 08:25:32 +02:00
|
|
|
}()
|
|
|
|
cell.avatarImageView.isHidden = true
|
|
|
|
}
|
|
|
|
|
|
|
|
private static func configureHashtag(cell: AutoCompleteTableViewCell, hashtagName: String) {
|
2021-08-04 09:47:29 +02:00
|
|
|
let metaContent = PlaintextMetaContent(string: "#" + hashtagName)
|
|
|
|
cell.titleLabel.configure(content: metaContent)
|
2021-05-18 08:25:32 +02:00
|
|
|
cell.subtitleLabel.text = " "
|
|
|
|
cell.avatarImageView.isHidden = true
|
|
|
|
}
|
|
|
|
|
|
|
|
private static func configureAccount(cell: AutoCompleteTableViewCell, account: Mastodon.Entity.Account) {
|
2021-08-04 09:47:29 +02:00
|
|
|
let mastodonContent = MastodonContent(content: account.displayNameWithFallback, emojis: account.emojiMeta)
|
|
|
|
do {
|
|
|
|
let metaContent = try MastodonMetaContent.convert(document: mastodonContent)
|
|
|
|
cell.titleLabel.configure(content: metaContent)
|
|
|
|
} catch {
|
|
|
|
let metaContent = PlaintextMetaContent(string: account.displayNameWithFallback)
|
|
|
|
cell.titleLabel.configure(content: metaContent)
|
|
|
|
}
|
2021-05-18 08:25:32 +02:00
|
|
|
cell.subtitleLabel.text = "@" + account.acct
|
|
|
|
cell.avatarImageView.isHidden = false
|
2022-01-27 14:23:39 +01:00
|
|
|
cell.avatarImageView.configure(configuration: .init(url: URL(string: account.avatar)))
|
2021-05-18 08:25:32 +02:00
|
|
|
}
|
|
|
|
|
2021-06-21 12:30:24 +02:00
|
|
|
private static func configureEmoji(cell: AutoCompleteTableViewCell, emoji: Mastodon.Entity.Emoji, isFirst: Bool) {
|
2021-08-04 09:47:29 +02:00
|
|
|
let metaContent = PlaintextMetaContent(string: ":" + emoji.shortcode + ":")
|
|
|
|
cell.titleLabel.configure(content: metaContent)
|
2021-06-21 12:30:24 +02:00
|
|
|
// FIXME: handle spacer enter to complete emoji
|
|
|
|
// cell.subtitleLabel.text = isFirst ? L10n.Scene.Compose.AutoComplete.spaceToAdd : " "
|
2021-05-18 08:25:32 +02:00
|
|
|
cell.subtitleLabel.text = " "
|
|
|
|
cell.avatarImageView.isHidden = false
|
2022-01-27 14:23:39 +01:00
|
|
|
cell.avatarImageView.configure(configuration: .init(url: URL(string: emoji.url)))
|
2021-05-18 08:25:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|