This file contains 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
class CreateTask( | |
private val clock: Clock, | |
private val localStorage: LocalStorage, | |
private val observers: List<TaskObserver> | |
) { | |
fun invoke(description: String) { | |
// normalize description | |
val normalizedDescription = if (description.length > MAX_DESCRIPTION_LENGTH) | |
description.substring(0, MAX_DESCRIPTION_LENGTH - 1) else | |
description | |
// create task | |
val currentTime = clock.now() | |
val initialStatus = Status.NotStarted | |
val newTask = Task(normalizedDescription, initialStatus, currentTime) | |
// save task | |
localStorage.save(newTask) | |
// notify | |
val observingNotStarting = mutableListOf<TaskObserver>() | |
for (i in 0..observers.size) { | |
val taskObserver = observers[i] | |
if (taskObserver.observedStatus == Status.NotStarted) { | |
observingNotStarting.add(taskObserver) | |
} | |
} | |
for (i in 0..observingNotStarting.size) { | |
observingNotStarting[i].notify(newTask) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment