Skip to content

Commit

Permalink
Replace Enum.values() w/ Enum.entries
Browse files Browse the repository at this point in the history
  • Loading branch information
robinlinden committed Jan 15, 2024
1 parent 5e758dd commit dd0438f
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 40 deletions.
9 changes: 5 additions & 4 deletions atox/src/main/kotlin/settings/Settings.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// SPDX-FileCopyrightText: 2020-2022 aTox contributors
// SPDX-FileCopyrightText: 2020-2024 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2022 aTox contributors
//
// SPDX-License-Identifier: GPL-3.0-only

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 3 additions & 4 deletions atox/src/main/kotlin/ui/chat/ChatAdapter.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2020-2021 aTox contributors
// SPDX-FileCopyrightText: 2019-2024 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: GPL-3.0-only

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
-> {
Expand Down
7 changes: 3 additions & 4 deletions atox/src/main/kotlin/ui/contactlist/ContactAdapter.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// SPDX-FileCopyrightText: 2020-2021 aTox contributors
// SPDX-FileCopyrightText: 2019-2024 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2021-2022 aTox contributors
//
// SPDX-License-Identifier: GPL-3.0-only

Expand All @@ -25,8 +26,6 @@ enum class ContactListItemType {
Contact,
}

private val types = ContactListItemType.values()

class ContactAdapter(
private val inflater: LayoutInflater,
private val context: Context,
Expand All @@ -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
Expand Down
9 changes: 5 additions & 4 deletions atox/src/main/kotlin/ui/settings/SettingsFragment.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// SPDX-FileCopyrightText: 2019-2022 aTox contributors
// SPDX-FileCopyrightText: 2019-2024 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2021-2022 aTox contributors
//
// SPDX-License-Identifier: GPL-3.0-only

Expand Down Expand Up @@ -144,7 +145,7 @@ class SettingsFragment : BaseFragment<FragmentSettingsBinding>(FragmentSettingsB
settingFtAutoAccept.setSelection(vm.getFtAutoAccept().ordinal)

settingFtAutoAccept.onItemSelectedListener {
vm.setFtAutoAccept(FtAutoAccept.values()[it])
vm.setFtAutoAccept(FtAutoAccept.entries[it])
}

settingConfirmQuitting.isChecked = vm.getConfirmQuitting()
Expand All @@ -170,7 +171,7 @@ class SettingsFragment : BaseFragment<FragmentSettingsBinding>(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.
Expand Down Expand Up @@ -272,7 +273,7 @@ class SettingsFragment : BaseFragment<FragmentSettingsBinding>(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
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/kotlin/db/Converters.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019-2020 aTox contributors
// SPDX-FileCopyrightText: 2019-2024 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: GPL-3.0-only

Expand All @@ -14,31 +14,31 @@ 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
fun fromStatus(status: UserStatus): Int = status.ordinal

@TypeConverter
@JvmStatic
fun toConnection(connection: Int): ConnectionStatus = ConnectionStatus.values()[connection]
fun toConnection(connection: Int): ConnectionStatus = ConnectionStatus.entries[connection]

@TypeConverter
@JvmStatic
fun fromConnection(connection: ConnectionStatus): Int = connection.ordinal

@TypeConverter
@JvmStatic
fun toSender(sender: Int): Sender = Sender.values()[sender]
fun toSender(sender: Int): Sender = Sender.entries[sender]

@TypeConverter
@JvmStatic
fun fromSender(sender: Sender): Int = sender.ordinal

@TypeConverter
@JvmStatic
fun toMessageType(type: Int): MessageType = MessageType.values()[type]
fun toMessageType(type: Int): MessageType = MessageType.entries[type]

@TypeConverter
@JvmStatic
Expand Down
10 changes: 5 additions & 5 deletions core/src/test/kotlin/db/ConvertersTest.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2020 aTox contributors
// SPDX-FileCopyrightText: 2020-2024 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: GPL-3.0-only

Expand All @@ -14,31 +14,31 @@ 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))
}
}

@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))
}
}

@Test
fun `sender can be converted`() {
Sender.values().forEach {
Sender.entries.forEach {
assertEquals(it.ordinal, Converters.fromSender(it))
assertEquals(it, Converters.toSender(it.ordinal))
}
}

@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))
}
Expand Down
8 changes: 4 additions & 4 deletions domain/src/main/kotlin/tox/ToxUtil.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019-2021 aTox contributors
// SPDX-FileCopyrightText: 2019-2024 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: GPL-3.0-only

Expand All @@ -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,
Expand Down
20 changes: 10 additions & 10 deletions domain/src/test/kotlin/tox/ToxUtilTest.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2020-2022 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2020-2024 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: GPL-3.0-only

Expand All @@ -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)
}

Expand All @@ -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())
}
Expand All @@ -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())
}

Expand Down

0 comments on commit dd0438f

Please sign in to comment.