Skip to content

Commit

Permalink
release 3.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Danie1s committed Feb 28, 2020
1 parent 95a59ea commit 8cde56c
Show file tree
Hide file tree
Showing 18 changed files with 4,559 additions and 610 deletions.
62 changes: 23 additions & 39 deletions Demo/BaseViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,19 @@ class BaseViewController: UIViewController {
tableView.dataSource = self
tableView.delegate = self
tableView.tableFooterView = UIView()
tableView.register(UINib(nibName: "\(DownloadTaskCell.self)", bundle: nil), forCellReuseIdentifier: DownloadTaskCell.reuseIdentifier)
tableView.register(UINib(nibName: "\(DownloadTaskCell.self)", bundle: nil),
forCellReuseIdentifier: DownloadTaskCell.reuseIdentifier)
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 164

configureNavigationItem()
}

func configureNavigationItem() {
let editingItem = UIBarButtonItem(title: tableView.isEditing ? "完成" : "编辑", style: .plain, target: self, action: #selector(toggleEditing))
let editingItem = UIBarButtonItem(title: tableView.isEditing ? "完成" : "编辑",
style: .plain,
target: self,
action: #selector(toggleEditing))
navigationItem.rightBarButtonItems = [editingItem]
}

Expand All @@ -64,19 +68,16 @@ class BaseViewController: UIViewController {
}

func updateUI() {
guard let downloadManager = sessionManager else { return }
totalTasksLabel.text = "总任务:\(downloadManager.succeededTasks.count)/\(downloadManager.tasks.count)"
totalSpeedLabel.text = "总速度:\(downloadManager.speedString)"
timeRemainingLabel.text = "剩余时间: \(downloadManager.timeRemainingString)"
let per = String(format: "%.2f", downloadManager.progress.fractionCompleted)
totalTasksLabel.text = "总任务:\(sessionManager.succeededTasks.count)/\(sessionManager.tasks.count)"
totalSpeedLabel.text = "总速度:\(sessionManager.speedString)"
timeRemainingLabel.text = "剩余时间: \(sessionManager.timeRemainingString)"
let per = String(format: "%.2f", sessionManager.progress.fractionCompleted)
totalProgressLabel.text = "总进度: \(per)"

}

func updateSwicth() {
guard let downloadManager = sessionManager else { return }
taskLimitSwitch.isOn = downloadManager.configuration.maxConcurrentTasksLimit < 3
cellularAccessSwitch.isOn = downloadManager.configuration.allowsCellularAccess
taskLimitSwitch.isOn = sessionManager.configuration.maxConcurrentTasksLimit < 3
cellularAccessSwitch.isOn = sessionManager.configuration.allowsCellularAccess
}

func setupManager() {
Expand All @@ -98,13 +99,15 @@ class BaseViewController: UIViewController {

extension BaseViewController {
@IBAction func totalStart(_ sender: Any) {
sessionManager.totalStart()
tableView.reloadData()
sessionManager.totalStart { [weak self] _ in
self?.tableView.reloadData()
}
}

@IBAction func totalSuspend(_ sender: Any) {
sessionManager.totalSuspend() { [weak self] _ in
self?.tableView.reloadData()

}
}

Expand All @@ -121,26 +124,21 @@ extension BaseViewController {
}

@IBAction func clearDisk(_ sender: Any) {
guard let downloadManager = sessionManager else { return }
downloadManager.cache.clearDiskCache()
sessionManager.cache.clearDiskCache()
updateUI()
}


@IBAction func taskLimit(_ sender: UISwitch) {
let isTaskLimit = sender.isOn
if isTaskLimit {
if sender.isOn {
sessionManager.configuration.maxConcurrentTasksLimit = 2
} else {
sessionManager.configuration.maxConcurrentTasksLimit = Int.max
}
updateSwicth()

}

@IBAction func cellularAccess(_ sender: UISwitch) {
sessionManager.configuration.allowsCellularAccess = sender.isOn
updateSwicth()
}
}

Expand All @@ -160,18 +158,7 @@ extension BaseViewController: UITableViewDataSource, UITableViewDelegate {

guard let task = sessionManager.tasks.safeObject(at: indexPath.row),
let cell = cell as? DownloadTaskCell else { return }

let image: UIImage
switch task.status {
case .running:
image = #imageLiteral(resourceName: "resume")
default:
image = #imageLiteral(resourceName: "suspend")
}


cell.controlButton.setImage(image, for: .normal)


cell.titleLabel.text = task.fileName

cell.updateProgress(task)
Expand All @@ -182,26 +169,23 @@ extension BaseViewController: UITableViewDataSource, UITableViewDelegate {
let task = self?.sessionManager.tasks.safeObject(at: indexPath.row)
else { return }
switch task.status {
case .running:
self?.sessionManager?.suspend(task)
case .waiting, .suspended, .failed:
self?.sessionManager?.start(task)
case .waiting, .running:
self?.sessionManager.suspend(task)
case .suspended, .failed:
self?.sessionManager.start(task)
default: break
}
}

task.progress { [weak cell] (task) in
cell?.controlButton.setImage(#imageLiteral(resourceName: "resume"), for: .normal)
cell?.updateProgress(task)
}
.success { [weak cell] (task) in
cell?.controlButton.setImage(#imageLiteral(resourceName: "suspend"), for: .normal)
cell?.updateProgress(task)
// 下载任务成功了

}
.failure { [weak cell] (task) in
cell?.controlButton.setImage(#imageLiteral(resourceName: "suspend"), for: .normal)
cell?.updateProgress(task)
if task.status == .suspended {
// 下载任务暂停了
Expand Down
29 changes: 27 additions & 2 deletions Demo/DownloadTaskCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class DownloadTaskCell: UITableViewCell {
@IBOutlet weak var timeRemainingLabel: UILabel!
@IBOutlet weak var startDateLabel: UILabel!
@IBOutlet weak var endDateLabel: UILabel!

@IBOutlet weak var statusLabel: UILabel!

var tapClosure: ((DownloadTaskCell) -> Void)?


Expand All @@ -30,12 +31,36 @@ class DownloadTaskCell: UITableViewCell {
}

func updateProgress(_ task: DownloadTask) {
progressView.progress = Float(task.progress.fractionCompleted)
progressView.observedProgress = task.progress
bytesLabel.text = "\(task.progress.completedUnitCount.tr.convertBytesToString())/\(task.progress.totalUnitCount.tr.convertBytesToString())"
speedLabel.text = task.speedString
timeRemainingLabel.text = "剩余时间:\(task.timeRemainingString)"
startDateLabel.text = "开始时间:\(task.startDateString)"
endDateLabel.text = "结束时间:\(task.endDateString)"

var image = #imageLiteral(resourceName: "suspend")
switch task.status {
case .suspended:
statusLabel.text = "暂停"
statusLabel.textColor = .black
case .running:
image = #imageLiteral(resourceName: "resume")
statusLabel.text = "下载中"
statusLabel.textColor = .blue
case .succeeded:
statusLabel.text = "成功"
statusLabel.textColor = .green
case .failed:
statusLabel.text = "失败"
statusLabel.textColor = .red
case .waiting:
statusLabel.text = "等待中"
statusLabel.textColor = .orange
default:
image = controlButton.imageView?.image ?? #imageLiteral(resourceName: "suspend")
break
}
controlButton.setImage(image, for: .normal)
}

}
17 changes: 13 additions & 4 deletions Demo/DownloadTaskCell.xib
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="15702" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="15705" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="15704"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="15706"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
Expand Down Expand Up @@ -32,7 +32,7 @@
<rect key="frame" x="16" y="121" width="231" height="2"/>
</progressView>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="usv-X3-4y3">
<rect key="frame" x="263" y="43" width="50" height="50"/>
<rect key="frame" x="263" y="23" width="50" height="50"/>
<constraints>
<constraint firstAttribute="height" constant="50" id="Fyj-ze-QKb"/>
<constraint firstAttribute="width" constant="50" id="u7N-L8-JOe"/>
Expand Down Expand Up @@ -66,6 +66,12 @@
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="等待中" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="kwN-Ec-QjZ">
<rect key="frame" x="265" y="95" width="46" height="18"/>
<fontDescription key="fontDescription" type="system" pointSize="15"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<constraints>
<constraint firstItem="usv-X3-4y3" firstAttribute="leading" secondItem="VxF-sI-uou" secondAttribute="trailing" constant="10" id="22M-LD-TvR"/>
Expand All @@ -83,12 +89,14 @@
<constraint firstItem="pUI-TD-eZ0" firstAttribute="leading" secondItem="Pyb-wY-gDY" secondAttribute="trailing" constant="20" id="iVT-4t-mhx"/>
<constraint firstItem="pUI-TD-eZ0" firstAttribute="centerY" secondItem="Pyb-wY-gDY" secondAttribute="centerY" id="khJ-8A-xom"/>
<constraint firstItem="usv-X3-4y3" firstAttribute="leading" secondItem="OhP-Hb-V0O" secondAttribute="trailing" constant="10" id="lK9-FZ-51Q"/>
<constraint firstItem="kwN-Ec-QjZ" firstAttribute="centerY" secondItem="pUI-TD-eZ0" secondAttribute="centerY" id="mmm-d2-gFf"/>
<constraint firstAttribute="trailing" secondItem="znY-KU-sa9" secondAttribute="trailing" constant="82" id="nPX-uo-1qF"/>
<constraint firstItem="usv-X3-4y3" firstAttribute="leading" secondItem="pUI-TD-eZ0" secondAttribute="trailing" constant="16" id="pd1-Z4-f6z"/>
<constraint firstItem="usv-X3-4y3" firstAttribute="centerY" secondItem="H2p-sc-9uM" secondAttribute="centerY" id="qlX-UR-Lvi"/>
<constraint firstItem="znY-KU-sa9" firstAttribute="top" secondItem="Pyb-wY-gDY" secondAttribute="bottom" constant="8" id="qqA-8Q-Dgp"/>
<constraint firstItem="PIA-pT-9a5" firstAttribute="leading" secondItem="Obr-qh-YFK" secondAttribute="trailing" constant="10" id="skJ-jS-9cz"/>
<constraint firstItem="kwN-Ec-QjZ" firstAttribute="centerX" secondItem="usv-X3-4y3" secondAttribute="centerX" id="uYp-IG-22X"/>
<constraint firstItem="PIA-pT-9a5" firstAttribute="trailing" secondItem="pUI-TD-eZ0" secondAttribute="trailing" id="va9-fh-oMg"/>
<constraint firstItem="usv-X3-4y3" firstAttribute="centerY" secondItem="VxF-sI-uou" secondAttribute="centerY" id="wUj-cR-kll"/>
<constraint firstItem="OhP-Hb-V0O" firstAttribute="leading" secondItem="Obr-qh-YFK" secondAttribute="leading" id="zjf-gB-1F4"/>
</constraints>
</tableViewCellContentView>
Expand All @@ -99,6 +107,7 @@
<outlet property="progressView" destination="znY-KU-sa9" id="MLX-NV-5cK"/>
<outlet property="speedLabel" destination="pUI-TD-eZ0" id="P7I-6L-O52"/>
<outlet property="startDateLabel" destination="VxF-sI-uou" id="pTT-Mr-1F3"/>
<outlet property="statusLabel" destination="kwN-Ec-QjZ" id="QLr-qx-dL4"/>
<outlet property="timeRemainingLabel" destination="PIA-pT-9a5" id="BoR-ox-aOO"/>
<outlet property="titleLabel" destination="Obr-qh-YFK" id="6cx-ab-1WY"/>
</connections>
Expand Down
Loading

0 comments on commit 8cde56c

Please sign in to comment.