UIAlertController+Extension.swift 13 KB

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