Better timeline handling, based on improved rust APIs.

This commit is contained in:
Stefan Ceriu
2022-03-10 16:45:34 +02:00
parent fc8e92eef1
commit 8d4f6f301b
11 changed files with 219 additions and 80 deletions
@@ -66,8 +66,7 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol
self.roomList = roomList
roomList.forEach({ roomProxy in
roomProxy.paginateBackwards(start: 0, finish: 1)
roomProxy.startLiveEventListener()
roomProxy.paginateBackwards(count: 1, callback: nil)
roomProxy.callbacks.sink { [weak self] callback in
switch callback {
case .updatedLastMessage:
@@ -32,7 +32,6 @@ private var dateFormatter: DateFormatter = {
}()
struct RoomScreenMessage: Identifiable, Equatable {
let id: String
let sender: String
let text: String
@@ -45,6 +44,5 @@ struct RoomScreenMessage: Identifiable, Equatable {
struct RoomScreenViewState: BindableState {
var roomTitle: String?
var isLoading: Bool = false
var messages: [RoomScreenMessage] = []
}
@@ -50,8 +50,6 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
self.state.messages = self.buildRoomScreenMessages(timelineController.timelineItems)
}
}.store(in: &cancellables)
timelineController.paginateBackwards(Constants.backPaginationPageSize)
}
// MARK: - Public
@@ -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(())
}
}
}