Use Compose's existing method to get plural string (#8194)

This commit is contained in:
Ivan Iskandar 2022-10-14 09:21:19 +07:00 committed by GitHub
parent 26a42ba9c0
commit 02b3718aa1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 9 additions and 40 deletions

View File

@ -14,9 +14,9 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.pluralStringResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import eu.kanade.presentation.util.quantityStringResource
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.util.system.getResourceColor
@ -37,7 +37,7 @@ fun ChapterHeader(
text = if (chapterCount == null) {
stringResource(R.string.chapters)
} else {
quantityStringResource(id = R.plurals.manga_num_chapters, quantity = chapterCount)
pluralStringResource(id = R.plurals.manga_num_chapters, count = chapterCount, chapterCount)
},
style = MaterialTheme.typography.titleMedium,
modifier = Modifier.weight(1f),

View File

@ -63,6 +63,7 @@ import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.layout.SubcomposeLayout
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.pluralStringResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
@ -76,7 +77,6 @@ import eu.kanade.domain.manga.model.Manga
import eu.kanade.presentation.components.MangaCover
import eu.kanade.presentation.components.TextButton
import eu.kanade.presentation.util.clickableNoIndication
import eu.kanade.presentation.util.quantityStringResource
import eu.kanade.presentation.util.secondaryItemAlpha
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.source.model.SManga
@ -184,7 +184,7 @@ fun MangaActionRow(
title = if (trackingCount == 0) {
stringResource(R.string.manga_tracking_tab)
} else {
quantityStringResource(id = R.plurals.num_trackers, quantity = trackingCount, trackingCount)
pluralStringResource(id = R.plurals.num_trackers, count = trackingCount, trackingCount)
},
icon = if (trackingCount == 0) Icons.Default.Sync else Icons.Default.Done,
color = if (trackingCount == 0) defaultActionButtonColor else MaterialTheme.colorScheme.primary,

View File

@ -16,13 +16,13 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.rememberVectorPainter
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.pluralStringResource
import androidx.compose.ui.res.stringResource
import eu.kanade.presentation.components.AppStateBanners
import eu.kanade.presentation.components.Divider
import eu.kanade.presentation.components.PreferenceRow
import eu.kanade.presentation.components.ScrollbarLazyColumn
import eu.kanade.presentation.components.SwitchPreference
import eu.kanade.presentation.util.quantityStringResource
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.ui.more.DownloadQueueState
import eu.kanade.tachiyomi.ui.more.MoreController
@ -86,9 +86,9 @@ fun MoreScreen(
stringResource(R.string.paused)
} else {
"${stringResource(R.string.paused)}${
quantityStringResource(
R.plurals.download_queue_summary,
pending,
pluralStringResource(
id = R.plurals.download_queue_summary,
count = pending,
pending,
)
}"
@ -96,7 +96,7 @@ fun MoreScreen(
}
is DownloadQueueState.Downloading -> {
val pending = (downloadQueueState as DownloadQueueState.Downloading).pending
quantityStringResource(R.plurals.download_queue_summary, pending, pending)
pluralStringResource(id = R.plurals.download_queue_summary, count = pending, pending)
}
},
painter = rememberVectorPainter(Icons.Outlined.GetApp),

View File

@ -2,9 +2,7 @@ package eu.kanade.presentation.util
import android.content.res.Resources
import androidx.annotation.DrawableRes
import androidx.annotation.PluralsRes
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.painter.BitmapPainter
@ -12,35 +10,6 @@ import androidx.compose.ui.platform.LocalContext
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.toBitmap
/**
* Load a quantity string resource.
*
* @param id the resource identifier
* @param quantity The number used to get the string for the current language's plural rules.
* @return the string data associated with the resource
*/
@Composable
@ReadOnlyComposable
fun quantityStringResource(@PluralsRes id: Int, quantity: Int): String {
val context = LocalContext.current
return context.resources.getQuantityString(id, quantity, quantity)
}
/**
* Load a quantity string resource with formatting.
*
* @param id the resource identifier
* @param quantity The number used to get the string for the current language's plural rules.
* @param formatArgs the format arguments
* @return the string data associated with the resource
*/
@Composable
@ReadOnlyComposable
fun quantityStringResource(@PluralsRes id: Int, quantity: Int, vararg formatArgs: Any): String {
val context = LocalContext.current
return context.resources.getQuantityString(id, quantity, *formatArgs)
}
/**
* Create a BitmapPainter from an drawable resource.
*