Skip to content

Commit

Permalink
修改了默认的日志打印级别、manager进度计算方式
Browse files Browse the repository at this point in the history
  • Loading branch information
Danie1s committed Mar 21, 2018
1 parent 4a10f5c commit 736151a
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 18 deletions.
6 changes: 4 additions & 2 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";
Expand Down
5 changes: 5 additions & 0 deletions Example/Example/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:.
}

Expand Down
39 changes: 33 additions & 6 deletions Example/Example/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

13 changes: 8 additions & 5 deletions Example/Example/BaseViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]()

Expand All @@ -38,15 +39,17 @@ class BaseViewController: UIViewController {
let free = UIDevice.current.tr.freeDiskSpaceInBytes / 1024 / 1024
print("手机剩余储存空间为: \(free)MB")

// 因为会读取缓存到沙盒的任务,所以第一次的时候,不要马上开始下载
downloadManager?.isStartDownloadImmediately = false
TRManager.logLevel = .high
}

func updateUI() {
guard let downloadManager = downloadManager else { return }
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)"

}


Expand All @@ -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()
}

Expand All @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions Example/Example/DownloadViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class DownloadViewController: BaseViewController {
super.viewDidLoad()
downloadManager = ListViewController.downloadManager

// 因为会读取缓存到沙盒的任务,所以第一次的时候,不要马上开始下载
downloadManager?.isStartDownloadImmediately = false

guard let downloadManager = downloadManager else { return }

// 设置manager的回调
Expand Down
2 changes: 2 additions & 0 deletions Example/Example/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key>
<string>Tiercel</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
Expand Down
2 changes: 1 addition & 1 deletion Example/Example/ViewController1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ViewController1: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

TRManager.logLevel = .high
}

deinit {
Expand Down
3 changes: 3 additions & 0 deletions Example/Example/ViewController2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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" })

Expand Down
3 changes: 3 additions & 0 deletions Example/Example/ViewController3.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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" })

Expand Down
1 change: 1 addition & 0 deletions Sources/TRCommon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func TiercelLog<T>(_ 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 ?? ""

Expand Down
6 changes: 3 additions & 3 deletions Sources/TRManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion Tiercel.podspec
Original file line number Diff line number Diff line change
@@ -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.'

Expand Down

0 comments on commit 736151a

Please sign in to comment.