Skip to content

Commit aaff78b

Browse files
authored
refactor userCache currentPermission as lazy (#74)
1 parent e18e380 commit aaff78b

1 file changed

Lines changed: 22 additions & 17 deletions

File tree

src/main/kotlin/presentation/CategoryUI.kt

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,36 @@ class CategoryUI(
1212
private val viewer: Viewer,
1313
private val reader: Reader,
1414
private val userCache: UserCache
15+
) : UiRunner {
1516

16-
): UiRunner {
17+
private val allowed by lazy { children.filter { it.isAllowed(userCache.currentPermission) } }
1718

1819
override fun run() {
19-
val permission = userCache.currentPermission
20-
val allowed = children.filter { it.isAllowed(permission) }
21-
2220
if (allowed.isEmpty()) {
2321
viewer.show("No available actions in $label.")
2422
return
2523
}
2624

25+
executeMenu()
26+
}
27+
28+
private fun executeMenu() {
2729
while (true) {
28-
viewer.show("=== $label ===")
29-
allowed.sortedBy { it.id }
30-
.forEach { viewer.show("${it.id}${it.label}") }
31-
viewer.show("X – Back")
32-
33-
when (val input = reader.read()?.trim()?.uppercase(Locale.getDefault())) {
34-
null,"X" -> return
35-
else -> allowed
36-
.firstOrNull { it.id == input.toIntOrNull() }
37-
?.run()
38-
?: viewer.show("Invalid choice")
39-
}
30+
showMenu()
31+
val input = reader.read()?.trim()?.uppercase(Locale.getDefault())
32+
33+
if (input.isNullOrEmpty() || input == "X") return
34+
35+
allowed.firstOrNull { it.id == input.toIntOrNull() }
36+
?.run()
37+
?: viewer.show("Invalid choice")
4038
}
4139
}
42-
}
40+
41+
private fun showMenu() {
42+
viewer.show("=== $label ===")
43+
allowed.sortedBy { it.id }
44+
.forEach { viewer.show("${it.id}${it.label}") }
45+
viewer.show("X – Back")
46+
}
47+
}

0 commit comments

Comments
 (0)