forked from insidegui/WWDC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSessionProgress.swift
106 lines (80 loc) · 3 KB
/
SessionProgress.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
// SessionProgress.swift
// WWDC
//
// Created by Guilherme Rambo on 11/05/17.
// Copyright © 2017 Guilherme Rambo. All rights reserved.
//
import Cocoa
import RealmSwift
import os.log
/// Defines the user action of adding a session as favorite
public final class SessionProgress: Object, HasCloudKitFields, SoftDeletable {
/// CloudKit system data
@objc public dynamic var ckFields = Data()
/// Soft delete (for syncing)
@objc public dynamic var isDeleted: Bool = false
/// Unique identifier
@objc public dynamic var identifier = UUID().uuidString
/// When the progress was created
@objc public dynamic var createdAt = Date()
/// When the progress was last update
@objc public dynamic var updatedAt = Date()
/// The current position in the video (in seconds)
@objc public dynamic var currentPosition: Double = 0
/// The current position in the video, relative to the duration (from 0 to 1)
@objc 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 let realm = realm else { return }
guard !duration.isNaN, !duration.isZero, !duration.isInfinite else { return }
guard !position.isNaN, !position.isZero, !position.isInfinite else { return }
do {
let mustCommit: Bool
if !realm.isInWriteTransaction {
realm.beginWrite()
mustCommit = true
} else {
mustCommit = false
}
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()
if mustCommit { try realm.commitWrite() }
} catch {
os_log("Error updating session progress: %{public}@", log: .default, type: .error, String(describing: error))
}
}
public func resetProgress() {
guard let realm = realm else { return }
do {
let mustCommit: Bool
if !realm.isInWriteTransaction {
realm.beginWrite()
mustCommit = true
} else {
mustCommit = false
}
progresses.removeAll()
if mustCommit { try realm.commitWrite() }
} catch {
os_log("Error updating session progress: %{public}@", log: .default, type: .error, String(describing: error))
}
}
public func currentPosition() -> Double {
return progresses.first?.currentPosition ?? 0
}
}