Skip to content

Commit

Permalink
refactor: inline string resource accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
tynn committed Nov 21, 2023
1 parent 0307eee commit 5499f35
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 25 deletions.
22 changes: 10 additions & 12 deletions astring/src/main/java/xyz/tynn/astring/ResourceDelegate.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

package xyz.tynn.astring;

import static androidx.core.os.ConfigurationCompat.getLocales;
import static androidx.core.os.ParcelCompat.readBoolean;
import static androidx.core.os.ParcelCompat.writeBoolean;
import static java.lang.Integer.MIN_VALUE;
import static java.lang.String.format;

import android.content.Context;
import android.content.res.Resources;
Expand All @@ -17,6 +19,7 @@
import androidx.annotation.StringRes;

import java.util.Arrays;
import java.util.Locale;
import java.util.Objects;

/**
Expand All @@ -40,21 +43,16 @@ private ResourceDelegate(

@Override
public CharSequence invoke(@NonNull Context context) {
return isPlural ? invokeWithQuantity(context)
: invokeWithoutQuantity(context);
}

private CharSequence invokeWithQuantity(Context context) {
Resources resources = context.getResources();
return isText ? resources.getQuantityText(resId, quantity) : formatArgs != null
? resources.getQuantityString(resId, quantity, formatArgs)
: resources.getQuantityString(resId, quantity);
CharSequence value = isPlural
? resources.getQuantityText(resId, quantity)
: resources.getText(resId);
return isText ? value : formatArgs == null ? value.toString()
: format(getLocale(resources), value.toString(), formatArgs);
}

private CharSequence invokeWithoutQuantity(Context context) {
return isText ? context.getText(resId) : formatArgs != null
? context.getString(resId, formatArgs)
: context.getString(resId);
private static Locale getLocale(Resources resources) {
return getLocales(resources.getConfiguration()).get(0);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package xyz.tynn.astring
import android.content.Context
import io.mockk.every
import io.mockk.mockk
import java.util.Locale
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
Expand All @@ -16,7 +17,7 @@ internal class QuantityStringResourceDelegateTest {
@Test
fun `invoke should return string without format args`() {
val context = mockk<Context> {
every { resources.getQuantityString(1, 2) } returns "foo"
every { resources.getQuantityText(1, 2) } returns "foo"
}

assertEquals(
Expand All @@ -28,11 +29,15 @@ internal class QuantityStringResourceDelegateTest {
@Test
fun `invoke should return string with format args`() {
val context = mockk<Context> {
every { resources.getQuantityString(1, 2, 3, "4") } returns "foo"
every { resources.getQuantityText(1, 2) } returns "foo%d%s"
every { resources.configuration } returns mockk {
@Suppress("DEPRECATION")
locale = Locale.getDefault()
}
}

assertEquals(
"foo",
"foo34",
ResourceDelegate.quantityString(1, 2, arrayOf(3, "4")).invoke(context),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package xyz.tynn.astring
import android.content.Context
import io.mockk.every
import io.mockk.mockk
import java.util.Locale
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
Expand All @@ -16,7 +17,7 @@ internal class StringResourceDelegateTest {
@Test
fun `invoke should return string without format args`() {
val context = mockk<Context> {
every { getString(1) } returns "foo"
every { resources.getText(1) } returns StringBuilder("foo")
}

assertEquals(
Expand All @@ -28,11 +29,15 @@ internal class StringResourceDelegateTest {
@Test
fun `invoke should return string with format args`() {
val context = mockk<Context> {
every { getString(1, 2, "3") } returns "foo"
every { resources.getText(1) } returns "foo%d%s"
every { resources.configuration } returns mockk {
@Suppress("DEPRECATION")
locale = Locale.getDefault()
}
}

assertEquals(
"foo",
"foo23",
ResourceDelegate.string(1, arrayOf(2, "3")).invoke(context),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal class TextResourceDelegateTest {
@Test
fun `invoke should return text`() {
val context = mockk<Context> {
every { getText(1) } returns "foo"
every { resources.getText(1) } returns "foo"
}

assertEquals(
Expand Down
12 changes: 6 additions & 6 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[versions]
android = "8.1.2"
compose-compiler = "1.5.3"
kotlin = "1.9.10"
android = "8.1.4"
compose-compiler = "1.5.4"
kotlin = "1.9.20"

[plugins]
android = { id = "com.android.library", version.ref = "android" }
Expand All @@ -12,21 +12,21 @@ publish = "io.github.gradle-nexus.publish-plugin:1.3.0"

[libraries]
androidx-appcompat = "androidx.appcompat:appcompat:1.6.1"
androidx-compose-bom = "androidx.compose:compose-bom:2023.10.00"
androidx-compose-bom = "androidx.compose:compose-bom:2023.10.01"
androidx-compose-ui.module = "androidx.compose.ui:ui"
androidx-compose-ui-test.module = "androidx.compose.ui:ui-test-junit4"
androidx-core = "androidx.core:core:1.12.0"
androidx-core-ktx = "androidx.core:core-ktx:1.12.0"
androidx-databinding-common = { module = "androidx.databinding:databinding-common", version.ref = "android" }
androidx-fragment = "androidx.fragment:fragment:1.6.1"
androidx-fragment = "androidx.fragment:fragment:1.6.2"
androidx-test-core = "androidx.test:core:1.5.0"
androidx-test-runner = "androidx.test:runner:1.5.2"
google-material = "com.google.android.material:material:1.10.0"
junit = "junit:junit:4.13.2"
kotlin-stdlib.module = "org.jetbrains.kotlin:kotlin-stdlib"
kotlin-test.module = "org.jetbrains.kotlin:kotlin-test-junit"
mockk = "io.mockk:mockk:1.13.8"
robolectric = "org.robolectric:robolectric:4.10.3"
robolectric = "org.robolectric:robolectric:4.11.1"
slf4j = "org.slf4j:slf4j-nop:2.0.9"

[bundles]
Expand Down

0 comments on commit 5499f35

Please sign in to comment.