Skip to content

Commit

Permalink
iOS fix for issue #285
Browse files Browse the repository at this point in the history
  • Loading branch information
781flyingdutchman committed Apr 12, 2024
1 parent 2ab3302 commit 3ee3e39
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
2 changes: 2 additions & 0 deletions example/integration_test/downloader_integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2443,6 +2443,7 @@ void main() {
taskStatusUpdate = TaskStatusUpdate(task, TaskStatus.failed); // reset
FileDownloader().registerCallbacks(
taskStatusCallback: (update) => taskStatusUpdate = update);
await Future.delayed(const Duration(milliseconds: 500));
await downloader.retrieveLocallyStoredData(); // triggers status update
await Future.delayed(const Duration(milliseconds: 500));
expect(taskStatusUpdate.status, equals(TaskStatus.complete));
Expand Down Expand Up @@ -2478,6 +2479,7 @@ void main() {
taskStatusUpdate = TaskStatusUpdate(task, TaskStatus.canceled); // reset
FileDownloader().registerCallbacks(
taskStatusCallback: (update) => taskStatusUpdate = update);
await Future.delayed(const Duration(milliseconds: 500));
await downloader.retrieveLocallyStoredData(); // triggers status update
await Future.delayed(const Duration(milliseconds: 500));
expect(taskStatusUpdate.status, equals(TaskStatus.failed));
Expand Down
13 changes: 13 additions & 0 deletions ios/Classes/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,16 @@ extension URL {
}
}
}

/// Converts the [map] to a [String:String] map with lowercased keys
func lowerCasedStringStringMap(_ map: [AnyHashable: Any]?) -> [String: String]? {
if map == nil {return nil}
var result: [String: String] = [:]
for (key, value) in map! {
if let stringKey = key as? String, let stringValue = value as? String {
result[stringKey.lowercased()] = stringValue
}
}
return result
}

26 changes: 24 additions & 2 deletions ios/Classes/Task.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,28 @@ enum TaskStatus: Int, Codable {
struct TaskStatusUpdate: Encodable {
var task: Task
var taskStatus: TaskStatus
var exception: TaskException?
var responseBody: String?
var responseStatusCode: Int?
var responseHeaders: [String: String]?
var mimeType: String?
var charSet: String?

func argList() -> [Any?] {
let finalState = isFinalState(status: taskStatus)
return taskStatus == .failed
? [taskStatus.rawValue,
exception?.type.rawValue,
exception?.description,
exception?.httpResponseCode,
responseBody]
: [taskStatus.rawValue,
finalState ? responseBody : nil,
finalState ? responseHeaders : nil,
(taskStatus == .complete || taskStatus == .notFound) ? responseStatusCode : nil,
finalState ? mimeType : nil,
finalState ? charSet : nil]
}
}

/** Holds data associated with a task progress update, for local storage */
Expand All @@ -216,7 +238,7 @@ struct ResumeData: Encodable {
}

/// The type of [TaskException]
enum ExceptionType: String {
enum ExceptionType: String, Encodable {
case

// General error
Expand Down Expand Up @@ -248,7 +270,7 @@ enum ExceptionType: String {
* The [description] is typically taken from the platform-generated
* error message, or from the plugin. The localization is undefined
*/
struct TaskException {
struct TaskException : Encodable {
var type: ExceptionType
var httpResponseCode: Int = -1
var description: String
Expand Down
16 changes: 12 additions & 4 deletions ios/Classes/TaskFunctions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,20 @@ func processStatusUpdate(task: Task, status: TaskStatus, taskException: TaskExce
let finalTaskException = taskException == nil
? TaskException(type: .general, httpResponseCode: -1, description: "")
: taskException
let arg: [Any?] = status == .failed
? [status.rawValue, finalTaskException!.type.rawValue, finalTaskException!.description, finalTaskException!.httpResponseCode, responseBody] as [Any?]
: [status.rawValue, responseBody, responseHeaders, finalResponseStatusCode, mimeType, charSet] as [Any?]
let statusUpdate = isFinalState(status: status)
? TaskStatusUpdate(task: task,
taskStatus: status,
exception: status == .failed ? finalTaskException : nil,
responseBody: responseBody,
responseStatusCode: (status == .complete || status == .notFound) ? finalResponseStatusCode : nil,
responseHeaders: lowerCasedStringStringMap(responseHeaders),
mimeType: mimeType,
charSet: charSet)
: TaskStatusUpdate(task: task, taskStatus: status)
let arg = statusUpdate.argList()
if !postOnBackgroundChannel(method: "statusUpdate", task: task, arg: arg) {
// store update locally as a merged task/status JSON string, without error info
guard let jsonData = try? JSONEncoder().encode(TaskStatusUpdate(task: task, taskStatus: status))
guard let jsonData = try? JSONEncoder().encode(statusUpdate)
else {
os_log("Could not store status update locally", log: log, type: .debug)
return }
Expand Down

0 comments on commit 3ee3e39

Please sign in to comment.