UIAlertController+Extension.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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, userBaseUrl: 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 NCNetworking.shared.createFolder(serverUrlFileName: serverUrl + "/" + fileNameFolder, account: userBaseUrl.account)
  40. if createFolderResults.error == .success {
  41. let error = await NCNetworkingE2EEMarkFolder().markFolderE2ee(account: userBaseUrl.account, fileName: fileNameFolder, serverUrl: serverUrl, userId: userBaseUrl.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: userBaseUrl.account, urlBase: userBaseUrl.urlBase, userId: userBaseUrl.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. let textCheck = FileNameValidator.shared.checkFileName(folderName)
  73. okAction.isEnabled = textCheck?.error == nil && !folderName.isEmpty
  74. alertController.message = textCheck?.error.localizedDescription
  75. }
  76. alertController.addAction(cancelAction)
  77. alertController.addAction(okAction)
  78. return alertController
  79. }
  80. static func withTextField(titleKey: String, textFieldConfiguration: ((UITextField) -> Void)?, completion: @escaping (String?) -> Void) -> UIAlertController {
  81. let alertController = UIAlertController(title: NSLocalizedString(titleKey, comment: ""), message: "", preferredStyle: .alert)
  82. alertController.addTextField { textField in
  83. textFieldConfiguration?(textField)
  84. }
  85. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .default) { _ in })
  86. let okAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default) { _ in
  87. completion(alertController.textFields?.first?.text)
  88. }
  89. alertController.addAction(okAction)
  90. return alertController
  91. }
  92. static func password(titleKey: String, completion: @escaping (String?) -> Void) -> UIAlertController {
  93. return .withTextField(titleKey: titleKey, textFieldConfiguration: { textField in
  94. textField.isSecureTextEntry = true
  95. textField.placeholder = NSLocalizedString("_password_", comment: "")
  96. }, completion: completion)
  97. }
  98. static func deleteFileOrFolder(titleString: String, message: String?, canDeleteServer: Bool, selectedMetadatas: [tableMetadata], completion: @escaping (_ cancelled: Bool) -> Void) -> UIAlertController {
  99. let alertController = UIAlertController(
  100. title: titleString,
  101. message: message,
  102. preferredStyle: .alert)
  103. if canDeleteServer {
  104. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_", comment: ""), style: .destructive) { (_: UIAlertAction) in
  105. Task {
  106. var error = NKError()
  107. var ocId: [String] = []
  108. for metadata in selectedMetadatas where error == .success {
  109. error = await NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: false)
  110. if error == .success {
  111. ocId.append(metadata.ocId)
  112. }
  113. }
  114. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDeleteFile, userInfo: ["ocId": ocId, "onlyLocalCache": false, "error": error])
  115. }
  116. completion(false)
  117. })
  118. }
  119. alertController.addAction(UIAlertAction(title: NSLocalizedString("_remove_local_file_", comment: ""), style: .default) { (_: UIAlertAction) in
  120. Task {
  121. var error = NKError()
  122. var ocId: [String] = []
  123. for metadata in selectedMetadatas where error == .success {
  124. error = await NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: true)
  125. if error == .success {
  126. ocId.append(metadata.ocId)
  127. }
  128. }
  129. if error != .success {
  130. NCContentPresenter().showError(error: error)
  131. }
  132. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDeleteFile, userInfo: ["ocId": ocId, "onlyLocalCache": true, "error": error])
  133. }
  134. completion(false)
  135. })
  136. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel) { (_: UIAlertAction) in
  137. completion(true)
  138. })
  139. return alertController
  140. }
  141. static func renameFile(fileName: String, completion: @escaping (_ newFileName: String) -> Void) -> UIAlertController {
  142. let alertController = UIAlertController(title: NSLocalizedString("_rename_", comment: ""), message: nil, preferredStyle: .alert)
  143. let okAction = UIAlertAction(title: NSLocalizedString("_save_", comment: ""), style: .default, handler: { _ in
  144. guard let newFileName = alertController.textFields?.first?.text else { return }
  145. completion(newFileName)
  146. })
  147. // text field is initially empty, no action
  148. okAction.isEnabled = false
  149. let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel)
  150. alertController.addTextField { textField in
  151. textField.text = fileName
  152. textField.autocapitalizationType = .words
  153. }
  154. // only allow saving if folder name exists
  155. NotificationCenter.default.addObserver(
  156. forName: UITextField.textDidBeginEditingNotification,
  157. object: alertController.textFields?.first,
  158. queue: .main) { _ in
  159. guard let textField = alertController.textFields?.first else { return }
  160. if let start = textField.position(from: textField.beginningOfDocument, offset: 0),
  161. let end = textField.position(from: start, offset: textField.text?.withRemovedFileExtension.count ?? 0) {
  162. textField.selectedTextRange = textField.textRange(from: start, to: end)
  163. }
  164. }
  165. NotificationCenter.default.addObserver(
  166. forName: UITextField.textDidChangeNotification,
  167. object: alertController.textFields?.first,
  168. queue: .main) { _ in
  169. guard let text = alertController.textFields?.first?.text else { return }
  170. let textCheck = FileNameValidator.shared.checkFileName(text)
  171. okAction.isEnabled = textCheck?.error == nil && !text.isEmpty
  172. alertController.message = textCheck?.error.localizedDescription
  173. }
  174. alertController.addAction(cancelAction)
  175. alertController.addAction(okAction)
  176. return alertController
  177. }
  178. static func renameFile(metadata: tableMetadata, indexPath: IndexPath) -> UIAlertController {
  179. let alertController = UIAlertController(title: NSLocalizedString("_rename_", comment: ""), message: nil, preferredStyle: .alert)
  180. let okAction = UIAlertAction(title: NSLocalizedString("_save_", comment: ""), style: .default, handler: { _ in
  181. guard let newFileName = alertController.textFields?.first?.text else { return }
  182. // verify if already exists
  183. if NCManageDatabase.shared.getMetadata(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileName == %@", metadata.account, metadata.serverUrl, newFileName)) != nil {
  184. NCContentPresenter().showError(error: NKError(errorCode: 0, errorDescription: "_rename_already_exists_"))
  185. return
  186. }
  187. NCActivityIndicator.shared.start()
  188. NCNetworking.shared.renameMetadata(metadata, fileNameNew: newFileName, indexPath: indexPath) { error in
  189. NCActivityIndicator.shared.stop()
  190. if error != .success {
  191. NCContentPresenter().showError(error: error)
  192. }
  193. }
  194. })
  195. // text field is initially empty, no action
  196. okAction.isEnabled = false
  197. let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel)
  198. alertController.addTextField { textField in
  199. textField.text = metadata.fileName
  200. textField.autocapitalizationType = .words
  201. }
  202. // only allow saving if folder name exists
  203. NotificationCenter.default.addObserver(
  204. forName: UITextField.textDidBeginEditingNotification,
  205. object: alertController.textFields?.first,
  206. queue: .main) { _ in
  207. guard let textField = alertController.textFields?.first else { return }
  208. if let start = textField.position(from: textField.beginningOfDocument, offset: 0),
  209. let end = textField.position(from: start, offset: textField.text?.withRemovedFileExtension.count ?? 0) {
  210. textField.selectedTextRange = textField.textRange(from: start, to: end)
  211. }
  212. }
  213. NotificationCenter.default.addObserver(
  214. forName: UITextField.textDidChangeNotification,
  215. object: alertController.textFields?.first,
  216. queue: .main) { _ in
  217. guard let text = alertController.textFields?.first?.text else { return }
  218. let textCheck = FileNameValidator.shared.checkFileName(text)
  219. okAction.isEnabled = textCheck?.error == nil && !text.isEmpty
  220. alertController.message = textCheck?.error.localizedDescription
  221. }
  222. alertController.addAction(cancelAction)
  223. alertController.addAction(okAction)
  224. return alertController
  225. }
  226. static func warning(title: String? = nil, message: String? = nil) -> UIAlertController {
  227. let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
  228. let okAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default)
  229. alertController.addAction(okAction)
  230. return alertController
  231. }
  232. }