智汀云盘-开发指南iOS端:文件夹管理

智汀云盘-开发指南iOS端:文件夹管理_第1张图片

1. 文件夹列表: FolderManageViewController.swift

   /// 请求数据
    private func requestData() {
        if folders.count == 0 {
            showLoading()
        }
        
        let page = (folders.count / 30) + 1
        NetworkManager.shared.folderList(page: page, pageSize: 30) { [weak self] response in
            guard let self = self else { return }
            self.hideLoading()
            self.folders.append(contentsOf: response.list)
            self.collectionView.mj_header?.endRefreshing()
            self.collectionView.mj_footer?.endRefreshing()
            if !response.pager.has_more {
                self.collectionView.mj_footer?.endRefreshingWithNoMoreData()
            }
            self.collectionView.reloadData()
        } failureCallback: { [weak self] code, err in
            self?.collectionView.mj_header?.endRefreshing()
            self?.collectionView.mj_footer?.endRefreshing()
            self?.collectionView.reloadData()
            self?.hideLoading()
            self?.showToast(err)
        }
    }
    /// cell配置
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: FolderManageCell.reusableIdentifier, for: indexPath) as! FolderManageCell
        
        let folder = folders[indexPath.row]
        cell.folder = folder
        /// menu按钮回调
        cell.menuCallback = { [weak self] in
            guard let self = self, let cell = self.collectionView.cellForItem(at: indexPath) else { return }
            let x = 35.ztScaleValue
            let y = 50.ztScaleValue + Screen.k_nav_height
            let alertPoint = cell.convert(CGPoint(x: x, y: y), to: self.view)
            let alert = MenuAlert(items: [.init(title: "更改密码", icon: .assets(.icon_lock))], alertPoint: alertPoint)
            alert.selectCallback = { [weak self] item in
                guard let self = self else { return }
                if item.title == "更改密码" {
                    let alert = FolderEditPwdAlert()
                    alert.saveCallback = { [weak self] old, new, confrim in
                        guard let self = self else { return }
                        LoadingView.show()
                        NetworkManager.shared.editFolderPwd(id: folder.id, oldPwd: old, newPwd: new, confirmPwd: confrim) { [weak self] _ in
                            guard let self = self else { return }
                            alert.removeFromSuperview()
                            LoadingView.hide()
                            SceneDelegate.shared.window?.makeToast("修改成功".localizedString)
                            self.reloadData()
                        } failureCallback: { code, err in
                            LoadingView.hide()
                            SceneDelegate.shared.window?.makeToast(err)
                        }
                    }
                    SceneDelegate.shared.window?.addSubview(alert)
                }
            }
            SceneDelegate.shared.window?.addSubview(alert)
        }         
        /// 状态cover 按钮回调
        cell.statusCoverCallback = { [weak self] index in
            guard let self = self else { return }
            switch folder.statusEnum {
            case .failToDelete:
                if index == 0 { // 修改文件夹失败 - 重试
                    self.showLoading()
                    NetworkManager.shared.restartAsyncTask(task_id: folder.task_id) { [weak self] _ in
                        guard let self = self else { return }
                        self.hideLoading()
                        self.reloadData()
                        
                    } failureCallback: { [weak self] code, err in
                        self?.hideLoading()
                        self?.showToast(err)
                    }
                }
            case .failToEdit:
                if index == 0 { // 修改文件夹失败 - 确定
                    self.showLoading()
                    NetworkManager.shared.deleteAsyncTask(task_id: folder.task_id) { [weak self] _ in
                        guard let self = self else { return }
                        self.hideLoading()
                        self.reloadData()
                        
                    } failureCallback: { [weak self] code, err in
                        self?.hideLoading()
                        self?.showToast(err)
                    }
                }
            default:
                break
            }
        }
        return cell
    }

2. 文件夹设置: FolderManageSettingViewController.swift

