From 1adca8bbb1a5f4f0f781e0ba6fc903f9e3951b38 Mon Sep 17 00:00:00 2001 From: Ryan Zhao Date: Thu, 17 Aug 2023 14:31:15 +1000 Subject: [PATCH] add logic for setting display name --- Session/Onboarding/DisplayNameView.swift | 47 +++++++++++++++++++++++- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/Session/Onboarding/DisplayNameView.swift b/Session/Onboarding/DisplayNameView.swift index 8b9a11b0d..57c60129b 100644 --- a/Session/Onboarding/DisplayNameView.swift +++ b/Session/Onboarding/DisplayNameView.swift @@ -2,9 +2,11 @@ import SwiftUI import SessionUIKit +import SessionMessagingKit +import SignalUtilitiesKit struct DisplayNameView: View { - @Environment(\.viewController) private var viewControllerHolder: UIViewController? + @EnvironmentObject var host: HostWrapper @State private var displayName: String = "" @@ -54,7 +56,7 @@ struct DisplayNameView: View { Spacer() Button { - + register() } label: { Text("continue_2".localized()) .bold() @@ -76,6 +78,47 @@ struct DisplayNameView: View { } } } + + private func register() { + func showError(title: String, message: String = "") { + let modal: ConfirmationModal = ConfirmationModal( + targetView: self.host.controller?.view, + info: ConfirmationModal.Info( + title: title, + body: .text(message), + cancelTitle: "BUTTON_OK".localized(), + cancelStyle: .alert_text + ) + ) + self.host.controller?.present(modal, animated: true) + } + let displayName = self.displayName.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) + guard !displayName.isEmpty else { + return showError(title: "vc_display_name_display_name_missing_error".localized()) + } + guard !ProfileManager.isToLong(profileName: displayName) else { + return showError(title: "vc_display_name_display_name_too_long_error".localized()) + } + + // Try to save the user name but ignore the result + ProfileManager.updateLocal( + queue: .global(qos: .default), + profileName: displayName + ) + + // If we are not in the registration flow then we are finished and should go straight + // to the home screen + guard self.flow == .register else { + self.flow.completeRegistration() + + // Go to the home screen + let homeVC: HomeVC = HomeVC() + self.host.controller?.navigationController?.setViewControllers([ homeVC ], animated: true) + return + } + + // Need to get the PN mode if registering + } } struct DisplayNameView_Previews: PreviewProvider {