UIAlertController+Extension.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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, markE2ee: Bool = false, 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. if markE2ee {
  37. Task {
  38. let createFolderResults = await NextcloudKit.shared.createFolder(serverUrlFileName: serverUrl + "/" + fileNameFolder)
  39. if createFolderResults.error == .success {
  40. let error = await NCNetworkingE2EEMarkFolder().markFolderE2ee(account: urlBase.account, fileName: fileNameFolder, serverUrl: serverUrl, userId: urlBase.userId)
  41. if error != .success {
  42. NCContentPresenter.shared.showError(error: error)
  43. }
  44. } else {
  45. NCContentPresenter.shared.showError(error: createFolderResults.error)
  46. }
  47. }
  48. } else {
  49. NCNetworking.shared.createFolder(fileName: fileNameFolder, serverUrl: serverUrl, account: urlBase.account, urlBase: urlBase.urlBase, userId: urlBase.userId, overwrite: false, withPush: true) { error in
  50. if let completion = completion {
  51. completion(error)
  52. } else if error != .success {
  53. NCContentPresenter.shared.showError(error: error)
  54. } // else: successful, no action
  55. }
  56. }
  57. })
  58. // text field is initially empty, no action
  59. okAction.isEnabled = false
  60. let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel)
  61. alertController.addTextField { textField in
  62. textField.autocapitalizationType = .words
  63. }
  64. // only allow saving if folder name exists
  65. NotificationCenter.default.addObserver(
  66. forName: UITextField.textDidChangeNotification,
  67. object: alertController.textFields?.first,
  68. queue: .main) { _ in
  69. guard let text = alertController.textFields?.first?.text,
  70. let folderName = CCUtility.removeForbiddenCharactersServer(text)?.trimmingCharacters(in: .whitespaces) else { return }
  71. okAction.isEnabled = !folderName.isEmpty && folderName != "." && folderName != ".."
  72. }
  73. alertController.addAction(cancelAction)
  74. alertController.addAction(okAction)
  75. return alertController
  76. }
  77. static func withTextField(titleKey: String, textFieldConfiguration: ((UITextField) -> Void)?, completion: @escaping (String?) -> Void) -> UIAlertController {
  78. let alertController = UIAlertController(title: NSLocalizedString(titleKey, comment: ""), message: "", preferredStyle: .alert)
  79. alertController.addTextField { textField in
  80. textFieldConfiguration?(textField)
  81. }
  82. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .default) { _ in })
  83. let okAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default) { _ in
  84. completion(alertController.textFields?.first?.text)
  85. }
  86. alertController.addAction(okAction)
  87. return alertController
  88. }
  89. static func password(titleKey: String, completion: @escaping (String?) -> Void) -> UIAlertController {
  90. return .withTextField(titleKey: titleKey, textFieldConfiguration: { textField in
  91. textField.isSecureTextEntry = true
  92. textField.placeholder = NSLocalizedString("_password_", comment: "")
  93. }, completion: completion)
  94. }
  95. }