Skip to content

Commit d3e334a

Browse files
committed
feat: Use common Drive fetch method
1 parent e7a52a7 commit d3e334a

File tree

5 files changed

+15
-13
lines changed

5 files changed

+15
-13
lines changed

kDrive/AppRouter.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ public struct AppRouter: AppNavigable {
606606
return
607607
}
608608

609-
let driveFileManager = try accountManager.getFirstAvailableDriveFileManager(for: account.userId)
609+
let driveFileManager = try await accountManager.getBestAvailableDriveFileManager(for: account)
610610
let drive = driveFileManager.drive
611611
accountManager.setCurrentDriveForCurrentAccount(for: drive.id, userId: drive.userId)
612612
showMainViewController(driveFileManager: driveFileManager, selectedIndex: nil)

kDrive/UI/Controller/Menu/SwitchUserViewController.swift

+7-5
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,14 @@ class SwitchUserViewController: UIViewController {
8686

8787
private func switchToConnectedAccount(_ account: Account) {
8888
do {
89-
let driveFileManager = try accountManager.getFirstAvailableDriveFileManager(for: account.userId)
90-
MatomoUtils.track(eventWithCategory: .account, name: "switch")
91-
MatomoUtils.connectUser()
89+
Task { @MainActor in
90+
let driveFileManager = try await accountManager.getBestAvailableDriveFileManager(for: account)
91+
MatomoUtils.track(eventWithCategory: .account, name: "switch")
92+
MatomoUtils.connectUser()
9293

93-
accountManager.switchAccount(newAccount: account)
94-
appNavigable.showMainViewController(driveFileManager: driveFileManager, selectedIndex: nil)
94+
accountManager.switchAccount(newAccount: account)
95+
appNavigable.showMainViewController(driveFileManager: driveFileManager, selectedIndex: nil)
96+
}
9597
} catch DriveError.NoDriveError.noDrive {
9698
let driveErrorNavigationViewController = DriveErrorViewController.instantiateInNavigationController(
9799
errorType: .noDrive,

kDrive/UI/Controller/PreloadingViewController.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class PreloadingViewController: UIViewController {
9494
Task {
9595
do {
9696
_ = try await accountManager.updateUser(for: currentAccount, registerToken: true)
97-
_ = try accountManager.getFirstAvailableDriveFileManager(for: currentAccount.userId)
97+
_ = try await accountManager.getBestAvailableDriveFileManager(for: currentAccount)
9898

9999
if let currentDriveFileManager = self.accountManager.currentDriveFileManager {
100100
let state = RootViewControllerState.mainViewController(driveFileManager: currentDriveFileManager)

kDriveCore/Data/Cache/AccountManager.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public protocol AccountManageable: AnyObject {
6767
func forceReload()
6868
func reloadTokensAndAccounts()
6969
func getDriveFileManager(for driveId: Int, userId: Int) -> DriveFileManager?
70-
func getFirstAvailableDriveFileManager(for userId: Int) throws -> DriveFileManager
70+
func getBestAvailableDriveFileManager(for account: Account) async throws -> DriveFileManager
7171

7272
/// Create on the fly an "in memory" DriveFileManager for a specific share
7373
func getInMemoryDriveFileManager(for publicShareId: String, driveId: Int, rootFileId: Int) -> DriveFileManager?
@@ -227,22 +227,22 @@ public class AccountManager: RefreshTokenDelegate, AccountManageable {
227227
return DriveFileManager(drive: frozenPublicShareDrive, apiFetcher: DriveApiFetcher(), context: context)
228228
}
229229

230-
public func getFirstAvailableDriveFileManager(for userId: Int) throws -> DriveFileManager {
231-
let userDrives = driveInfosManager.getDrives(for: userId)
230+
public func getBestAvailableDriveFileManager(for account: Account) async throws -> DriveFileManager {
231+
let userDrives = Array(driveInfosManager.getDrives(for: account.userId))
232232

233233
guard !userDrives.isEmpty else {
234234
throw DriveError.NoDriveError.noDrive
235235
}
236236

237-
guard let firstAvailableDrive = userDrives.first(where: { !$0.inMaintenance }) else {
237+
guard let mainDrive = try? await bestAvailableDrive(from: userDrives, account: account) else {
238238
if userDrives[0].isInTechnicalMaintenance {
239239
throw DriveError.NoDriveError.maintenance(drive: userDrives[0])
240240
} else {
241241
throw DriveError.NoDriveError.blocked(drive: userDrives[0])
242242
}
243243
}
244244

245-
guard let driveFileManager = getDriveFileManager(for: firstAvailableDrive.id, userId: firstAvailableDrive.userId) else {
245+
guard let driveFileManager = getDriveFileManager(for: mainDrive.id, userId: mainDrive.userId) else {
246246
// We should always have a driveFileManager here
247247
throw DriveError.NoDriveError.noDriveFileManager
248248
}

kDriveTests/kDrive/Launch/MockAccountManager.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class MockAccountManager: AccountManageable, RefreshTokenDelegate {
4949

5050
func getDriveFileManager(for driveId: Int, userId: Int) -> DriveFileManager? { currentDriveFileManager }
5151

52-
func getFirstAvailableDriveFileManager(for userId: Int) throws -> DriveFileManager { fatalError("Not implemented") }
52+
func getBestAvailableDriveFileManager(for account: Account) async throws -> DriveFileManager { fatalError("Not implemented") }
5353

5454
func getApiFetcher(for userId: Int, token: ApiToken) -> DriveApiFetcher { fatalError("Not implemented") }
5555

0 commit comments

Comments
 (0)