Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hoc081098/phdownloadertest
RxAlamofire + RxSwift Downloader
https://github.com/hoc081098/phdownloadertest
functional-reactive-programming reactive-programming rxcocoa rxswift swift swift-downloader swifty-downloader
Last synced: about 1 month ago
JSON representation
RxAlamofire + RxSwift Downloader
- Host: GitHub
- URL: https://github.com/hoc081098/phdownloadertest
- Owner: hoc081098
- Created: 2020-07-05T11:58:53.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-12-15T02:16:25.000Z (about 1 year ago)
- Last Synced: 2024-10-16T05:45:39.519Z (3 months ago)
- Topics: functional-reactive-programming, reactive-programming, rxcocoa, rxswift, swift, swift-downloader, swifty-downloader
- Language: Swift
- Homepage:
- Size: 68.4 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# PhDownloaderTest
RxAlamofire + RxSwift Downloader## Create downloader
```swift
private let downloader: PhDownloader = PhDownloaderFactory.makeDownloader(with: .init(
maxConcurrent: 2,
throttleProgress: .milliseconds(500))
)
```## Obseve download result (show snackbar, toast, alert)
```swift
self.downloader
.downloadResult$
.subscribe(onNext: { result in
switch result {
case .success(let request):
print("[Result] Success: id=\(request.identifier)")
case .failure(let request, let error):
print("[Result] Failure: id=\(request.identifier), error=\(error)")
case .cancelled(let request):
print("[Result] Cancelled: id=\(request.identifier)")
}
})
.disposed(by: self.disposeBag)
```## Obseve download state (for update UI)
```swift
self.downloader
.observe(by: self.items.map { $0.request.identifier })
.subscribe(onNext: { [weak self] tasks in
guard let self = self else { return }
let newItems: [Item] = self.items.map { item in
var copy = item
copy.state = tasks[item.request.identifier]?.state ?? .undefined
return copy
}
let indexPaths = zip(self.items, newItems)
.enumerated()
.compactMap { (index, tuple) -> IndexPath? in
let (old, new) = tuple
if old.state != new.state { return IndexPath(row: index, section: 0) }
return nil
}
self.items = newItems
self.tableView.reloadRows(at: indexPaths, with: .none)
})
.disposed(by: self.disposeBag)
```## Enqueue, cancel, cancelAll, remove:
```swift
let id = "Request id"self.downloader
.enqueue(
.init(
identifier: String(id),
url: URL(string: "https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_1920_18MG.mp4")!,
fileName: "test_file_\(id).mp4",
savedDir: FileManager.default
.urls(for: .documentDirectory, in: .userDomainMask)
.first!
.appendingPathComponent("downloads", isDirectory: true)
)
)
.subscribe(
onCompleted: { print("[Enqueue] Success: id=\(id)") },
onError: { print("[Enqueue] Failure: id=\(id), error=\($0)") }
)
.disposed(by: self.disposeBag)self.downloader
.cancel(by: id)
.subscribe(
onCompleted: { print("[Cancel] Success: id=\(id)") },
onError: { print("[Cancel] Failure: id=\(id), error=\($0)") }
)
.disposed(by: self.disposeBag)self.downloader
.cancelAll()
.subscribe()
.disposed(by: self.disposeBag)
self.downloader
.remove(identifier: id, deleteFile: true)
.subscribe()
.disposed(by: self.disposeBag)
```