|
| 1 | +// |
| 2 | +// CLBreakPointResumeCell.swift |
| 3 | +// CLDemo |
| 4 | +// |
| 5 | +// Created by Chen JmoVxia on 2021/6/10. |
| 6 | +// Copyright © 2021 JmoVxia. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | + |
| 11 | +//MARK: - JmoVxia---类-属性 |
| 12 | +class CLBreakPointResumeCell: UITableViewCell { |
| 13 | + private var item: CLBreakPointResumeItem? |
| 14 | + private lazy var nameLabel: UILabel = { |
| 15 | + let view = UILabel() |
| 16 | + view.font = PingFangSCMedium(16) |
| 17 | + view.textColor = .black |
| 18 | + view.textAlignment = .left |
| 19 | + return view |
| 20 | + }() |
| 21 | + private lazy var progressLabel: UILabel = { |
| 22 | + let view = UILabel() |
| 23 | + view.font = PingFangSCMedium(12) |
| 24 | + view.textColor = .orange |
| 25 | + view.textAlignment = .left |
| 26 | + return view |
| 27 | + }() |
| 28 | + private lazy var progressView: UIProgressView = { |
| 29 | + let view = UIProgressView() |
| 30 | + return view |
| 31 | + }() |
| 32 | + private lazy var downloadButton: UIButton = { |
| 33 | + let view = UIButton() |
| 34 | + view.titleLabel?.font = PingFangSCMedium(16) |
| 35 | + view.setTitle("下载", for: .normal) |
| 36 | + view.setTitle("下载", for: .selected) |
| 37 | + view.setTitle("下载", for: .highlighted) |
| 38 | + view.setTitleColor(.black, for: .normal) |
| 39 | + view.setTitleColor(.black, for: .selected) |
| 40 | + view.setTitleColor(.black, for: .highlighted) |
| 41 | + view.addTarget(self, action: #selector(downloadAction), for: .touchUpInside) |
| 42 | + return view |
| 43 | + }() |
| 44 | + private lazy var cancelButton: UIButton = { |
| 45 | + let view = UIButton() |
| 46 | + view.titleLabel?.font = PingFangSCMedium(16) |
| 47 | + view.setTitle("取消", for: .normal) |
| 48 | + view.setTitle("取消", for: .selected) |
| 49 | + view.setTitle("取消", for: .highlighted) |
| 50 | + view.setTitleColor(.black, for: .normal) |
| 51 | + view.setTitleColor(.black, for: .selected) |
| 52 | + view.setTitleColor(.black, for: .highlighted) |
| 53 | + view.addTarget(self, action: #selector(cancelAction), for: .touchUpInside) |
| 54 | + return view |
| 55 | + }() |
| 56 | + private lazy var deleteButton: UIButton = { |
| 57 | + let view = UIButton() |
| 58 | + view.titleLabel?.font = PingFangSCMedium(16) |
| 59 | + view.setTitle("删除", for: .normal) |
| 60 | + view.setTitle("删除", for: .selected) |
| 61 | + view.setTitle("删除", for: .highlighted) |
| 62 | + view.setTitleColor(.black, for: .normal) |
| 63 | + view.setTitleColor(.black, for: .selected) |
| 64 | + view.setTitleColor(.black, for: .highlighted) |
| 65 | + view.addTarget(self, action: #selector(deleteAction), for: .touchUpInside) |
| 66 | + return view |
| 67 | + }() |
| 68 | + override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { |
| 69 | + super.init(style: style, reuseIdentifier: reuseIdentifier) |
| 70 | + initUI() |
| 71 | + makeConstraints() |
| 72 | + } |
| 73 | + required init?(coder: NSCoder) { |
| 74 | + fatalError("init(coder:) has not been implemented") |
| 75 | + } |
| 76 | +} |
| 77 | +//MARK: - JmoVxia---布局 |
| 78 | +private extension CLBreakPointResumeCell { |
| 79 | + func initUI() { |
| 80 | + selectionStyle = .none |
| 81 | + contentView.layer.borderColor = UIColor.orange.cgColor |
| 82 | + contentView.layer.borderWidth = 0.5 |
| 83 | + contentView.addSubview(nameLabel) |
| 84 | + contentView.addSubview(progressView) |
| 85 | + contentView.addSubview(progressLabel) |
| 86 | + contentView.addSubview(downloadButton) |
| 87 | + contentView.addSubview(cancelButton) |
| 88 | + contentView.addSubview(deleteButton) |
| 89 | + } |
| 90 | + func makeConstraints() { |
| 91 | + nameLabel.snp.makeConstraints { make in |
| 92 | + make.left.top.equalTo(15) |
| 93 | + make.height.equalTo(20) |
| 94 | + } |
| 95 | + progressView.snp.makeConstraints { make in |
| 96 | + make.left.equalTo(15) |
| 97 | + make.right.equalTo(-15) |
| 98 | + make.top.equalTo(nameLabel.snp.bottom).offset(10) |
| 99 | + } |
| 100 | + progressLabel.snp.makeConstraints { make in |
| 101 | + make.left.equalTo(15) |
| 102 | + make.centerY.equalTo(downloadButton) |
| 103 | + } |
| 104 | + downloadButton.snp.makeConstraints { make in |
| 105 | + make.width.equalTo(60) |
| 106 | + make.height.equalTo(30) |
| 107 | + make.bottom.equalTo(-15) |
| 108 | + make.right.equalTo(-15) |
| 109 | + } |
| 110 | + cancelButton.snp.makeConstraints { make in |
| 111 | + make.right.equalTo(downloadButton.snp.left).offset(-10) |
| 112 | + make.size.centerY.equalTo(downloadButton) |
| 113 | + } |
| 114 | + deleteButton.snp.makeConstraints { make in |
| 115 | + make.right.equalTo(cancelButton.snp.left).offset(-10) |
| 116 | + make.size.centerY.equalTo(downloadButton) |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | +@objc extension CLBreakPointResumeCell { |
| 121 | + func downloadAction() { |
| 122 | + guard let item = item else { return } |
| 123 | + CLBreakPointResumeManager.download(item.url) { progress in |
| 124 | + item.progress = progress |
| 125 | + self.progressView.progress = Float(progress) |
| 126 | + self.progressLabel.text = String(format: "%.2f", progress * 100) + "%" |
| 127 | + } completionBlock: { result in |
| 128 | + result.failure { error in |
| 129 | + CLLog("下载失败,error:\(error)") |
| 130 | + }.success { path in |
| 131 | + CLLog("下载成功,path:\(path)") |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + func cancelAction() { |
| 136 | + guard let item = item else { return } |
| 137 | + CLBreakPointResumeManager.cancel(item.url) |
| 138 | + } |
| 139 | + func deleteAction() { |
| 140 | + guard let item = item else { return } |
| 141 | + do { |
| 142 | + try CLBreakPointResumeManager.delete(item.url) |
| 143 | + item.progress = 0 |
| 144 | + progressView.progress = 0 |
| 145 | + progressLabel.text = "0.00%" |
| 146 | + } catch { |
| 147 | + CLLog("删除失败, error:\(error)") |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | +extension CLBreakPointResumeCell: CLCellProtocol { |
| 152 | + func setItem(_ item: CLCellItemProtocol, tableView: UITableView, indexPath: IndexPath) { |
| 153 | + guard let item = item as? CLBreakPointResumeItem else { return } |
| 154 | + self.item = item |
| 155 | + nameLabel.text = item.url.lastPathComponent |
| 156 | + progressView.progress = Float(item.progress) |
| 157 | + progressLabel.text = String(format: "%.2f", item.progress * 100) + "%" |
| 158 | + } |
| 159 | +} |
0 commit comments