-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(OnboardingViewController): Abstracted away the post login an…
…d register code to be able to use it from elsewhere. feat: UpsaleViewController actions are performing the auth actions like expected.
- Loading branch information
1 parent
93bbb8e
commit 28552a8
Showing
6 changed files
with
183 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
Infomaniak kDrive - iOS App | ||
Copyright (C) 2024 Infomaniak Network SA | ||
|
||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
|
||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
|
||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import CocoaLumberjackSwift | ||
import InfomaniakCore | ||
import InfomaniakDI | ||
import InfomaniakLogin | ||
import kDriveCore | ||
import kDriveResources | ||
import Lottie | ||
import UIKit | ||
|
||
/// Something to abstract away from the view the post login/register logic | ||
public class LoginDelegateHandler: InfomaniakLoginDelegate { | ||
@LazyInjectService var accountManager: AccountManageable | ||
@LazyInjectService var router: AppNavigable | ||
|
||
var didStartLoginCompletion: (() -> Void)? | ||
var didCompleteLoginCompletion: (() -> Void)? | ||
var didFailLoginWithErrorCompletion: ((Error) -> Void)? | ||
|
||
public init(didCompleteLoginCompletion: (() -> Void)? = nil) { | ||
self.didCompleteLoginCompletion = didCompleteLoginCompletion | ||
} | ||
|
||
@MainActor public func didCompleteLoginWith(code: String, verifier: String) { | ||
guard let topMostViewController = router.topMostViewController else { return } | ||
|
||
MatomoUtils.track(eventWithCategory: .account, name: "loggedIn") | ||
let previousAccount = accountManager.currentAccount | ||
|
||
didStartLoginCompletion?() | ||
|
||
Task { | ||
do { | ||
_ = try await accountManager.createAndSetCurrentAccount(code: code, codeVerifier: verifier) | ||
guard let currentDriveFileManager = accountManager.currentDriveFileManager else { | ||
throw DriveError.NoDriveError.noDriveFileManager | ||
} | ||
|
||
MatomoUtils.connectUser() | ||
goToMainScreen(with: currentDriveFileManager) | ||
} catch { | ||
didCompleteLoginWithError(error, previousAccount: previousAccount, topMostViewController: topMostViewController) | ||
} | ||
|
||
didCompleteLoginCompletion?() | ||
} | ||
} | ||
|
||
@MainActor private func goToMainScreen(with driveFileManager: DriveFileManager) { | ||
UserDefaults.shared.legacyIsFirstLaunch = false | ||
UserDefaults.shared.numberOfConnections = 1 | ||
_ = router.showMainViewController(driveFileManager: driveFileManager, selectedIndex: nil) | ||
} | ||
|
||
private func didCompleteLoginWithError(_ error: Error, | ||
previousAccount: Account?, | ||
topMostViewController: UIViewController) { | ||
DDLogError("Error on didCompleteLoginWith \(error)") | ||
|
||
if let previousAccount { | ||
accountManager.switchAccount(newAccount: previousAccount) | ||
} | ||
|
||
if let noDriveError = error as? InfomaniakCore.ApiError, noDriveError.code == DriveError.noDrive.code { | ||
let driveErrorVC = DriveErrorViewController.instantiate(errorType: .noDrive, drive: nil) | ||
topMostViewController.present(driveErrorVC, animated: true) | ||
} else if let driveError = error as? DriveError, | ||
driveError == .noDrive | ||
|| driveError == .productMaintenance | ||
|| driveError == .driveMaintenance | ||
|| driveError == .blocked { | ||
let errorViewType: DriveErrorViewController.DriveErrorViewType | ||
switch driveError { | ||
case .productMaintenance, .driveMaintenance: | ||
errorViewType = .maintenance | ||
case .blocked: | ||
errorViewType = .blocked | ||
default: | ||
errorViewType = .noDrive | ||
} | ||
|
||
let driveErrorVC = DriveErrorViewController.instantiate(errorType: errorViewType, drive: nil) | ||
topMostViewController.present(driveErrorVC, animated: true) | ||
} else { | ||
let metadata = [ | ||
"Underlying Error": error.asAFError?.underlyingError.debugDescription ?? "Not an AFError" | ||
] | ||
SentryDebug.capture(error: error, context: metadata, contextKey: "Error") | ||
|
||
topMostViewController.okAlert( | ||
title: KDriveResourcesStrings.Localizable.errorTitle, | ||
message: KDriveResourcesStrings.Localizable.errorConnection | ||
) | ||
} | ||
} | ||
|
||
public func didFailLoginWith(error: Error) { | ||
didFailLoginWithErrorCompletion?(error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters