-
-
Save le0nidas/c29ea33494488dc9c1345f579b9c5af6 to your computer and use it in GitHub Desktop.
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