Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a UITableView component for the timeline. #349

Merged
merged 10 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 8 additions & 48 deletions ElementX.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

"room_timeline_replying_to" = "Replying to %@";
"room_timeline_editing" = "Editing";
"room_timeline_syncing" = "Syncing";

"session_verification_banner_title" = "Help keep your messages secure";
"session_verification_banner_message" = "Looks like you’re using a new device. Verify its you.";
Expand Down
2 changes: 2 additions & 0 deletions ElementX/Sources/Generated/Strings+Untranslated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ extension ElementL10n {
public static let roomTimelineStyleBubbledLongDescription = ElementL10n.tr("Untranslated", "room_timeline_style_bubbled_long_description")
/// Plain Timeline
public static let roomTimelineStylePlainLongDescription = ElementL10n.tr("Untranslated", "room_timeline_style_plain_long_description")
/// Syncing
public static let roomTimelineSyncing = ElementL10n.tr("Untranslated", "room_timeline_syncing")
/// Would you like to submit a bug report?
public static let screenshotDetectedMessage = ElementL10n.tr("Untranslated", "screenshot_detected_message")
/// You took a screenshot
Expand Down
15 changes: 7 additions & 8 deletions ElementX/Sources/Other/ScrollViewAdapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,29 @@ class ScrollViewAdapter: NSObject, UIScrollViewDelegate {
}
}

var isScrolling = PassthroughSubject<Bool, Never>()
var isScrolling = CurrentValueSubject<Bool, Never>(false)

private func update() {
guard let scrollView else { return }
private func update(_ scrollView: UIScrollView) {
isScrolling.send(scrollView.isDragging || scrollView.isDecelerating)
}

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
update()
update(scrollView)
stefanceriu marked this conversation as resolved.
Show resolved Hide resolved
}

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
update()
update(scrollView)
}

func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
update()
update(scrollView)
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
update()
update(scrollView)
}

func scrollViewDidScrollToTop(_ scrollView: UIScrollView) {
update()
update(scrollView)
}
}
14 changes: 12 additions & 2 deletions ElementX/Sources/Screens/RoomScreen/RoomScreenModels.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// limitations under the License.
//

import Foundation
import Combine
import UIKit

