From 355244a8e27b3b842769aefebe53287358e39a28 Mon Sep 17 00:00:00 2001 From: Nik Clayton Date: Fri, 26 Jan 2024 12:32:45 +0100 Subject: [PATCH] refactor: Destructure maps for more idiomatic code (#388) --- .../components/filters/EditFilterActivity.kt | 4 ++-- .../java/app/pachli/view/ClickableSpanTextView.kt | 14 +++++++------- .../kotlin/app/pachli/core/network/ServerTest.kt | 8 ++++---- .../testing/fakes/InMemorySharedPreferences.kt | 6 +++--- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/app/pachli/components/filters/EditFilterActivity.kt b/app/src/main/java/app/pachli/components/filters/EditFilterActivity.kt index 55d42345b..3a47daf2f 100644 --- a/app/src/main/java/app/pachli/components/filters/EditFilterActivity.kt +++ b/app/src/main/java/app/pachli/components/filters/EditFilterActivity.kt @@ -156,8 +156,8 @@ class EditFilterActivity : BaseActivity() { } lifecycleScope.launch { viewModel.contexts.collect { contexts -> - for (entry in contextSwitches) { - entry.key.isChecked = contexts.contains(entry.value) + for ((key, value) in contextSwitches) { + key.isChecked = contexts.contains(value) } } } diff --git a/app/src/main/java/app/pachli/view/ClickableSpanTextView.kt b/app/src/main/java/app/pachli/view/ClickableSpanTextView.kt index d7d3383a4..d8872157c 100644 --- a/app/src/main/java/app/pachli/view/ClickableSpanTextView.kt +++ b/app/src/main/java/app/pachli/view/ClickableSpanTextView.kt @@ -213,9 +213,9 @@ class ClickableSpanTextView @JvmOverloads constructor( val y = event.y // If the user has clicked directly on a span then use it, ignoring any overlap - for (entry in spanRects) { - if (!entry.key.contains(x, y)) continue - clickedSpan = entry.value + for ((rect, span) in spanRects) { + if (!rect.contains(x, y)) continue + clickedSpan = span Timber.v("span click: ${(clickedSpan as URLSpan).url}") return super.onTouchEvent(event) } @@ -354,12 +354,12 @@ class ClickableSpanTextView @JvmOverloads constructor( // showSpanBoundaries is false. if (BuildConfig.DEBUG && showSpanBoundaries) { canvas.save() - for (entry in delegateRects) { - canvas.drawRect(entry.key, paddingDebugPaint) + for (rect in delegateRects.keys) { + canvas.drawRect(rect, paddingDebugPaint) } - for (entry in spanRects) { - canvas.drawRect(entry.key, spanDebugPaint) + for (rect in spanRects.keys) { + canvas.drawRect(rect, spanDebugPaint) } canvas.restore() } diff --git a/core/network/src/test/kotlin/app/pachli/core/network/ServerTest.kt b/core/network/src/test/kotlin/app/pachli/core/network/ServerTest.kt index 4c24a9562..dad95d246 100644 --- a/core/network/src/test/kotlin/app/pachli/core/network/ServerTest.kt +++ b/core/network/src/test/kotlin/app/pachli/core/network/ServerTest.kt @@ -305,11 +305,11 @@ class ServerVersionTest() { .that(serverVersions.size) .isGreaterThan(5) - for (entry in serverVersions.entries) { - for (version in entry.value) { + for ((name, versions) in serverVersions.entries) { + for (version in versions) { val serverKind = ServerKind.from( NodeInfo.Software( - name = entry.key, + name = name, version = version, ), ) @@ -323,7 +323,7 @@ class ServerVersionTest() { version, ) - assertWithMessage("${entry.key} : $version") + assertWithMessage("$name : $version") .that(result) .isInstanceOf(Ok::class.java) } diff --git a/core/testing/src/main/kotlin/app/pachli/core/testing/fakes/InMemorySharedPreferences.kt b/core/testing/src/main/kotlin/app/pachli/core/testing/fakes/InMemorySharedPreferences.kt index 3b66ed79d..27dc7d100 100644 --- a/core/testing/src/main/kotlin/app/pachli/core/testing/fakes/InMemorySharedPreferences.kt +++ b/core/testing/src/main/kotlin/app/pachli/core/testing/fakes/InMemorySharedPreferences.kt @@ -123,9 +123,9 @@ class InMemorySharedPreferences( listeners.forEach { it.onSharedPreferenceChanged(sharedPreferences, key) } } - edits.forEach { entry -> - store[entry.key] = entry.value - listeners.forEach { it.onSharedPreferenceChanged(sharedPreferences, entry.key) } + edits.forEach { (key, value) -> + store[key] = value + listeners.forEach { it.onSharedPreferenceChanged(sharedPreferences, key) } } deletes.clear()