智汀云盘-开发指南iOS端:文件夹管理_第2张图片

   /// 保存文件夹设置
    @objc
    private func tapSave() {
        guard let pool = selectedPool, let partition = selectedPartition else { return }

        showLoading()
        
        NetworkManager.shared.setFolderSettings(poolName: pool.name, partitionName: partition.name, autoDel: autoDeleteCell.switchBtn.isOn) { [weak self] _ in
            self?.hideLoading()
            SceneDelegate.shared.window?.makeToast("保存成功".localizedString)
            self?.navigationController?.popViewController(animated: true)
        } failureCallback: { [weak self] code, err in
            self?.hideLoading()
            self?.showToast(err)
        }
    }
        /// 获取文件夹设置
    private func getFolderSettings() {
        showLoading(.custom(.white_ffffff))
        let sp = DispatchSemaphore(value: 1)
        DispatchQueue.global().async {
            sp.wait()
            /// 1获取存储池列表
            NetworkManager.shared.storagePoolList(page: 0, pageSize: 0) { [weak self] response in
                guard let self = self else { return }
                self.storagePools = response.list
                sp.signal()

            } failureCallback: { _, _ in
                sp.signal()
            }
            sp.wait()
            NetworkManager.shared.getFolderSettings { [weak self] settings in
                sp.signal()
                guard let self = self else { return }
                self.hideLoading()
                self.autoDeleteCell.switchBtn.setIsOn(settings.is_auto_del) 
                self.storageDefaultCell.nameLabel.text = "\(settings.pool_name)-\(settings.partition_name)"
                self.selectedPool = self.storagePools.first(where: { $0.name == settings.pool_name })
                if let selectedPool = self.selectedPool {
                    self.selectedPartition = selectedPool.lv.first(where: { $0.name == settings.partition_name })
                }
                
            } failureCallback: { [weak self] code, err in
                sp.signal()
                self?.hideLoading()
                self?.showToast(err)
            }
        }
    }

3. 创建、编辑、删除文件夹:EditFolderViewController.swift

智汀云盘-开发指南iOS端:文件夹管理_第3张图片
智汀云盘-开发指南iOS端:文件夹管理_第4张图片
智汀云盘-开发指南iOS端:文件夹管理_第5张图片

 /// 删除文件夹
    @objc
    private func tapDelete() {
        let alert = TipsAlertView(title: "确定删除该文件夹吗?", detail: "删除后,该文件夹及其包含的所有文件夹/文件都全部删除。", warning: "操作不可撤销,请谨慎操作!", sureBtnTitle: "确认删除")
        
        alert.sureCallback = { [weak self] in
            guard let self = self else { return }
            guard let id = self.type.folderId else { return }

            LoadingView.show()
            NetworkManager.shared.deleteFolder(id: id) { [weak self] _ in
                LoadingView.hide()
                self?.showToast("删除成功".localizedString)
                alert.removeFromSuperview()
                self?.navigationController?.popViewController(animated: true)
            } failureCallback: { [weak self] code, err in
                LoadingView.hide()
                self?.showToast(err)
                alert.removeFromSuperview()
            }
        }
        SceneDelegate.shared.window?.addSubview(alert)
    }
    
    /// 保存修改
    @objc
    private func save() {
        if type == .create {
            let name = nameCell.textField.text ?? ""
            let pool_name = selectedPool?.name ?? ""
            let partition_name = selectedPartition?.name ?? ""
            let is_encrypt = isSecureCell.selectView.selectedIndex == 0 ? 1 : 0
            let pwd = pwdCell1.textField.text ?? ""
            let confirmPwd = pwdCell2.textField.text ?? ""
            var mode: FolderModel.FolderMode = .shared
            if folderTypeCell.selectView.selectedIndex == 0 {
                mode = .private
            }
            showLoading()
            NetworkManager.shared.createFolder(name: name, pool_name: pool_name, partition_name: partition_name, is_encrypt: is_encrypt, pwd: pwd, confirm_pwd: confirmPwd, mode: mode, auth: members) { [weak self] _ in
                self?.showToast("保存成功".localizedString)
                self?.navigationController?.popViewController(animated: true)

            } failureCallback: { [weak self] code, err in
                self?.hideLoading()
                self?.showToast(err)
            }
        } else {
            guard let folder = folder else { return }
            let name = nameCell.textField.text ?? ""
            let pool_name = selectedPool?.name ?? ""
            let partition_name = selectedPartition?.name ?? ""
            var mode: FolderModel.FolderMode = .shared
            if folderTypeCell.selectView.selectedIndex == 0 {
                mode = .private
            }
            //如果修改了分区
            if pool_name != folder.pool_name || partition_name != folder.partition_name {
                let tipsAlert = TipsAlertView(title: "存储分区转移".localizedString, titleColor: .custom(.black_3f4663), detail: "\(name)存储分区从“\(folder.pool_name ?? "")-\(folder.partition_name ?? "")”改为“\(pool_name)-\(partition_name)”", detailColor: .custom(.black_3f4663), warning: "修改预计需要一段时间处理,且中途不可取消。确定要修改吗?".localizedString, warningColor: .custom(.red_fe0000), sureBtnTitle: "确定".localizedString)
                tipsAlert.sureCallback = { [weak self] in
                    guard let self = self else { return }
                    self.editFolder(id: folder.id, name: name, pool_name: pool_name, partition_name: partition_name, is_encrypt: folder.is_encrypt, mode: mode, auth: self.members)
                    tipsAlert.removeFromSuperview()
                }
                
                SceneDelegate.shared.window?.addSubview(tipsAlert)
                
            } else {
                editFolder(id: folder.id, name: name, pool_name: pool_name, partition_name: partition_name, is_encrypt: folder.is_encrypt, mode: mode, auth: members)
            }    
        }
    }
    
    /// 编辑文件夹
    private func editFolder(id: Int, name: String, pool_name: String, partition_name: String, is_encrypt: Int, mode: FolderModel.FolderMode, auth: [User]) {
        showLoading()
        NetworkManager.shared.editFolder(id: id, name: name, pool_name: pool_name, partition_name: partition_name, is_encrypt: is_encrypt, mode: mode, auth: auth) { [weak self] _ in
            guard let self = self else { return }
            if pool_name != self.folder?.pool_name || partition_name != self.folder?.partition_name {
                //如果修改了分区
                let alert = SingleTipsAlertView(detail: "存储分区转移" + "\n\n" + "\(name)存储分区正在从“\(self.folder?.pool_name ?? "")-\(self.folder?.partition_name ?? "")”改为“\(pool_name)-\(partition_name)”,已为您后台运行,可返回列表查看。", sureBtnTitle: "确定".localizedString)
                alert.sureCallback = { [weak self] in
                    guard let self = self else { return }
                    alert.removeFromSuperview()
                    self.showToast("保存成功".localizedString)
                    self.navigationController?.popViewController(animated: true)
                }
                SceneDelegate.shared.window?.addSubview(alert)

            } else {
                self.showToast("保存成功".localizedString)
                self.navigationController?.popViewController(animated: true)
            }
        } failureCallback: { [weak self] code, err in
            guard let self = self else { return }
            self.hideLoading()
            if code == 20019 {
                //目标分区容量不足,不能迁移
                let errAlert = SingleTipsAlertView(detail: "存储分区修改失败".localizedString + "\n\n" + "分区容量不足!".localizedString, sureBtnTitle: "确定".localizedString)
                errAlert.sureCallback = {
                    errAlert.removeFromSuperview()
                }
                SceneDelegate.shared.window?.addSubview(errAlert)
                
            } else {
                self.showToast(err)
            } 
        }
    }

你可能感兴趣的:(ios)