UIAlertController+Extension.swift 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 Foundation
  24. import UIKit
  25. import NextcloudKit
  26. extension UIAlertController {
  27. /// Creates a alert controller with a textfield, asking to create a new folder
  28. /// - Parameters:
  29. /// - serverUrl: Server url of the location where the folder should be created
  30. /// - urlBase: UrlBase object
  31. /// - completion: If not` nil` it overrides the default behavior which shows an error using `NCContentPresenter`
  32. /// - Returns: The presentable alert controller
  33. static func createFolder(serverUrl: String, urlBase: NCUserBaseUrl, markE2ee: Bool = false, sceneIdentifier: String? = nil, completion: ((_ error: NKError) -> Void)? = nil) -> UIAlertController {
  34. let alertController = UIAlertController(title: NSLocalizedString("_create_folder_", comment: ""), message: nil, preferredStyle: .alert)
  35. let okAction = UIAlertAction(title: NSLocalizedString("_save_", comment: ""), style: .default, handler: { _ in
  36. guard let fileNameFolder = alertController.textFields?.first?.text else { return }
  37. if markE2ee {
  38. Task {
  39. let createFolderResults = await NextcloudKit.shared.createFolder(serverUrlFileName: serverUrl + "/" + fileNameFolder)
  40. if createFolderResults.error == .success {
  41. let error = await NCNetworkingE2EEMarkFolder().markFolderE2ee(account: urlBase.account, fileName: fileNameFolder, serverUrl: serverUrl, userId: urlBase.userId)
  42. if error != .success {
  43. NCContentPresenter().showError(error: error)
  44. }
  45. } else {
  46. NCContentPresenter().showError(error: createFolderResults.error)
  47. }
  48. }
  49. } else {
  50. NCNetworking.shared.createFolder(fileName: fileNameFolder, serverUrl: serverUrl, account: urlBase.account, urlBase: urlBase.urlBase, userId: urlBase.userId, overwrite: false, withPush: true, sceneIdentifier: sceneIdentifier) { error in
  51. if let completion = completion {
  52. completion(error)
  53. } else if error != .success {
  54. NCContentPresenter().showError(error: error)
  55. } // else: successful, no action
  56. }
  57. }
  58. })
  59. // text field is initially empty, no action
  60. okAction.isEnabled = false
  61. let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel)
  62. alertController.addTextField { textField in
  63. textField.autocapitalizationType = .words
  64. }
  65. // only allow saving if folder name exists
  66. NotificationCenter.default.addObserver(
  67. forName: UITextField.textDidChangeNotification,
  68. object: alertController.textFields?.first,
  69. queue: .main) { _ in
  70. guard let text = alertController.textFields?.first?.text else { return }
  71. let folderName = text.trimmingCharacters(in: .whitespaces)
  72. okAction.isEnabled = !folderName.isEmpty && folderName != "." && folderName != ".."
  73. }
  74. alertController.addAction(cancelAction)
  75. alertController.addAction(okAction)
  76. return alertController
  77. }
  78. static func withTextField(titleKey: String, textFieldConfiguration: ((UITextField) -> Void)?, completion: @escaping (String?) -> Void) -> UIAlertController {
  79. let alertController = UIAlertController(title: NSLocalizedString(titleKey, comment: ""), message: "", preferredStyle: .alert)
  80. alertController.addTextField { textField in
  81. textFieldConfiguration?(textField)
  82. }
  83. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .default) { _ in })
  84. let okAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default) { _ in
  85. completion(alertController.textFields?.first?.text)
  86. }
  87. alertController.addAction(okAction)
  88. return alertController
  89. }
  90. static func password(titleKey: String, completion: @escaping (String?) -> Void) -> UIAlertController {
  91. return .withTextField(titleKey: titleKey, textFieldConfiguration: { textField in
  92. textField.isSecureTextEntry = true
  93. textField.placeholder = NSLocalizedString("_password_", comment: "")
  94. }, completion: completion)
  95. }
  96. static func deleteFileOrFolder(titleString: String, message: String?, canDeleteServer: Bool, selectedMetadatas: [tableMetadata], completion: @escaping (_ cancelled: Bool) -> Void) -> UIAlertController {
  97. let alertController = UIAlertController(
  98. title: titleString,
  99. message: message,
  100. preferredStyle: .alert)
  101. if canDeleteServer {
  102. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_", comment: ""), style: .destructive) { (_: UIAlertAction) in
  103. Task {
  104. var error = NKError()
  105. var ocId: [String] = []
  106. for metadata in selectedMetadatas where error == .success {
  107. error = await NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: false)
  108. if error == .success {
  109. ocId.append(metadata.ocId)
  110. }
  111. }
  112. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDeleteFile, userInfo: ["ocId": ocId, "onlyLocalCache": false, "error": error])
  113. }
  114. completion(false)
  115. })
  116. }
  117. alertController.addAction(UIAlertAction(title: NSLocalizedString("_remove_local_file_", comment: ""), style: .default) { (_: UIAlertAction) in
  118. Task {
  119. var error = NKError()
  120. var ocId: [String] = []
  121. for metadata in selectedMetadatas where error == .success {
  122. error = await NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: true)
  123. if error == .success {
  124. ocId.append(metadata.ocId)
  125. }
  126. }
  127. if error != .success {
  128. NCContentPresenter().showError(error: error)
  129. }
  130. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDeleteFile, userInfo: ["ocId": ocId, "onlyLocalCache": true, "error": error])
  131. }
  132. completion(false)
  133. })
  134. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel) { (_: UIAlertAction) in
  135. completion(true)
  136. })
  137. return alertController
  138. }
  139. }