Skip to content

Commit e628f3e

Browse files
author
JmoVxia
committed
增加断点续传
1 parent 6671cb4 commit e628f3e

18 files changed

+786
-86
lines changed

CLDemo.xcodeproj/project.pbxproj

Lines changed: 47 additions & 13 deletions
Large diffs are not rendered by default.

CLDemo/AppDelegate.h

Lines changed: 0 additions & 17 deletions
This file was deleted.

CLDemo/AppDelegate.m

Lines changed: 0 additions & 37 deletions
This file was deleted.

CLDemo/AppDelegate.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// AppDelegate.swift
3+
// demo
4+
//
5+
// Created by Chen JmoVxia on 2021/6/21.
6+
//
7+
8+
import UIKit
9+
10+
@main
11+
class AppDelegate: UIResponder, UIApplicationDelegate {
12+
var window: UIWindow?
13+
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
14+
let tabbarController = CLTabbarController()
15+
window?.rootViewController = tabbarController
16+
window?.makeKeyAndVisible()
17+
return true
18+
}
19+
}
20+
extension AppDelegate {
21+
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
22+
if let keyWindow = application.keyWindow, keyWindow.isKind(of: CLPopupManagerWindow.self) {
23+
return keyWindow.rootViewController?.supportedInterfaceOrientations ?? .all
24+
}else {
25+
return .all
26+
}
27+
}
28+
}

CLDemo/CLDemo-Bridging-Header.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
#import "CLBaseViewController.h"
66
#import "TZImagePickerController.h"
77
#import "CLRecorder.h"
8-
#import "AppDelegate.h"
98
#import "CLBroadcastView.h"
109
#import "CLBroadcastMainCell.h"
10+
#import "CLTabbarController.h"
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//
2+
// CLBreakPointResumeController.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+
class CLBreakPointResumeController: CLBaseViewController {
12+
private lazy var tableViewHepler: CLTableViewHepler = {
13+
let hepler = CLTableViewHepler()
14+
return hepler
15+
}()
16+
private lazy var tableView: UITableView = {
17+
let tableView = UITableView()
18+
tableView.dataSource = tableViewHepler
19+
tableView.delegate = tableViewHepler
20+
tableView.backgroundColor = UIColor.clear
21+
tableView.separatorStyle = .none
22+
if #available(iOS 11.0, *) {
23+
tableView.contentInsetAdjustmentBehavior = .never
24+
}
25+
return tableView
26+
}()
27+
28+
override func viewDidLoad() {
29+
super.viewDidLoad()
30+
title = "最大并发3"
31+
view.addSubview(tableView)
32+
tableView.snp.makeConstraints { make in
33+
make.left.right.bottom.equalTo(view)
34+
make.top.equalTo(statusBarHeight() + (navigationController?.navigationBar.bounds.height ?? 0))
35+
}
36+
do {
37+
let item = CLBreakPointResumeItem(url: URL(string: "https://dldir1.qq.com/qqtv/mac/TencentVideo_V2.21.0.52979.dmg")!)
38+
tableViewHepler.dataSource.append(item)
39+
}
40+
do {
41+
let item = CLBreakPointResumeItem(url: URL(string: "https://package.mac.wpscdn.cn/mac_wps_pkg/3.7.0/WPS_Office_3.7.0(5920).dmg")!)
42+
tableViewHepler.dataSource.append(item)
43+
}
44+
do {
45+
let item = CLBreakPointResumeItem(url: URL(string: "https://dldir1.qq.com/qqfile/QQforMac/QQCatalyst/QQ_8.4.10.118.dmg")!)
46+
tableViewHepler.dataSource.append(item)
47+
}
48+
do {
49+
let item = CLBreakPointResumeItem(url: URL(string: "https://d1.music.126.net/dmusic/NeteaseMusic_2.3.5_852_web.dmg")!)
50+
tableViewHepler.dataSource.append(item)
51+
}
52+
do {
53+
let item = CLBreakPointResumeItem(url: URL(string: "https://down.sandai.net/mac/thunder_4.0.1.14502.dmg")!)
54+
tableViewHepler.dataSource.append(item)
55+
}
56+
do {
57+
let item = CLBreakPointResumeItem(url: URL(string: "https://down.sandai.net/mac/player_3.0.1.12449.dmg")!)
58+
tableViewHepler.dataSource.append(item)
59+
}
60+
do {
61+
let item = CLBreakPointResumeItem(url: URL(string: "https://dldir1.qq.com/weixin/mac/WeChatMac.dmg")!)
62+
tableViewHepler.dataSource.append(item)
63+
}
64+
tableView.reloadData()
65+
}
66+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// CLBreakPointResumeItem.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+
12+
class CLBreakPointResumeItem: NSObject {
13+
let url: URL!
14+
var progress: CGFloat = 0
15+
init(url: URL) {
16+
self.url = url
17+
let fileAttribute = CLBreakPointResumeManager.fileAttribute(url)
18+
progress = fileAttribute.totalBytes <= 0 ? 0 : min(max(0, CGFloat(fileAttribute.currentBytes) / CGFloat(fileAttribute.totalBytes)), 1)
19+
}
20+
}
21+
extension CLBreakPointResumeItem: CLCellItemProtocol {
22+
func bindCell() -> UITableViewCell.Type {
23+
return CLBreakPointResumeCell.self
24+
}
25+
func cellHeight() -> CGFloat {
26+
return 110
27+
}
28+
}

0 commit comments

Comments
 (0)