From dd0438ff95b301575bdf434e49ece436eaedc517 Mon Sep 17 00:00:00 2001 From: Robin Linden Date: Sun, 14 Jan 2024 03:07:00 +0100 Subject: [PATCH] Replace Enum.values() w/ Enum.entries --- atox/src/main/kotlin/settings/Settings.kt | 9 +++++---- atox/src/main/kotlin/ui/chat/ChatAdapter.kt | 7 +++---- .../kotlin/ui/contactlist/ContactAdapter.kt | 7 +++---- .../kotlin/ui/settings/SettingsFragment.kt | 9 +++++---- core/src/main/kotlin/db/Converters.kt | 10 +++++----- core/src/test/kotlin/db/ConvertersTest.kt | 10 +++++----- domain/src/main/kotlin/tox/ToxUtil.kt | 8 ++++---- domain/src/test/kotlin/tox/ToxUtilTest.kt | 20 +++++++++---------- 8 files changed, 40 insertions(+), 40 deletions(-) diff --git a/atox/src/main/kotlin/settings/Settings.kt b/atox/src/main/kotlin/settings/Settings.kt index 54bda3a1e..05e2216e0 100644 --- a/atox/src/main/kotlin/settings/Settings.kt +++ b/atox/src/main/kotlin/settings/Settings.kt @@ -1,4 +1,5 @@ -// SPDX-FileCopyrightText: 2020-2022 aTox contributors +// SPDX-FileCopyrightText: 2020-2024 Robin Lindén +// SPDX-FileCopyrightText: 2022 aTox contributors // // SPDX-License-Identifier: GPL-3.0-only @@ -66,7 +67,7 @@ class Settings @Inject constructor(private val ctx: Context) { set(seconds) = preferences.edit().putLong("auto_away_seconds", seconds).apply() var proxyType: ProxyType - get() = ProxyType.values()[preferences.getInt("proxy_type", 0)] + get() = ProxyType.entries[preferences.getInt("proxy_type", 0)] set(type) = preferences.edit { putInt("proxy_type", type.ordinal) } var proxyAddress: String @@ -78,11 +79,11 @@ class Settings @Inject constructor(private val ctx: Context) { set(port) = preferences.edit { putInt("proxy_port", port) } var ftAutoAccept: FtAutoAccept - get() = FtAutoAccept.values()[preferences.getInt("ft_auto_accept", 0)] + get() = FtAutoAccept.entries[preferences.getInt("ft_auto_accept", 0)] set(autoAccept) = preferences.edit { putInt("ft_auto_accept", autoAccept.ordinal) } var bootstrapNodeSource: BootstrapNodeSource - get() = BootstrapNodeSource.values()[preferences.getInt("bootstrap_node_source", 0)] + get() = BootstrapNodeSource.entries[preferences.getInt("bootstrap_node_source", 0)] set(source) = preferences.edit { putInt("bootstrap_node_source", source.ordinal) } var disableScreenshots: Boolean diff --git a/atox/src/main/kotlin/ui/chat/ChatAdapter.kt b/atox/src/main/kotlin/ui/chat/ChatAdapter.kt index 9dc381a85..9ecffefb9 100644 --- a/atox/src/main/kotlin/ui/chat/ChatAdapter.kt +++ b/atox/src/main/kotlin/ui/chat/ChatAdapter.kt @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2020-2021 aTox contributors +// SPDX-FileCopyrightText: 2019-2024 Robin Lindén // // SPDX-License-Identifier: GPL-3.0-only @@ -64,7 +64,6 @@ private enum class ChatItemType { SentFileTransfer, } -private val types = ChatItemType.values() private val timeFormatter = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT) private class MessageViewHolder(row: View) { @@ -98,7 +97,7 @@ class ChatAdapter( override fun getCount(): Int = messages.size override fun getItem(position: Int): Any = messages[position] override fun getItemId(position: Int): Long = position.toLong() - override fun getViewTypeCount(): Int = types.size + override fun getViewTypeCount(): Int = ChatItemType.entries.size override fun getItemViewType(position: Int): Int = with(messages[position]) { when (type) { MessageType.Normal -> when (sender) { @@ -117,7 +116,7 @@ class ChatAdapter( } override fun getView(position: Int, convertView: View?, parent: ViewGroup): View = - when (val type = types[getItemViewType(position)]) { + when (val type = ChatItemType.entries[getItemViewType(position)]) { ChatItemType.ReceivedMessage, ChatItemType.SentMessage, ChatItemType.ReceivedAction, ChatItemType.SentAction, -> { diff --git a/atox/src/main/kotlin/ui/contactlist/ContactAdapter.kt b/atox/src/main/kotlin/ui/contactlist/ContactAdapter.kt index 364aee367..fd03e03db 100644 --- a/atox/src/main/kotlin/ui/contactlist/ContactAdapter.kt +++ b/atox/src/main/kotlin/ui/contactlist/ContactAdapter.kt @@ -1,4 +1,5 @@ -// SPDX-FileCopyrightText: 2020-2021 aTox contributors +// SPDX-FileCopyrightText: 2019-2024 Robin Lindén +// SPDX-FileCopyrightText: 2021-2022 aTox contributors // // SPDX-License-Identifier: GPL-3.0-only @@ -25,8 +26,6 @@ enum class ContactListItemType { Contact, } -private val types = ContactListItemType.values() - class ContactAdapter( private val inflater: LayoutInflater, private val context: Context, @@ -48,7 +47,7 @@ class ContactAdapter( } override fun getView(position: Int, convertView: View?, parent: ViewGroup): View = - when (types[getItemViewType(position)]) { + when (ContactListItemType.entries[getItemViewType(position)]) { ContactListItemType.FriendRequest -> { val view: View val vh: FriendRequestViewHolder diff --git a/atox/src/main/kotlin/ui/settings/SettingsFragment.kt b/atox/src/main/kotlin/ui/settings/SettingsFragment.kt index 04f887f2e..d1054eb6a 100644 --- a/atox/src/main/kotlin/ui/settings/SettingsFragment.kt +++ b/atox/src/main/kotlin/ui/settings/SettingsFragment.kt @@ -1,4 +1,5 @@ -// SPDX-FileCopyrightText: 2019-2022 aTox contributors +// SPDX-FileCopyrightText: 2019-2024 Robin Lindén +// SPDX-FileCopyrightText: 2021-2022 aTox contributors // // SPDX-License-Identifier: GPL-3.0-only @@ -144,7 +145,7 @@ class SettingsFragment : BaseFragment(FragmentSettingsB settingFtAutoAccept.setSelection(vm.getFtAutoAccept().ordinal) settingFtAutoAccept.onItemSelectedListener { - vm.setFtAutoAccept(FtAutoAccept.values()[it]) + vm.setFtAutoAccept(FtAutoAccept.entries[it]) } settingConfirmQuitting.isChecked = vm.getConfirmQuitting() @@ -170,7 +171,7 @@ class SettingsFragment : BaseFragment(FragmentSettingsB proxyType.setSelection(vm.getProxyType().ordinal) proxyType.onItemSelectedListener { - val selected = ProxyType.values()[it] + val selected = ProxyType.entries[it] vm.setProxyType(selected) // Disable UDP if a proxy is selected to ensure all traffic goes through the proxy. @@ -272,7 +273,7 @@ class SettingsFragment : BaseFragment(FragmentSettingsB settingBootstrapNodes.setSelection(vm.getBootstrapNodeSource().ordinal) settingBootstrapNodes.onItemSelectedListener { - val source = BootstrapNodeSource.values()[it] + val source = BootstrapNodeSource.entries[it] // Hack to avoid triggering the document chooser again if the user has set it to UserProvided. if (source == vm.getBootstrapNodeSource()) return@onItemSelectedListener diff --git a/core/src/main/kotlin/db/Converters.kt b/core/src/main/kotlin/db/Converters.kt index 32f9c314b..d880671fe 100644 --- a/core/src/main/kotlin/db/Converters.kt +++ b/core/src/main/kotlin/db/Converters.kt @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2019-2020 aTox contributors +// SPDX-FileCopyrightText: 2019-2024 Robin Lindén // // SPDX-License-Identifier: GPL-3.0-only @@ -14,7 +14,7 @@ class Converters private constructor() { companion object { @TypeConverter @JvmStatic - fun toStatus(status: Int): UserStatus = UserStatus.values()[status] + fun toStatus(status: Int): UserStatus = UserStatus.entries[status] @TypeConverter @JvmStatic @@ -22,7 +22,7 @@ class Converters private constructor() { @TypeConverter @JvmStatic - fun toConnection(connection: Int): ConnectionStatus = ConnectionStatus.values()[connection] + fun toConnection(connection: Int): ConnectionStatus = ConnectionStatus.entries[connection] @TypeConverter @JvmStatic @@ -30,7 +30,7 @@ class Converters private constructor() { @TypeConverter @JvmStatic - fun toSender(sender: Int): Sender = Sender.values()[sender] + fun toSender(sender: Int): Sender = Sender.entries[sender] @TypeConverter @JvmStatic @@ -38,7 +38,7 @@ class Converters private constructor() { @TypeConverter @JvmStatic - fun toMessageType(type: Int): MessageType = MessageType.values()[type] + fun toMessageType(type: Int): MessageType = MessageType.entries[type] @TypeConverter @JvmStatic diff --git a/core/src/test/kotlin/db/ConvertersTest.kt b/core/src/test/kotlin/db/ConvertersTest.kt index 3501b2481..b8aae36ac 100644 --- a/core/src/test/kotlin/db/ConvertersTest.kt +++ b/core/src/test/kotlin/db/ConvertersTest.kt @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2020 aTox contributors +// SPDX-FileCopyrightText: 2020-2024 Robin Lindén // // SPDX-License-Identifier: GPL-3.0-only @@ -14,7 +14,7 @@ import ltd.evilcorp.core.vo.UserStatus class ConvertersTest { @Test fun `user status can be converted`() { - UserStatus.values().forEach { + UserStatus.entries.forEach { assertEquals(it.ordinal, Converters.fromStatus(it)) assertEquals(it, Converters.toStatus(it.ordinal)) } @@ -22,7 +22,7 @@ class ConvertersTest { @Test fun `connection status can be converted`() { - ConnectionStatus.values().forEach { + ConnectionStatus.entries.forEach { assertEquals(it.ordinal, Converters.fromConnection(it)) assertEquals(it, Converters.toConnection(it.ordinal)) } @@ -30,7 +30,7 @@ class ConvertersTest { @Test fun `sender can be converted`() { - Sender.values().forEach { + Sender.entries.forEach { assertEquals(it.ordinal, Converters.fromSender(it)) assertEquals(it, Converters.toSender(it.ordinal)) } @@ -38,7 +38,7 @@ class ConvertersTest { @Test fun `message type can be converted`() { - MessageType.values().forEach { + MessageType.entries.forEach { assertEquals(it.ordinal, Converters.fromMessageType(it)) assertEquals(it, Converters.toMessageType(it.ordinal)) } diff --git a/domain/src/main/kotlin/tox/ToxUtil.kt b/domain/src/main/kotlin/tox/ToxUtil.kt index 6c1b38cfc..16443d90e 100644 --- a/domain/src/main/kotlin/tox/ToxUtil.kt +++ b/domain/src/main/kotlin/tox/ToxUtil.kt @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2019-2021 aTox contributors +// SPDX-FileCopyrightText: 2019-2024 Robin Lindén // // SPDX-License-Identifier: GPL-3.0-only @@ -18,9 +18,9 @@ import ltd.evilcorp.core.vo.UserStatus fun String.hexToBytes(): ByteArray = chunked(2).map { it.uppercase().toInt(radix = 16).toByte() }.toByteArray() fun ByteArray.bytesToHex(): String = this.joinToString("") { "%02X".format(it) } -fun ToxUserStatus.toUserStatus(): UserStatus = UserStatus.values()[this.ordinal] -fun ToxConnection.toConnectionStatus(): ConnectionStatus = ConnectionStatus.values()[this.ordinal] -fun ToxMessageType.toMessageType(): MessageType = MessageType.values()[this.ordinal] +fun ToxUserStatus.toUserStatus(): UserStatus = UserStatus.entries[this.ordinal] +fun ToxConnection.toConnectionStatus(): ConnectionStatus = ConnectionStatus.entries[this.ordinal] +fun ToxMessageType.toMessageType(): MessageType = MessageType.entries[this.ordinal] fun SaveOptions.toToxOptions(): ToxOptions = ToxOptions( true, udpEnabled, diff --git a/domain/src/test/kotlin/tox/ToxUtilTest.kt b/domain/src/test/kotlin/tox/ToxUtilTest.kt index d245f2abd..126216bc1 100644 --- a/domain/src/test/kotlin/tox/ToxUtilTest.kt +++ b/domain/src/test/kotlin/tox/ToxUtilTest.kt @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2020-2022 Robin Lindén +// SPDX-FileCopyrightText: 2020-2024 Robin Lindén // // SPDX-License-Identifier: GPL-3.0-only @@ -22,10 +22,10 @@ private fun byteArrayOf(vararg bytes: Int) = ByteArray(bytes.size) { bytes[it].t class ToxUtilTest { @Test fun `connection enums can be converted`() { - assert(ToxConnection.values().size == ConnectionStatus.values().size) - assert(ConnectionStatus.values().size == 3) + assert(ToxConnection.entries.size == ConnectionStatus.entries.size) + assert(ConnectionStatus.entries.size == 3) - ToxConnection.values().forEach { + ToxConnection.entries.forEach { assertEquals(it.ordinal, it.toConnectionStatus().ordinal) } @@ -36,9 +36,9 @@ class ToxUtilTest { @Test fun `message type enums can be converted`() { - assertEquals(2, ToxMessageType.values().size) + assertEquals(2, ToxMessageType.entries.size) - ToxMessageType.values().forEach { type -> + ToxMessageType.entries.forEach { type -> assertEquals(type.ordinal, type.toMessageType().ordinal) assertEquals(type, type.toMessageType().toToxType()) } @@ -49,14 +49,14 @@ class ToxUtilTest { @Test fun `status enums can be converted`() { - assert(ToxUserStatus.values().size == UserStatus.values().size) - assert(UserStatus.values().size == 3) + assert(ToxUserStatus.entries.size == UserStatus.entries.size) + assert(UserStatus.entries.size == 3) - ToxUserStatus.values().forEach { + ToxUserStatus.entries.forEach { assertEquals(it.ordinal, it.toUserStatus().ordinal) } - UserStatus.values().forEach { type -> + UserStatus.entries.forEach { type -> assertEquals(type, type.toToxType().toUserStatus()) }