UIAlertController+Extension.swift 15 KB

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