Add start of downloading library item on iOS

This commit is contained in:
advplyr 2022-05-14 11:50:53 -05:00
parent 60ee33cb72
commit d626686614
3 changed files with 97 additions and 4 deletions

View file

@ -15,6 +15,7 @@ struct LibraryItem: Codable {
var folderId: String
var path: String
var relPath: String
var isFile: Bool
var mtimeMs: Int64
var ctimeMs: Int64
var birthtimeMs: Int64
@ -27,6 +28,7 @@ struct LibraryItem: Codable {
var mediaType: String
var media: MediaType
var libraryFiles: [LibraryFile]
var userMediaProgress:MediaProgress?
}
struct MediaType: Codable {
var libraryItemId: String?
@ -125,3 +127,15 @@ struct LibraryFile: Codable {
var ino: String
var metadata: FileMetadata
}
struct MediaProgress:Codable {
var id:String
var libraryItemId:String
var episodeId:String?
var duration:Double
var progress:Double
var currentTime:Double
var isFinished:Bool
var lastUpdate:Int64
var startedAt:Int64
var finishedAt:Int64?
}

View file

@ -60,6 +60,27 @@ class ApiClient {
}
}
}
public static func getResource<T: Decodable>(endpoint: String, decodable: T.Type = T.self, callback: ((_ param: T?) -> Void)?) {
if (Store.serverConfig == nil) {
NSLog("Server config not set")
callback?(nil)
return
}
let headers: HTTPHeaders = [
"Authorization": "Bearer \(Store.serverConfig!.token)"
]
AF.request("\(Store.serverConfig!.address)/\(endpoint)", method: .get, encoding: JSONEncoding.default, headers: headers).responseDecodable(of: decodable) { response in
switch response.result {
case .success(let obj):
callback?(obj)
case .failure(let error):
NSLog("api request to \(endpoint) failed")
print(error)
}
}
}
public static func startPlaybackSession(libraryItemId: String, episodeId: String?, forceTranscode:Bool, callback: @escaping (_ param: PlaybackSession) -> Void) {
var endpoint = "api/items/\(libraryItemId)/play"
@ -83,4 +104,14 @@ class ApiClient {
public static func reportPlaybackProgress(report: PlaybackReport, sessionId: String) {
try? postResource(endpoint: "api/session/\(sessionId)/sync", parameters: report.asDictionary().mapValues({ value in "\(value)" }), callback: nil)
}
public static func getLibraryItemWithProgress(libraryItemId:String, episodeId:String?, callback: @escaping (_ param: LibraryItem?) -> Void) {
var endpoint = "api/items/\(libraryItemId)?expanded=1&include=progress"
if episodeId != nil {
endpoint += "&episodeId=\(episodeId!)"
}
ApiClient.getResource(endpoint: endpoint, decodable: LibraryItem.self) { obj in
callback(obj)
}
}
}