VLCEditController.swift 6.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*****************************************************************************
 * VLCEditController.swift
 *
 * Copyright © 2018 VLC authors and VideoLAN
 * Copyright © 2018 Videolabs
 *
 * Authors: Soomin Lee <bubu@mikan.io>
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/

protocol VLCEditControllerDataSource {
    func toolbarNeedsUpdate(editing: Bool)
}

class VLCEditController: NSObject {
17
    private var selectedCellIndexPaths = Set<IndexPath>()
18 19 20 21 22 23 24
    private let collectionView: UICollectionView
    private let category: MediaLibraryBaseModel

    private lazy var editToolbar: VLCEditToolbar = {
        let editToolbar = VLCEditToolbar(frame: CGRect(x: 0, y: 550,
                                                       width: collectionView.frame.width, height: 50))
        editToolbar.isHidden = true
25
        editToolbar.delegate = self
26 27 28 29 30 31 32 33 34 35 36 37 38
        return editToolbar
    }()

    init(collectionView: UICollectionView, category: MediaLibraryBaseModel) {
        self.collectionView = collectionView
        self.category = category
        super.init()

        collectionView.addSubview(editToolbar)
        collectionView.bringSubview(toFront: editToolbar)
    }
}

39 40 41 42 43 44 45 46 47
// MARK: - Helpers

private extension VLCEditController {
    private func resetCell(at indexPath: IndexPath) {
        if let cell = collectionView.cellForItem(at: indexPath) as? VLCMediaViewEditCell {
            cell.checkView.isEnabled = false
        }
        collectionView.reloadData()
    }
48 49 50 51 52 53

    private func resetAllVisibleCell() {
        for case let cell as VLCMediaViewEditCell in collectionView.visibleCells {
            cell.checkView.isEnabled = false
        }
    }
54 55
}

56 57 58
extension VLCEditController: VLCEditControllerDataSource {
    func toolbarNeedsUpdate(editing: Bool) {
        editToolbar.isHidden = !editing
59 60 61 62
        if !editing {
            // not in editing mode anymore should reset
            selectedCellIndexPaths.removeAll(keepingCapacity: false)
        }
63 64 65
    }
}

66 67 68 69 70 71
extension VLCEditController: VLCEditToolbarDelegate {
    func createPlaylist() {

    }

    func delete() {
72 73 74 75 76 77 78 79 80 81 82 83 84 85
        var objectsToDelete = [VLCMLObject]()

        for indexPath in selectedCellIndexPaths {
            objectsToDelete.append(category.anyfiles[indexPath.row])
        }

        let cancelButton = VLCAlertButton(title: NSLocalizedString("BUTTON_CANCEL", comment: ""))
        let deleteButton = VLCAlertButton(title: NSLocalizedString("BUTTON_DELETE", comment: ""),
                                          action: {
                                            [weak self] action in
                                            self?.category.delete(objectsToDelete)
                                            self?.selectedCellIndexPaths.removeAll()
                                            self?.resetAllVisibleCell()
        })
86

87 88 89 90 91
        VLCAlertViewController.alertViewManager(title: NSLocalizedString("DELETE_TITLE", comment: ""),
                                                errorMessage: NSLocalizedString("DELETE_MESSAGE", comment: ""),
                                                viewController: (UIApplication.shared.keyWindow?.rootViewController)!,
                                                buttonsAction: [cancelButton,
                                                                deleteButton])
92 93 94
    }

    func rename() {
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
        for indexPath in selectedCellIndexPaths {
            if let media = category.anyfiles[indexPath.row] as? VLCMLMedia {
                // Not using VLCAlertViewController to have more customization in text fields
                let alertController = UIAlertController(title: String(format: NSLocalizedString("RENAME_MEDIA_TO", comment: ""), media.title),
                                                        message: "",
                                                        preferredStyle: .alert)

                alertController.addTextField(configurationHandler: {
                    textField in
                    textField.placeholder = NSLocalizedString("NEW_NAME", comment: "")
                })

                let cancelButton = UIAlertAction(title: NSLocalizedString("BUTTON_CANCEL", comment: ""),
                                                 style: .default)

110

111 112 113 114 115
                let confirmAction = UIAlertAction(title: NSLocalizedString("BUTTON_DONE", comment: ""), style: .default) {
                    [weak alertController, weak self] _ in
                    guard let alertController = alertController,
                        let textField = alertController.textFields?.first else { return }
                    media.updateTitle(textField.text)
116
                    self?.resetCell(at: indexPath)
117 118 119 120 121 122 123 124
                }

                alertController.addAction(cancelButton)
                alertController.addAction(confirmAction)

                UIApplication.shared.keyWindow?.rootViewController?.present(alertController, animated: true, completion: nil)
            }
        }
125 126 127
    }
}

128 129 130 131 132 133 134 135
extension VLCEditController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return category.anyfiles.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: VLCMediaViewEditCell.identifier,
                                                         for: indexPath) as? VLCMediaViewEditCell {
136 137
            if let media = category.anyfiles[indexPath.row] as? VLCMLMedia {
                cell.titleLabel.text = media.title
138 139
                cell.subInfoLabel.text = media.formatDuration()
                cell.sizeLabel.text = media.formatSize()
140
            }
141 142 143 144 145 146 147 148 149 150
            return cell
        }
        return UICollectionViewCell()
    }
}

extension VLCEditController: UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let cell = collectionView.cellForItem(at: indexPath) as? VLCMediaViewEditCell {
            cell.checkView.isEnabled = !cell.checkView.isEnabled
151 152 153 154 155 156
            if cell.checkView.isEnabled {
                // cell selected, saving indexPath
                selectedCellIndexPaths.insert(indexPath)
            } else {
                selectedCellIndexPaths.remove(indexPath)
            }
157 158 159 160 161 162 163 164 165 166 167 168
        }
    }
}

extension VLCEditController: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let contentInset = collectionView.contentInset
        // FIXME: 5 should be cell padding, but not usable maybe static?
        let insetToRemove = contentInset.left + contentInset.right + (5 * 2)
        return CGSize(width: collectionView.frame.width - insetToRemove, height: VLCMediaViewEditCell.height)
    }
}