Created
August 12, 2015 20:54
-
-
Save flyinghyrax/151e7ebe0139a4aa8aac to your computer and use it in GitHub Desktop.
Idea for implementing session structure in a way that supports polymorphism
This file contains hidden or 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
/* | |
It won't work, b/c doing the JSON parsing will be a unbelievable pain! | |
The "snazziness" of this solution isn't worth that pain and the time it takes to implement, | |
considering how much we would use it. | |
*/ | |
// MARK: Session type protocols | |
protocol Session { | |
// session id, unique to the user | |
var id: Int { get } | |
// video for the session | |
var videoId: Int { get } | |
// whether or not this session is complete | |
var completed: Bool { get } | |
// for making sure local changes are synced to the API | |
var synchronized: Bool { get } | |
func start(at: NSDate) -> StartedSession | |
} | |
extension Session { | |
// by default, sessions are not complete | |
var completed: Bool { | |
return false | |
} | |
} | |
protocol StartedSession: Session { | |
// date when the session started | |
var startedDate: NSDate { get } | |
func complete(at: NSDate, pts: Double, activeTime: Int, pausedTime: Int) -> CompletedSession | |
} | |
protocol ScheduledSession: Session { | |
// date when the session is scheduled to happen | |
var scheduledDate: NSDate { get } | |
// if the session is schedule, incomplete, and in the past, then it was missed | |
var missed: Bool { get } | |
} | |
protocol CompletedSession: StartedSession { | |
// date when the session was completed (user *finished* the video) | |
var completedDate: NSDate { get } | |
// points earned in the session | |
var pointsEarned: Double { get } | |
// seconds spent in the workout, not including time paused | |
var secondsActive: Int { get } | |
// seconds spent paused | |
var secondsPaused: Int { get } | |
} | |
extension CompletedSession { | |
// A session implementing CompletedSession will return completed = true | |
var completed: Bool { | |
return true | |
} | |
} | |
// MARK: Shared session implementation | |
class BaseSession { | |
let id: Int | |
let videoId: Int | |
var synchronized: Bool = true | |
init(id: Int, video: Int) { | |
self.id = id | |
self.videoId = video | |
} | |
} | |
// MARK: Ad-hoc sessions | |
class AdHocSession: BaseSession, Session { | |
var completed: Bool { | |
return false | |
} | |
func start(at: NSDate = NSDate()) -> StartedSession { | |
return StartedAdHocSession(unstarted: self, started: at) | |
} | |
} | |
class StartedAdHocSession: AdHocSession, StartedSession { | |
var startedDate: NSDate | |
init(id: Int, video: Int, started: NSDate) { | |
startedDate = started | |
super.init(id: id, video: video) | |
} | |
convenience init(unstarted: AdHocSession, started: NSDate) { | |
self.init(id: unstarted.id, video: unstarted.videoId, started: started) | |
} | |
func complete(at: NSDate = NSDate(), pts: Double, activeTime: Int, pausedTime: Int) -> CompletedSession { | |
return CompletedAdHocSession(startedSession: self, completed: at, pts: pts, active: activeTime, paused: pausedTime) | |
} | |
} | |
class CompletedAdHocSession: StartedAdHocSession, CompletedSession { | |
var completedDate: NSDate | |
var pointsEarned: Double | |
var secondsActive: Int | |
var secondsPaused: Int | |
init(id: Int, video: Int, started: NSDate, completed: NSDate, pts: Double, active: Int, paused: Int) { | |
self.completedDate = completed | |
self.pointsEarned = pts | |
self.secondsActive = active | |
self.secondsPaused = paused | |
super.init(id: id, video: video, started: started) | |
} | |
convenience init(startedSession: StartedAdHocSession, completed: NSDate, pts: Double, active: Int, paused: Int) { | |
self.init(id: startedSession.id, | |
video: startedSession.videoId, | |
started: startedSession.startedDate, | |
completed: completed, | |
pts: pts, | |
active: active, | |
paused: paused) | |
} | |
} | |
// did not implement corresponding `Scheduled*Session` hierarchy, but it would be similar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment