Batman
Batman

Reputation: 21

NSProgressIndicator Swift - Alamofire

I´m trying to show progress when downloading a file with Alamofire in Swift 3 for a macOS app.

Here's what I have:

func downloadFile() {
    progressIndicator.isHidden = false
    progressIndicator.minValue = 0
    progressIndicator.maxValue = 10
    progressIndicator.isIndeterminate = false

    outputTextField.stringValue = ""

    let mainURL = "https://somethinghere.org/macos/\(comboCellBox.stringValue)"
    let destination = DownloadRequest.suggestedDownloadDestination(for: .downloadsDirectory)
    Alamofire.download(mainURL, to: destination)
        .downloadProgress(queue: DispatchQueue.global(qos: .utility)) { progress in
            print("Download Progress: \(progress.fractionCompleted)")

         // I want to show progress from .fractionCompleted to NSProgressIndicator here

        }
        .responseJSON { response in
            debugPrint(response)
            }
}

For each .fractionCompleted, I want the NSProgressIndicator to display and animate the download progress.

I don't know where to start, and I was wondering if someone can help me getting started. I've read the Alamofire documention, but I can't find anything related.

EDIT:

The output of:

print("Download Progress: \(progress.fractionCompleted)")

Looks like this:

Download Progress: 0.00208984202190035
Download Progress: 0.198987043477936
Download Progress: 0.298296286511484
Download Progress: 0.397831087926645
Download Progress: 0.49933983672564
Download Progress: 0.598874638140801
Download Progress: 0.698409439555962
Download Progress: 0.797944240971122
Download Progress: 0.897479042386283
Download Progress: 0.997013843801444
Download Progress: 1.0

ANSWER:

self.progressIndicator.doubleValue = progress.fractionCompleted

Upvotes: 1

Views: 735

Answers (1)

Batman
Batman

Reputation: 21

ANSWER

func downloadFile() {
    progressIndicator.isHidden = false
    progressIndicator.minValue = 0
    progressIndicator.maxValue = 10
    progressIndicator.isIndeterminate = false

    outputTextField.stringValue = ""

    let mainURL = "https://somethinghere.org/macos/\ .(comboCellBox.stringValue)"
    let destination = DownloadRequest.suggestedDownloadDestination(for: .downloadsDirectory)
    Alamofire.download(mainURL, to: destination)
    .downloadProgress { progress in
            print("Download Progress: \(progress.fractionCompleted)")
            self.progressIndicator.doubleValue = progress.fractionCompleted
        }
    }
    .responseJSON { response in
        debugPrint(response)
        }
}

Upvotes: 1

Related Questions