2022-04-13 14:43:16 +02:00
|
|
|
//
|
|
|
|
// DiscoverySection.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK on 2022-4-13.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2022-10-08 07:43:06 +02:00
|
|
|
import MastodonCore
|
2022-04-13 14:43:16 +02:00
|
|
|
import MastodonUI
|
2022-05-16 13:42:03 +02:00
|
|
|
import MastodonSDK
|
2022-04-13 14:43:16 +02:00
|
|
|
|
|
|
|
enum DiscoverySection: CaseIterable {
|
|
|
|
// case posts
|
|
|
|
case hashtags
|
|
|
|
case news
|
|
|
|
case forYou
|
|
|
|
}
|
|
|
|
|
|
|
|
extension DiscoverySection {
|
2023-11-20 13:51:20 +01:00
|
|
|
|
2022-04-15 11:17:39 +02:00
|
|
|
class Configuration {
|
2022-10-09 14:07:57 +02:00
|
|
|
let authContext: AuthContext
|
2022-04-15 11:17:39 +02:00
|
|
|
weak var profileCardTableViewCellDelegate: ProfileCardTableViewCellDelegate?
|
2022-05-16 13:42:03 +02:00
|
|
|
let familiarFollowers: Published<[Mastodon.Entity.FamiliarFollowers]>.Publisher?
|
2023-11-20 13:51:20 +01:00
|
|
|
|
2022-05-16 13:42:03 +02:00
|
|
|
public init(
|
2022-10-09 14:07:57 +02:00
|
|
|
authContext: AuthContext,
|
2022-05-16 13:42:03 +02:00
|
|
|
profileCardTableViewCellDelegate: ProfileCardTableViewCellDelegate? = nil,
|
|
|
|
familiarFollowers: Published<[Mastodon.Entity.FamiliarFollowers]>.Publisher? = nil
|
|
|
|
) {
|
2022-10-09 14:07:57 +02:00
|
|
|
self.authContext = authContext
|
2022-04-15 11:17:39 +02:00
|
|
|
self.profileCardTableViewCellDelegate = profileCardTableViewCellDelegate
|
2022-05-16 13:42:03 +02:00
|
|
|
self.familiarFollowers = familiarFollowers
|
2022-04-15 11:17:39 +02:00
|
|
|
}
|
|
|
|
}
|
2023-11-20 13:51:20 +01:00
|
|
|
|
2022-04-13 14:43:16 +02:00
|
|
|
static func diffableDataSource(
|
|
|
|
tableView: UITableView,
|
|
|
|
context: AppContext,
|
|
|
|
configuration: Configuration
|
|
|
|
) -> UITableViewDiffableDataSource<DiscoverySection, DiscoveryItem> {
|
2023-11-20 13:51:20 +01:00
|
|
|
|
2022-04-13 14:43:16 +02:00
|
|
|
tableView.register(TrendTableViewCell.self, forCellReuseIdentifier: String(describing: TrendTableViewCell.self))
|
|
|
|
tableView.register(NewsTableViewCell.self, forCellReuseIdentifier: String(describing: NewsTableViewCell.self))
|
2022-04-14 15:15:21 +02:00
|
|
|
tableView.register(ProfileCardTableViewCell.self, forCellReuseIdentifier: String(describing: ProfileCardTableViewCell.self))
|
2022-04-13 14:43:16 +02:00
|
|
|
tableView.register(TimelineBottomLoaderTableViewCell.self, forCellReuseIdentifier: String(describing: TimelineBottomLoaderTableViewCell.self))
|
2022-04-14 15:15:21 +02:00
|
|
|
|
2023-11-20 13:51:20 +01:00
|
|
|
return UITableViewDiffableDataSource(tableView: tableView) {
|
|
|
|
tableView,
|
|
|
|
indexPath,
|
|
|
|
item in
|
2022-04-13 14:43:16 +02:00
|
|
|
switch item {
|
2023-11-20 13:51:20 +01:00
|
|
|
case .hashtag(let tag):
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: TrendTableViewCell.self), for: indexPath) as! TrendTableViewCell
|
|
|
|
cell.trendView.configure(tag: tag)
|
|
|
|
return cell
|
|
|
|
case .link(let link):
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: NewsTableViewCell.self), for: indexPath) as! NewsTableViewCell
|
|
|
|
cell.newsView.configure(link: link)
|
|
|
|
return cell
|
|
|
|
case .account(let account, relationship: let relationship):
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: ProfileCardTableViewCell.self), for: indexPath) as! ProfileCardTableViewCell
|
2023-11-17 14:48:23 +01:00
|
|
|
|
2023-11-20 13:51:20 +01:00
|
|
|
cell.configure(
|
|
|
|
tableView: tableView,
|
|
|
|
account: account,
|
|
|
|
relationship: relationship,
|
|
|
|
profileCardTableViewCellDelegate: configuration.profileCardTableViewCellDelegate
|
|
|
|
)
|
2023-11-17 14:48:23 +01:00
|
|
|
|
2022-05-16 13:42:03 +02:00
|
|
|
// bind familiarFollowers
|
|
|
|
if let familiarFollowers = configuration.familiarFollowers {
|
|
|
|
familiarFollowers
|
2023-11-17 14:48:23 +01:00
|
|
|
.map { array in array.first(where: { $0.id == account.id }) }
|
2022-05-16 13:42:03 +02:00
|
|
|
.assign(to: \.familiarFollowers, on: cell.profileCardView.viewModel)
|
|
|
|
.store(in: &cell.disposeBag)
|
|
|
|
} else {
|
|
|
|
cell.profileCardView.viewModel.familiarFollowers = nil
|
|
|
|
}
|
2023-11-20 13:51:20 +01:00
|
|
|
|
|
|
|
return cell
|
|
|
|
case .bottomLoader:
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: TimelineBottomLoaderTableViewCell.self), for: indexPath) as! TimelineBottomLoaderTableViewCell
|
|
|
|
cell.activityIndicatorView.startAnimating()
|
|
|
|
return cell
|
2022-04-13 14:43:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-20 13:51:20 +01:00
|
|
|
|
2022-04-13 14:43:16 +02:00
|
|
|
}
|