Files
WWDC/ConfCore/SessionProgress.swift
2017-05-11 21:42:04 -03:00

71 lines
2.1 KiB
Swift

//
// SessionProgress.swift
// WWDC
//
// Created by Guilherme Rambo on 11/05/17.
// Copyright © 2017 Guilherme Rambo. All rights reserved.
//
import Cocoa
import RealmSwift
/// Defines the user action of adding a session as favorite
public class SessionProgress: Object {
/// Unique identifier
public dynamic var identifier = UUID().uuidString
/// When the progress was created
public dynamic var createdAt = Date()
/// When the progress was last update
public dynamic var updatedAt = Date()
/// The current position in the video (in seconds)
public dynamic var currentPosition: Double = 0
/// The current position in the video, relative to the duration (from 0 to 1)
public dynamic var relativePosition: Double = 0
/// The session this progress is associated with
public let session = LinkingObjects(fromType: Session.self, property: "progresses")
public override class func primaryKey() -> String? {
return "identifier"
}
}
extension Session {
public func setCurrentPosition(_ position: Double, _ duration: Double) {
guard Thread.isMainThread else { return }
guard !duration.isNaN, !duration.isZero, !duration.isInfinite else { return }
guard !position.isNaN, !position.isZero, !position.isInfinite else { return }
do {
try self.realm?.write {
var progress: SessionProgress
if let p = progresses.first {
progress = p
} else {
progress = SessionProgress()
progresses.append(progress)
}
progress.currentPosition = position
progress.relativePosition = position / duration
progress.updatedAt = Date()
}
} catch {
NSLog("Error updating session progress: \(error)")
}
}
public func currentPosition() -> Double {
return progresses.first?.currentPosition ?? 0
}
}