diff --git a/src/main/kotlin/chatroom/users/Users.kt b/src/main/kotlin/chatroom/users/Users.kt index 233675d..460bbbe 100644 --- a/src/main/kotlin/chatroom/users/Users.kt +++ b/src/main/kotlin/chatroom/users/Users.kt @@ -1,6 +1,7 @@ package chatroom.users import chatroom.ChatroomWindow +import chatroom.LEFT_WIDTH import org.koin.dsl.module val usersModule = @@ -8,6 +9,14 @@ val usersModule = scope { scoped { UsersPanel(presenter = get()) } scoped { UsersListUseCase() } - scoped { UsersListPresenter(users = get(), scope = get().windowScope, dispatchers = get()) } + scoped { + UsersListPresenter( + users = get(), + truncate = get(), + scope = get().windowScope, + dispatchers = get(), + ) + } } + factory { UsersListTruncation(LEFT_WIDTH - 3) } } diff --git a/src/main/kotlin/chatroom/users/UsersListPresenter.kt b/src/main/kotlin/chatroom/users/UsersListPresenter.kt index e79cc0c..c74726c 100644 --- a/src/main/kotlin/chatroom/users/UsersListPresenter.kt +++ b/src/main/kotlin/chatroom/users/UsersListPresenter.kt @@ -4,23 +4,27 @@ import arch.Presenter import arch.RokyDispatchers import chatroom.users.UsersViewState.* import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch import kotlinx.coroutines.withContext class UsersListPresenter( private val users: UsersListUseCase, + private val truncate: UsersListTruncation, private val scope: CoroutineScope, dispatchers: RokyDispatchers, ) : Presenter(dispatchers) { override fun onAttach(view: UsersListView) { view.show(Empty) scope.launch(dispatchers.io) { - users().map(::Users).collect { state -> - withContext(dispatchers.main) { - show(state) + users() + .truncateUsernames() + .map(::Users).collect { state -> + withContext(dispatchers.main) { + show(state) + } } - } } } @@ -31,4 +35,9 @@ class UsersListPresenter( override fun onDetach(view: UsersListView) { // Deliberately empty } + + private fun Flow>.truncateUsernames() = + map { username -> + username.map(truncate::invoke) + } } diff --git a/src/main/kotlin/chatroom/users/UsersListTruncation.kt b/src/main/kotlin/chatroom/users/UsersListTruncation.kt new file mode 100644 index 0000000..75a1582 --- /dev/null +++ b/src/main/kotlin/chatroom/users/UsersListTruncation.kt @@ -0,0 +1,7 @@ +package chatroom.users + +import utils.cutOff + +class UsersListTruncation(private val width: Int) { + operator fun invoke(input: String): String = input.cutOff(width) +} diff --git a/src/test/kotlin/chatroom/users/UsersListPresenterTest.kt b/src/test/kotlin/chatroom/users/UsersListPresenterTest.kt index 2956b95..54c465c 100644 --- a/src/test/kotlin/chatroom/users/UsersListPresenterTest.kt +++ b/src/test/kotlin/chatroom/users/UsersListPresenterTest.kt @@ -33,7 +33,7 @@ class UsersListPresenterTest { every { io } returns dispatcher } scope = CoroutineScope(dispatcher) - presenter = UsersListPresenter(listUsers, scope, dispatchers) + presenter = UsersListPresenter(listUsers, UsersListTruncation(10), scope, dispatchers) } @Test @@ -78,6 +78,24 @@ class UsersListPresenterTest { } } + @Test + fun `when users list contains very long usernames, then truncate long usernames only`() = + runTest(dispatcher) { + val userList = listOf("Allie", "Kai", "A very very long username") + coEvery { listUsers() } coAnswersDelayed { flowOf(userList) } + presenter.attach(view) + advanceUntilIdle() + verifyOrder { + view.show(Empty) + view.show( + withArg { + assertTrue { it is Users } + assertTrue { (it as Users).users == listOf("Allie", "Kai", "A very ve…") } + }, + ) + } + } + companion object { private val dispatcher = StandardTestDispatcher() }