Tapping on user avatar/name in the timeline opens the room member details (#1020)

* Implementation completed

* changelog

* code improvement

* Apply suggestions from code review

Co-authored-by: Stefan Ceriu <stefanc@matrix.org>

* pr suggestions

---------

Co-authored-by: Stefan Ceriu <stefanc@matrix.org>
This commit is contained in:
Mauro
2023-06-06 10:46:04 +02:00
committed by GitHub
parent ea4aa943e0
commit 4b2aba9367
24 changed files with 371 additions and 61 deletions
@@ -102,7 +102,7 @@ private class PreviewItem: NSObject, QLPreviewItem {
// MARK: - Previews
struct MediaUploadPreviewScreen_Previews: PreviewProvider {
static let viewModel = MediaUploadPreviewScreenViewModel(userIndicatorController: MockUserIndicatorController(),
static let viewModel = MediaUploadPreviewScreenViewModel(userIndicatorController: UserIndicatorControllerMock.default,
roomProxy: RoomProxyMock(),
mediaUploadingPreprocessor: MediaUploadingPreprocessor(),
title: nil,
@@ -165,7 +165,7 @@ struct RoomDetailsEditScreen_Previews: PreviewProvider {
static let viewModel = RoomDetailsEditScreenViewModel(accountOwner: RoomMemberProxyMock.mockAlice,
mediaProvider: MockMediaProvider(),
roomProxy: RoomProxyMock(with: .init(name: "Room", displayName: "Room")),
userIndicatorController: MockUserIndicatorController())
userIndicatorController: UserIndicatorControllerMock.default)
static var previews: some View {
NavigationStack {
@@ -31,6 +31,7 @@ enum RoomScreenCoordinatorAction {
case presentMediaUploadPreviewScreen(URL)
case presentRoomDetails
case presentEmojiPicker(itemID: String)
case presentRoomMemberDetails(member: RoomMemberProxyProtocol)
}
final class RoomScreenCoordinator: CoordinatorProtocol {
@@ -74,6 +75,8 @@ final class RoomScreenCoordinator: CoordinatorProtocol {
actionsSubject.send(.presentMediaUploadPicker(.documents))
case .displayMediaUploadPreviewScreen(let url):
actionsSubject.send(.presentMediaUploadPreviewScreen(url))
case .displayRoomMemberDetails(let member):
actionsSubject.send(.presentRoomMemberDetails(member: member))
}
}
}
@@ -27,6 +27,7 @@ enum RoomScreenViewModelAction {
case displayMediaPicker
case displayDocumentPicker
case displayMediaUploadPreviewScreen(url: URL)
case displayRoomMemberDetails(member: RoomMemberProxyProtocol)
}
enum RoomScreenComposerMode: Equatable {
@@ -65,6 +66,7 @@ enum RoomScreenViewAction {
case displayDocumentPicker
case handlePasteOrDrop(provider: NSItemProvider)
case tappedOnUser(userID: String)
}
struct RoomScreenViewState: BindableState {
@@ -29,12 +29,16 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
private let roomProxy: RoomProxyProtocol
private let timelineController: RoomTimelineControllerProtocol
private unowned let userIndicatorController: UserIndicatorControllerProtocol
private var loadingTask: Task<Void, Never>?
init(timelineController: RoomTimelineControllerProtocol,
mediaProvider: MediaProviderProtocol,
roomProxy: RoomProxyProtocol) {
roomProxy: RoomProxyProtocol,
userIndicatorController: UserIndicatorControllerProtocol = ServiceLocator.shared.userIndicatorController) {
self.roomProxy = roomProxy
self.timelineController = timelineController
self.userIndicatorController = userIndicatorController
super.init(initialViewState: RoomScreenViewState(roomId: timelineController.roomID,
roomTitle: roomProxy.roomTitle,
@@ -130,6 +134,8 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
callback?(.displayDocumentPicker)
case .handlePasteOrDrop(let provider):
handlePasteOrDrop(provider)
case .tappedOnUser(userID: let userID):
Task { await handleTappedUser(userID: userID) }
}
}
@@ -246,10 +252,10 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
title: L10n.commonError,
message: message)
case .toast(let message):
ServiceLocator.shared.userIndicatorController.submitIndicator(UserIndicator(id: Constants.toastErrorID,
type: .toast,
title: message,
iconName: "xmark"))
userIndicatorController.submitIndicator(UserIndicator(id: Constants.toastErrorID,
type: .toast,
title: message,
iconName: "xmark"))
}
}
@@ -376,9 +382,9 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
_ = provider.loadDataRepresentation(for: contentType) { data, error in
Task { @MainActor in
let loadingIndicatorIdentifier = UUID().uuidString
ServiceLocator.shared.userIndicatorController.submitIndicator(UserIndicator(id: loadingIndicatorIdentifier, type: .modal, title: L10n.commonLoading, persistent: true))
self.userIndicatorController.submitIndicator(UserIndicator(id: loadingIndicatorIdentifier, type: .modal, title: L10n.commonLoading, persistent: true))
defer {
ServiceLocator.shared.userIndicatorController.retractIndicatorWithId(loadingIndicatorIdentifier)
self.userIndicatorController.retractIndicatorWithId(loadingIndicatorIdentifier)
}
if let error {
@@ -421,6 +427,37 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
return messageItem.contentType
}
private func handleTappedUser(userID: String) async {
// This is generally fast but it could take some time for rooms with thousands of users on first load
// Show a loader only if it takes more than 0.1 seconds
loadingTask = showLoadingIndicator(with: .milliseconds(100))
let result = await roomProxy.getMember(userID: userID)
loadingTask?.cancel()
hideLoadingIndicator()
switch result {
case .success(let member):
callback?(.displayRoomMemberDetails(member: member))
case .failure(let error):
displayError(.alert(L10n.screenRoomErrorFailedRetrievingUserDetails))
MXLog.error("Failed retrieving the user given the following id \(userID) with error: \(error)")
}
}
private static let loadingIndicatorIdentifier = "RoomScreenLoadingIndicator"
private func showLoadingIndicator(with delay: Duration) -> Task<Void, Never> {
userIndicatorController.submitIndicator(UserIndicator(id: Self.loadingIndicatorIdentifier,
type: .modal(interactiveDismissDisabled: true),
title: L10n.commonLoading,
persistent: true),
delay: delay)
}
private func hideLoadingIndicator() {
userIndicatorController.retractIndicatorWithId(Self.loadingIndicatorIdentifier)
}
}
// MARK: - Mocks
@@ -68,6 +68,9 @@ struct TimelineItemBubbledStylerView<Content: View>: View {
.padding(.vertical, senderNameVerticalPadding)
}
.accessibilityElement(children: .combine)
.onTapGesture {
context.send(viewAction: .tappedOnUser(userID: timelineItem.sender.id))
}
}
}
}
@@ -70,12 +70,17 @@ struct TimelineItemPlainStylerView<Content: View>: View {
private var header: some View {
if shouldShowSenderDetails {
HStack {
TimelineSenderAvatarView(timelineItem: timelineItem)
Text(timelineItem.sender.displayName ?? timelineItem.sender.id)
.font(.subheadline)
.foregroundColor(.element.primaryContent)
.fontWeight(.semibold)
.lineLimit(1)
HStack {
TimelineSenderAvatarView(timelineItem: timelineItem)
Text(timelineItem.sender.displayName ?? timelineItem.sender.id)
.font(.subheadline)
.foregroundColor(.element.primaryContent)
.fontWeight(.semibold)
.lineLimit(1)
}
.onTapGesture {
context.send(viewAction: .tappedOnUser(userID: timelineItem.sender.id))
}
Spacer()
Text(timelineItem.timestamp)
.foregroundColor(Color.element.tertiaryContent)