-
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(SaveFileViewController): Split the class for better readabil…
…ity and further development
- Loading branch information
1 parent
f67e99f
commit a745bee
Showing
6 changed files
with
371 additions
and
271 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
kDrive/UI/Controller/Files/Save File/SaveFileViewController+SelectDriveDelegate.swift
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,36 @@ | ||
/* | ||
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 Foundation | ||
import kDriveCore | ||
|
||
// MARK: - SelectDriveDelegate | ||
|
||
extension SaveFileViewController: SelectDriveDelegate { | ||
func didSelectDrive(_ drive: Drive) { | ||
if let selectedDriveFileManager = accountManager.getDriveFileManager(for: drive.id, userId: drive.userId) { | ||
self.selectedDriveFileManager = selectedDriveFileManager | ||
selectedDirectory = getBestDirectory() | ||
sections = [.fileName, .driveSelection, .directorySelection] | ||
if itemProvidersContainHeicPhotos { | ||
sections.append(.photoFormatOption) | ||
} | ||
} | ||
updateButton() | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
kDrive/UI/Controller/Files/Save File/SaveFileViewController+SelectFolderDelegate.swift
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,34 @@ | ||
/* | ||
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 kDriveCore | ||
import UIKit | ||
|
||
// MARK: - SelectFolderDelegate | ||
|
||
extension SaveFileViewController: SelectFolderDelegate { | ||
func didSelectFolder(_ folder: File) { | ||
if folder.id == DriveFileManager.constants.rootID { | ||
selectedDirectory = selectedDriveFileManager?.getCachedRootFile() | ||
} else { | ||
selectedDirectory = folder | ||
} | ||
updateButton() | ||
tableView.reloadData() | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
kDrive/UI/Controller/Files/Save File/SaveFileViewController+SelectPhotoFormatDelegate.swift
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,35 @@ | ||
/* | ||
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 Foundation | ||
import kDriveCore | ||
|
||
// MARK: - SelectPhotoFormatDelegate | ||
|
||
extension SaveFileViewController: SelectPhotoFormatDelegate { | ||
func didSelectPhotoFormat(_ format: PhotoFileFormat) { | ||
if userPreferredPhotoFormat != format { | ||
userPreferredPhotoFormat = format | ||
if itemProviders?.isEmpty == false { | ||
setItemProviders() | ||
} else { | ||
setAssetIdentifiers() | ||
} | ||
} | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
kDrive/UI/Controller/Files/Save File/SaveFileViewController+UITableViewDataSource.swift
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,123 @@ | ||
/* | ||
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 kDriveResources | ||
import UIKit | ||
|
||
// MARK: - UITableViewDataSource | ||
|
||
extension SaveFileViewController: UITableViewDataSource { | ||
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | ||
let section = sections[section] | ||
if section == .fileName { | ||
return items.count | ||
} else { | ||
return 1 | ||
} | ||
} | ||
|
||
func numberOfSections(in tableView: UITableView) -> Int { | ||
return sections.count | ||
} | ||
|
||
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | ||
switch sections[indexPath.section] { | ||
case .alert: | ||
let cell = tableView.dequeueReusableCell(type: AlertTableViewCell.self, for: indexPath) | ||
cell.configure(with: .warning, message: KDriveResourcesStrings.Localizable.snackBarUploadError(errorCount)) | ||
return cell | ||
case .fileName: | ||
let item = items[indexPath.row] | ||
if items.count > 1 { | ||
let cell = tableView.dequeueReusableCell(type: UploadTableViewCell.self, for: indexPath) | ||
cell.initWithPositionAndShadow( | ||
isFirst: indexPath.row == 0, | ||
isLast: indexPath.row == self.tableView(tableView, numberOfRowsInSection: indexPath.section) - 1 | ||
) | ||
cell.configureWith(importedFile: item) | ||
return cell | ||
} else { | ||
let cell = tableView.dequeueReusableCell(type: FileNameTableViewCell.self, for: indexPath) | ||
cell.textField.text = item.name | ||
cell.textDidChange = { [weak self] text in | ||
guard let self else { return } | ||
item.name = text ?? KDriveResourcesStrings.Localizable.allUntitledFileName | ||
if let text, !text.isEmpty { | ||
updateButton() | ||
} else { | ||
enableButton = false | ||
} | ||
} | ||
return cell | ||
} | ||
case .driveSelection: | ||
let cell = tableView.dequeueReusableCell(type: LocationTableViewCell.self, for: indexPath) | ||
cell.initWithPositionAndShadow(isFirst: true, isLast: true) | ||
cell.configure(with: selectedDriveFileManager?.drive) | ||
return cell | ||
case .directorySelection: | ||
let cell = tableView.dequeueReusableCell(type: LocationTableViewCell.self, for: indexPath) | ||
cell.initWithPositionAndShadow(isFirst: true, isLast: true) | ||
cell.configure(with: selectedDirectory, drive: selectedDriveFileManager!.drive) | ||
return cell | ||
case .photoFormatOption: | ||
let cell = tableView.dequeueReusableCell(type: PhotoFormatTableViewCell.self, for: indexPath) | ||
cell.initWithPositionAndShadow(isFirst: true, isLast: true) | ||
cell.configure(with: userPreferredPhotoFormat) | ||
return cell | ||
case .importing: | ||
let cell = tableView.dequeueReusableCell(type: ImportingTableViewCell.self, for: indexPath) | ||
cell.importationProgressView.observedProgress = importProgress | ||
return cell | ||
default: | ||
fatalError("Not supported by this datasource") | ||
} | ||
} | ||
|
||
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { | ||
switch sections[section] { | ||
case .fileName: | ||
return HomeTitleView.instantiate(title: "") | ||
case .driveSelection: | ||
return HomeTitleView.instantiate(title: "kDrive") | ||
case .directorySelection: | ||
return HomeTitleView.instantiate(title: KDriveResourcesStrings.Localizable.allPathTitle) | ||
case .photoFormatOption: | ||
return HomeTitleView.instantiate(title: KDriveResourcesStrings.Localizable.photoFormatTitle) | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { | ||
if section == tableView.numberOfSections - 1 && !importInProgress { | ||
return 124 | ||
} | ||
return 32 | ||
} | ||
|
||
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { | ||
if section == tableView.numberOfSections - 1 && !importInProgress { | ||
let view = FooterButtonView.instantiate(title: KDriveResourcesStrings.Localizable.buttonSave) | ||
view.delegate = self | ||
view.footerButton.isEnabled = enableButton | ||
return view | ||
} | ||
return nil | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
kDrive/UI/Controller/Files/Save File/SaveFileViewController+UITableViewDelegate.swift
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,69 @@ | ||
/* | ||
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 kDriveCore | ||
import kDriveResources | ||
import UIKit | ||
|
||
// MARK: - UITableViewDelegate | ||
|
||
extension SaveFileViewController: UITableViewDelegate { | ||
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | ||
tableView.deselectRow(at: indexPath, animated: true) | ||
switch sections[indexPath.section] { | ||
case .fileName: | ||
let item = items[indexPath.row] | ||
if items.count > 1 { | ||
let alert = AlertFieldViewController( | ||
title: KDriveResourcesStrings.Localizable.buttonRename, | ||
placeholder: KDriveResourcesStrings.Localizable.hintInputFileName, | ||
text: item.name, | ||
action: KDriveResourcesStrings.Localizable.buttonSave, | ||
loading: false | ||
) { newName in | ||
item.name = newName | ||
tableView.reloadRows(at: [indexPath], with: .automatic) | ||
} | ||
alert.textFieldConfiguration = .fileNameConfiguration | ||
alert.textFieldConfiguration.selectedRange = item.name | ||
.startIndex ..< (item.name.lastIndex { $0 == "." } ?? item.name.endIndex) | ||
present(alert, animated: true) | ||
} | ||
case .driveSelection: | ||
let selectDriveViewController = SelectDriveViewController.instantiate() | ||
selectDriveViewController.selectedDrive = selectedDriveFileManager?.drive | ||
selectDriveViewController.delegate = self | ||
navigationController?.pushViewController(selectDriveViewController, animated: true) | ||
case .directorySelection: | ||
guard let driveFileManager = selectedDriveFileManager else { return } | ||
let selectFolderNavigationController = SelectFolderViewController.instantiateInNavigationController( | ||
driveFileManager: driveFileManager, | ||
startDirectory: selectedDirectory, | ||
delegate: self | ||
) | ||
present(selectFolderNavigationController, animated: true) | ||
case .photoFormatOption: | ||
let selectPhotoFormatViewController = SelectPhotoFormatViewController | ||
.instantiate(selectedFormat: userPreferredPhotoFormat) | ||
selectPhotoFormatViewController.delegate = self | ||
navigationController?.pushViewController(selectPhotoFormatViewController, animated: true) | ||
default: | ||
break | ||
} | ||
} | ||
} |
Oops, something went wrong.