UIAlertController+Extension.swift 15 KB

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