enum RoomScreenViewModelAction {
Expand All @@ -29,7 +29,7 @@ enum RoomScreenComposerMode: Equatable {
}

enum RoomScreenViewAction {
case loadPreviousPage
case paginateBackwards
case itemAppeared(id: String)
case itemDisappeared(id: String)
case itemTapped(id: String)
Expand All @@ -56,12 +56,22 @@ struct RoomScreenViewState: BindableState {
var sendButtonDisabled: Bool {
bindings.composerText.count == 0
}

let scrollToBottomPublisher = PassthroughSubject<Void, Never>()

/// Returns the opacity that the supplied timeline item's cell should be.
func opacity(for item: RoomTimelineViewProvider) -> CGFloat {
guard case let .reply(selectedItemID, _) = composerMode else { return 1.0 }
return selectedItemID == item.id ? 1.0 : 0.5
}
}

struct RoomScreenViewStateBindings {
var composerText: String
var composerFocused: Bool

var scrollToBottomButtonVisible = false

/// Information describing the currently displayed alert.
var alertInfo: AlertInfo<RoomScreenErrorType>?

Expand Down
30 changes: 25 additions & 5 deletions ElementX/Sources/Screens/RoomScreen/RoomScreenViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.
//

import Combine
import SwiftUI

typealias RoomScreenViewModelType = StateStoreViewModel<RoomScreenViewState, RoomScreenViewAction>
Expand All @@ -26,6 +27,9 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
private let timelineController: RoomTimelineControllerProtocol
private let timelineViewFactory: RoomTimelineViewFactoryProtocol
private let mediaProvider: MediaProviderProtocol

/// A publisher used to throttle back pagination requests.
private let paginateBackwardsPublisher = PassthroughSubject<Void, Never>()
pixlwave marked this conversation as resolved.
Show resolved Hide resolved

// MARK: - Setup

Expand Down Expand Up @@ -60,14 +64,26 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
self.state.items[viewIndex] = timelineViewFactory.buildTimelineViewFor(timelineItem: timelineItem)
case .startedBackPaginating:
self.state.isBackPaginating = true
ServiceLocator.shared.userNotificationController.submitNotification(UserNotification(id: "pagination",
type: .toast,
title: ElementL10n.roomTimelineSyncing,
persistent: true))
case .finishedBackPaginating:
self.state.isBackPaginating = false
ServiceLocator.shared.userNotificationController.retractNotificationWithId("pagination")
pixlwave marked this conversation as resolved.
Show resolved Hide resolved
}
}
.store(in: &cancellables)

state.contextMenuBuilder = buildContexMenuForItemId(_:)

paginateBackwardsPublisher
.collect(.byTime(DispatchQueue.main, 0.1))
.sink { [weak self] _ in
Task { await self?.paginateBackwards() }
}
.store(in: &cancellables)

buildTimelineViews()

if let roomAvatarUrl {
Expand All @@ -86,11 +102,8 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol

override func process(viewAction: RoomScreenViewAction) async {
switch viewAction {
case .loadPreviousPage:
switch await timelineController.paginateBackwards(Constants.backPaginationPageSize) {
default:
#warning("Treat errors")
}
case .paginateBackwards:
paginateBackwardsPublisher.send(())
case .itemAppeared(let id):
await timelineController.processItemAppearance(id)
case .itemDisappeared(let id):
Expand Down Expand Up @@ -118,6 +131,13 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
}

// MARK: - Private

private func paginateBackwards() async {
switch await timelineController.paginateBackwards(Constants.backPaginationPageSize) {
default:
#warning("Treat errors")
}
}

private func itemTapped(with itemId: String) async {
state.showLoading = true
Expand Down
89 changes: 54 additions & 35 deletions ElementX/Sources/Screens/RoomScreen/View/RoomScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,51 +17,70 @@
import SwiftUI

struct RoomScreen: View {
@ObservedObject private var settings = ElementSettings.shared
@ObservedObject var context: RoomScreenViewModel.Context

var body: some View {
ZStack {
VStack(spacing: 0.0) {
TimelineView()
.environmentObject(context)

MessageComposer(text: $context.composerText,
focused: $context.composerFocused,
sendingDisabled: context.viewState.sendButtonDisabled,
type: context.viewState.composerMode) {
sendMessage()
} replyCancellationAction: {
context.send(viewAction: .cancelReply)
} editCancellationAction: {
context.send(viewAction: .cancelEdit)
}
.padding()
}
timeline
.safeAreaInset(edge: .bottom) { messageComposer }
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
RoomHeaderView(context: context)
}
}
.toolbar { toolbar }
.overlay { loadingIndicator }
.alert(item: $context.alertInfo) { $0.alert }
.sheet(item: $context.debugInfo) { DebugScreen(info: $0) }

if context.viewState.showLoading {
ProgressView()
.progressViewStyle(.circular)
.tint(.element.primaryContent)
.padding(16)
.background(Color.element.quinaryContent)
.cornerRadius(8)
}
}

var timeline: some View {
TimelineTableView()
.environmentObject(context)
.timelineStyle(settings.timelineStyle)
.overlay(alignment: .bottomTrailing) { scrollToBottomButton }
}

var messageComposer: some View {
MessageComposer(text: $context.composerText,
focused: $context.composerFocused,
sendingDisabled: context.viewState.sendButtonDisabled,
type: context.viewState.composerMode) {
sendMessage()
} replyCancellationAction: {
context.send(viewAction: .cancelReply)
} editCancellationAction: {
context.send(viewAction: .cancelEdit)
}
.padding()
}

private func sendMessage() {
guard !context.viewState.sendButtonDisabled else {
return
var scrollToBottomButton: some View {
Button { context.viewState.scrollToBottomPublisher.send(()) } label: {
Image(uiImage: Asset.Images.timelineScrollToBottom.image)
.shadow(radius: 2.0)
.padding()
}

.opacity(context.scrollToBottomButtonVisible ? 1.0 : 0.0)
.animation(.elementDefault, value: context.scrollToBottomButtonVisible)
}

@ViewBuilder
var loadingIndicator: some View {
if context.viewState.showLoading {
ProgressView()
.progressViewStyle(.circular)
.tint(.element.primaryContent)
.padding(16)
.background(Color.element.quinaryContent)
.cornerRadius(8)
}
}

var toolbar: some ToolbarContent {
ToolbarItem(placement: .navigationBarLeading) {
RoomHeaderView(context: context)
}
}

private func sendMessage() {
guard !context.viewState.sendButtonDisabled else { return }
context.send(viewAction: .sendMessage)
}
}
Expand Down
Loading