Handle external links to a user. (#2690)

This commit is contained in:
Doug
2024-04-15 11:08:00 +01:00
committed by GitHub
parent e0138eeb35
commit e7af7fb59c
22 changed files with 450 additions and 154 deletions
@@ -23,11 +23,12 @@ struct RoomMemberDetailsScreenCoordinatorParameters {
let clientProxy: ClientProxyProtocol
let mediaProvider: MediaProviderProtocol
let userIndicatorController: UserIndicatorControllerProtocol
let analytics: AnalyticsService
}
enum RoomMemberDetailsScreenCoordinatorAction {
case openUserProfile
case openDirectChat(displayName: String?)
case openDirectChat(roomID: String)
}
final class RoomMemberDetailsScreenCoordinator: CoordinatorProtocol {
@@ -45,7 +46,8 @@ final class RoomMemberDetailsScreenCoordinator: CoordinatorProtocol {
roomProxy: parameters.roomProxy,
clientProxy: parameters.clientProxy,
mediaProvider: parameters.mediaProvider,
userIndicatorController: parameters.userIndicatorController)
userIndicatorController: parameters.userIndicatorController,
analytics: parameters.analytics)
}
func start() {
@@ -55,8 +57,8 @@ final class RoomMemberDetailsScreenCoordinator: CoordinatorProtocol {
switch action {
case .openUserProfile:
actionsSubject.send(.openUserProfile)
case .openDirectChat(let displayName):
actionsSubject.send(.openDirectChat(displayName: displayName))
case .openDirectChat(let roomID):
actionsSubject.send(.openDirectChat(roomID: roomID))
}
}
.store(in: &cancellables)
@@ -18,7 +18,7 @@ import Foundation
enum RoomMemberDetailsScreenViewModelAction {
case openUserProfile
case openDirectChat(displayName: String?)
case openDirectChat(roomID: String)
}
struct RoomMemberDetailsScreenViewState: BindableState {
@@ -86,5 +86,6 @@ enum RoomMemberDetailsScreenViewAction {
}
enum RoomMemberDetailsScreenError: Hashable {
case failedOpeningDirectChat
case unknown
}
@@ -24,6 +24,7 @@ class RoomMemberDetailsScreenViewModel: RoomMemberDetailsScreenViewModelType, Ro
private let clientProxy: ClientProxyProtocol
private let mediaProvider: MediaProviderProtocol
private let userIndicatorController: UserIndicatorControllerProtocol
private let analytics: AnalyticsService
private var actionsSubject: PassthroughSubject<RoomMemberDetailsScreenViewModelAction, Never> = .init()
@@ -37,11 +38,13 @@ class RoomMemberDetailsScreenViewModel: RoomMemberDetailsScreenViewModelType, Ro
roomProxy: RoomProxyProtocol,
clientProxy: ClientProxyProtocol,
mediaProvider: MediaProviderProtocol,
userIndicatorController: UserIndicatorControllerProtocol) {
userIndicatorController: UserIndicatorControllerProtocol,
analytics: AnalyticsService) {
self.roomProxy = roomProxy
self.clientProxy = clientProxy
self.mediaProvider = mediaProvider
self.userIndicatorController = userIndicatorController
self.analytics = analytics
let initialViewState = RoomMemberDetailsScreenViewState(userID: userID, bindings: .init())
@@ -85,13 +88,9 @@ class RoomMemberDetailsScreenViewModel: RoomMemberDetailsScreenViewModelType, Ro
case .unignoreConfirmed:
Task { await unignoreUser() }
case .displayAvatar:
displayFullScreenAvatar()
Task { await displayFullScreenAvatar() }
case .openDirectChat:
guard let roomMemberProxy else {
fatalError()
}
actionsSubject.send(.openDirectChat(displayName: roomMemberProxy.displayName))
Task { await openDirectChat() }
}
}
@@ -145,7 +144,7 @@ class RoomMemberDetailsScreenViewModel: RoomMemberDetailsScreenViewModelType, Ro
}
}
private func displayFullScreenAvatar() {
private func displayFullScreenAvatar() async {
guard let roomMemberProxy else {
fatalError()
}
@@ -156,16 +155,32 @@ class RoomMemberDetailsScreenViewModel: RoomMemberDetailsScreenViewModelType, Ro
let loadingIndicatorIdentifier = "roomMemberAvatarLoadingIndicator"
userIndicatorController.submitIndicator(UserIndicator(id: loadingIndicatorIdentifier, type: .modal, title: L10n.commonLoading, persistent: true))
Task {
defer {
userIndicatorController.retractIndicatorWithId(loadingIndicatorIdentifier)
}
defer { userIndicatorController.retractIndicatorWithId(loadingIndicatorIdentifier) }
// We don't actually know the mime type here, assume it's an image.
if case let .success(file) = await mediaProvider.loadFileFromSource(.init(url: avatarURL, mimeType: "image/jpeg")) {
state.bindings.mediaPreviewItem = MediaPreviewItem(file: file, title: roomMemberProxy.displayName)
// We don't actually know the mime type here, assume it's an image.
if case let .success(file) = await mediaProvider.loadFileFromSource(.init(url: avatarURL, mimeType: "image/jpeg")) {
state.bindings.mediaPreviewItem = MediaPreviewItem(file: file, title: roomMemberProxy.displayName)
}
}
private func openDirectChat() async {
guard let roomMemberProxy else { fatalError() }
let loadingIndicatorIdentifier = "openDirectChatLoadingIndicator"
userIndicatorController.submitIndicator(UserIndicator(id: loadingIndicatorIdentifier,
type: .modal(progress: .indeterminate, interactiveDismissDisabled: true, allowsInteraction: false),
title: L10n.commonLoading,
persistent: true))
defer { userIndicatorController.retractIndicatorWithId(loadingIndicatorIdentifier) }
switch await clientProxy.createDirectRoomIfNeeded(with: roomMemberProxy.userID, expectedRoomName: roomMemberProxy.displayName) {
case .success((let roomID, let isNewRoom)):
if isNewRoom {
analytics.trackCreatedRoom(isDM: true)
}
actionsSubject.send(.openDirectChat(roomID: roomID))
case .failure:
state.bindings.alertInfo = .init(id: .failedOpeningDirectChat)
}
}
@@ -114,41 +114,9 @@ struct RoomMemberDetailsScreen: View {
// MARK: - Previews
struct RoomMemberDetailsScreen_Previews: PreviewProvider, TestablePreview {
static let otherUserViewModel = {
let member = RoomMemberProxyMock.mockDan
let roomProxyMock = RoomProxyMock(with: .init(name: ""))
roomProxyMock.getMemberUserIDReturnValue = .success(member)
return RoomMemberDetailsScreenViewModel(userID: member.userID,
roomProxy: roomProxyMock,
clientProxy: ClientProxyMock(.init()),
mediaProvider: MockMediaProvider(),
userIndicatorController: ServiceLocator.shared.userIndicatorController)
}()
static let accountOwnerViewModel = {
let member = RoomMemberProxyMock.mockMe
let roomProxyMock = RoomProxyMock(with: .init(name: ""))
roomProxyMock.getMemberUserIDReturnValue = .success(member)
return RoomMemberDetailsScreenViewModel(userID: member.userID,
roomProxy: roomProxyMock,
clientProxy: ClientProxyMock(.init()),
mediaProvider: MockMediaProvider(),
userIndicatorController: ServiceLocator.shared.userIndicatorController)
}()
static let ignoredUserViewModel = {
let member = RoomMemberProxyMock.mockIgnored
let roomProxyMock = RoomProxyMock(with: .init(name: ""))
roomProxyMock.getMemberUserIDReturnValue = .success(member)
return RoomMemberDetailsScreenViewModel(userID: member.userID,
roomProxy: roomProxyMock,
clientProxy: ClientProxyMock(.init()),
mediaProvider: MockMediaProvider(),
userIndicatorController: ServiceLocator.shared.userIndicatorController)
}()
static let otherUserViewModel = makeViewModel(member: .mockDan)
static let accountOwnerViewModel = makeViewModel(member: .mockMe)
static let ignoredUserViewModel = makeViewModel(member: .mockIgnored)
static var previews: some View {
RoomMemberDetailsScreen(context: otherUserViewModel.context)
@@ -161,4 +129,16 @@ struct RoomMemberDetailsScreen_Previews: PreviewProvider, TestablePreview {
.previewDisplayName("Ignored User")
.snapshot(delay: 0.25)
}
static func makeViewModel(member: RoomMemberProxyMock) -> RoomMemberDetailsScreenViewModel {
let roomProxyMock = RoomProxyMock(with: .init(name: ""))
roomProxyMock.getMemberUserIDReturnValue = .success(member)
return RoomMemberDetailsScreenViewModel(userID: member.userID,
roomProxy: roomProxyMock,
clientProxy: ClientProxyMock(.init()),
mediaProvider: MockMediaProvider(),
userIndicatorController: ServiceLocator.shared.userIndicatorController,
analytics: ServiceLocator.shared.analytics)
}
}
@@ -19,13 +19,16 @@ import SwiftUI
struct UserProfileScreenCoordinatorParameters {
let userID: String
let isPresentedModally: Bool
let clientProxy: ClientProxyProtocol
let mediaProvider: MediaProviderProtocol
let userIndicatorController: UserIndicatorControllerProtocol
let analytics: AnalyticsService
}
enum UserProfileScreenCoordinatorAction {
case openDirectChat(displayName: String?)
case openDirectChat(roomID: String)
case dismiss
}
final class UserProfileScreenCoordinator: CoordinatorProtocol {
@@ -40,9 +43,11 @@ final class UserProfileScreenCoordinator: CoordinatorProtocol {
init(parameters: UserProfileScreenCoordinatorParameters) {
viewModel = UserProfileScreenViewModel(userID: parameters.userID,
isPresentedModally: parameters.isPresentedModally,
clientProxy: parameters.clientProxy,
mediaProvider: parameters.mediaProvider,
userIndicatorController: parameters.userIndicatorController)
userIndicatorController: parameters.userIndicatorController,
analytics: parameters.analytics)
}
func start() {
@@ -50,8 +55,10 @@ final class UserProfileScreenCoordinator: CoordinatorProtocol {
guard let self else { return }
switch action {
case .openDirectChat(let displayName):
actionsSubject.send(.openDirectChat(displayName: displayName))
case .openDirectChat(let roomID):
actionsSubject.send(.openDirectChat(roomID: roomID))
case .dismiss:
actionsSubject.send(.dismiss)
}
}
.store(in: &cancellables)
@@ -17,12 +17,14 @@
import Foundation
enum UserProfileScreenViewModelAction {
case openDirectChat(displayName: String?)
case openDirectChat(roomID: String)
case dismiss
}
struct UserProfileScreenViewState: BindableState {
let userID: String
let isOwnUser: Bool
let isPresentedModally: Bool
var userProfile: UserProfileProxy?
var permalink: URL?
@@ -40,8 +42,10 @@ struct UserProfileScreenViewStateBindings {
enum UserProfileScreenViewAction {
case displayAvatar
case openDirectChat
case dismiss
}
enum UserProfileScreenError: Hashable {
case failedOpeningDirectChat
case unknown
}
@@ -24,6 +24,7 @@ class UserProfileScreenViewModel: UserProfileScreenViewModelType, UserProfileScr
private let clientProxy: ClientProxyProtocol
private let mediaProvider: MediaProviderProtocol
private let userIndicatorController: UserIndicatorControllerProtocol
private let analytics: AnalyticsService
private var actionsSubject: PassthroughSubject<UserProfileScreenViewModelAction, Never> = .init()
var actionsPublisher: AnyPublisher<UserProfileScreenViewModelAction, Never> {
@@ -31,23 +32,27 @@ class UserProfileScreenViewModel: UserProfileScreenViewModelType, UserProfileScr
}
init(userID: String,
isPresentedModally: Bool,
clientProxy: ClientProxyProtocol,
mediaProvider: MediaProviderProtocol,
userIndicatorController: UserIndicatorControllerProtocol) {
userIndicatorController: UserIndicatorControllerProtocol,
analytics: AnalyticsService) {
self.clientProxy = clientProxy
self.mediaProvider = mediaProvider
self.userIndicatorController = userIndicatorController
self.analytics = analytics
let initialViewState = UserProfileScreenViewState(userID: userID,
isOwnUser: userID == clientProxy.userID,
isPresentedModally: isPresentedModally,
bindings: .init())
super.init(initialViewState: initialViewState, imageProvider: mediaProvider)
showMemberLoadingIndicator()
showLoadingIndicator(allowsInteraction: true)
Task {
defer {
hideMemberLoadingIndicator()
hideLoadingIndicator()
}
switch await clientProxy.profile(for: userID) {
@@ -67,37 +72,49 @@ class UserProfileScreenViewModel: UserProfileScreenViewModelType, UserProfileScr
// Work around QLPreviewController dismissal issues, see the InteractiveQuickLookModifier.
state.bindings.mediaPreviewItem = nil
hideMemberLoadingIndicator()
hideLoadingIndicator()
}
override func process(viewAction: UserProfileScreenViewAction) {
switch viewAction {
case .displayAvatar:
displayFullScreenAvatar()
Task { await displayFullScreenAvatar() }
case .openDirectChat:
guard let userProfile = state.userProfile else { fatalError() }
actionsSubject.send(.openDirectChat(displayName: userProfile.displayName))
Task { await openDirectChat() }
case .dismiss:
actionsSubject.send(.dismiss)
}
}
// MARK: - Private
private func displayFullScreenAvatar() {
private func displayFullScreenAvatar() async {
guard let userProfile = state.userProfile else { fatalError() }
guard let avatarURL = userProfile.avatarURL else { return }
let loadingIndicatorIdentifier = "roomMemberAvatarLoadingIndicator"
userIndicatorController.submitIndicator(UserIndicator(id: loadingIndicatorIdentifier, type: .modal, title: L10n.commonLoading, persistent: true))
showLoadingIndicator(allowsInteraction: false)
defer { hideLoadingIndicator() }
Task {
defer {
userIndicatorController.retractIndicatorWithId(loadingIndicatorIdentifier)
}
// We don't actually know the mime type here, assume it's an image.
if case let .success(file) = await mediaProvider.loadFileFromSource(.init(url: avatarURL, mimeType: "image/jpeg")) {
state.bindings.mediaPreviewItem = MediaPreviewItem(file: file, title: userProfile.displayName)
}
}
private func openDirectChat() async {
guard let userProfile = state.userProfile else { fatalError() }
showLoadingIndicator(allowsInteraction: false)
defer { hideLoadingIndicator() }
// We don't actually know the mime type here, assume it's an image.
if case let .success(file) = await mediaProvider.loadFileFromSource(.init(url: avatarURL, mimeType: "image/jpeg")) {
state.bindings.mediaPreviewItem = MediaPreviewItem(file: file, title: userProfile.displayName)
switch await clientProxy.createDirectRoomIfNeeded(with: userProfile.userID, expectedRoomName: userProfile.displayName) {
case .success((let roomID, let isNewRoom)):
if isNewRoom {
analytics.trackCreatedRoom(isDM: true)
}
actionsSubject.send(.openDirectChat(roomID: roomID))
case .failure:
state.bindings.alertInfo = .init(id: .failedOpeningDirectChat)
}
}
@@ -105,15 +122,15 @@ class UserProfileScreenViewModel: UserProfileScreenViewModelType, UserProfileScr
private static let loadingIndicatorIdentifier = "\(UserProfileScreenViewModel.self)-Loading"
private func showMemberLoadingIndicator() {
private func showLoadingIndicator(allowsInteraction: Bool) {
userIndicatorController.submitIndicator(UserIndicator(id: Self.loadingIndicatorIdentifier,
type: .modal(progress: .indeterminate, interactiveDismissDisabled: false, allowsInteraction: true),
type: .modal(progress: .indeterminate, interactiveDismissDisabled: false, allowsInteraction: allowsInteraction),
title: L10n.commonLoading,
persistent: true),
delay: .milliseconds(100))
}
private func hideMemberLoadingIndicator() {
private func hideLoadingIndicator() {
userIndicatorController.retractIndicatorWithId(Self.loadingIndicatorIdentifier)
}
}
@@ -30,6 +30,8 @@ struct UserProfileScreen: View {
}
.compoundList()
.navigationTitle(L10n.screenRoomMemberDetailsTitle)
.navigationBarTitleDisplayMode(.inline)
.toolbar { toolbar }
.alert(item: $context.alertInfo)
.track(screen: .User)
.interactiveQuickLook(item: $context.mediaPreviewItem, shouldHideControls: true)
@@ -73,6 +75,17 @@ struct UserProfileScreen: View {
.accessibilityIdentifier(A11yIdentifiers.roomMemberDetailsScreen.directChat)
}
}
@ToolbarContentBuilder
private var toolbar: some ToolbarContent {
if context.viewState.isPresentedModally {
ToolbarItem(placement: .confirmationAction) {
Button(L10n.actionDone) {
context.send(viewAction: .dismiss)
}
}
}
}
}
// MARK: - Previews
@@ -92,8 +105,10 @@ struct UserProfileScreen_Previews: PreviewProvider, TestablePreview {
static func makeViewModel(userID: String) -> UserProfileScreenViewModel {
UserProfileScreenViewModel(userID: userID,
isPresentedModally: false,
clientProxy: ClientProxyMock(.init()),
mediaProvider: MockMediaProvider(),
userIndicatorController: ServiceLocator.shared.userIndicatorController)
userIndicatorController: ServiceLocator.shared.userIndicatorController,
analytics: ServiceLocator.shared.analytics)
}
}