Function: Download/Update of a contentApp

Use the function to install / update a contentApp.

Example to download / update of a contentApp
CDockSDK.downloadProject(projectModel)

Handling download/update of a contentApp

To handle and visualize what the current download progress/status is, use the SDK Callbacks and the SDK ProjectState.

Example to handle a download
import UIKit
import CDockFramework

class VCCustomProjectDetails: UIViewController {
    var projectModel: ProjectModel!

    @IBOutlet weak var lblTitle: UILabel!
    @IBOutlet weak var lblDescription: UILabel!
    @IBOutlet weak var btnOpen: UIButton!
    @IBOutlet weak var btnDownload: UIButton!
    @IBOutlet weak var btnUpdate: UIButton!
    @IBOutlet weak var lblProgress: UILabel!
    @IBOutlet weak var lblEstimate: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        updateControlsState()
        handleUpdates()

        self.lblTitle.text = projectModel.title ?? ""
        self.lblDescription.text = projectModel.projectDescription ?? ""

        refreshProject()
    }

    @IBAction func onBtnUpdateTapped(_ sender: Any) {
        CDockSDK.downloadProject(self.projectModel)
        self.updateControlsState()
        self.updateProgress(progress: 0)
        self.updateEstimate(estimate: 0, speed: 0)
    }

    /*
    Example for handling the SDK callbacks for installing / updating a contentApp
    */
    func handleUpdates() {
        CDockSDK.setBlockCallbacks { [weak self] (type: CDockSDKCallbackType, userInfo: [AnyHashable: Any]!) in
            guard let s = self else {
                return
            }

            guard let projectId = userInfo["projectId"] as? Int, projectId == s.projectModel.projectId else {
                return
            }

            switch (type) {
            case syncProgress:
                if let progress = userInfo["progress"] as? Double {
                    if projectId == self?.projectModel.projectId ?? -1 {
                        self?.updateProgress(progress: progress)
                    }
                }
                break;

            case syncSuccess:
                s.updateControlsState()
                break;

            case syncFailed:
                s.updateControlsState()
                break;

            case syncEstimate:
                if let estimate = userInfo["estimate"] as? Double, let speed = userInfo["speed"] as? Double {
                    s.updateEstimate(estimate: estimate, speed: speed)
                }

                break;

            default:
                break;
            }
        }
    }

    /*
    An example of which actions could be made per project status. 
    In our example we have a button to start the contentApp, a button for the installation/download 
    of the contentApp, a button for the update of an already installed contentApp as well as two 
    additional labels for displaying the progress in percent and the expected duration of the 
    download of a contentApp. 
    */
    func updateControlsState() {
        let projectState = CDockSDK.projectState(projectModel)
        switch projectState {
        case none:
            self.btnOpen.isEnabled = false
            self.btnDownload.isEnabled = true
            self.btnUpdate.isEnabled = false
            self.lblProgress.isHidden = true
            self.lblEstimate.isHidden = true
            break;
        case syncInProgress:
            self.btnOpen.isEnabled = false
            self.btnDownload.isEnabled = false
            self.btnUpdate.isEnabled = false
            self.lblProgress.isHidden = false
            self.lblEstimate.isHidden = false
            break;
        case syncFinished:
            self.btnOpen.isEnabled = true
            self.btnDownload.isEnabled = false
            self.btnUpdate.isEnabled = false
            self.lblProgress.isHidden = true
            self.lblEstimate.isHidden = true
            break;
        case syncInterrupted:
            self.btnOpen.isEnabled = false
            self.btnDownload.isEnabled = true
            self.btnUpdate.isEnabled = false
            self.lblProgress.isHidden = true
            self.lblEstimate.isHidden = true
            break;
        case needUpdate:
            self.btnOpen.isEnabled = true
            self.btnDownload.isEnabled = false
            self.btnUpdate.isEnabled = true
            self.lblProgress.isHidden = true
            self.lblEstimate.isHidden = true
            break;

        default:
            break;

        }
    }

    /*
    Example for displaying the progress when downloading a contentApp
    */
    func updateProgress(progress: Double) {
        self.lblProgress.text = String(format: "%d%%", Int(progress * 100))
    }

    /*
    Example for the display of the expected duration when downloading a contentApp
    */
    func updateEstimate(estimate: Double, speed: Double) {
        if (estimate == 0) {
            self.lblEstimate.text = "";
            return;
        }
        
        let f = DateComponentsFormatter()
        f.allowedUnits = [.second, .minute, .hour]
        f.unitsStyle = .abbreviated
        f.zeroFormattingBehavior = [.dropLeading, .pad]
        self.lblEstimate.text = String(format:"%0.2f kB/s / %@", speed / 1024, f.string(from: estimate) ?? "")
    }

    /*
    Example for loading the current project information from contentDock
    */
    private func refreshProject() {
        CDockSDK.getProject(self.projectModel.projectId) { [weak self] model in
            self?.projectModel = model
            self?.updateControlsState()
        }
    }
}

More information


Feedback or Questions

We are always happy to help you with questions about the code or other questions.

Was this page helpful? Yes No

Topics on this page