123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- import UIKit
- import NextcloudKit
- extension UIAlertController {
-
-
-
-
-
-
- static func createFolder(serverUrl: String, urlBase: NCUserBaseUrl, markE2ee: Bool = false, completion: ((_ error: NKError) -> Void)? = nil) -> UIAlertController {
- let alertController = UIAlertController(title: NSLocalizedString("_create_folder_", comment: ""), message: nil, preferredStyle: .alert)
- let okAction = UIAlertAction(title: NSLocalizedString("_save_", comment: ""), style: .default, handler: { _ in
- guard let fileNameFolder = alertController.textFields?.first?.text else { return }
- if markE2ee {
- Task {
- let createFolderResults = await NextcloudKit.shared.createFolder(serverUrlFileName: serverUrl + "/" + fileNameFolder)
- if createFolderResults.error == .success {
- let error = await NCNetworkingE2EEMarkFolder().markFolderE2ee(account: urlBase.account, fileName: fileNameFolder, serverUrl: serverUrl, userId: urlBase.userId)
- if error != .success {
- NCContentPresenter.shared.showError(error: error)
- }
- } else {
- NCContentPresenter.shared.showError(error: createFolderResults.error)
- }
- }
- } else {
- NCNetworking.shared.createFolder(fileName: fileNameFolder, serverUrl: serverUrl, account: urlBase.account, urlBase: urlBase.urlBase, userId: urlBase.userId, overwrite: false, withPush: true) { error in
- if let completion = completion {
- completion(error)
- } else if error != .success {
- NCContentPresenter.shared.showError(error: error)
- }
- }
- }
- })
-
- okAction.isEnabled = false
- let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel)
- alertController.addTextField { textField in
- textField.autocapitalizationType = .words
- }
-
- NotificationCenter.default.addObserver(
- forName: UITextField.textDidChangeNotification,
- object: alertController.textFields?.first,
- queue: .main) { _ in
- guard let text = alertController.textFields?.first?.text,
- let folderName = CCUtility.removeForbiddenCharactersServer(text)?.trimmingCharacters(in: .whitespaces) else { return }
- okAction.isEnabled = !folderName.isEmpty && folderName != "." && folderName != ".."
- }
- alertController.addAction(cancelAction)
- alertController.addAction(okAction)
- return alertController
- }
- static func withTextField(titleKey: String, textFieldConfiguration: ((UITextField) -> Void)?, completion: @escaping (String?) -> Void) -> UIAlertController {
- let alertController = UIAlertController(title: NSLocalizedString(titleKey, comment: ""), message: "", preferredStyle: .alert)
- alertController.addTextField { textField in
- textFieldConfiguration?(textField)
- }
- alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .default) { _ in })
- let okAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default) { _ in
- completion(alertController.textFields?.first?.text)
- }
- alertController.addAction(okAction)
- return alertController
- }
- static func password(titleKey: String, completion: @escaping (String?) -> Void) -> UIAlertController {
- return .withTextField(titleKey: titleKey, textFieldConfiguration: { textField in
- textField.isSecureTextEntry = true
- textField.placeholder = NSLocalizedString("_password_", comment: "")
- }, completion: completion)
- }
- }
|