-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from tobyspark/feature-server-status
Feature: Thing screen verified + published status updates from server - gets study start+end dates from server - gets video verified status from server - displays video verified status, with messaging - displays published status, based on study end date and verified status
- Loading branch information
Showing
9 changed files
with
262 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// | ||
// Participant+ServerStatus.swift | ||
// ORBIT Camera | ||
// | ||
// Created by Toby Harris on 24/05/2020. | ||
// Copyright © 2020 Toby Harris. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import GRDB | ||
import os | ||
|
||
extension Participant { | ||
struct APIGETResponse: Codable { | ||
let studyStart: Date | ||
let studyEnd: Date | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case studyStart = "study_start" | ||
case studyEnd = "study_end" | ||
} | ||
} | ||
|
||
static func updateServerStatuses(url: URL = URL(string: Settings.endpointParticipant)!) { | ||
var request = URLRequest(url: url) | ||
request.setValue(appNetwork.authCredential, forHTTPHeaderField: "Authorization") | ||
|
||
let jsonDecoder = JSONDecoder() | ||
jsonDecoder.dateDecodingStrategy = .formatted(Settings.apiDateFomatter) | ||
|
||
let task = URLSession.shared.dataTask(with: request) { data, response, error in | ||
if let error = error { | ||
os_log("Participant.updateServerStatuses failed, received error", log: appNetLog) | ||
print(error) | ||
return | ||
} | ||
guard let httpResponse = response as? HTTPURLResponse | ||
else { | ||
os_log("Participant.updateServerStatuses failed, cannot parse response", log: appNetLog) | ||
return | ||
} | ||
guard httpResponse.statusCode == 200 | ||
else { | ||
os_log("Participant.updateServerStatuses failed: %d", log: appNetLog, httpResponse.statusCode) | ||
return | ||
} | ||
guard | ||
let mimeType = httpResponse.mimeType, | ||
mimeType == "application/json", | ||
let data = data, | ||
let participantData = try? jsonDecoder.decode(APIGETResponse.self, from: data) | ||
else { | ||
os_log("Participant.updateServerStatuses failed, could not decode data") | ||
return | ||
} | ||
|
||
var participant = try! Participant.appParticipant() | ||
if participant.studyStart != participantData.studyStart || participant.studyEnd != participantData.studyEnd { | ||
participant.studyStart = participantData.studyStart | ||
participant.studyEnd = participantData.studyEnd | ||
try! dbQueue.write { db in try participant.save(db) } | ||
} | ||
} | ||
task.resume() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// | ||
// Video+ServerStatus.swift | ||
// ORBIT Camera | ||
// | ||
// Created by Toby Harris on 23/05/2020. | ||
// Copyright © 2020 Toby Harris. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import GRDB | ||
import os | ||
|
||
extension Video { | ||
struct APIGETItemResponse: Codable { | ||
let id: Int | ||
let thing: Int | ||
let file: String | ||
let technique: String | ||
let validation: String | ||
} | ||
|
||
struct APIGETPageResponse: Codable { | ||
let count: Int | ||
let next: String? | ||
let previous: String? | ||
let results: [APIGETItemResponse] | ||
} | ||
|
||
static func updateServerStatuses(url: URL = URL(string: Settings.endpointVideo)!) { | ||
var request = URLRequest(url: url) | ||
request.setValue(appNetwork.authCredential, forHTTPHeaderField: "Authorization") | ||
|
||
let task = URLSession.shared.dataTask(with: request) { data, response, error in | ||
if let error = error { | ||
os_log("Video.updateServerStatuses failed, received error", log: appNetLog) | ||
print(error) | ||
return | ||
} | ||
guard let httpResponse = response as? HTTPURLResponse | ||
else { | ||
os_log("Video.updateServerStatuses failed, cannot parse response", log: appNetLog) | ||
return | ||
} | ||
guard httpResponse.statusCode == 200 | ||
else { | ||
os_log("Video.updateServerStatuses failed: %d", log: appNetLog, httpResponse.statusCode) | ||
return | ||
} | ||
guard | ||
let mimeType = httpResponse.mimeType, | ||
mimeType == "application/json", | ||
let data = data, | ||
let pageData = try? JSONDecoder().decode(APIGETPageResponse.self, from: data) | ||
else { | ||
os_log("Video.updateServerStatuses failed, could not decode data") | ||
return | ||
} | ||
|
||
// Update videos | ||
try! dbQueue.write { db in | ||
for result in pageData.results { | ||
guard var video = try Video.filter(Video.Columns.orbitID == result.id).fetchOne(db) | ||
else { | ||
os_log("Video GET returned unknown orbitID: %d", log: appNetLog, type: .error, result.id) | ||
continue | ||
} | ||
guard let verifiedServerStatus = Video.Verified.init(rawValue: result.validation) | ||
else { | ||
os_log("Video GET returned unknown validation: %{public}s", log: appNetLog, type: .error, result.validation) | ||
continue | ||
} | ||
if video.verified != verifiedServerStatus { | ||
video.verified = verifiedServerStatus | ||
try video.save(db) | ||
} | ||
} | ||
} | ||
|
||
// Process next page if needed | ||
if let nextPage = pageData.next { | ||
Video.updateServerStatuses(url: URL(string: nextPage)!) | ||
} | ||
} | ||
task.resume() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters