Refactoring

This commit is contained in:
Justin Mazzocchi 2020-10-05 16:44:15 -07:00
parent 2f888e1968
commit 4c6e242215
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C
7 changed files with 22 additions and 19 deletions

View file

@ -34,6 +34,7 @@ extension ContextItemsInfo {
&& section[index + 1].record.inReplyToId == statusInfo.record.id && section[index + 1].record.inReplyToId == statusInfo.record.id
return .status(.init(status: .init(info: statusInfo), return .status(.init(status: .init(info: statusInfo),
isContextParent: statusInfo.record.id == parent.record.id,
isReplyInContext: isReplyInContext, isReplyInContext: isReplyInContext,
hasReplyFollowing: hasReplyFollowing)) hasReplyFollowing: hasReplyFollowing))
} }

View file

@ -51,7 +51,7 @@ extension TimelineItemsInfo {
if let pinnedStatusInfos = pinnedStatusesInfo?.pinnedStatusInfos { if let pinnedStatusInfos = pinnedStatusesInfo?.pinnedStatusInfos {
return [pinnedStatusInfos.filtered(regularExpression: filterRegularExpression) return [pinnedStatusInfos.filtered(regularExpression: filterRegularExpression)
.map { CollectionItem.status(.init(status: .init(info: $0), pinned: true)) }, .map { CollectionItem.status(.init(status: .init(info: $0), isPinned: true)) },
timelineItems] timelineItems]
} else { } else {
return [timelineItems] return [timelineItems]

View file

@ -11,13 +11,19 @@ public enum CollectionItem: Hashable {
public extension CollectionItem { public extension CollectionItem {
struct StatusConfiguration: Hashable { struct StatusConfiguration: Hashable {
public let status: Status public let status: Status
public let pinned: Bool public let isContextParent: Bool
public let isPinned: Bool
public let isReplyInContext: Bool public let isReplyInContext: Bool
public let hasReplyFollowing: Bool public let hasReplyFollowing: Bool
init(status: Status, pinned: Bool = false, isReplyInContext: Bool = false, hasReplyFollowing: Bool = false) { init(status: Status,
isContextParent: Bool = false,
isPinned: Bool = false,
isReplyInContext: Bool = false,
hasReplyFollowing: Bool = false) {
self.status = status self.status = status
self.pinned = pinned self.isContextParent = isContextParent
self.isPinned = isPinned
self.isReplyInContext = isReplyInContext self.isReplyInContext = isReplyInContext
self.hasReplyFollowing = hasReplyFollowing self.hasReplyFollowing = hasReplyFollowing
} }

View file

@ -7,7 +7,6 @@ public protocol CollectionService {
var nextPageMaxId: AnyPublisher<String, Never> { get } var nextPageMaxId: AnyPublisher<String, Never> { get }
var title: AnyPublisher<String, Never> { get } var title: AnyPublisher<String, Never> { get }
var navigationService: NavigationService { get } var navigationService: NavigationService { get }
var contextParentId: String? { get }
func request(maxId: String?, minId: String?) -> AnyPublisher<Never, Error> func request(maxId: String?, minId: String?) -> AnyPublisher<Never, Error>
} }
@ -15,6 +14,4 @@ extension CollectionService {
public var nextPageMaxId: AnyPublisher<String, Never> { Empty().eraseToAnyPublisher() } public var nextPageMaxId: AnyPublisher<String, Never> { Empty().eraseToAnyPublisher() }
public var title: AnyPublisher<String, Never> { Empty().eraseToAnyPublisher() } public var title: AnyPublisher<String, Never> { Empty().eraseToAnyPublisher() }
public var contextParentId: String? { nil }
} }

View file

@ -9,7 +9,6 @@ import MastodonAPI
public struct ContextService { public struct ContextService {
public let sections: AnyPublisher<[[CollectionItem]], Error> public let sections: AnyPublisher<[[CollectionItem]], Error>
public let navigationService: NavigationService public let navigationService: NavigationService
public var contextParentId: String? { id }
private let id: Status.Id private let id: Status.Id
private let mastodonAPIClient: MastodonAPIClient private let mastodonAPIClient: MastodonAPIClient

View file

@ -81,7 +81,7 @@ extension CollectionItemsViewModel: CollectionViewModel {
public func canSelect(indexPath: IndexPath) -> Bool { public func canSelect(indexPath: IndexPath) -> Bool {
switch items.value[indexPath.section][indexPath.item] { switch items.value[indexPath.section][indexPath.item] {
case let .status(configuration): case let .status(configuration):
return configuration.status.id != collectionService.contextParentId return !configuration.isContextParent
case .loadMore: case .loadMore:
return !((viewModel(indexPath: indexPath) as? LoadMoreViewModel)?.loading ?? false) return !((viewModel(indexPath: indexPath) as? LoadMoreViewModel)?.loading ?? false)
default: default:
@ -104,8 +104,8 @@ extension CollectionItemsViewModel: CollectionViewModel {
cache(viewModel: viewModel, forItem: item) cache(viewModel: viewModel, forItem: item)
} }
viewModel.isContextParent = configuration.status.id == collectionService.contextParentId viewModel.isContextParent = configuration.isContextParent
viewModel.isPinned = configuration.pinned viewModel.isPinned = configuration.isPinned
viewModel.isReplyInContext = configuration.isReplyInContext viewModel.isReplyInContext = configuration.isReplyInContext
viewModel.hasReplyFollowing = configuration.hasReplyFollowing viewModel.hasReplyFollowing = configuration.hasReplyFollowing
@ -156,14 +156,14 @@ private extension CollectionItemsViewModel {
maintainScrollPositionOfItem = nil // clear old value maintainScrollPositionOfItem = nil // clear old value
// Maintain scroll position of parent after initial load of context // Maintain scroll position of parent after initial load of context
if let contextParentId = collectionService.contextParentId { if collectionService is ContextService,
let contextParentIdentifier = CollectionItemIdentifier(id: contextParentId, kind: .status, info: [:]) items.value.isEmpty || items.value.map(\.count) == [0, 1, 0],
let onlyContextParentId = [[], [contextParentIdentifier], []] let contextParent = newItems.reduce([], +).first(where: {
guard case let .status(configuration) = $0 else { return false }
if items.value.isEmpty return configuration.isContextParent
|| items.value.map({ $0.map(CollectionItemIdentifier.init(item:)) }) == onlyContextParentId { }) {
maintainScrollPositionOfItem = contextParentIdentifier maintainScrollPositionOfItem = .init(item: contextParent)
}
} }
} }
} }

View file

@ -27,7 +27,7 @@ extension CollectionItemIdentifier {
case let .status(configuration): case let .status(configuration):
id = configuration.status.id id = configuration.status.id
kind = .status kind = .status
info = configuration.pinned ? [.pinned: true] : [:] info = configuration.isPinned ? [.pinned: true] : [:]
case let .loadMore(loadMore): case let .loadMore(loadMore):
id = loadMore.afterStatusId id = loadMore.afterStatusId
kind = .loadMore kind = .loadMore