UIAlertController+Extension.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //
  2. // UIAlertController+Extension.swift
  3. // Nextcloud
  4. //
  5. // Created by Henrik Storch on 27.01.22.
  6. // Copyright © 2022 Henrik Storch. All rights reserved.
  7. //
  8. // Author Henrik Storch <henrik.storch@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. extension UIAlertController {
  25. /// Creates a alert controller with a textfield, asking to create a new folder
  26. /// - Parameters:
  27. /// - serverUrl: Server url of the location where the folder should be created
  28. /// - urlBase: UrlBase object
  29. /// - completion: If not` nil` it overrides the default behavior which shows an error using `NCContentPresenter`
  30. /// - Returns: The presentable alert controller
  31. static func createFolder(serverUrl: String, urlBase: NCUserBaseUrl, completion: ((_ errorCode: Int, _ errorDescription: String) -> Void)? = nil) -> UIAlertController {
  32. let alertController = UIAlertController(title: NSLocalizedString("_create_folder_", comment: ""), message: nil, preferredStyle: .alert)
  33. let okAction = UIAlertAction(title: NSLocalizedString("_save_", comment: ""), style: .default, handler: { _ in
  34. guard let fileNameFolder = alertController.textFields?.first?.text else { return }
  35. NCNetworking.shared.createFolder(fileName: fileNameFolder, serverUrl: serverUrl, account: urlBase.account, urlBase: urlBase.urlBase, overwrite: false) { errorCode, errorDescription in
  36. if let completion = completion {
  37. completion(errorCode, errorDescription)
  38. } else if errorCode != 0 {
  39. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  40. } // else: successful, no action
  41. }
  42. })
  43. // text field is initially empty, no action
  44. okAction.isEnabled = false
  45. let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel)
  46. alertController.addTextField { textField in
  47. textField.autocapitalizationType = .words
  48. }
  49. // only allow saving if folder name exists
  50. NotificationCenter.default.addObserver(
  51. forName: UITextField.textDidChangeNotification,
  52. object: alertController.textFields?.first,
  53. queue: .main) { _ in
  54. guard let text = alertController.textFields?.first?.text,
  55. let folderName = CCUtility.removeForbiddenCharactersServer(text)?.trimmingCharacters(in: .whitespaces) else { return }
  56. okAction.isEnabled = !folderName.isEmpty && folderName != "." && folderName != ".."
  57. }
  58. alertController.addAction(cancelAction)
  59. alertController.addAction(okAction)
  60. return alertController
  61. }
  62. }