-
-
Save le0nidas/48171affa15f34734da33bddb1188b42 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) { | |
val newTask = createNewTask(description) | |
localStorage.save(newTask) | |
notifyAnyObservers(newTask) | |
} | |
private fun createNewTask(description: String): Task { | |
val normalizedDescription = normalize(description) | |
return Task(normalizedDescription, Status.NotStarted, clock.now()) | |
} | |
private fun normalize(description: String): String { | |
return if (description.length > MAX_DESCRIPTION_LENGTH) | |
description.substring(0, MAX_DESCRIPTION_LENGTH - 1) else | |
description | |
} | |
private fun notifyAnyObservers(newTask: Task) { | |
observers | |
.filter { taskObserver -> taskObserver.observedStatus == Status.NotStarted } | |
.forEach { taskObserver -> taskObserver.notify(newTask) } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment