Skip to content

Commit

Permalink
Merge pull request #891 from tchapgouv/837-inviter-a-rejoindre-tchap-…
Browse files Browse the repository at this point in the history
…erreur-avec-le-mainlining-1-83

837 inviter a rejoindre tchap erreur avec le mainlining 1 83
  • Loading branch information
NicolasBuquet authored Oct 12, 2023
2 parents 5109571 + 52e76b0 commit ff44486
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 424 deletions.
3 changes: 0 additions & 3 deletions Riot/Modules/Application/AppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -591,9 +591,6 @@ extension AppCoordinator: SplitViewCoordinatorDelegate {
// MARK: - SideMenuCoordinatorDelegate
extension AppCoordinator: SideMenuCoordinatorDelegate {
func sideMenuCoordinator(_ coordinator: SideMenuCoordinatorType, didTapMenuItem menuItem: SideMenuItem, fromSourceView sourceView: UIView) {
if menuItem == .inviteFriends {
self.splitViewCoordinator?.presentInvitePeople()
}
}
}

Expand Down
95 changes: 9 additions & 86 deletions Riot/Modules/Home/AllChats/AllChatsCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ class AllChatsCoordinatorParameters {
}

class AllChatsCoordinator: NSObject, SplitViewMasterCoordinatorProtocol {
func presentInvitePeople() {
//
}


// MARK: Properties

// MARK: Private
Expand Down Expand Up @@ -80,8 +75,6 @@ class AllChatsCoordinator: NSObject, SplitViewMasterCoordinatorProtocol {

private var indicators = [UserIndicator]()
private var signOutFlowPresenter: SignOutFlowPresenter?
// Tchap: Add invite service for user invitation
private var inviteService: InviteServiceType?
private var errorPresenter: ErrorPresenter?
private weak var currentAlertController: UIAlertController?

Expand Down Expand Up @@ -361,7 +354,7 @@ class AllChatsCoordinator: NSObject, SplitViewMasterCoordinatorProtocol {
!UserService.isExternalUser(for: userID) {
subMenuActions.append(UIAction(title: TchapL10n.sideMenuActionInviteFriends, image: UIImage(systemName: "square.and.arrow.up.fill")) { [weak self] action in
guard let self = self else { return }
self.showInviteFriends(from: self.avatarMenuButton)
self.allChatsViewController.startChat()
})
}
}
Expand Down Expand Up @@ -653,17 +646,14 @@ class AllChatsCoordinator: NSObject, SplitViewMasterCoordinatorProtocol {
// }

private func showInviteFriends(from sourceView: UIView?) {
// Tchap: Use Tchap specific mechanism.
promptUserToFillAnEmailToInvite { [weak self] email in
self?.sendEmailInvite(to: email)
}

// let myUserId = self.parameters.userSessionsService.mainUserSession?.userId ?? ""
//
// let inviteFriendsPresenter = InviteFriendsPresenter()
// inviteFriendsPresenter.present(for: myUserId, from: self.navigationRouter.toPresentable(), sourceView: sourceView, animated: true)
}

// Tchap: commented because Tchap now uses Element Direct Message mechanism

// let myUserId = self.parameters.userSessionsService.mainUserSession?.userId ?? ""
//
// let inviteFriendsPresenter = InviteFriendsPresenter()
// inviteFriendsPresenter.present(for: myUserId, from: self.navigationRouter.toPresentable(), sourceView: sourceView, animated: true)
}

private func showBugReport() {
let bugReportViewController = BugReportViewController()

Expand Down Expand Up @@ -918,73 +908,6 @@ extension AllChatsCoordinator {

self.navigationRouter.toPresentable().present(alertController, animated: true)
}

private func sendEmailInvite(to email: String) {
guard let session = self.currentMatrixSession else { return }
if self.inviteService == nil {
self.inviteService = InviteService(session: session)
}
guard let inviteService = self.inviteService else { return }

self.activityIndicatorPresenter.presentActivityIndicator(on: self.navigationRouter.toPresentable().view, animated: true)
inviteService.sendEmailInvite(to: email) { [weak self] (response) in
guard let sself = self else {
return
}

sself.activityIndicatorPresenter.removeCurrentActivityIndicator(animated: true)
switch response {
case .success(let result):
var message: String
var discoveredUserID: String?
switch result {
case .inviteHasBeenSent(roomID: _):
message = TchapL10n.inviteSendingSucceeded
case .inviteAlreadySent(roomID: _):
message = TchapL10n.inviteAlreadySentByEmail(email)
case .inviteIgnoredForDiscoveredUser(userID: let userID):
discoveredUserID = userID
message = TchapL10n.inviteNotSentForDiscoveredUser
case .inviteIgnoredForUnauthorizedEmail:
message = TchapL10n.inviteNotSentForUnauthorizedEmail(email)
}

sself.currentAlertController?.dismiss(animated: false)

let alert = UIAlertController(title: TchapL10n.inviteInformationTitle, message: message, preferredStyle: .alert)

let okTitle = VectorL10n.ok
let okAction = UIAlertAction(title: okTitle, style: .default, handler: { action in
if let userID = discoveredUserID {
// Open the discussion
AppDelegate.theDelegate().startDirectChat(withUserId: userID, completion: nil)
}
})
alert.addAction(okAction)
sself.currentAlertController = alert

sself.navigationRouter.toPresentable().present(alert, animated: true, completion: nil)
case .failure(let error):
let errorPresentable = sself.inviteErrorPresentable(from: error)
sself.errorPresenter?.present(errorPresentable: errorPresentable, animated: true)
}
}
}

private func inviteErrorPresentable(from error: Error) -> ErrorPresentable {
let errorTitle = TchapL10n.inviteSendingFailedTitle
let errorMessage: String

let nsError = error as NSError

if let message = nsError.userInfo[NSLocalizedDescriptionKey] as? String {
errorMessage = message
} else {
errorMessage = TchapL10n.errorMessageDefault
}

return ErrorPresentableImpl(title: errorTitle, message: errorMessage)
}
}

// Tchap: Add delegate for Room Preview
Expand Down
2 changes: 2 additions & 0 deletions Riot/Modules/SideMenu/SideMenuCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ final class SideMenuCoordinator: NSObject, SideMenuCoordinatorType {
}

private func showInviteFriends(from sourceView: UIView?) {
// Tchap: commented because Tchap now uses Element Direct Message mechanism

// let myUserId = self.parameters.userSessionsService.mainUserSession?.userId ?? ""
//
// let inviteFriendsPresenter = InviteFriendsPresenter()
Expand Down
4 changes: 0 additions & 4 deletions Riot/Modules/SplitView/SplitViewCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,6 @@ final class SplitViewCoordinator: NSObject, SplitViewCoordinatorType {
func showAppStateIndicator(with text: String, icon: UIImage?, action: ToastViewState.Action? = nil) {
masterCoordinator?.showAppStateIndicator(with: text, icon: icon, action: action) // Tchap : add tap action
}

func presentInvitePeople() {
masterCoordinator?.presentInvitePeople()
}

// MARK: - Private methods

Expand Down
4 changes: 0 additions & 4 deletions Riot/Modules/SplitView/SplitViewCoordinatorType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,4 @@ protocol SplitViewCoordinatorType: Coordinator, Presentable {

/// Hide the message related to the application state currently displayed.
func hideAppStateIndicator()

// Tchap: redirect to invite people alert
/// Present invite people alert (with textField)
func presentInvitePeople()
}
4 changes: 0 additions & 4 deletions Riot/Modules/TabBar/SplitViewMasterCoordinatorProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,4 @@ protocol SplitViewMasterCoordinatorProtocol: Coordinator, SplitViewMasterPresent

/// Hide the message related to the application state currently displayed.
func hideAppStateIndicator()

// Tchap: redirect to invite people alert
/// Present invite people alert (with textField)
func presentInvitePeople()
}
87 changes: 7 additions & 80 deletions Riot/Modules/TabBar/TabBarCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ final class TabBarCoordinator: NSObject, SplitViewMasterCoordinatorProtocol {
}

private var indicators = [UserIndicator]()
// Tchap: Add invite service for user invitation
private var inviteService: InviteServiceType?
private var errorPresenter: ErrorPresenter?
private weak var currentAlertController: UIAlertController?

Expand Down Expand Up @@ -239,12 +237,6 @@ final class TabBarCoordinator: NSObject, SplitViewMasterCoordinatorProtocol {
appSateIndicator = nil
}

func presentInvitePeople() {
promptUserToFillAnEmailToInvite { [weak self] email in
self?.sendEmailInvite(to: email)
}
}

// MARK: - SplitViewMasterPresentable

var selectedNavigationRouter: NavigationRouterType? {
Expand All @@ -263,10 +255,12 @@ final class TabBarCoordinator: NSObject, SplitViewMasterCoordinatorProtocol {
}

private func showInviteFriends(from sourceView: UIView?) {
let myUserId = self.parameters.userSessionsService.mainUserSession?.userId ?? ""

let inviteFriendsPresenter = InviteFriendsPresenter()
inviteFriendsPresenter.present(for: myUserId, from: self.navigationRouter.toPresentable(), sourceView: sourceView, animated: true)
// Tchap: commented because Tchap now uses Element Direct Message mechanism

// let myUserId = self.parameters.userSessionsService.mainUserSession?.userId ?? ""
//
// let inviteFriendsPresenter = InviteFriendsPresenter()
// inviteFriendsPresenter.present(for: myUserId, from: self.navigationRouter.toPresentable(), sourceView: sourceView, animated: true)
}

private func showBugReport() {
Expand Down Expand Up @@ -798,7 +792,7 @@ final class TabBarCoordinator: NSObject, SplitViewMasterCoordinatorProtocol {
var subMenuActions: [UIAction] = []
if BuildSettings.sideMenuShowInviteFriends {
subMenuActions.append(UIAction(title: VectorL10n.inviteTo(AppInfo.current.displayName), image: UIImage(systemName: "envelope")) { [weak self] action in
self?.showInviteFriends(from: nil)
self?.showInviteFriends(from: nil)
})
}

Expand Down Expand Up @@ -1019,73 +1013,6 @@ extension TabBarCoordinator {

masterTabBarController.present(alertController, animated: true)
}

private func sendEmailInvite(to email: String) {
guard let session = self.currentMatrixSession else { return }
if self.inviteService == nil {
self.inviteService = InviteService(session: session)
}
guard let inviteService = self.inviteService else { return }

self.activityIndicatorPresenter.presentActivityIndicator(on: masterTabBarController.view, animated: true)
inviteService.sendEmailInvite(to: email) { [weak self] (response) in
guard let sself = self else {
return
}

sself.activityIndicatorPresenter.removeCurrentActivityIndicator(animated: true)
switch response {
case .success(let result):
var message: String
var discoveredUserID: String?
switch result {
case .inviteHasBeenSent(roomID: _):
message = TchapL10n.inviteSendingSucceeded
case .inviteAlreadySent(roomID: _):
message = TchapL10n.inviteAlreadySentByEmail(email)
case .inviteIgnoredForDiscoveredUser(userID: let userID):
discoveredUserID = userID
message = TchapL10n.inviteNotSentForDiscoveredUser
case .inviteIgnoredForUnauthorizedEmail:
message = TchapL10n.inviteNotSentForUnauthorizedEmail(email)
}

sself.currentAlertController?.dismiss(animated: false)

let alert = UIAlertController(title: TchapL10n.inviteInformationTitle, message: message, preferredStyle: .alert)

let okTitle = VectorL10n.ok
let okAction = UIAlertAction(title: okTitle, style: .default, handler: { action in
if let userID = discoveredUserID {
// Open the discussion
AppDelegate.theDelegate().startDirectChat(withUserId: userID, completion: nil)
}
})
alert.addAction(okAction)
sself.currentAlertController = alert

sself.masterTabBarController.present(alert, animated: true, completion: nil)
case .failure(let error):
let errorPresentable = sself.inviteErrorPresentable(from: error)
sself.errorPresenter?.present(errorPresentable: errorPresentable, animated: true)
}
}
}

private func inviteErrorPresentable(from error: Error) -> ErrorPresentable {
let errorTitle = TchapL10n.inviteSendingFailedTitle
let errorMessage: String

let nsError = error as NSError

if let message = nsError.userInfo[NSLocalizedDescriptionKey] as? String {
errorMessage = message
} else {
errorMessage = TchapL10n.errorMessageDefault
}

return ErrorPresentableImpl(title: errorTitle, message: errorMessage)
}
}

// MARK: - MasterTabBarControllerDelegate
Expand Down
Loading

0 comments on commit ff44486

Please sign in to comment.