Skip to content

Commit

Permalink
feat: downloader auto-refresh downloadURL
Browse files Browse the repository at this point in the history
  • Loading branch information
wzxha committed Dec 12, 2023
1 parent d94cff6 commit 54c1563
Show file tree
Hide file tree
Showing 14 changed files with 638 additions and 334 deletions.
8 changes: 8 additions & 0 deletions Demo/Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

/* Begin PBXBuildFile section */
C9BCCDBF097A44F1DFD43B1C /* Pods_Demo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4675D93D8A32028B17600942 /* Pods_Demo.framework */; };
F4B3F72F2B280EC300D9E122 /* FileCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4B3F72E2B280EC300D9E122 /* FileCell.swift */; };
F4B3F7312B280FA400D9E122 /* DisplayItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4B3F7302B280FA400D9E122 /* DisplayItem.swift */; };
F4C4FBB82B108DDD002407EC /* FileListViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4C4FBB72B108DDD002407EC /* FileListViewController.swift */; };
F4E3E5452B0F41580041AE19 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4E3E5442B0F41580041AE19 /* AppDelegate.swift */; };
F4E3E5472B0F41580041AE19 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4E3E5462B0F41580041AE19 /* SceneDelegate.swift */; };
Expand All @@ -21,6 +23,8 @@
4675D93D8A32028B17600942 /* Pods_Demo.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Demo.framework; sourceTree = BUILT_PRODUCTS_DIR; };
9CAE1A0A2DB08870265E86AE /* Pods-Demo.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Demo.debug.xcconfig"; path = "Target Support Files/Pods-Demo/Pods-Demo.debug.xcconfig"; sourceTree = "<group>"; };
AFA546012E2373B08060C87B /* Pods-Demo.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Demo.release.xcconfig"; path = "Target Support Files/Pods-Demo/Pods-Demo.release.xcconfig"; sourceTree = "<group>"; };
F4B3F72E2B280EC300D9E122 /* FileCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileCell.swift; sourceTree = "<group>"; };
F4B3F7302B280FA400D9E122 /* DisplayItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DisplayItem.swift; sourceTree = "<group>"; };
F4C4FBB72B108DDD002407EC /* FileListViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileListViewController.swift; sourceTree = "<group>"; };
F4E3E5412B0F41580041AE19 /* Demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Demo.app; sourceTree = BUILT_PRODUCTS_DIR; };
F4E3E5442B0F41580041AE19 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -78,6 +82,8 @@
F4E3E5442B0F41580041AE19 /* AppDelegate.swift */,
F4E3E5462B0F41580041AE19 /* SceneDelegate.swift */,
F4E3E5482B0F41580041AE19 /* ViewController.swift */,
F4B3F72E2B280EC300D9E122 /* FileCell.swift */,
F4B3F7302B280FA400D9E122 /* DisplayItem.swift */,
F4C4FBB72B108DDD002407EC /* FileListViewController.swift */,
F4E3E54A2B0F41580041AE19 /* Main.storyboard */,
F4E3E54D2B0F41590041AE19 /* Assets.xcassets */,
Expand Down Expand Up @@ -214,7 +220,9 @@
files = (
F4E3E5492B0F41580041AE19 /* ViewController.swift in Sources */,
F4E3E5452B0F41580041AE19 /* AppDelegate.swift in Sources */,
F4B3F7312B280FA400D9E122 /* DisplayItem.swift in Sources */,
F4C4FBB82B108DDD002407EC /* FileListViewController.swift in Sources */,
F4B3F72F2B280EC300D9E122 /* FileCell.swift in Sources */,
F4E3E5472B0F41580041AE19 /* SceneDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
2 changes: 1 addition & 1 deletion Demo/Demo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
credentials: .pkce))

func applicationDidFinishLaunching(_ application: UIApplication) {
Aliyunpan.setLogLevel(.error)
Aliyunpan.setLogLevel(.info)
}

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
Expand Down
53 changes: 53 additions & 0 deletions Demo/Demo/DisplayItem.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// DisplayItem.swift
// Demo
//
// Created by zhaixian on 2023/12/12.
//

import Foundation
import AliyunpanSDK

extension AliyunpanFile: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(name)
}

public static func == (lhs: AliyunpanFile, rhs: AliyunpanFile) -> Bool {
lhs.hashValue == rhs.hashValue
}
}

extension DownloadResult: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(progress)
hasher.combine(url)
}

public static func == (lhs: DownloadResult, rhs: DownloadResult) -> Bool {
lhs.hashValue == rhs.hashValue
}
}

struct DisplayItem: Hashable {
let file: AliyunpanFile
let downloadResult: DownloadResult?

public func hash(into hasher: inout Hasher) {
hasher.combine(file)
hasher.combine(downloadResult)
}

public static func == (lhs: DisplayItem, rhs: DisplayItem) -> Bool {
lhs.hashValue == rhs.hashValue
}

init(file: AliyunpanFile, downloadResult: DownloadResult?) {
self.file = file
self.downloadResult = downloadResult
}

init(_ file: AliyunpanFile) {
self.init(file: file, downloadResult: nil)
}
}
134 changes: 134 additions & 0 deletions Demo/Demo/FileCell.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
//
// FileCell.swift
// Demo
//
// Created by zhaixian on 2023/12/12.
//

import UIKit
import AliyunpanSDK

protocol FileCellDelegate: AnyObject {
func getDownloader(for item: DisplayItem) -> AliyunpanDownloader?

func fileCell(_ cell: FileCell, didUpdateDownloadResult result: DownloadResult, for item: DisplayItem)
func fileCell(_ cell: FileCell, willOpen item: DisplayItem)
}

class FileCell: UICollectionViewListCell {
weak var delegate: FileCellDelegate?
weak var client: AliyunpanClient?

private var item: DisplayItem?

private var downloader: AliyunpanDownloader? {
guard let item else {
return nil
}
return delegate?.getDownloader(for: item)
}

private lazy var pauseButton: UIButton = {
let pauseButton = UIButton()
pauseButton.addTarget(self, action: #selector(pause), for: .touchUpInside)
pauseButton.setImage(UIImage(systemName: "pause"), for: .normal)
pauseButton.tintColor = .gray
return pauseButton
}()

private lazy var downloadButton: UIButton = {
let downloadButton = UIButton()
downloadButton.addTarget(self, action: #selector(download), for: .touchUpInside)
downloadButton.setImage(.add, for: .normal)
return downloadButton
}()

private lazy var progressLabel: UILabel = {
let progressLabel = UILabel()
progressLabel.textColor = .gray
progressLabel.font = .monospacedDigitSystemFont(ofSize: 12, weight: .regular)
progressLabel.adjustsFontSizeToFitWidth = true
return progressLabel
}()

private lazy var openButton: UIButton = {
let openFileButton = UIButton()
openFileButton.addTarget(self, action: #selector(openFile), for: .touchUpInside)
openFileButton.setImage(.actions, for: .normal)
return openFileButton
}()

@objc private func download() {
guard let item else {
return
}

if downloader?.state == .pause {
downloader?.resume()
return
}

downloader?.download { [weak self] progress in
DispatchQueue.main.async { [weak self] in
guard let self else {
return
}
self.delegate?.fileCell(self, didUpdateDownloadResult: .progressing(progress), for: item)
}
} completionHandle: { [weak self] result in
if let url = try? result.get() {
DispatchQueue.main.async { [weak self] in
guard let self else {
return
}
self.delegate?.fileCell(self, didUpdateDownloadResult: .completed(url), for: item)
}
}
}
}

@objc private func pause() {
downloader?.pause()

if let item {
fill(item)
}
}

@objc private func openFile() {
guard let item else {
return
}
delegate?.fileCell(self, willOpen: item)
}

func fill(_ item: DisplayItem) {
self.item = item
if item.file.isFolder {
accessories = [.disclosureIndicator()]
return
}

if let progress = item.downloadResult?.progress {
progressLabel.text = "\(String(format: "%.2f", progress * 100))%"
} else {
progressLabel.text = nil
}

var views: [UIView] = [progressLabel]
if item.downloadResult?.url != nil {
views.append(openButton)
} else {
if downloader?.state == .downloading {
views.append(pauseButton)
} else {
views.append(downloadButton)
}
}
accessories = views.map {
UICellAccessory.customView(
configuration: .init(customView: $0, placement: .trailing()))
}
}
}

Loading

0 comments on commit 54c1563

Please sign in to comment.