Skip to content

Commit

Permalink
update: remove, removeAll, local source, example
Browse files Browse the repository at this point in the history
  • Loading branch information
hoc081098 committed Jul 20, 2020
1 parent 52e2f80 commit ab33769
Show file tree
Hide file tree
Showing 3 changed files with 232 additions and 103 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="16097" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="zKl-Gz-Znn">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="16097" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
Expand All @@ -17,7 +17,7 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="L8a-8e-NfV">
<rect key="frame" x="0.0" y="88" width="414" height="774"/>
<rect key="frame" x="0.0" y="44" width="414" height="818"/>
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" reuseIdentifier="DownloadCell" textLabel="p58-k2-Dka" detailTextLabel="w8Z-LW-qbR" style="IBUITableViewCellStyleSubtitle" id="CdR-hv-rCI">
Expand Down Expand Up @@ -65,23 +65,5 @@
</objects>
<point key="canvasLocation" x="1047.8260869565217" y="90.401785714285708"/>
</scene>
<!--Navigation Controller-->
<scene sceneID="pRS-0P-maS">
<objects>
<navigationController automaticallyAdjustsScrollViewInsets="NO" id="zKl-Gz-Znn" sceneMemberID="viewController">
<toolbarItems/>
<navigationBar key="navigationBar" contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" id="GFy-xL-BvE">
<rect key="frame" x="0.0" y="44" width="414" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<nil name="viewControllers"/>
<connections>
<segue destination="BYZ-38-t0r" kind="relationship" relationship="rootViewController" id="fJ8-4a-UlH"/>
</connections>
</navigationController>
<placeholder placeholderIdentifier="IBFirstResponder" id="A5t-0Z-B2y" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="137.68115942028987" y="90.401785714285708"/>
</scene>
</scenes>
</document>
59 changes: 52 additions & 7 deletions PhDownloader Example/PhDownloader Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,19 @@ class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

self.navigationItem.rightBarButtonItems = [
.init(title: "Cancel all", style: .plain, target: self, action: #selector(cancelAll)),
.init(title: "Remove", style: .plain, target: self, action: #selector(remove)),
]

self.tableView.dataSource = self
self.tableView.delegate = self

self.observeDownloader()
}

func observeDownloader() {
print(
FileManager.default
.urls(for: .documentDirectory, in: .userDomainMask)
.first!
)

self.downloader
.downloadResult$
.subscribe(onNext: { result in
Expand Down Expand Up @@ -101,6 +106,13 @@ class ViewController: UIViewController {
.disposed(by: self.disposeBag)
}

@objc func removeAll() {
self.downloader
.removeAll()
.subscribe()
.disposed(by: self.disposeBag)
}

@objc func remove() {
let alert = UIAlertController(
title: "Remove",
Expand All @@ -112,7 +124,7 @@ class ViewController: UIViewController {
alert.addAction(.init(title: "OK", style: .default, handler: { [weak alert, weak self] _ in
if let id = alert?.textFields?.first?.text, let self = self {
self.downloader
.remove(identifier: id, deleteFile: true)
.remove(identifier: id)
.subscribe()
.disposed(by: self.disposeBag)
}
Expand All @@ -122,6 +134,36 @@ class ViewController: UIViewController {
}

extension ViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let buttonCancelAll = UIButton(type: .system)
buttonCancelAll.setTitle("Cancel all", for: .normal)
buttonCancelAll.addTarget(self, action: #selector(cancelAll), for: .touchUpInside)

let buttonRemove = UIButton(type: .system)
buttonRemove.setTitle("Remove", for: .normal)
buttonRemove.addTarget(self, action: #selector(remove), for: .touchUpInside)

let buttonRemoveAll = UIButton(type: .system)
buttonRemoveAll.setTitle("Remove all", for: .normal)
buttonRemoveAll.addTarget(self, action: #selector(removeAll), for: .touchUpInside)

let header = UIStackView()
header.axis = .horizontal
header.distribution = .fillEqually
header.alignment = .center
header.spacing = 16
header.addArrangedSubview(buttonCancelAll)
header.addArrangedSubview(buttonRemove)
header.addArrangedSubview(buttonRemoveAll)
header.frame = .init(x: 0, y: 0, width: self.view.frame.width, height: 48)
header.backgroundColor = .white
return header
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
48
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
items.count
}
Expand All @@ -131,8 +173,11 @@ extension ViewController: UITableViewDataSource, UITableViewDelegate {
let item = self.items[indexPath.row]

cell.textLabel?.text = item.request.url.absoluteString
cell.textLabel?.font = .systemFont(ofSize: 13)

cell.detailTextLabel?.text = "\(item.state)"
cell.detailTextLabel?.textColor = color(for: item.state)
cell.detailTextLabel?.font = .systemFont(ofSize: 13)

return cell
}
Expand Down Expand Up @@ -182,7 +227,7 @@ func color(for state: PhDownloadState) -> UIColor {
case .enqueued:
return .orange
case .downloading:
return .green
return .init(red: 0x00 / 255.0, green: 0xC8 / 255.0, blue: 0x53 / 255.0, alpha: 1)
case .completed:
return .blue
case .failed:
Expand Down
Loading

0 comments on commit ab33769

Please sign in to comment.