mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-03 16:10:20 +00:00
Better timeline handling, based on improved rust APIs.
This commit is contained in:
@@ -15,33 +15,47 @@
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import Introspect
|
||||
import Combine
|
||||
|
||||
struct RoomScreen: View {
|
||||
|
||||
let timelineBottomDividerIdentifier = "TimelineBottomDividerIdentifier"
|
||||
@State private var scrollViewObserver: ScrollViewObserver = ScrollViewObserver()
|
||||
@State private var messages: [RoomScreenMessage] = []
|
||||
|
||||
@State private var didRequestBackPagination = false
|
||||
@State private var hasPendingMessages = false
|
||||
@State private var wasBottomVisible = false
|
||||
|
||||
@State private var previousTopMostMessageIdentifier: String?
|
||||
|
||||
private let timelineBottomAnchor = "TimelineBottomAnchor"
|
||||
|
||||
@ObservedObject var context: RoomScreenViewModel.Context
|
||||
@State var backPaginationMessageIdentifier: String?
|
||||
|
||||
var body: some View {
|
||||
ScrollViewReader { reader in
|
||||
ScrollViewReader { scrollViewProxy in
|
||||
List {
|
||||
if backPaginationMessageIdentifier != nil {
|
||||
if didRequestBackPagination == false {
|
||||
Color
|
||||
.clear
|
||||
.onAppear {
|
||||
guard didRequestBackPagination == false else {
|
||||
return
|
||||
}
|
||||
|
||||
didRequestBackPagination = true
|
||||
context.send(viewAction: .loadPreviousPage)
|
||||
}
|
||||
} else {
|
||||
HStack {
|
||||
Spacer()
|
||||
ProgressView()
|
||||
Spacer()
|
||||
}
|
||||
} else {
|
||||
Rectangle()
|
||||
.onAppear {
|
||||
let _ = MXLog.debug("Request load previous page")
|
||||
backPaginationMessageIdentifier = context.viewState.messages.first?.id
|
||||
context.send(viewAction: .loadPreviousPage)
|
||||
}
|
||||
.frame(height: 0.0)
|
||||
}
|
||||
ForEach(context.viewState.messages) { message in
|
||||
|
||||
ForEach(messages) { message in
|
||||
VStack(alignment: .leading) {
|
||||
HStack {
|
||||
Text(message.sender)
|
||||
@@ -55,23 +69,117 @@ struct RoomScreen: View {
|
||||
.id(message.id)
|
||||
}
|
||||
|
||||
Divider()
|
||||
.id(timelineBottomDividerIdentifier)
|
||||
Color.clear
|
||||
.listRowSeparator(.hidden)
|
||||
.id(timelineBottomAnchor)
|
||||
}
|
||||
.listStyle(.plain)
|
||||
.onAppear {
|
||||
reader.scrollTo(timelineBottomDividerIdentifier, anchor: .bottom)
|
||||
}
|
||||
.onChange(of: context.viewState.messages) { _ in
|
||||
if backPaginationMessageIdentifier != nil {
|
||||
reader.scrollTo(backPaginationMessageIdentifier, anchor: .bottom)
|
||||
self.backPaginationMessageIdentifier = nil
|
||||
.environment(\.defaultMinListRowHeight, 0.0)
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
// Fetch the underlying UIScrollView and start observing it
|
||||
.introspectTableView { scrollView in
|
||||
if scrollView == scrollViewObserver.scrollView {
|
||||
return
|
||||
}
|
||||
|
||||
reader.scrollTo(timelineBottomDividerIdentifier, anchor: .bottom)
|
||||
scrollViewObserver = ScrollViewObserver(scrollView: scrollView)
|
||||
}
|
||||
// Scroll to the bottom when the timeline first appears
|
||||
.onAppear {
|
||||
scrollViewProxy.scrollTo(timelineBottomAnchor, anchor: .bottom)
|
||||
}
|
||||
// When the view state changes check whether the user is interacting with the scroll view.
|
||||
// Updating in that case causes undesired scrolling. Delay until the scroll view stops scrolling.
|
||||
// Also store previous top most message identifier to have something to scroll to after the update.
|
||||
.onChange(of: context.viewState.messages) { newValue in
|
||||
previousTopMostMessageIdentifier = messages.first?.id
|
||||
wasBottomVisible = scrollViewObserver.isBottomVisible
|
||||
|
||||
if scrollViewObserver.isTracking == true {
|
||||
hasPendingMessages = true
|
||||
return
|
||||
}
|
||||
|
||||
messages = newValue
|
||||
}
|
||||
// Check if we have pending messages to apply and apply them when the scroll finishes scrolling
|
||||
.onReceive(scrollViewObserver.didEndScrolling, perform: {
|
||||
if hasPendingMessages {
|
||||
messages = context.viewState.messages
|
||||
hasPendingMessages = false
|
||||
}
|
||||
})
|
||||
// Process timeline updates
|
||||
.onChange(of: messages, perform: { _ in
|
||||
if didRequestBackPagination && wasBottomVisible {
|
||||
scrollViewProxy.scrollTo(timelineBottomAnchor, anchor: .bottom)
|
||||
} else if didRequestBackPagination == false {
|
||||
if wasBottomVisible {
|
||||
scrollViewProxy.scrollTo(timelineBottomAnchor, anchor: .bottom)
|
||||
}
|
||||
} else {
|
||||
// Manual scrolling breaks inertia. Don't do it if the scroll view is decelerating
|
||||
if scrollViewObserver.isDecelerating == false {
|
||||
scrollViewProxy.scrollTo(previousTopMostMessageIdentifier, anchor: .top)
|
||||
}
|
||||
}
|
||||
|
||||
didRequestBackPagination = false
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Simple wrapper around an UIScrollView that publishes when it finishes scrolling
|
||||
class ScrollViewObserver: NSObject, UIScrollViewDelegate {
|
||||
private(set) var scrollView: UIScrollView?
|
||||
|
||||
let didEndScrolling = PassthroughSubject<Void, Never>()
|
||||
|
||||
override init() {
|
||||
|
||||
}
|
||||
|
||||
init(scrollView: UIScrollView) {
|
||||
self.scrollView = scrollView
|
||||
super.init()
|
||||
|
||||
scrollView.delegate = self
|
||||
}
|
||||
|
||||
var isTracking: Bool {
|
||||
self.scrollView?.isTracking == true
|
||||
}
|
||||
|
||||
var isDecelerating: Bool {
|
||||
self.scrollView?.isDecelerating == true
|
||||
}
|
||||
|
||||
var isTopVisible: Bool {
|
||||
guard let scrollView = scrollView else {
|
||||
return false
|
||||
}
|
||||
|
||||
return (scrollView.contentOffset.y + scrollView.adjustedContentInset.top) <= 0.0
|
||||
}
|
||||
|
||||
var isBottomVisible: Bool {
|
||||
guard let scrollView = scrollView else {
|
||||
return false
|
||||
}
|
||||
|
||||
return (scrollView.contentOffset.y) >= (scrollView.contentSize.height - scrollView.frame.size.height)
|
||||
}
|
||||
|
||||
// MARK: - UIScrollViewDelegate
|
||||
|
||||
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
|
||||
didEndScrolling.send(())
|
||||
}
|
||||
|
||||
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerating: Bool) {
|
||||
if decelerating == false {
|
||||
didEndScrolling.send(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user