UIAlertController+Extension.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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, account: String, 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 session = NCSession.shared.getSession(account: account)
  36. let isDirectoryEncrypted = NCUtilityFileSystem().isDirectoryE2EE(session: session, serverUrl: serverUrl)
  37. let okAction = UIAlertAction(title: NSLocalizedString("_save_", comment: ""), style: .default, handler: { _ in
  38. guard let fileNameFolder = alertController.textFields?.first?.text else { return }
  39. if markE2ee {
  40. if NCNetworking.shared.isOffline {
  41. return NCContentPresenter().showInfo(error: NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_offline_not_allowed_"))
  42. }
  43. Task {
  44. let createFolderResults = await NCNetworking.shared.createFolder(serverUrlFileName: serverUrl + "/" + fileNameFolder, account: session.account)
  45. if createFolderResults.error == .success {
  46. let error = await NCNetworkingE2EEMarkFolder().markFolderE2ee(account: session.account, fileName: fileNameFolder, serverUrl: serverUrl, userId: session.userId)
  47. if error != .success {
  48. NCContentPresenter().showError(error: error)
  49. }
  50. } else {
  51. NCContentPresenter().showError(error: createFolderResults.error)
  52. }
  53. }
  54. } else if isDirectoryEncrypted {
  55. if NCNetworking.shared.isOffline {
  56. return NCContentPresenter().showInfo(error: NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_offline_not_allowed_"))
  57. }
  58. #if !EXTENSION
  59. Task {
  60. await NCNetworkingE2EECreateFolder().createFolder(fileName: fileNameFolder, serverUrl: serverUrl, withPush: true, sceneIdentifier: sceneIdentifier, session: session)
  61. }
  62. #endif
  63. } else {
  64. let metadataForCreateFolder = NCManageDatabase.shared.createMetadata(fileName: fileNameFolder,
  65. fileNameView: fileNameFolder,
  66. ocId: NSUUID().uuidString,
  67. serverUrl: serverUrl,
  68. url: "",
  69. contentType: "httpd/unix-directory",
  70. directory: true,
  71. session: session,
  72. sceneIdentifier: sceneIdentifier)
  73. metadataForCreateFolder.status = NCGlobal.shared.metadataStatusWaitCreateFolder
  74. metadataForCreateFolder.sessionDate = Date()
  75. NCManageDatabase.shared.addMetadata(metadataForCreateFolder)
  76. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterCreateFolder, userInfo: ["ocId": metadataForCreateFolder.ocId, "serverUrl": metadataForCreateFolder.serverUrl, "account": metadataForCreateFolder.account, "withPush": true, "sceneIdentifier": sceneIdentifier as Any])
  77. }
  78. })
  79. // text field is initially empty, no action
  80. okAction.isEnabled = false
  81. let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel)
  82. alertController.addTextField { textField in
  83. textField.autocapitalizationType = .words
  84. }
  85. // only allow saving if folder name exists
  86. NotificationCenter.default.addObserver(
  87. forName: UITextField.textDidChangeNotification,
  88. object: alertController.textFields?.first,
  89. queue: .main) { _ in
  90. guard let text = alertController.textFields?.first?.text else { return }
  91. let folderName = text.trimmingCharacters(in: .whitespaces)
  92. let textCheck = FileNameValidator.shared.checkFileName(folderName, account: account)
  93. okAction.isEnabled = textCheck?.error == nil && !folderName.isEmpty
  94. alertController.message = textCheck?.error.localizedDescription
  95. }
  96. alertController.addAction(cancelAction)
  97. alertController.addAction(okAction)
  98. return alertController
  99. }
  100. static func withTextField(titleKey: String, textFieldConfiguration: ((UITextField) -> Void)?, completion: @escaping (String?) -> Void) -> UIAlertController {
  101. let alertController = UIAlertController(title: NSLocalizedString(titleKey, comment: ""), message: "", preferredStyle: .alert)
  102. alertController.addTextField { textField in
  103. textFieldConfiguration?(textField)
  104. }
  105. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .default) { _ in })
  106. let okAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default) { _ in
  107. completion(alertController.textFields?.first?.text)
  108. }
  109. alertController.addAction(okAction)
  110. return alertController
  111. }
  112. static func password(titleKey: String, completion: @escaping (String?) -> Void) -> UIAlertController {
  113. return .withTextField(titleKey: titleKey, textFieldConfiguration: { textField in
  114. textField.isSecureTextEntry = true
  115. textField.placeholder = NSLocalizedString("_password_", comment: "")
  116. }, completion: completion)
  117. }
  118. static func deleteFileOrFolder(titleString: String, message: String?, canDeleteServer: Bool, selectedMetadatas: [tableMetadata], sceneIdentifier: String?, completion: @escaping (_ cancelled: Bool) -> Void) -> UIAlertController {
  119. let alertController = UIAlertController(
  120. title: titleString,
  121. message: message,
  122. preferredStyle: .alert)
  123. if canDeleteServer {
  124. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_", comment: ""), style: .destructive) { (_: UIAlertAction) in
  125. NCNetworking.shared.deleteMetadatas(selectedMetadatas, sceneIdentifier: sceneIdentifier)
  126. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource)
  127. completion(false)
  128. })
  129. }
  130. alertController.addAction(UIAlertAction(title: NSLocalizedString("_remove_local_file_", comment: ""), style: .default) { (_: UIAlertAction) in
  131. Task {
  132. var error = NKError()
  133. var ocId: [String] = []
  134. for metadata in selectedMetadatas where error == .success {
  135. error = await NCNetworking.shared.deleteCache(metadata, sceneIdentifier: sceneIdentifier)
  136. if error == .success {
  137. ocId.append(metadata.ocId)
  138. }
  139. }
  140. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDeleteFile, userInfo: ["ocId": ocId, "error": error])
  141. }
  142. completion(false)
  143. })
  144. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel) { (_: UIAlertAction) in
  145. completion(true)
  146. })
  147. return alertController
  148. }
  149. static func renameFile(fileName: String, account: String, completion: @escaping (_ newFileName: String) -> Void) -> UIAlertController {
  150. let alertController = UIAlertController(title: NSLocalizedString("_rename_", comment: ""), message: nil, preferredStyle: .alert)
  151. let okAction = UIAlertAction(title: NSLocalizedString("_save_", comment: ""), style: .default, handler: { _ in
  152. guard let newFileName = alertController.textFields?.first?.text else { return }
  153. completion(newFileName)
  154. })
  155. // text field is initially empty, no action
  156. okAction.isEnabled = false
  157. let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel)
  158. alertController.addTextField { textField in
  159. textField.text = fileName
  160. textField.autocapitalizationType = .words
  161. }
  162. // only allow saving if folder name exists
  163. NotificationCenter.default.addObserver(
  164. forName: UITextField.textDidBeginEditingNotification,
  165. object: alertController.textFields?.first,
  166. queue: .main) { _ in
  167. guard let textField = alertController.textFields?.first else { return }
  168. if let start = textField.position(from: textField.beginningOfDocument, offset: 0),
  169. let end = textField.position(from: start, offset: textField.text?.withRemovedFileExtension.count ?? 0) {
  170. textField.selectedTextRange = textField.textRange(from: start, to: end)
  171. }
  172. }
  173. NotificationCenter.default.addObserver(
  174. forName: UITextField.textDidChangeNotification,
  175. object: alertController.textFields?.first,
  176. queue: .main) { _ in
  177. guard let text = alertController.textFields?.first?.text else { return }
  178. let textCheck = FileNameValidator.shared.checkFileName(text, account: account)
  179. okAction.isEnabled = textCheck?.error == nil && !text.isEmpty
  180. alertController.message = textCheck?.error.localizedDescription
  181. }
  182. alertController.addAction(cancelAction)
  183. alertController.addAction(okAction)
  184. return alertController
  185. }
  186. static func renameFile(metadata: tableMetadata) -> UIAlertController {
  187. let alertController = UIAlertController(title: NSLocalizedString("_rename_", comment: ""), message: nil, preferredStyle: .alert)
  188. let okAction = UIAlertAction(title: NSLocalizedString("_save_", comment: ""), style: .default, handler: { _ in
  189. guard let fileNameNew = alertController.textFields?.first?.text else { return }
  190. // verify if already exists
  191. if NCManageDatabase.shared.getMetadata(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileName == %@", metadata.account, metadata.serverUrl, fileNameNew)) != nil {
  192. NCContentPresenter().showError(error: NKError(errorCode: 0, errorDescription: "_rename_already_exists_"))
  193. return
  194. }
  195. NCNetworking.shared.renameMetadata(metadata, fileNameNew: fileNameNew)
  196. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource, userInfo: ["serverUrl": metadata.serverUrl])
  197. })
  198. // text field is initially empty, no action
  199. okAction.isEnabled = false
  200. let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel)
  201. alertController.addTextField { textField in
  202. textField.text = metadata.fileNameView
  203. textField.autocapitalizationType = .words
  204. }
  205. // only allow saving if folder name exists
  206. NotificationCenter.default.addObserver(
  207. forName: UITextField.textDidBeginEditingNotification,
  208. object: alertController.textFields?.first,
  209. queue: .main) { _ in
  210. guard let textField = alertController.textFields?.first else { return }
  211. if let start = textField.position(from: textField.beginningOfDocument, offset: 0),
  212. let end = textField.position(from: start, offset: textField.text?.withRemovedFileExtension.count ?? 0) {
  213. textField.selectedTextRange = textField.textRange(from: start, to: end)
  214. }
  215. }
  216. NotificationCenter.default.addObserver(
  217. forName: UITextField.textDidChangeNotification,
  218. object: alertController.textFields?.first,
  219. queue: .main) { _ in
  220. guard let text = alertController.textFields?.first?.text else { return }
  221. let textCheck = FileNameValidator.shared.checkFileName(text, account: NCManageDatabase.shared.getActiveTableAccount()?.account)
  222. okAction.isEnabled = textCheck?.error == nil && !text.isEmpty
  223. alertController.message = textCheck?.error.localizedDescription
  224. }
  225. alertController.addAction(cancelAction)
  226. alertController.addAction(okAction)
  227. return alertController
  228. }
  229. static func warning(title: String? = nil, message: String? = nil) -> UIAlertController {
  230. let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
  231. let okAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default)
  232. alertController.addAction(okAction)
  233. return alertController
  234. }
  235. }