瀏覽代碼

EditToolbar: multiple renaming and prefill rename textfield with title

Before it tried to present the alert over the old one. We need to wait for the completion
(closes #476)
Carola Nitz 6 年之前
父節點
當前提交
6017b9c35e
共有 2 個文件被更改,包括 33 次插入22 次删除
  1. 0 1
      Resources/en.lproj/Localizable.strings
  2. 33 21
      Sources/EditController.swift

+ 0 - 1
Resources/en.lproj/Localizable.strings

@@ -330,7 +330,6 @@
 "ERROR_PLAYLIST_CREATION" = "Failed to create a playlist";
 "ERROR_PLAYLIST_TRACKS" = "Failed to retrieve tracks";
 
-"RENAME_PLACEHOLDER" = "New title";
 "ERROR_RENAME_FAILED" = "Renaming failed";
 "ERROR_EMPTY_NAME" = "The title can't be empty";
 

+ 33 - 21
Sources/EditController.swift

@@ -54,15 +54,18 @@ private extension EditController {
         var alertTitle: String
         var alertDescription: String
         var placeHolder: String
+        var textfieldText: String
         var confirmActionTitle: String
 
         init(alertTitle: String = "",
              alertDescription: String = "",
              placeHolder: String = "",
+             textfieldText: String = "",
              confirmActionTitle: String = NSLocalizedString("BUTTON_DONE", comment: "")) {
             self.alertTitle = alertTitle
             self.alertDescription = alertDescription
             self.placeHolder = placeHolder
+            self.textfieldText = textfieldText
             self.confirmActionTitle = confirmActionTitle
         }
     }
@@ -75,6 +78,7 @@ private extension EditController {
 
         alertController.addTextField(configurationHandler: {
             textField in
+            textField.text = info.textfieldText
             textField.placeholder = info.placeHolder
         })
 
@@ -220,27 +224,35 @@ extension EditController: EditToolbarDelegate {
     }
 
     func editToolbarDidRename(_ editToolbar: EditToolbar) {
-        // FIXME: Multiple renaming of files(multiple alert can get unfriendly if too many files)
-        for indexPath in selectedCellIndexPaths {
-            if let media = model.anyfiles[indexPath.row] as? VLCMLMedia {
-                // Not using VLCAlertViewController to have more customization in text fields
-                let alertInfo = TextFieldAlertInfo(alertTitle: String(format: NSLocalizedString("RENAME_MEDIA_TO", comment: ""), media.title),
-                                                   placeHolder: NSLocalizedString("RENAME_PLACEHOLDER", comment: ""),
-                                                   confirmActionTitle: NSLocalizedString("BUTTON_RENAME", comment: ""))
-                presentTextFieldAlert(with: alertInfo, completionHandler: {
-                    [weak self] text -> Void in
-                    guard text != "" else {
-                        VLCAlertViewController.alertViewManager(title: NSLocalizedString("ERROR_RENAME_FAILED", comment: ""),
-                                                                errorMessage: NSLocalizedString("ERROR_EMPTY_NAME", comment: ""),
-                                                                viewController: (UIApplication.shared.keyWindow?.rootViewController)!)
-                        return
-                    }
-                    media.updateTitle(text)
-                    if let strongself = self {
-                        strongself.delegate?.editController(editController: strongself, cellforItemAt: indexPath)?.isChecked = false
-                    }
-                })
-            }
+
+        guard let indexPath = selectedCellIndexPaths.first else {
+            assertionFailure("called without selectedcells")
+            return
+        }
+        if let media = model.anyfiles[indexPath.row] as? VLCMLMedia {
+            // Not using VLCAlertViewController to have more customization in text fields
+            let alertInfo = TextFieldAlertInfo(alertTitle: String(format: NSLocalizedString("RENAME_MEDIA_TO", comment: ""), media.title),
+                                               textfieldText: media.title,
+                                               confirmActionTitle: NSLocalizedString("BUTTON_RENAME", comment: ""))
+            presentTextFieldAlert(with: alertInfo, completionHandler: {
+                [weak self] text -> Void in
+                guard text != "" else {
+                    VLCAlertViewController.alertViewManager(title: NSLocalizedString("ERROR_RENAME_FAILED", comment: ""),
+                                                            errorMessage: NSLocalizedString("ERROR_EMPTY_NAME", comment: ""),
+                                                            viewController: (UIApplication.shared.keyWindow?.rootViewController)!)
+                    return
+                }
+                media.updateTitle(text)
+                guard let strongself = self else {
+                    return
+                }
+                strongself.delegate?.editController(editController: strongself, cellforItemAt: indexPath)?.isChecked = false
+                strongself.selectedCellIndexPaths.remove(indexPath)
+                //call until all items are renamed
+                if !strongself.selectedCellIndexPaths.isEmpty {
+                    strongself.editToolbarDidRename(editToolbar)
+                }
+            })
         }
     }
 }