Switch to Xcode 14 and handle the UICollectionView-backed List. (#229)

* Fix Timeline on Xcode 14/iOS 16

Raise requirement to iOS 16+
Reduce pagination jumping.
Sonarcloud fixes.
Fix verification test.
Adopt if let optional { syntax.

* Remove unused ScrollViewReader

The ScrollViewReader didn't appear to change the behaviour.

* Fix warnings on Run Scripts.

Run script build phase 'SwiftLint' will be run during every build because it does not specify any outputs. To address this warning, either add output dependencies to the script phase, or configure it to run in every build by unchecking "Based on dependency analysis" in the script phase.
This commit is contained in:
Doug
2022-10-17 09:56:17 +01:00
committed by GitHub
parent 5e1057ecab
commit 0e90bff34c
53 changed files with 266 additions and 233 deletions
@@ -17,7 +17,7 @@
import Combine
import UIKit
class ListTableViewAdapter: NSObject, UITableViewDelegate {
class ListCollectionViewAdapter: NSObject, UICollectionViewDelegate {
private enum ContentOffsetDetails {
case topOffset(previousVisibleIndexPath: IndexPath, previousItemCount: Int)
case bottomOffset
@@ -34,7 +34,7 @@ class ListTableViewAdapter: NSObject, UITableViewDelegate {
private var isAnimatingKeyboardAppearance = false
private var previousFrame: CGRect = .zero
private(set) var tableView: UITableView?
private(set) var collectionView: UICollectionView?
let scrollViewDidRestPublisher = PassthroughSubject<Void, Never>()
let scrollViewTopVisiblePublisher = CurrentValueSubject<Bool, Never>(false)
@@ -45,38 +45,36 @@ class ListTableViewAdapter: NSObject, UITableViewDelegate {
bottomDetectionOffset = 0.0
}
init(tableView: UITableView, topDetectionOffset: CGFloat, bottomDetectionOffset: CGFloat) {
self.tableView = tableView
init(collectionView: UICollectionView, topDetectionOffset: CGFloat, bottomDetectionOffset: CGFloat) {
self.collectionView = collectionView
self.topDetectionOffset = topDetectionOffset
self.bottomDetectionOffset = bottomDetectionOffset
super.init()
tableView.clipsToBounds = true
tableView.keyboardDismissMode = .onDrag
collectionView.clipsToBounds = true
collectionView.keyboardDismissMode = .onDrag
registerContentOfffsetObserver()
registerContentOffsetObserver()
registerBoundsObserver()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow(notification:)), name: UIResponder.keyboardDidShowNotification, object: nil)
tableView.panGestureRecognizer.addTarget(self, action: #selector(handlePanGesture(_:)))
collectionView.panGestureRecognizer.addTarget(self, action: #selector(handlePanGesture(_:)))
}
func saveCurrentOffset() {
guard let tableView = tableView,
tableView.numberOfSections > 0 else {
guard let collectionView,
collectionView.numberOfSections > 0 else {
return
}
if computeIsBottomVisible() {
offsetDetails = .bottomOffset
} else if computeIsTopVisible() {
if let topIndexPath = tableView.indexPathsForVisibleRows?.first {
offsetDetails = .topOffset(previousVisibleIndexPath: topIndexPath,
previousItemCount: tableView.numberOfRows(inSection: 0))
}
} else if computeIsTopVisible(), let topIndexPath = collectionView.indexPathsForVisibleItems.first {
offsetDetails = .topOffset(previousVisibleIndexPath: topIndexPath,
previousItemCount: collectionView.numberOfItems(inSection: 0))
}
}
@@ -85,20 +83,20 @@ class ListTableViewAdapter: NSObject, UITableViewDelegate {
offsetDetails = nil
}
guard let tableView = tableView,
tableView.numberOfSections > 0 else {
guard let collectionView,
collectionView.numberOfSections > 0 else {
return
}
let currentItemCount = tableView.numberOfRows(inSection: 0)
let currentItemCount = collectionView.numberOfItems(inSection: 0)
switch offsetDetails {
case .bottomOffset:
tableView.scrollToRow(at: .init(row: max(0, currentItemCount - 1), section: 0), at: .bottom, animated: false)
collectionView.scrollToItem(at: .init(item: max(0, currentItemCount - 1), section: 0), at: .bottom, animated: false)
case .topOffset(let indexPath, let previousItemCount):
let row = indexPath.row + max(0, currentItemCount - previousItemCount)
if row < currentItemCount {
tableView.scrollToRow(at: .init(row: row, section: 0), at: .top, animated: false)
let item = indexPath.item + max(0, currentItemCount - previousItemCount)
if item < currentItemCount {
collectionView.scrollToItem(at: .init(item: item, section: 0), at: .top, animated: false)
}
case .none:
break
@@ -106,35 +104,35 @@ class ListTableViewAdapter: NSObject, UITableViewDelegate {
}
var isTracking: Bool {
tableView?.isTracking == true
collectionView?.isTracking == true
}
var isDecelerating: Bool {
tableView?.isDecelerating == true
collectionView?.isDecelerating == true
}
func scrollToBottom(animated: Bool = false) {
guard let tableView = tableView,
tableView.numberOfSections > 0 else {
guard let collectionView,
collectionView.numberOfSections > 0 else {
return
}
let currentItemCount = tableView.numberOfRows(inSection: 0)
let currentItemCount = collectionView.numberOfItems(inSection: 0)
guard currentItemCount > 1 else {
return
}
tableView.scrollToRow(at: .init(row: currentItemCount - 1, section: 0), at: .bottom, animated: animated)
collectionView.scrollToItem(at: .init(item: currentItemCount - 1, section: 0), at: .bottom, animated: animated)
}
// MARK: - Private
private func registerContentOfffsetObserver() {
// Don't attempt stealing the UITableView delegate away from the List.
private func registerContentOffsetObserver() {
// Don't attempt stealing the UICollectionView delegate away from the List.
// Doing so results in undefined behavior e.g. context menus not working
contentOffsetObserverToken = tableView?.observe(\.contentOffset, options: .new, changeHandler: { [weak self] _, _ in
contentOffsetObserverToken = collectionView?.observe(\.contentOffset, options: .new) { [weak self] _, _ in
self?.handleScrollViewScroll()
})
}
}
private func deregisterContentOffsetObserver() {
@@ -142,10 +140,10 @@ class ListTableViewAdapter: NSObject, UITableViewDelegate {
}
private func registerBoundsObserver() {
boundsObserverToken = tableView?.observe(\.frame, options: .new, changeHandler: { [weak self] tableView, _ in
self?.previousFrame = tableView.frame
boundsObserverToken = collectionView?.observe(\.frame, options: .new) { [weak self] collectionView, _ in
self?.previousFrame = collectionView.frame
self?.handleScrollViewScroll()
})
}
}
private func deregisterBoundsObserver() {
@@ -161,18 +159,18 @@ class ListTableViewAdapter: NSObject, UITableViewDelegate {
}
private func handleScrollViewScroll() {
guard let tableView = tableView else {
guard let collectionView else {
return
}
let hasScrolledBecauseOfFrameChange = (previousFrame != tableView.frame)
let hasScrolledBecauseOfFrameChange = (previousFrame != collectionView.frame)
let shouldPinToBottom = scrollViewBottomVisiblePublisher.value && (isAnimatingKeyboardAppearance || hasScrolledBecauseOfFrameChange)
if shouldPinToBottom {
deregisterContentOffsetObserver()
scrollToBottom()
DispatchQueue.main.async {
self.registerContentOfffsetObserver()
self.registerContentOffsetObserver()
}
return
}
@@ -187,19 +185,19 @@ class ListTableViewAdapter: NSObject, UITableViewDelegate {
scrollViewBottomVisiblePublisher.send(isBottomVisible)
}
if !draggingInitiated, tableView.isDragging {
if !draggingInitiated, collectionView.isDragging {
draggingInitiated = true
} else if draggingInitiated, !tableView.isDragging {
} else if draggingInitiated, !collectionView.isDragging {
draggingInitiated = false
scrollViewDidRestPublisher.send(())
}
}
@objc private func handlePanGesture(_ sender: UIPanGestureRecognizer) {
guard let tableView = tableView,
guard let collectionView,
sender.state == .ended,
draggingInitiated == true,
!tableView.isDecelerating else {
draggingInitiated,
!collectionView.isDecelerating else {
return
}
@@ -208,7 +206,7 @@ class ListTableViewAdapter: NSObject, UITableViewDelegate {
}
private func computeIsTopVisible() -> Bool {
guard let scrollView = tableView else {
guard let scrollView = collectionView else {
return false
}
@@ -216,7 +214,7 @@ class ListTableViewAdapter: NSObject, UITableViewDelegate {
}
private func computeIsBottomVisible() -> Bool {
guard let scrollView = tableView else {
guard let scrollView = collectionView else {
return false
}
@@ -38,7 +38,7 @@ struct PlaceholderAvatarImage: View {
}
private var bgColor: Color {
guard let contentId = contentId else {
guard let contentId else {
return .element.accent
}
@@ -19,7 +19,7 @@ import Introspect
import SwiftUI
struct TimelineItemList: View {
@State private var tableViewObserver = ListTableViewAdapter()
@State private var collectionViewObserver = ListCollectionViewAdapter()
@State private var timelineItems: [RoomTimelineViewProvider] = []
@State private var hasPendingChanges = false
@ObservedObject private var settings = ElementSettings.shared
@@ -32,105 +32,106 @@ struct TimelineItemList: View {
@State private var viewFrame: CGRect = .zero
var body: some View {
// The observer behaves differently when not in an reader
ScrollViewReader { _ in
List {
ProgressView()
.frame(maxWidth: .infinity)
.opacity(context.viewState.isBackPaginating ? 1.0 : 0.0)
.animation(.elementDefault, value: context.viewState.isBackPaginating)
List {
ProgressView()
.frame(maxWidth: .infinity)
.opacity(context.viewState.isBackPaginating ? 1.0 : 0.0)
.animation(.elementDefault, value: context.viewState.isBackPaginating)
.listRowBackground(Color.clear)
.listRowSeparator(.hidden)
// timelineItems won't be set for static Xcode Previews until they're run.
ForEach(isPreview ? context.viewState.items : timelineItems) { timelineItem in
timelineItem
.contextMenu {
context.viewState.contextMenuBuilder?(timelineItem.id)
}
.opacity(opacityForItem(timelineItem))
.listRowBackground(Color.clear)
.listRowSeparator(.hidden)
// No idea why previews don't work otherwise
ForEach(isPreview ? context.viewState.items : timelineItems) { timelineItem in
timelineItem
.contextMenu {
context.viewState.contextMenuBuilder?(timelineItem.id)
}
.opacity(opacityForItem(timelineItem))
.listRowBackground(Color.clear)
.listRowSeparator(.hidden)
.listRowInsets(settings.timelineStyle.listRowInsets)
.onAppear {
context.send(viewAction: .itemAppeared(id: timelineItem.id))
}
.onDisappear {
context.send(viewAction: .itemDisappeared(id: timelineItem.id))
}
.environment(\.openURL, OpenURLAction { url in
context.send(viewAction: .linkClicked(url: url))
return .systemAction
})
}
.listRowInsets(settings.timelineStyle.listRowInsets)
.onAppear {
context.send(viewAction: .itemAppeared(id: timelineItem.id))
}
.onDisappear {
context.send(viewAction: .itemDisappeared(id: timelineItem.id))
}
.environment(\.openURL, OpenURLAction { url in
context.send(viewAction: .linkClicked(url: url))
return .systemAction
})
}
.listStyle(.plain)
.background(ViewFrameReader(frame: $viewFrame))
.environment(\.timelineWidth, viewFrame.width)
.timelineStyle(settings.timelineStyle)
.environment(\.defaultMinListRowHeight, 0.0)
.introspectTableView { tableView in
if tableView == tableViewObserver.tableView {
return
}
tableViewObserver = ListTableViewAdapter(tableView: tableView,
topDetectionOffset: tableView.bounds.size.height / 3.0,
bottomDetectionOffset: 10.0)
tableViewObserver.scrollToBottom()
// Check if there are enough items. Otherwise ask for more
attemptBackPagination()
}
.onAppear {
if timelineItems != context.viewState.items {
timelineItems = context.viewState.items
}
}
.onReceive(scrollToBottomPublisher) {
tableViewObserver.scrollToBottom(animated: true)
}
.onReceive(tableViewObserver.scrollViewTopVisiblePublisher) { isTopVisible in
if !isTopVisible || context.viewState.isBackPaginating {
return
}
attemptBackPagination()
}
.onReceive(tableViewObserver.scrollViewBottomVisiblePublisher) { isBottomVisible in
bottomVisiblePublisher.send(isBottomVisible)
}
.onChange(of: context.viewState.items) { _ in
// Don't update the list while moving
if tableViewObserver.isDecelerating || tableViewObserver.isTracking {
hasPendingChanges = true
return
}
tableViewObserver.saveCurrentOffset()
}
.listStyle(.plain)
.background(ViewFrameReader(frame: $viewFrame))
.environment(\.timelineWidth, viewFrame.width)
.timelineStyle(settings.timelineStyle)
.environment(\.defaultMinListRowHeight, 0.0)
.introspectCollectionView { collectionView in
if collectionView == collectionViewObserver.collectionView { return }
collectionViewObserver = ListCollectionViewAdapter(collectionView: collectionView,
topDetectionOffset: collectionView.bounds.size.height / 3.0,
bottomDetectionOffset: 10.0)
collectionViewObserver.scrollToBottom()
// Check if there are enough items. Otherwise ask for more
attemptBackPagination()
}
.onAppear {
if timelineItems != context.viewState.items {
timelineItems = context.viewState.items
}
.onReceive(tableViewObserver.scrollViewDidRestPublisher) {
if hasPendingChanges == false {
return
}
tableViewObserver.saveCurrentOffset()
}
.onReceive(scrollToBottomPublisher) {
collectionViewObserver.scrollToBottom(animated: true)
}
.onReceive(collectionViewObserver.scrollViewTopVisiblePublisher) { isTopVisible in
if !isTopVisible || context.viewState.isBackPaginating {
return
}
attemptBackPagination()
}
.onReceive(collectionViewObserver.scrollViewBottomVisiblePublisher) { isBottomVisible in
bottomVisiblePublisher.send(isBottomVisible)
}
.onChange(of: context.viewState.items) { _ in
// If the count hasn't changed then don't observe a pagination
guard context.viewState.items.count != timelineItems.count else {
timelineItems = context.viewState.items
hasPendingChanges = false
return
}
.onChange(of: timelineItems) { _ in
tableViewObserver.restoreSavedOffset()
// Check if there are enough items. Otherwise ask for more
attemptBackPagination()
// Don't update the list while moving
if collectionViewObserver.isDecelerating || collectionViewObserver.isTracking {
hasPendingChanges = true
return
}
collectionViewObserver.saveCurrentOffset()
timelineItems = context.viewState.items
}
.onReceive(collectionViewObserver.scrollViewDidRestPublisher) {
if hasPendingChanges == false {
return
}
collectionViewObserver.saveCurrentOffset()
timelineItems = context.viewState.items
hasPendingChanges = false
}
.onChange(of: timelineItems.count) { _ in
collectionViewObserver.restoreSavedOffset()
// Check if there are enough items. Otherwise ask for more
attemptBackPagination()
}
}
func scrollToBottom(animated: Bool = false) {
tableViewObserver.scrollToBottom(animated: animated)
collectionViewObserver.scrollToBottom(animated: animated)
}
private func attemptBackPagination() {
@@ -138,7 +139,7 @@ struct TimelineItemList: View {
return
}
if tableViewObserver.scrollViewTopVisiblePublisher.value == false {
if collectionViewObserver.scrollViewTopVisiblePublisher.value == false {
return
}