diff --git a/Kiwix-iOS/Controller/Library/DownloadTasksController.swift b/Kiwix-iOS/Controller/Library/DownloadTasksController.swift index 8ec26b2a..d22e0ed2 100644 --- a/Kiwix-iOS/Controller/Library/DownloadTasksController.swift +++ b/Kiwix-iOS/Controller/Library/DownloadTasksController.swift @@ -105,7 +105,14 @@ class DownloadTasksController: UITableViewController, NSFetchedResultsController guard let progress = Network.shared.operations[id]?.progress else {return} cell.progressLabel.text = progress.fractionCompletedDescription cell.progressView.setProgress(Float(progress.fractionCompleted), animated: animated) - cell.detailLabel.text = progress.localizedAdditionalDescription.stringByReplacingOccurrencesOfString(" – ", withString: "\n") + cell.detailLabel.text = { + let string = progress.progressAndSpeedDescription + if string.containsString(" — ") { + return string.stringByReplacingOccurrencesOfString(" — ", withString: "\n") + } else { + return string + "\n" + NSLocalizedString("Estimating Speed and Remaining Time", comment: "") + } + }() } // MARK: Other Data Source @@ -147,13 +154,13 @@ class DownloadTasksController: UITableViewController, NSFetchedResultsController override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {} override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? { - let remove = UITableViewRowAction(style: .Destructive, title: LocalizedStrings.remove) { (action, indexPath) -> Void in + let cancel = UITableViewRowAction(style: .Destructive, title: LocalizedStrings.Common.cancel) { (action, indexPath) -> Void in guard let downloadTask = self.fetchedResultController.objectAtIndexPath(indexPath) as? DownloadTask, let bookID = downloadTask.book?.id else {return} let operation = CancelBookDownloadOperation(bookID: bookID) GlobalOperationQueue.sharedInstance.addOperation(operation) } - return [remove] + return [cancel] } // MARK: - Fetched Results Controller diff --git a/Kiwix-iOS/Info.plist b/Kiwix-iOS/Info.plist index b70e77b5..0df05dba 100644 --- a/Kiwix-iOS/Info.plist +++ b/Kiwix-iOS/Info.plist @@ -49,7 +49,7 @@ CFBundleVersion - 1.7.1441 + 1.7.1481 ITSAppUsesNonExemptEncryption LSRequiresIPhoneOS diff --git a/Kiwix-iOS/Model/DownloadProgress.swift b/Kiwix-iOS/Model/DownloadProgress.swift deleted file mode 100644 index afdb5268..00000000 --- a/Kiwix-iOS/Model/DownloadProgress.swift +++ /dev/null @@ -1,129 +0,0 @@ -// -// DownloadProgress.swift -// Kiwix -// -// Created by Chris Li on 3/23/16. -// Copyright © 2016 Chris. All rights reserved. -// - -import UIKit - -class DownloadProgress: NSProgress { - let book: Book - private let observationCount = 9 - private let sampleFrequency: NSTimeInterval = 0.3 - private var speeds = [Double]() - private var timer: NSTimer? - private weak var task: NSURLSessionDownloadTask? - - init(book: Book) { - self.book = book - super.init(parent: nil, userInfo: [NSProgressFileOperationKindKey: NSProgressFileOperationKindDownloading]) - self.kind = NSProgressKindFile - self.totalUnitCount = book.fileSize - self.completedUnitCount = book.downloadTask?.totalBytesWritten ?? 0 - print("totalBytesWritten: \(book.downloadTask?.totalBytesWritten)") - } - - deinit { - timer?.invalidate() - } - - func downloadStarted(task: NSURLSessionDownloadTask) { - self.task = task - recordSpeed() - timer = NSTimer.scheduledTimerWithTimeInterval(sampleFrequency, target: self, selector: #selector(DownloadProgress.recordSpeed), userInfo: nil, repeats: true) - } - - func downloadTerminated() { - timer?.invalidate() - speeds.removeAll() - setUserInfoObject(nil, forKey: NSProgressThroughputKey) - setUserInfoObject(nil, forKey: NSProgressEstimatedTimeRemainingKey) - } - - func recordSpeed() { - guard let task = task else {return} - - /* - Check if the countOfBytesReceived and countOfBytesExpectedToReceive in NSURLSessionDownloadTask - object is both zero. When a NSURLSessionDownloadTask resumes, these two value will be zero at first. - We don't want to accidently set these two values in this progress object to be all 0. - */ - guard task.countOfBytesReceived != 0 && task.countOfBytesExpectedToReceive != 0 else {return} - - let previousCompletedUnitCount = completedUnitCount - completedUnitCount = task.countOfBytesReceived - totalUnitCount = task.countOfBytesExpectedToReceive - let speed = Double(completedUnitCount - previousCompletedUnitCount) / sampleFrequency - speeds.insert(speed, atIndex: 0) - if speeds.count > observationCount {speeds.popLast()} - } - - var maSpeed: Double? { - let alpha = 0.5 - var remainingWeight = 1.0 - var speedMA = 0.0 - - guard speeds.count >= observationCount else {return nil} - for index in 0.. 0.0 ? speedMA : nil - } - - // MARK: - Descriptions - - var sizeDescription: String { - return localizedAdditionalDescription.componentsSeparatedByString(" — ").first ?? "" - } - - var speedAndRemainingTimeDescription: String? { - guard let maSpeed = self.maSpeed else {return nil} - setUserInfoObject(NSNumber(double: maSpeed), forKey: NSProgressThroughputKey) - - let remainingSeconds = Double(totalUnitCount - completedUnitCount) / maSpeed - setUserInfoObject(NSNumber(double: remainingSeconds), forKey: NSProgressEstimatedTimeRemainingKey) - - let components = localizedAdditionalDescription.componentsSeparatedByString(" — ") - return components.count > 1 ? components.last : nil - } - - var percentDescription: String { - let formatter = NSNumberFormatter() - formatter.numberStyle = .PercentStyle - formatter.maximumIntegerDigits = 3 - formatter.maximumFractionDigits = 0 - formatter.locale = NSLocale.currentLocale() - return formatter.stringFromNumber(NSNumber(double: fractionCompleted)) ?? "" - } - - var sizeAndPercentDescription: String { - var strings = [String]() - strings.append(sizeDescription) - strings.append(percentDescription) - return strings.joinWithSeparator(" - ") - } - - override var description: String { - guard let state = book.downloadTask?.state else {return " \n "} - switch state { - case .Queued: return sizeAndPercentDescription + "\n" + LocalizedStrings.queued - case .Downloading: return sizeAndPercentDescription + "\n" + (speedAndRemainingTimeDescription ?? LocalizedStrings.estimatingSpeedAndRemainingTime) - case .Paused: return sizeAndPercentDescription + "\n" + LocalizedStrings.paused - case .Error: return sizeDescription + "\n" + LocalizedStrings.downloadError - } - } -} - -extension LocalizedStrings { - class var starting: String {return NSLocalizedString("Starting", comment: "Library: Download task description")} - class var resuming: String {return NSLocalizedString("Resuming", comment: "Library: Download task description")} - class var paused: String {return NSLocalizedString("Paused", comment: "Library: Download task description")} - class var downloadError: String {return NSLocalizedString("Download Error", comment: "Library: Download task description")} - class var queued: String {return NSLocalizedString("Queued", comment: "Library: Download task description")} - class var estimatingSpeedAndRemainingTime: String {return NSLocalizedString("Estimating speed and remaining time", comment: "Library: Download task description")} -} diff --git a/Kiwix-iOS/Storyboard/Library.storyboard b/Kiwix-iOS/Storyboard/Library.storyboard index 467e1a46..66ed8390 100644 --- a/Kiwix-iOS/Storyboard/Library.storyboard +++ b/Kiwix-iOS/Storyboard/Library.storyboard @@ -469,11 +469,11 @@ - - + + - +