Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions src/main/kotlin/presentation/CategoryUI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,36 @@ class CategoryUI(
private val viewer: Viewer,
private val reader: Reader,
private val userCache: UserCache
) : UiRunner {

): UiRunner {
private val allowed by lazy { children.filter { it.isAllowed(userCache.currentPermission) } }

override fun run() {
val permission = userCache.currentPermission
val allowed = children.filter { it.isAllowed(permission) }

if (allowed.isEmpty()) {
viewer.show("No available actions in $label.")
return
}

executeMenu()
}

private fun executeMenu() {
while (true) {
viewer.show("=== $label ===")
allowed.sortedBy { it.id }
.forEach { viewer.show("${it.id} – ${it.label}") }
viewer.show("X – Back")

when (val input = reader.read()?.trim()?.uppercase(Locale.getDefault())) {
null,"X" -> return
else -> allowed
.firstOrNull { it.id == input.toIntOrNull() }
?.run()
?: viewer.show("Invalid choice")
}
showMenu()
val input = reader.read()?.trim()?.uppercase(Locale.getDefault())

if (input.isNullOrEmpty() || input == "X") return

allowed.firstOrNull { it.id == input.toIntOrNull() }
?.run()
?: viewer.show("Invalid choice")
}
}
}

private fun showMenu() {
viewer.show("=== $label ===")
allowed.sortedBy { it.id }
.forEach { viewer.show("${it.id} – ${it.label}") }
viewer.show("X – Back")
}
}