Skip to content

Instantly share code, notes, and snippets.

@le0nidas
Created December 26, 2020 08:40
Show Gist options
  • Save le0nidas/c29ea33494488dc9c1345f579b9c5af6 to your computer and use it in GitHub Desktop.
Save le0nidas/c29ea33494488dc9c1345f579b9c5af6 to your computer and use it in GitHub Desktop.
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