Skip to content

Commit

Permalink
Merge pull request #1023 from Infomaniak/api-v3
Browse files Browse the repository at this point in the history
feat: Api v3
  • Loading branch information
PhilippeWeidmann authored Jan 30, 2024
2 parents 7296fb9 + 8f807fb commit 573d1d0
Show file tree
Hide file tree
Showing 36 changed files with 612 additions and 261 deletions.
4 changes: 2 additions & 2 deletions .package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/Infomaniak/ios-core",
"state" : {
"revision" : "a1341fb7d6d5bc48576cc8400682476c441f2d23",
"version" : "6.0.0"
"branch" : "cursored-api-response",
"revision" : "eb17c44e90e1df9267b385cf8da93d7d6fa08d7c"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Project.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ let project = Project(name: "kDrive",
packages: [
.package(url: "https://github.com/apple/swift-algorithms", .upToNextMajor(from: "1.2.0")),
.package(url: "https://github.com/Alamofire/Alamofire", .upToNextMajor(from: "5.2.2")),
.package(url: "https://github.com/Infomaniak/ios-core", .upToNextMajor(from: "6.0.0")),
.package(url: "https://github.com/Infomaniak/ios-core", .branch("cursored-api-response")),
.package(url: "https://github.com/Infomaniak/ios-core-ui", .upToNextMajor(from: "4.0.0")),
.package(url: "https://github.com/Infomaniak/ios-login", .upToNextMajor(from: "6.0.1")),
.package(url: "https://github.com/Infomaniak/ios-dependency-injection", .upToNextMajor(from: "2.0.0")),
Expand Down
12 changes: 6 additions & 6 deletions kDrive/UI/Controller/Favorite/FavoritesViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@ class FavoritesViewModel: FileListViewModel {
.filesSorted(by: sortType))
}

override func loadFiles(page: Int = 1, forceRefresh: Bool = false) async throws {
guard !isLoading || page > 1 else { return }
override func loadFiles(cursor: String? = nil, forceRefresh: Bool = false) async throws {
guard !isLoading || cursor != nil else { return }

// Only show loading indicator if we have nothing in cache
if !currentDirectory.canLoadChildrenFromCache {
startRefreshing(page: page)
startRefreshing(cursor: cursor)
}
defer {
endRefreshing()
}

let (_, moreComing) = try await driveFileManager.favorites(page: page, sortType: sortType, forceRefresh: true)
let (_, nextCursor) = try await driveFileManager.favorites(cursor: cursor, sortType: sortType, forceRefresh: true)
endRefreshing()
if moreComing {
try await loadFiles(page: page + 1, forceRefresh: true)
if let nextCursor {
try await loadFiles(cursor: nextCursor)
}
}
}
37 changes: 15 additions & 22 deletions kDrive/UI/Controller/Files/File List/FileListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,34 +97,27 @@ class ConcreteFileListViewModel: FileListViewModel {
files = AnyRealmCollection(AnyRealmCollection(currentDirectory.children).filesSorted(by: sortType))
}

override func loadFiles(page: Int = 1, forceRefresh: Bool = false) async throws {
guard !isLoading || page > 1 else { return }
override func loadFiles(cursor: String? = nil, forceRefresh: Bool = false) async throws {
guard !isLoading || cursor != nil else { return }

if currentDirectory.canLoadChildrenFromCache && !forceRefresh {
try await loadActivitiesIfNeeded()
} else {
startRefreshing(page: page)
defer {
endRefreshing()
}

let (_, moreComing) = try await driveFileManager.files(
in: currentDirectory.proxify(),
page: page,
sortType: sortType,
forceRefresh: forceRefresh
)
startRefreshing(cursor: cursor)
defer {
endRefreshing()
if moreComing {
try await loadFiles(page: page + 1, forceRefresh: forceRefresh)
} else if !forceRefresh {
try await loadActivities()
}
}

let (_, nextCursor) = try await driveFileManager.fileListing(
in: currentDirectory.proxify(),
sortType: sortType,
forceRefresh: forceRefresh
)
endRefreshing()
if let nextCursor {
try await loadFiles(cursor: nextCursor)
}
}

override func loadActivities() async throws {
_ = try await driveFileManager.fileActivities(file: currentDirectory.proxify())
try await loadFiles()
}

override func barButtonPressed(type: FileListBarButtonType) {
Expand Down
10 changes: 5 additions & 5 deletions kDrive/UI/Controller/Files/File List/FileListViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,10 @@ class FileListViewModel: SelectDelegate {
}
}

func startRefreshing(page: Int) {
func startRefreshing(cursor: String?) {
isLoading = true

if page == 1 {
if cursor == nil {
showLoadingIndicatorIfNeeded()
}
}
Expand All @@ -346,7 +346,7 @@ class FileListViewModel: SelectDelegate {
// Implemented by subclasses
}

func loadFiles(page: Int = 1, forceRefresh: Bool = false) async throws {
func loadFiles(cursor: String? = nil, forceRefresh: Bool = false) async throws {
// Implemented by subclasses
}

Expand Down Expand Up @@ -425,7 +425,7 @@ class FileListViewModel: SelectDelegate {
func forceRefresh() {
endRefreshing()
Task {
try await loadFiles(page: 1, forceRefresh: true)
try await loadFiles(cursor: nil, forceRefresh: true)
}
}

Expand All @@ -434,7 +434,7 @@ class FileListViewModel: SelectDelegate {
let responseAtDate = Date(timeIntervalSince1970: Double(currentDirectory.responseAt))
let now = Date()
if responseAtDate.distance(to: now) > Constants.activitiesReloadTimeOut {
try await loadFiles(page: 1, forceRefresh: true)
try await loadFiles(cursor: nil, forceRefresh: true)
} else {
try await loadActivities()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class InMemoryFileListViewModel: FileListViewModel {
/// - Parameters:
/// - fetchedFiles: The list of files to add.
/// - page: The page of the files.
final func addPage(files fetchedFiles: [File], fullyDownloaded: Bool, copyInRealm: Bool = false, page: Int) {
final func addPage(files fetchedFiles: [File], fullyDownloaded: Bool, copyInRealm: Bool = false, cursor: String?) {
try? realm.write {
var children = [File]()
if copyInRealm {
Expand All @@ -67,7 +67,7 @@ class InMemoryFileListViewModel: FileListViewModel {
children = fetchedFiles
}

if page == 1 {
if cursor == nil {
currentDirectory.children.removeAll()
}
currentDirectory.children.insert(objectsIn: children)
Expand Down
9 changes: 5 additions & 4 deletions kDrive/UI/Controller/Files/FileDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ class FileDetailViewController: UIViewController {
}

override var preferredStatusBarStyle: UIStatusBarStyle {
if (tableView != nil && tableView.contentOffset.y > 0) || UIDevice.current.orientation.isLandscape || !file.hasThumbnail {
if (tableView != nil && tableView.contentOffset.y > 0) || UIDevice.current.orientation.isLandscape || !file.supportedBy
.contains(.thumbnail) {
return .default
} else {
return .lightContent
Expand All @@ -134,7 +135,7 @@ class FileDetailViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.tintColor = tableView.contentOffset.y == 0 && UIDevice.current.orientation
.isPortrait && file.hasThumbnail ? .white : nil
.isPortrait && file.supportedBy.contains(.thumbnail) ? .white : nil
let navigationBarAppearanceStandard = UINavigationBarAppearance()
navigationBarAppearanceStandard.configureWithTransparentBackground()
navigationBarAppearanceStandard.backgroundColor = KDriveResourcesAsset.backgroundColor.color
Expand Down Expand Up @@ -551,7 +552,7 @@ extension FileDetailViewController: UITableViewDelegate, UITableViewDataSource {

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
if !file.hasThumbnail {
if !file.supportedBy.contains(.thumbnail) {
let cell = tableView.dequeueReusableCell(type: FileDetailHeaderAltTableViewCell.self, for: indexPath)
cell.delegate = self
cell.configureWith(file: file)
Expand Down Expand Up @@ -798,7 +799,7 @@ extension FileDetailViewController {
navigationController?.navigationBar.tintColor = nil
} else {
title = ""
navigationController?.navigationBar.tintColor = file.hasThumbnail ? .white : nil
navigationController?.navigationBar.tintColor = file.supportedBy.contains(.thumbnail) ? .white : nil
}
} else {
title = scrollView.contentOffset.y > 200 ? file.name : ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ extension PreviewViewController: UICollectionViewDataSource {
cell.previewDelegate = self
return cell
}
} else if file.hasThumbnail && !ConvertedType.ignoreThumbnailTypes.contains(file.convertedType) {
} else if file.supportedBy.contains(.thumbnail) && !ConvertedType.ignoreThumbnailTypes.contains(file.convertedType) {
let cell = collectionView.dequeueReusableCell(type: DownloadingPreviewCollectionViewCell.self, for: indexPath)
if let downloadOperation = currentDownloadOperation,
let progress = downloadOperation.task?.progress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class RecentActivityFilesViewModel: InMemoryFileListViewModel {
convenience init(driveFileManager: DriveFileManager, activities: [FileActivity]) {
self.init(driveFileManager: driveFileManager)
activity = activities.first
addPage(files: activities.compactMap(\.file).map { $0.detached() }, fullyDownloaded: true, page: 1)
addPage(files: activities.compactMap(\.file).map { $0.detached() }, fullyDownloaded: true, cursor: nil)
}

required init(driveFileManager: DriveFileManager, currentDirectory: File? = nil) {
Expand Down Expand Up @@ -57,7 +57,7 @@ class RecentActivityFilesViewModel: InMemoryFileListViewModel {
let realm = driveFileManager.getRealm()
activity = realm.object(ofType: FileActivity.self, forPrimaryKey: activityId)?.freeze()
let cachedFiles = fileIds.compactMap { driveFileManager.getCachedFile(id: $0, using: realm) }.map { $0.detached() }
addPage(files: cachedFiles, fullyDownloaded: true, page: 1)
addPage(files: cachedFiles, fullyDownloaded: true, cursor: nil)

forceRefresh()
}
Expand Down
24 changes: 12 additions & 12 deletions kDrive/UI/Controller/Files/Search/SearchViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,21 +142,21 @@ class SearchFilesViewModel: FileListViewModel {
sortingChanged()
}

override func loadFiles(page: Int = 1, forceRefresh: Bool = false) async throws {
override func loadFiles(cursor: String? = nil, forceRefresh: Bool = false) async throws {
guard isDisplayingSearchResults else { return }

var moreComing = false
var nextCursor: String?
if ReachabilityListener.instance.currentStatus == .offline {
searchOffline()
} else {
do {
moreComing = try await driveFileManager.searchFile(query: currentSearchText,
date: filters.date?.dateInterval,
fileType: filters.fileType,
categories: Array(filters.categories),
belongToAllCategories: filters.belongToAllCategories,
page: page,
sortType: sortType)
(_, nextCursor) = try await driveFileManager.searchFile(query: currentSearchText,
date: filters.date?.dateInterval,
fileType: filters.fileType,
categories: Array(filters.categories),
belongToAllCategories: filters.belongToAllCategories,
cursor: cursor,
sortType: sortType)
} catch {
if let error = error as? DriveError,
error == .networkError {
Expand All @@ -173,8 +173,8 @@ class SearchFilesViewModel: FileListViewModel {
}

endRefreshing()
if moreComing {
try await loadFiles(page: page + 1)
if let nextCursor {
try await loadFiles(cursor: nextCursor)
}
}

Expand Down Expand Up @@ -225,7 +225,7 @@ class SearchFilesViewModel: FileListViewModel {
}
if isDisplayingSearchResults {
currentTask = Task {
try? await loadFiles(page: 1, forceRefresh: true)
try? await loadFiles(cursor: nil, forceRefresh: true)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions kDrive/UI/Controller/Home/HomeOfflineFilesController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class HomeOfflineFilesController: HomeRecentFilesController {
listStyleEnabled: true)
}

override func getFiles() async throws -> [File] {
return driveFileManager.getAvailableOfflineFiles()
override func getFiles() async throws -> (files: [File], nextCursor: String?) {
return (driveFileManager.getAvailableOfflineFiles(), nil)
}

override func refreshIfNeeded(with file: File) {
Expand Down
4 changes: 2 additions & 2 deletions kDrive/UI/Controller/Home/HomePhotoListController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class HomePhotoListController: HomeRecentFilesController {
listStyleEnabled: false)
}

override func getFiles() async throws -> [File] {
return try await driveFileManager.lastPictures(page: page).files
override func getFiles() async throws -> (files: [File], nextCursor: String?) {
return try await driveFileManager.lastPictures(cursor: nextCursor)
}

override func getLayout(for style: ListStyle, layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection {
Expand Down
15 changes: 8 additions & 7 deletions kDrive/UI/Controller/Home/HomeRecentActivitiesController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,17 @@ class HomeRecentActivitiesController: HomeRecentFilesController {

Task {
do {
let activities = try await driveFileManager.apiFetcher.recentActivity(drive: driveFileManager.drive, page: page)
self.empty = self.page == 1 && activities.isEmpty
self.moreComing = activities.count == Endpoint.itemsPerPage
let activitiesResponse = try await driveFileManager.apiFetcher.recentActivity(drive: driveFileManager.drive,
cursor: nextCursor)
self.empty = self.nextCursor == nil && activitiesResponse.data.isEmpty
self.moreComing = activitiesResponse.response.hasMore

display(activities: activities)
display(activities: activitiesResponse.data)
// Update cache
if self.page == 1 {
self.driveFileManager.setLocalRecentActivities(activities)
if nextCursor == nil {
self.driveFileManager.setLocalRecentActivities(activitiesResponse.data)
}
self.page += 1
self.nextCursor = activitiesResponse.response.cursor
} catch {
let activities = self.driveFileManager.getLocalRecentActivities()
self.empty = activities.isEmpty
Expand Down
14 changes: 7 additions & 7 deletions kDrive/UI/Controller/Home/HomeRecentFilesController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class HomeRecentFilesController {

var listStyle: ListStyle = .list
var listStyleEnabled: Bool
var page = 1
var nextCursor: String?
var empty = false
var loading = false
var moreComing = true
Expand Down Expand Up @@ -71,7 +71,7 @@ class HomeRecentFilesController {
self.homeViewController = homeViewController
}

func getFiles() async throws -> [File] {
func getFiles() async throws -> (files: [File], nextCursor: String?) {
fatalError(#function + " needs to be overwritten")
}

Expand Down Expand Up @@ -104,7 +104,7 @@ class HomeRecentFilesController {

func resetController() {
files = []
page = 1
nextCursor = nil
loading = false
moreComing = true
}
Expand All @@ -123,10 +123,10 @@ class HomeRecentFilesController {
Task {
do {
let fetchedFiles = try await getFiles()
self.files.append(contentsOf: fetchedFiles)
self.empty = self.page == 1 && fetchedFiles.isEmpty
self.moreComing = fetchedFiles.count == Endpoint.itemsPerPage
self.page += 1
self.files.append(contentsOf: fetchedFiles.files)
self.empty = self.nextCursor == nil && fetchedFiles.files.isEmpty
self.moreComing = fetchedFiles.nextCursor == nil
self.nextCursor = fetchedFiles.nextCursor

guard !self.invalidated else {
return
Expand Down
2 changes: 1 addition & 1 deletion kDrive/UI/Controller/Home/HomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ class HomeViewController: UICollectionViewController, UpdateAccountDelegate, Top
headerView?.titleLabel.text = currentRecentFilesController.title
headerView?.switchLayoutButton.isHidden = !currentRecentFilesController.listStyleEnabled

if currentRecentFilesController.page == 1 {
if currentRecentFilesController.nextCursor == nil {
reload(newViewModel: HomeViewModel(topRows: viewModel.topRows,
recentFiles: .file([]),
recentFilesEmpty: false,
Expand Down
14 changes: 7 additions & 7 deletions kDrive/UI/Controller/Menu/LastModificationsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,22 @@ class LastModificationsViewModel: FileListViewModel {
files = AnyRealmCollection(files.sorted(by: [sortType.value.sortDescriptor]))
}

override func loadFiles(page: Int = 1, forceRefresh: Bool = false) async throws {
guard !isLoading || page > 1 else { return }
override func loadFiles(cursor: String? = nil, forceRefresh: Bool = false) async throws {
guard !isLoading || cursor != nil else { return }

startRefreshing(page: page)
startRefreshing(cursor: cursor)
defer {
endRefreshing()
}

let (_, moreComing) = try await driveFileManager.lastModifiedFiles(page: page)
let (_, nextCursor) = try await driveFileManager.lastModifiedFiles(cursor: cursor)
endRefreshing()
if moreComing {
try await loadFiles(page: page + 1, forceRefresh: forceRefresh)
if let nextCursor {
try await loadFiles(cursor: nextCursor)
}
}

override func loadActivities() async throws {
try await loadFiles(page: 1, forceRefresh: true)
try await loadFiles(cursor: nil, forceRefresh: true)
}
}
Loading

0 comments on commit 573d1d0

Please sign in to comment.