|
@@ -0,0 +1,69 @@
|
|
|
+//
|
|
|
+// UIAlertController+Extension.swift
|
|
|
+// Nextcloud
|
|
|
+//
|
|
|
+// Created by Henrik Storch on 27.01.22.
|
|
|
+// Copyright © 2022 Henrik Storch. All rights reserved.
|
|
|
+//
|
|
|
+// Author Henrik Storch <henrik.storch@nextcloud.com>
|
|
|
+//
|
|
|
+// This program is free software: you can redistribute it and/or modify
|
|
|
+// it under the terms of the GNU General Public License as published by
|
|
|
+// the Free Software Foundation, either version 3 of the License, or
|
|
|
+// (at your option) any later version.
|
|
|
+//
|
|
|
+// This program is distributed in the hope that it will be useful,
|
|
|
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
+// GNU General Public License for more details.
|
|
|
+//
|
|
|
+// You should have received a copy of the GNU General Public License
|
|
|
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
+//
|
|
|
+
|
|
|
+import UIKit
|
|
|
+
|
|
|
+extension UIAlertController {
|
|
|
+ /// Creates a alert controller with a textfield, asking to create a new folder
|
|
|
+ /// - Parameters:
|
|
|
+ /// - serverUrl: Server url of the location where the folder should be created
|
|
|
+ /// - urlBase: UrlBase object
|
|
|
+ /// - completion: If not` nil` it overrides the default behavior which shows an error using `NCContentPresenter`
|
|
|
+ /// - Returns: The presentable alert controller
|
|
|
+ static func createFolder(serverUrl: String, urlBase: NCUserBaseUrl, completion: ((_ errorCode: Int, _ errorDescription: String) -> 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 }
|
|
|
+ NCNetworking.shared.createFolder(fileName: fileNameFolder, serverUrl: serverUrl, account: urlBase.account, urlBase: urlBase.urlBase, overwrite: false) { errorCode, errorDescription in
|
|
|
+ if let completion = completion {
|
|
|
+ completion(errorCode, errorDescription)
|
|
|
+ } else if errorCode != 0 {
|
|
|
+ NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
|
|
|
+ } // else: successful, no action
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ // text field is initially empty, no action
|
|
|
+ okAction.isEnabled = false
|
|
|
+ let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel)
|
|
|
+
|
|
|
+ alertController.addTextField { textField in
|
|
|
+ textField.autocapitalizationType = .words
|
|
|
+ }
|
|
|
+
|
|
|
+ // only allow saving if folder name exists
|
|
|
+ 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
|
|
|
+ }
|
|
|
+}
|