UIAlertController+Extension.swift 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. import NextcloudKit
  25. extension UIAlertController {
  26. /// Creates a alert controller with a textfield, asking to create a new folder
  27. /// - Parameters:
  28. /// - serverUrl: Server url of the location where the folder should be created
  29. /// - urlBase: UrlBase object
  30. /// - completion: If not` nil` it overrides the default behavior which shows an error using `NCContentPresenter`
  31. /// - Returns: The presentable alert controller
  32. static func createFolder(serverUrl: String, urlBase: NCUserBaseUrl, completion: ((_ error: NKError) -> Void)? = nil) -> UIAlertController {
  33. let alertController = UIAlertController(title: NSLocalizedString("_create_folder_", comment: ""), message: nil, preferredStyle: .alert)
  34. let okAction = UIAlertAction(title: NSLocalizedString("_save_", comment: ""), style: .default, handler: { _ in
  35. guard let fileNameFolder = alertController.textFields?.first?.text else { return }
  36. NCNetworking.shared.createFolder(fileName: fileNameFolder, serverUrl: serverUrl, account: urlBase.account, urlBase: urlBase.urlBase, userId: urlBase.userId, overwrite: false) { error in
  37. if let completion = completion {
  38. completion(error)
  39. } else if error != .success {
  40. NCContentPresenter.shared.showError(error: error)
  41. } // else: successful, no action
  42. }
  43. })
  44. // text field is initially empty, no action
  45. okAction.isEnabled = false
  46. let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel)
  47. alertController.addTextField { textField in
  48. textField.autocapitalizationType = .words
  49. }
  50. // only allow saving if folder name exists
  51. NotificationCenter.default.addObserver(
  52. forName: UITextField.textDidChangeNotification,
  53. object: alertController.textFields?.first,
  54. queue: .main) { _ in
  55. guard let text = alertController.textFields?.first?.text,
  56. let folderName = CCUtility.removeForbiddenCharactersServer(text)?.trimmingCharacters(in: .whitespaces) else { return }
  57. okAction.isEnabled = !folderName.isEmpty && folderName != "." && folderName != ".."
  58. }
  59. alertController.addAction(cancelAction)
  60. alertController.addAction(okAction)
  61. return alertController
  62. }
  63. static func withTextField(titleKey: String, textFieldConfiguration: ((UITextField) -> Void)?, completion: @escaping (String?) -> Void) -> UIAlertController {
  64. let alertController = UIAlertController(title: NSLocalizedString(titleKey, comment: ""), message: "", preferredStyle: .alert)
  65. alertController.addTextField { textField in
  66. textFieldConfiguration?(textField)
  67. }
  68. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .default) { _ in })
  69. let okAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default) { _ in
  70. completion(alertController.textFields?.first?.text)
  71. }
  72. alertController.addAction(okAction)
  73. return alertController
  74. }
  75. static func password(titleKey: String, completion: @escaping (String?) -> Void) -> UIAlertController {
  76. return .withTextField(titleKey: titleKey, textFieldConfiguration: { textField in
  77. textField.isSecureTextEntry = true
  78. textField.placeholder = NSLocalizedString("_password_", comment: "")
  79. }, completion: completion)
  80. }
  81. }