@@ -10,106 +10,113 @@ import UIKit
1010import TUSKit
1111
1212class ViewController : UIViewController , TUSDelegate , UIImagePickerControllerDelegate , UINavigationControllerDelegate {
13-
13+
1414 let imagePicker = UIImagePickerController ( )
1515
1616 @IBOutlet weak var numberOfFilesLabel : UILabel !
1717 @IBOutlet weak var progressLabel : UILabel !
1818 @IBOutlet weak var progressBar : UIProgressView !
1919 @IBOutlet weak var numberOfFilesUploadingLabel : UILabel !
2020 @IBOutlet weak var numberOfFileUploadLabel : UILabel !
21-
21+
2222 var files : [ URL ] = [ ]
2323 var numOfUploaded = 0
24-
24+
2525 override func viewDidLoad( ) {
2626 super. viewDidLoad ( )
2727 //Image picker setup for example.
2828 imagePicker. sourceType = . photoLibrary
2929 imagePicker. delegate = self
30-
30+
3131 //Set the deleagte of TUSClient
3232 TUSClient . shared. delegate = self
33-
34-
33+
34+
3535// if let path = Bundle.main.path(forResource: "test", ofType:"mp4") {
3636// let number = Int.random(in: 0 ..< 100) //TODO: Remove before release: this is only set so we can run multiple files while developer
3737// let upload: TUSUpload = TUSUpload(withId: String(format: "%@%@", "video", String(number)), andFilePathURL: URL(fileURLWithPath: path), andFileType: ".mp4")
3838// //Create or resume upload
3939//
4040// TUSClient.shared.createOrResume(forUpload: upload)
4141// }
42-
42+
4343 }
44-
44+
4545 func updateLabel( ) {
4646 numberOfFilesLabel. text = " \( files. count) of files ready for upload "
4747 numberOfFilesUploadingLabel. text = " \( String ( describing: TUSClient . shared. currentUploads!. count) ) files uploading "
4848 numberOfFileUploadLabel. text = " \( numOfUploaded) files uploaded "
4949 }
50-
50+
5151 @IBAction func addFileAction( _ sender: Any ) {
5252 present ( imagePicker, animated: true , completion: nil )
5353 }
54-
54+
5555 @IBAction func uploadAction( _ sender: Any ) {
56+ if ( TUSClient . shared. status == TUSClientStaus . ready
57+ && TUSClient . shared. currentUploads!. count > 0
58+ && files. count <= 0 ) {
59+ TUSClient . shared. resumeAll ( )
60+ return ;
61+ }
62+
5663 for file in files {
5764 let number = Int . random ( in: 0 ..< 1000 ) //TODO: Remove before release: this is only set so we can run multiple files while developer
58-
65+
5966 //When you have a file, create an upload, and give it a Id.
60- var fileData = try ! Data ( contentsOf: file)
61- let upload : TUSUpload = TUSUpload ( withId: String ( number) , andData: fileData, andFileType: " jpeg " )
67+ let upload : TUSUpload = TUSUpload ( withId: String ( number) , andFilePathURL: file, andFileType: " .jpeg " )
6268 upload. metadata = [ " hello " : " world " ]
6369 //Create or resume upload
6470 TUSClient . shared. createOrResume ( forUpload: upload, withCustomHeaders: [ " Header " : " Value " ] )
6571 }
6672 updateLabel ( )
6773 }
68-
69-
74+
75+
7076 func imagePickerController( _ picker: UIImagePickerController , didFinishPickingMediaWithInfo info: [ UIImagePickerController . InfoKey : Any ] ) {
7177
7278 if #available( iOS 11 . 0 , * ) {
7379 guard let imageURL = info [ . imageURL] else {
7480 return
7581 }
76-
82+
7783 files. append ( imageURL as! URL )
7884 updateLabel ( )
7985 }
80-
86+
8187 dismiss ( animated: true ) {
8288 }
8389 }
8490//
8591 func imagePickerControllerDidCancel( _ picker: UIImagePickerController ) {
8692 dismiss ( animated: true , completion: nil )
8793 }
88-
94+
8995 //MARK: TUSClient Deleagte
90-
96+
9197 func TUSProgress( bytesUploaded uploaded: Int , bytesRemaining remaining: Int ) {
92- //
93- print ( uploaded)
94- print ( remaining)
98+ print ( " Global upload: \( uploaded) / \( remaining) " ) ;
9599 self . progressLabel. text = " \( uploaded) / \( remaining) "
96100 self . progressBar. progress = Float ( uploaded) / Float( remaining)
97101 }
98-
102+
99103 func TUSProgress( forUpload upload: TUSUpload , bytesUploaded uploaded: Int , bytesRemaining remaining: Int ) {
100- //
101- print ( uploaded)
102- print ( remaining)
104+ print ( " Upload for: \( upload. id) \( uploaded) / \( remaining) " ) ;
103105 }
104-
106+
105107 func TUSSuccess( forUpload upload: TUSUpload ) {
106108 print ( upload. uploadLocationURL)
107109 TUSClient . shared. getFile ( forUpload: upload)
108110 numOfUploaded = numOfUploaded + 1
109- updateLabel ( )
111+ // Delay the update a second, because we will get the pending uploads
112+ // from TUS. After a upload has finished it may take some short amount of time after the
113+ // persistence layer has been updated.
114+ DispatchQueue . main. asyncAfter ( deadline: . now( ) + 1.0 ) {
115+ self . updateLabel ( )
116+ }
110117 //
111118 }
112-
119+
113120 func TUSFailure( forUpload upload: TUSUpload ? , withResponse response: TUSResponse ? , andError error: Error ? ) {
114121 //
115122 if ( response != nil ) {
0 commit comments