From 736151af65fbe12b9174e91189991c8d0574e929 Mon Sep 17 00:00:00 2001 From: Daniels <176516837@qq.com> Date: Wed, 21 Mar 2018 16:15:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E7=9A=84=E6=97=A5=E5=BF=97=E6=89=93=E5=8D=B0=E7=BA=A7=E5=88=AB?= =?UTF-8?q?=E3=80=81manager=E8=BF=9B=E5=BA=A6=E8=AE=A1=E7=AE=97=E6=96=B9?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Example/Example.xcodeproj/project.pbxproj | 6 ++- Example/Example/AppDelegate.swift | 5 +++ Example/Example/Base.lproj/Main.storyboard | 39 +++++++++++++++++--- Example/Example/BaseViewController.swift | 13 ++++--- Example/Example/DownloadViewController.swift | 3 ++ Example/Example/Info.plist | 2 + Example/Example/ViewController1.swift | 2 +- Example/Example/ViewController2.swift | 3 ++ Example/Example/ViewController3.swift | 3 ++ Sources/TRCommon.swift | 1 + Sources/TRManager.swift | 6 +-- Tiercel.podspec | 2 +- 12 files changed, 67 insertions(+), 18 deletions(-) diff --git a/Example/Example.xcodeproj/project.pbxproj b/Example/Example.xcodeproj/project.pbxproj index 43a9290..04f1478 100644 --- a/Example/Example.xcodeproj/project.pbxproj +++ b/Example/Example.xcodeproj/project.pbxproj @@ -345,10 +345,11 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = Y3437T5EGX; INFOPLIST_FILE = Example/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 8.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = com.Daniels.Tiercel.Example; + PRODUCT_BUNDLE_IDENTIFIER = com.Daniels.Tiercel; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2"; @@ -360,10 +361,11 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = Y3437T5EGX; INFOPLIST_FILE = Example/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 8.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = com.Daniels.Tiercel.Example; + PRODUCT_BUNDLE_IDENTIFIER = com.Daniels.Tiercel; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2"; diff --git a/Example/Example/AppDelegate.swift b/Example/Example/AppDelegate.swift index a8aebdd..1e0c1c8 100644 --- a/Example/Example/AppDelegate.swift +++ b/Example/Example/AppDelegate.swift @@ -22,22 +22,27 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. + print("applicationWillResignActive") } func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. + print("applicationDidEnterBackground") } func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. + print("applicationWillEnterForeground") } func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. + print("applicationDidBecomeActive") } func applicationWillTerminate(_ application: UIApplication) { + print("applicationWillTerminate") // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } diff --git a/Example/Example/Base.lproj/Main.storyboard b/Example/Example/Base.lproj/Main.storyboard index fc39533..2ea9411 100644 --- a/Example/Example/Base.lproj/Main.storyboard +++ b/Example/Example/Base.lproj/Main.storyboard @@ -146,7 +146,7 @@ - + @@ -291,13 +291,13 @@ - + - + @@ -345,7 +345,7 @@ - + @@ -436,6 +436,12 @@ + @@ -448,6 +454,7 @@ + @@ -459,6 +466,7 @@ + @@ -479,6 +487,7 @@ + @@ -490,7 +499,7 @@ - + @@ -588,6 +597,12 @@ + @@ -606,6 +621,7 @@ + @@ -616,6 +632,7 @@ + @@ -633,6 +650,7 @@ + @@ -644,7 +662,7 @@ - + @@ -735,17 +753,25 @@ + + + @@ -779,6 +805,7 @@ + diff --git a/Example/Example/BaseViewController.swift b/Example/Example/BaseViewController.swift index c0e6285..2bd28a0 100644 --- a/Example/Example/BaseViewController.swift +++ b/Example/Example/BaseViewController.swift @@ -14,7 +14,8 @@ class BaseViewController: UIViewController { @IBOutlet weak var totalTasksLabel: UILabel! @IBOutlet weak var totalSpeedLabel: UILabel! @IBOutlet weak var timeRemainingLabel: UILabel! - + @IBOutlet weak var totalProgressLabel: UILabel! + // 由于执行删除running的task,结果是异步回调的,所以最好是用downloadURLStrings作为数据源 lazy var downloadURLStrings = [String]() @@ -38,8 +39,7 @@ class BaseViewController: UIViewController { let free = UIDevice.current.tr.freeDiskSpaceInBytes / 1024 / 1024 print("手机剩余储存空间为: \(free)MB") - // 因为会读取缓存到沙盒的任务,所以第一次的时候,不要马上开始下载 - downloadManager?.isStartDownloadImmediately = false + TRManager.logLevel = .high } func updateUI() { @@ -47,6 +47,9 @@ class BaseViewController: UIViewController { totalTasksLabel.text = "总任务:\(downloadManager.completedTasks.count)/\(downloadManager.tasks.count)" totalSpeedLabel.text = "总速度:\(downloadManager.speed.tr.convertSpeedToString())" timeRemainingLabel.text = "剩余时间: \(downloadManager.timeRemaining.tr.convertTimeToString())" + let per = String(format: "%.2f", downloadManager.progress.fractionCompleted) + totalProgressLabel.text = "总进度: \(per)" + } @@ -73,7 +76,6 @@ extension BaseViewController { @IBAction func clearDisk(_ sender: Any) { guard let downloadManager = downloadManager else { return } downloadManager.cache.clearDiskCache() - print("总任务:\(downloadManager.completedTasks.count)/\(downloadManager.tasks.count)") updateUI() } @@ -97,7 +99,8 @@ extension BaseViewController: UITableViewDataSource, UITableViewDelegate { let URLString = downloadURLStrings[indexPath.row] guard let downloadManager = downloadManager, - let task = downloadManager.fetchTask(URLString) else { return cell } + let task = downloadManager.fetchTask(URLString) + else { return cell } var image: UIImage = #imageLiteral(resourceName: "resume") switch task.status { diff --git a/Example/Example/DownloadViewController.swift b/Example/Example/DownloadViewController.swift index a35678d..d914ebb 100644 --- a/Example/Example/DownloadViewController.swift +++ b/Example/Example/DownloadViewController.swift @@ -15,6 +15,9 @@ class DownloadViewController: BaseViewController { super.viewDidLoad() downloadManager = ListViewController.downloadManager + // 因为会读取缓存到沙盒的任务,所以第一次的时候,不要马上开始下载 + downloadManager?.isStartDownloadImmediately = false + guard let downloadManager = downloadManager else { return } // 设置manager的回调 diff --git a/Example/Example/Info.plist b/Example/Example/Info.plist index d5afed6..cf7045c 100644 --- a/Example/Example/Info.plist +++ b/Example/Example/Info.plist @@ -4,6 +4,8 @@ CFBundleDevelopmentRegion $(DEVELOPMENT_LANGUAGE) + CFBundleDisplayName + Tiercel CFBundleExecutable $(EXECUTABLE_NAME) CFBundleIdentifier diff --git a/Example/Example/ViewController1.swift b/Example/Example/ViewController1.swift index b082475..a139ff1 100644 --- a/Example/Example/ViewController1.swift +++ b/Example/Example/ViewController1.swift @@ -24,7 +24,7 @@ class ViewController1: UIViewController { override func viewDidLoad() { super.viewDidLoad() - + TRManager.logLevel = .high } deinit { diff --git a/Example/Example/ViewController2.swift b/Example/Example/ViewController2.swift index 6dd38fc..746f85b 100644 --- a/Example/Example/ViewController2.swift +++ b/Example/Example/ViewController2.swift @@ -14,6 +14,9 @@ class ViewController2: BaseViewController { super.viewDidLoad() downloadManager = TRManager("ViewController2", isStoreInfo: true) + + // 因为会读取缓存到沙盒的任务,所以第一次的时候,不要马上开始下载 + downloadManager?.isStartDownloadImmediately = false URLStrings = (1...9).map({ "http://120.25.226.186:32812/resources/videos/minion_0\($0).mp4" }) diff --git a/Example/Example/ViewController3.swift b/Example/Example/ViewController3.swift index 0a3f94a..83aae6a 100644 --- a/Example/Example/ViewController3.swift +++ b/Example/Example/ViewController3.swift @@ -15,6 +15,9 @@ class ViewController3: BaseViewController { super.viewDidLoad() downloadManager = TRManager("ViewController3", isStoreInfo: true) + + // 因为会读取缓存到沙盒的任务,所以第一次的时候,不要马上开始下载 + downloadManager?.isStartDownloadImmediately = false URLStrings = (1...5).map({ "http://120.25.226.186:32812/resources/videos/minion_0\($0).mp4" }) diff --git a/Sources/TRCommon.swift b/Sources/TRCommon.swift index 0491a31..459740d 100644 --- a/Sources/TRCommon.swift +++ b/Sources/TRCommon.swift @@ -192,6 +192,7 @@ func TiercelLog(_ message: T, file: String = #file, method: String = #functio switch TRManager.logLevel { case .high: + print("") print("***************FalconLog****************") let threadNum = (Thread.current.description as NSString).components(separatedBy: "{").last?.components(separatedBy: ",").first ?? "" diff --git a/Sources/TRManager.swift b/Sources/TRManager.swift index 5a6cd65..0b8f9e5 100644 --- a/Sources/TRManager.swift +++ b/Sources/TRManager.swift @@ -39,7 +39,7 @@ public class TRManager { // 最大并发数 public var maxConcurrentTasksLimit = Int.max - public static var logLevel: TRLogLevel = .high + public static var logLevel: TRLogLevel = .none private let queue: DispatchQueue = DispatchQueue(label: "com.Daniels.Falcon.queue") @@ -110,8 +110,8 @@ public class TRManager { private let internalProgress = Progress() public var progress: Progress { - internalProgress.completedUnitCount = tasks.filter({ $0.status == .running }).reduce(0, { $0 + $1.progress.completedUnitCount }) - internalProgress.totalUnitCount = tasks.filter({ $0.status == .running }).reduce(0, { $0 + $1.progress.totalUnitCount }) + internalProgress.completedUnitCount = tasks.reduce(0, { $0 + $1.progress.completedUnitCount }) + internalProgress.totalUnitCount = tasks.reduce(0, { $0 + $1.progress.totalUnitCount }) return internalProgress } diff --git a/Tiercel.podspec b/Tiercel.podspec index 50a125e..b58a155 100644 --- a/Tiercel.podspec +++ b/Tiercel.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = 'Tiercel' - s.version = '1.0.1' + s.version = '1.0.3' s.swift_version = '4.0' s.summary = 'Tiercel is a lightweight, pure-Swift download framework.'