metatext/Extensions/CollectionItem+Extensions.swift

117 lines
4.2 KiB
Swift
Raw Normal View History

2020-09-23 01:00:56 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
2021-01-31 07:43:48 +00:00
import Mastodon
2020-10-30 07:11:24 +00:00
import UIKit
2020-09-23 01:00:56 +00:00
import ViewModels
2020-10-15 07:44:01 +00:00
extension CollectionItem {
2020-10-30 07:11:24 +00:00
static let cellClasses = [
2021-01-29 06:22:13 +00:00
StatusTableViewCell.self,
AccountTableViewCell.self,
LoadMoreTableViewCell.self,
NotificationTableViewCell.self,
ConversationTableViewCell.self,
2021-01-25 07:42:39 +00:00
TagTableViewCell.self,
2021-02-09 02:48:02 +00:00
SeparatorConfiguredTableViewCell.self]
2020-10-15 07:44:01 +00:00
2020-09-23 01:00:56 +00:00
var cellClass: AnyClass {
switch self {
case .status:
2021-01-29 06:22:13 +00:00
return StatusTableViewCell.self
2020-09-23 01:00:56 +00:00
case .account:
2021-01-29 06:22:13 +00:00
return AccountTableViewCell.self
case .loadMore:
2021-01-29 06:22:13 +00:00
return LoadMoreTableViewCell.self
2020-10-30 07:11:24 +00:00
case let .notification(_, statusConfiguration):
2021-01-29 06:22:13 +00:00
return statusConfiguration == nil ? NotificationTableViewCell.self : StatusTableViewCell.self
2020-10-29 06:03:45 +00:00
case .conversation:
2021-01-29 06:22:13 +00:00
return ConversationTableViewCell.self
2021-01-24 03:12:30 +00:00
case .tag:
return TagTableViewCell.self
2021-01-25 07:42:39 +00:00
case .moreResults:
2021-02-09 02:48:02 +00:00
return SeparatorConfiguredTableViewCell.self
2020-09-23 01:00:56 +00:00
}
}
2021-01-19 00:46:38 +00:00
2021-01-26 00:06:35 +00:00
func estimatedHeight(width: CGFloat, identityContext: IdentityContext) -> CGFloat {
2021-01-19 00:46:38 +00:00
switch self {
2021-02-08 01:46:51 +00:00
case let .status(status, configuration, _):
2021-01-19 00:46:38 +00:00
return StatusView.estimatedHeight(
width: width,
2021-01-26 00:06:35 +00:00
identityContext: identityContext,
2021-01-19 00:46:38 +00:00
status: status,
configuration: configuration)
2021-02-08 01:46:51 +00:00
case let .account(account, configuration, _):
2021-01-26 06:57:44 +00:00
return AccountView.estimatedHeight(width: width, account: account, configuration: configuration)
2021-01-20 02:47:21 +00:00
case .loadMore:
return LoadMoreView.estimatedHeight
case let .notification(notification, configuration):
return NotificationView.estimatedHeight(
width: width,
2021-01-26 00:06:35 +00:00
identityContext: identityContext,
2021-01-20 02:47:21 +00:00
notification: notification,
configuration: configuration)
case let .conversation(conversation):
return ConversationView.estimatedHeight(
width: width,
2021-01-26 00:06:35 +00:00
identityContext: identityContext,
2021-01-20 02:47:21 +00:00
conversation: conversation)
2021-01-24 03:12:30 +00:00
case let .tag(tag):
return TagView.estimatedHeight(width: width, tag: tag)
2021-01-25 07:42:39 +00:00
case .moreResults:
return UITableView.automaticDimension
2021-01-19 00:46:38 +00:00
}
}
2021-01-31 07:43:48 +00:00
func mediaPrefetchURLs(identityContext: IdentityContext) -> Set<URL> {
switch self {
2021-02-08 01:46:51 +00:00
case let .status(status, _, _):
2021-01-31 07:43:48 +00:00
return status.mediaPrefetchURLs(identityContext: identityContext)
2021-02-08 01:46:51 +00:00
case let .account(account, _, _):
2021-01-31 07:43:48 +00:00
return account.mediaPrefetchURLs(identityContext: identityContext)
case let .notification(notification, _):
var urls = notification.account.mediaPrefetchURLs(identityContext: identityContext)
if let status = notification.status {
urls.formUnion(status.mediaPrefetchURLs(identityContext: identityContext))
}
return urls
case let .conversation(conversation):
return conversation.accounts.reduce(Set<URL>()) {
$0.union($1.mediaPrefetchURLs(identityContext: identityContext))
}
default:
return []
}
}
}
private extension Account {
func mediaPrefetchURLs(identityContext: IdentityContext) -> Set<URL> {
2021-02-27 20:27:49 +00:00
var urls = Set(emojis.compactMap {
identityContext.appPreferences.animateCustomEmojis ? $0.url : $0.staticUrl
}
.compactMap(URL.init(string:)))
2021-01-31 07:43:48 +00:00
2021-03-06 02:25:18 +00:00
if identityContext.appPreferences.animateAvatars == .everywhere {
2021-01-31 07:43:48 +00:00
urls.insert(avatar)
} else {
urls.insert(avatarStatic)
}
return urls
}
}
private extension Status {
func mediaPrefetchURLs(identityContext: IdentityContext) -> Set<URL> {
displayStatus.account.mediaPrefetchURLs(identityContext: identityContext)
.union(displayStatus.mediaAttachments.compactMap(\.previewUrl))
2021-02-27 20:27:49 +00:00
.union(displayStatus.emojis.compactMap {
identityContext.appPreferences.animateCustomEmojis ? $0.url : $0.staticUrl
}
.compactMap(URL.init(string:)))
2021-01-31 07:43:48 +00:00
}
2020-09-23 01:00:56 +00:00
}