Skip to content

Commit

Permalink
Delegate early
Browse files Browse the repository at this point in the history
  • Loading branch information
MMasterson committed May 4, 2020
1 parent 9c08336 commit 19bdb53
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 26 deletions.
7 changes: 6 additions & 1 deletion Example/TUSKit/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,18 @@ class ViewController: UIViewController, TUSDelegate, UIImagePickerControllerDele

func TUSProgress(bytesUploaded uploaded: Int, bytesRemaining remaining: Int) {
//
print(uploaded)
print(remaining)
}

func TUSProgress(forUpload upload: TUSUpload, bytesUploaded uploaded: Int, bytesRemaining remaining: Int) {
//
print(uploaded)
print(remaining)
}

func TUSSuccess(forUpload upload: TUSUpload, withResponse response: TUSResponse) {
func TUSSuccess(forUpload upload: TUSUpload) {
print(upload.uploadLocationURL)
//
}

Expand Down
43 changes: 26 additions & 17 deletions TUSKit/Classes/TUSClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

import UIKit

public class TUSClient: NSObject {
public class TUSClient: NSObject, URLSessionTaskDelegate {


// MARK: Properties

internal var tusSession: TUSSession
internal var tusSession: TUSSession = TUSSession()
public var uploadURL: URL?
public var delegate: TUSDelegate?
private let executor: TUSExecutor
Expand All @@ -20,19 +21,20 @@ public class TUSClient: NSObject {
private static var config: TUSConfig?
internal var logger: TUSLogger
public var chunkSize: Int = TUSConstants.chunkSize //Default chunksize can be overwritten

public var currentUploads: [TUSUpload]? {
get {
guard let data = UserDefaults.standard.object(forKey: TUSConstants.kSavedTUSUploadsDefaultsKey) as? Data else {
return nil
}
return NSKeyedUnarchiver.unarchiveObject(with: data) as? [TUSUpload]
}
set(currentUploads) {
let data = NSKeyedArchiver.archivedData(withRootObject: currentUploads!)
UserDefaults.standard.set(data, forKey: TUSConstants.kSavedTUSUploadsDefaultsKey)
}
}
public var currentUploads: [TUSUpload]?
//TODO: Fix this
// public var currentUploads: [TUSUpload]? {
// get {
// guard let data = UserDefaults.standard.object(forKey: TUSConstants.kSavedTUSUploadsDefaultsKey) as? Data else {
// return nil
// }
// return NSKeyedUnarchiver.unarchiveObject(with: data) as? [TUSUpload]
// }
// set(currentUploads) {
// let data = NSKeyedArchiver.archivedData(withRootObject: currentUploads!)
// UserDefaults.standard.set(data, forKey: TUSConstants.kSavedTUSUploadsDefaultsKey)
// }
// }

public var status: TUSClientStaus? {
get {
Expand All @@ -56,15 +58,19 @@ public class TUSClient: NSObject {
fatalError("Error - you must call setup before accessing TUSClient")
}
uploadURL = config.uploadURL
tusSession = TUSSession(customConfiguration: config.URLSessionConfig)
executor = TUSExecutor()
logger = TUSLogger(config.debugLogEnabled)
fileManager.createFileDirectory()
super.init()
tusSession = TUSSession(customConfiguration: config.URLSessionConfig, andDelegate: self)
currentUploads = []

}

// MARK: Create methods

public func createOrResume(forUpload upload: TUSUpload, withRetries retries: Int) {
currentUploads?.append(upload)
let fileName = String(format: "%@%@", upload.id!, upload.fileType!)
if (fileManager.fileExists(withName: fileName) == false) {
if (upload.filePath != nil) {
Expand Down Expand Up @@ -137,6 +143,9 @@ public class TUSClient: NSObject {
//Delete stuff here
}


public func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
var upload = currentUploads![0]
self.delegate?.TUSProgress(bytesUploaded: Int(upload.uploadOffset!)!, bytesRemaining: Int(upload.uploadLength!)!)
}

}
2 changes: 1 addition & 1 deletion TUSKit/Classes/TUSDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public protocol TUSDelegate {

func TUSProgress(forUpload upload: TUSUpload, bytesUploaded uploaded: Int, bytesRemaining remaining: Int)

func TUSSuccess(forUpload upload: TUSUpload, withResponse response: TUSResponse)
func TUSSuccess(forUpload upload: TUSUpload)

func TUSFailure(forUpload upload: TUSUpload, withResponse response: TUSResponse, andError error: Error)

Expand Down
4 changes: 2 additions & 2 deletions TUSKit/Classes/TUSExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import UIKit

class TUSExecutor: NSObject {
class TUSExecutor: NSObject, URLSessionDelegate {

// MARK: Private Networking / Upload methods

Expand Down Expand Up @@ -75,7 +75,6 @@ class TUSExecutor: NSObject {
let request: URLRequest = urlRequest(withFullURL: upload.uploadLocationURL!, andMethod: "PATCH", andContentLength: upload.contentLength!, andUploadLength: upload.uploadLength!, andFilename: upload.id!, andHeaders: ["Content-Type":"application/offset+octet-stream", "Upload-Offset": upload.uploadOffset!, "Content-Length": String(chunks[position].count)])
let task = TUSClient.shared.tusSession.session.uploadTask(with: request, from: chunks[position], completionHandler: { (data, response, error) in
if let httpResponse = response as? HTTPURLResponse {
print(httpResponse.debugDescription)
switch httpResponse.statusCode {
case 200..<300:
//success
Expand All @@ -87,6 +86,7 @@ class TUSExecutor: NSObject {
TUSClient.shared.logger.log(String(format: "Chunk %u / %u complete", position + 1, chunks.count))
if (position + 1 == chunks.count) {
TUSClient.shared.logger.log(String(format: "File %@ uploaded at %@", upload.id!, upload.uploadLocationURL!.absoluteString))
TUSClient.shared.delegate?.TUSSuccess(forUpload: upload)
}
}
break
Expand Down
13 changes: 9 additions & 4 deletions TUSKit/Classes/TUSSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ import Foundation

class TUSSession {
var session: URLSession

init() {
session = URLSession(configuration: .default)
//
session = URLSession()
}

init(withDelegate delegate: URLSessionTaskDelegate) {
session = URLSession(configuration: .default, delegate: delegate, delegateQueue: OperationQueue.main)
}

init(customConfiguration configuration: URLSessionConfiguration) {
session = URLSession(configuration: configuration)
init(customConfiguration configuration: URLSessionConfiguration, andDelegate delegate: URLSessionTaskDelegate) {
session = URLSession(configuration: configuration, delegate: delegate, delegateQueue: OperationQueue.main)
}
}
2 changes: 1 addition & 1 deletion TUSKit/Classes/TUSUpload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class TUSUpload: NSObject {
var fileType: String? // TODO: Make sure only ".fileExtension" gets set. Current setup sets fileType as something like "1A1F31FE6-BB39-4A78-AECD-3C9BDE6D129E.jpeg"
var filePath: URL?
var data: Data?
internal var uploadLocationURL: URL?
public var uploadLocationURL: URL?


var contentLength: String? {
Expand Down

0 comments on commit 19bdb53

Please sign in to comment.