Finish the implementation of sessionWatcher and DefaultWorkItemProvider. SessionWatcher will be responsible for tracking user inactivity in the app for a given period of time. If there was no user action received during the given session time then onTimeExceeded completion should be called from SessionWatcher instance. Creation of a DispatchWorkItem will take place in DefaultWorkItemProvider. The created work item will be used in the SessionWatcher Class.
Requirements
Provide an implementation for the following methods and classes:
DefaultWorkItemProvider
func workItem(actionBlock: @escaping () -> ()) -> DispatchWorkItem?
This method should return DispatchWorkItem with the given actionBlock to execute
SessionWatcher
start()
o The start method will be called when the session time starts. As an effect, DispatchWorkItem created from DefaultWorkItemProvider should be started to track the user's inactivity.
o Anew DispatchWorkItem needs to be created when start is called.
o The created DispatchWorkItem should be executed asynchronously on a queue given in the SessionWatcher Tavtireliv4ole
o The actionBlock of DispatchWorkItem should be called after the given sessionTime.
o The previously created DispatchWorkItem needs to be cancelled.
receivedUserAction()
o This method will be called every time the user triggers some action in the app. So SessionWatcher should take care of any DispatchWorkItem that has already been created, and start counting again with the new one.
stop()
o The stop method will be called when the app stops watching the session time. The current DispatchWorkItem should be cancelled.
Protocols and structures
Below, you can find the protocol that is used in sessionWatcher. It will be useful if you want to work in a separate Xcode project or playground in order to provide a solution.
Please do not change the following protocol. Do not copy it to the editor - it will be added automatically during compilation process
protocol WorkItemProvider { func workItem(actionBlock: @escaping () -> ()) -> DispatchWorkItem?
}
import Foundation
class DefaultWorkItemProvider: WorkItemProvider {
func workItem(actionBlock: @escaping () -> ()) -> DispatchWorkItem? {
return nil
}
}
class SessionWatcher {
private var workItemProvider: WorkItemProvider
private var workItem: DispatchWorkItem?
private let sessionTime: TimeInterval
private let queue: DispatchQueue
var onTimeExceeded: (() -> Void)?
init(sessionTime: TimeInterval = 5, workItemProvider: WorkItemProvider, queue: DispatchQueue) {
[login to view URL] = workItemProvider
[login to view URL] = sessionTime
[login to view URL] = queue
}
func start() {
}
func receivedUserAction() {
}
func stop() {
}
}