From 5cbd9e473d4b1666abdcfa3e08b4d49a9e22d10e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20G=C3=B6ransson?= Date: Tue, 17 Oct 2023 13:36:24 +0200 Subject: [PATCH] Move DeviceState name logic to actual Device --- .../mullvad/mullvadvpn/compose/screen/DeviceListScreen.kt | 2 +- .../mullvad/mullvadvpn/viewmodel/WelcomeViewModelTest.kt | 2 +- .../main/kotlin/net/mullvad/mullvadvpn/model/Device.kt | 8 ++++++++ .../kotlin/net/mullvad/mullvadvpn/model/DeviceState.kt | 8 +------- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/DeviceListScreen.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/DeviceListScreen.kt index 42ab9faef08a..4aee08fed9da 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/DeviceListScreen.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/DeviceListScreen.kt @@ -193,7 +193,7 @@ fun DeviceListScreen( Column { state.deviceUiItems.forEach { deviceUiState -> ListItem( - text = deviceUiState.device.name, + text = deviceUiState.device.displayName(), subText = deviceUiState.device.created.parseAsDateTime()?.let { creationDate -> diff --git a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/WelcomeViewModelTest.kt b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/WelcomeViewModelTest.kt index 52713c0b0d31..b16eeec2f890 100644 --- a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/WelcomeViewModelTest.kt +++ b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/WelcomeViewModelTest.kt @@ -126,7 +126,7 @@ class WelcomeViewModelTest { // Arrange val expectedAccountNumber = "4444555566667777" val device: Device = mockk() - every { device.name } returns "" + every { device.displayName() } returns "" // Act, Assert viewModel.uiState.test { diff --git a/android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/model/Device.kt b/android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/model/Device.kt index 4918ee0832c8..25c729eb8709 100644 --- a/android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/model/Device.kt +++ b/android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/model/Device.kt @@ -25,4 +25,12 @@ data class Device(val id: String, val name: String, val pubkey: ByteArray, val c result = 31 * result + pubkey.contentHashCode() return result } + + fun displayName(): String = name.capitalizeFirstCharOfEachWord() +} + +private fun String.capitalizeFirstCharOfEachWord(): String { + return split(" ") + .joinToString(" ") { word -> word.replaceFirstChar { firstChar -> firstChar.uppercase() } } + .trimEnd() } diff --git a/android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/model/DeviceState.kt b/android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/model/DeviceState.kt index 2af9b01362f5..fb34c9e64588 100644 --- a/android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/model/DeviceState.kt +++ b/android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/model/DeviceState.kt @@ -19,16 +19,10 @@ sealed class DeviceState : Parcelable { } fun deviceName(): String? { - return (this as? LoggedIn)?.accountAndDevice?.device?.name?.capitalizeFirstCharOfEachWord() + return (this as? LoggedIn)?.accountAndDevice?.device?.displayName() } fun token(): String? { return (this as? LoggedIn)?.accountAndDevice?.account_token } } - -private fun String.capitalizeFirstCharOfEachWord(): String { - return split(" ") - .joinToString(" ") { word -> word.replaceFirstChar { firstChar -> firstChar.uppercase() } } - .trimEnd() -}