mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-10-31 08:15:24 +00:00
dffeb4f9bb
* Add HealthChat API and extended profile services Expose OpenID tokens and homeserver credentials from the client proxy, and add fork-only services for the HealthChat member directory backend (api.healthchat.ch) plus a service that reads MSC4133 extended profile fields directly from the homeserver. Groundwork for the members tab and the profile screens. Claude-Session: https://claude.ai/code/session_013qdTbAWsZj2quQzEkagXaS * Show extended profile details on the profile screens Add a UserProfileScreenHook that lets the fork inject an extra Form section below the header of the user profile and room member details screens. The HealthChat implementation loads the MSC4133 extended profile fields and renders them with tappable phone/email/website links, hiding the section entirely for profiles without any fields. Strings live in a new fork-only HealthChat.strings table (de/en/fr, generated as HealthChatL10n via SwiftGen) since Untranslated.strings pins the English bundle. Claude-Session: https://claude.ai/code/session_013qdTbAWsZj2quQzEkagXaS
202 lines
8.0 KiB
Swift
202 lines
8.0 KiB
Swift
//
|
|
// Copyright 2025 Element Creations Ltd.
|
|
// Copyright 2022-2025 New Vector Ltd.
|
|
//
|
|
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
|
// Please see LICENSE files in the repository root for full details.
|
|
//
|
|
|
|
import Combine
|
|
import MatrixRustSDK
|
|
import SwiftUI
|
|
|
|
typealias UserProfileScreenViewModelType = StateStoreViewModelV2<UserProfileScreenViewState, UserProfileScreenViewAction>
|
|
|
|
class UserProfileScreenViewModel: UserProfileScreenViewModelType, UserProfileScreenViewModelProtocol {
|
|
private let userSession: UserSessionProtocol
|
|
private let analytics: AnalyticsServiceProtocol
|
|
private let userIndicatorController: UserIndicatorControllerProtocol
|
|
|
|
private var actionsSubject: PassthroughSubject<UserProfileScreenViewModelAction, Never> = .init()
|
|
var actionsPublisher: AnyPublisher<UserProfileScreenViewModelAction, Never> {
|
|
actionsSubject.eraseToAnyPublisher()
|
|
}
|
|
|
|
init(userID: String,
|
|
isPresentedModally: Bool,
|
|
userSession: UserSessionProtocol,
|
|
analytics: AnalyticsServiceProtocol,
|
|
userIndicatorController: UserIndicatorControllerProtocol) {
|
|
self.userSession = userSession
|
|
self.analytics = analytics
|
|
self.userIndicatorController = userIndicatorController
|
|
|
|
let initialViewState = UserProfileScreenViewState(userID: userID,
|
|
isOwnUser: userID == userSession.clientProxy.userID,
|
|
isPresentedModally: isPresentedModally,
|
|
appHooks: appHooks,
|
|
bindings: .init())
|
|
|
|
super.init(initialViewState: userSession.userHooks.userProfileScreenHook.update(initialViewState),
|
|
mediaProvider: userSession.mediaProvider)
|
|
|
|
showLoadingIndicator(allowsInteraction: true)
|
|
Task {
|
|
await loadProfile()
|
|
hideLoadingIndicator()
|
|
}
|
|
}
|
|
|
|
// MARK: - Public
|
|
|
|
func stop() {
|
|
// Work around QLPreviewController dismissal issues, see the InteractiveQuickLookModifier.
|
|
state.bindings.mediaPreviewItem = nil
|
|
|
|
hideLoadingIndicator()
|
|
}
|
|
|
|
override func process(viewAction: UserProfileScreenViewAction) {
|
|
switch viewAction {
|
|
case .displayAvatar(let url):
|
|
Task { await displayFullScreenAvatar(url) }
|
|
case .openDirectChat:
|
|
openDirectChat()
|
|
case .createDirectChat:
|
|
Task { await createDirectChat() }
|
|
case .startCall(let roomID, let isVoiceCall):
|
|
Task { await startCall(roomID: roomID, isVoiceCall: isVoiceCall) }
|
|
case .dismiss:
|
|
actionsSubject.send(.dismiss)
|
|
}
|
|
}
|
|
|
|
// MARK: - Private
|
|
|
|
// The proxies aren't Sendable, fetch through these helpers so that
|
|
// they never leave the main actor when running calls in parallel.
|
|
|
|
private func fetchProfile() async -> Result<UserProfile, ClientProxyError> {
|
|
await userSession.clientProxy.profile(for: state.userID)
|
|
}
|
|
|
|
private func fetchUserIdentity() async -> Result<UserIdentityProxyProtocol?, ClientProxyError> {
|
|
await userSession.clientProxy.userIdentity(for: state.userID, fallBackToServer: true)
|
|
}
|
|
|
|
private func loadProfile() async {
|
|
async let profileResult = fetchProfile()
|
|
async let identityResult = fetchUserIdentity()
|
|
|
|
switch await profileResult {
|
|
case .success(let userProfile):
|
|
state.userProfile = userProfile
|
|
state.permalink = (try? matrixToUserPermalink(userId: state.userID)).flatMap(URL.init(string:))
|
|
|
|
switch userSession.clientProxy.directRoomForUserID(userProfile.id) {
|
|
case .success(let roomID):
|
|
state.dmRoomID = roomID
|
|
case .failure:
|
|
break
|
|
}
|
|
case .failure(let error):
|
|
state.bindings.alertInfo = .init(id: .unknown)
|
|
MXLog.error("Failed to find user profile: \(error)")
|
|
}
|
|
|
|
if case let .success(.some(identity)) = await identityResult {
|
|
state.isIdentityKnown = true
|
|
state.isVerified = identity.verificationState == .verified
|
|
} else {
|
|
state.isIdentityKnown = false
|
|
MXLog.error("Failed to find the user's identity.")
|
|
}
|
|
}
|
|
|
|
private func displayFullScreenAvatar(_ url: URL) async {
|
|
guard let userProfile = state.userProfile else { fatalError() }
|
|
|
|
showLoadingIndicator(allowsInteraction: false)
|
|
defer { hideLoadingIndicator() }
|
|
|
|
state.bindings.mediaPreviewItem = await MediaPreviewItem.load(from: url, title: userProfile.displayName, using: userSession.mediaProvider)
|
|
}
|
|
|
|
private func openDirectChat() {
|
|
guard let userProfile = state.userProfile else { fatalError() }
|
|
|
|
showLoadingIndicator(allowsInteraction: false)
|
|
defer { hideLoadingIndicator() }
|
|
|
|
switch userSession.clientProxy.directRoomForUserID(userProfile.id) {
|
|
case .success(let roomID):
|
|
if let roomID {
|
|
actionsSubject.send(.openDirectChat(roomID: roomID))
|
|
} else {
|
|
Task {
|
|
let isUnknown = if case let .success(identity) = await userSession.clientProxy.userIdentity(for: userProfile.id, fallBackToServer: false) {
|
|
identity == nil
|
|
} else {
|
|
true
|
|
}
|
|
state.bindings.inviteConfirmationUser = .init(user: userProfile, isUnknown: isUnknown)
|
|
}
|
|
}
|
|
case .failure:
|
|
state.bindings.alertInfo = .init(id: .failedOpeningDirectChat)
|
|
}
|
|
}
|
|
|
|
private func createDirectChat() async {
|
|
guard let userProfile = state.userProfile else { fatalError() }
|
|
|
|
showLoadingIndicator(allowsInteraction: false)
|
|
defer { hideLoadingIndicator() }
|
|
|
|
switch await userSession.clientProxy.createDirectRoom(with: userProfile.id, expectedRoomName: userProfile.displayName) {
|
|
case .success(let roomID):
|
|
analytics.trackCreatedRoom(isDM: true)
|
|
actionsSubject.send(.openDirectChat(roomID: roomID))
|
|
case .failure:
|
|
state.bindings.alertInfo = .init(id: .failedOpeningDirectChat)
|
|
}
|
|
}
|
|
|
|
private func startCall(roomID: String, isVoiceCall: Bool) async {
|
|
guard case let .joined(roomProxy) = await userSession.clientProxy.roomForIdentifier(roomID) else {
|
|
showErrorIndicator()
|
|
return
|
|
}
|
|
actionsSubject.send(.startCall(roomProxy: roomProxy, isVoiceCall: isVoiceCall))
|
|
}
|
|
|
|
// MARK: User Indicators
|
|
|
|
private var loadingIndicatorIdentifier: String {
|
|
"\(Self.self)-Loading"
|
|
}
|
|
|
|
private var statusIndicatorIdentifier: String {
|
|
"\(Self.self)-Status"
|
|
}
|
|
|
|
private func showLoadingIndicator(allowsInteraction: Bool) {
|
|
userIndicatorController.submitIndicator(UserIndicator(id: loadingIndicatorIdentifier,
|
|
type: .modal(progress: .indeterminate, interactiveDismissDisabled: false, allowsInteraction: allowsInteraction),
|
|
title: L10n.commonLoading,
|
|
persistent: true),
|
|
delay: .milliseconds(100))
|
|
}
|
|
|
|
private func hideLoadingIndicator() {
|
|
userIndicatorController.retractIndicatorWithId(loadingIndicatorIdentifier)
|
|
}
|
|
|
|
private func showErrorIndicator() {
|
|
userIndicatorController.submitIndicator(UserIndicator(id: statusIndicatorIdentifier,
|
|
type: .toast,
|
|
title: L10n.errorUnknown,
|
|
icon: \.close))
|
|
}
|
|
}
|