Refactor duplicate chapter number formatters

This commit is contained in:
arkon 2023-07-23 18:09:08 -04:00
parent 7aa172c512
commit 2556e9f08c
6 changed files with 28 additions and 36 deletions

View File

@ -21,12 +21,11 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import eu.kanade.presentation.manga.components.MangaCover
import eu.kanade.presentation.util.formatChapterNumber
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.util.lang.toTimestampString
import tachiyomi.domain.history.model.HistoryWithRelations
import tachiyomi.presentation.core.components.material.padding
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
private val HISTORY_ITEM_HEIGHT = 96.dp
@ -68,7 +67,7 @@ fun HistoryItem(
text = if (history.chapterNumber > -1) {
stringResource(
R.string.recent_manga_time,
chapterFormatter.format(history.chapterNumber),
formatChapterNumber(history.chapterNumber),
readAt,
)
} else {
@ -88,8 +87,3 @@ fun HistoryItem(
}
}
}
private val chapterFormatter = DecimalFormat(
"#.###",
DecimalFormatSymbols().apply { decimalSeparator = '.' },
)

View File

@ -57,12 +57,12 @@ import eu.kanade.presentation.manga.components.MangaBottomActionMenu
import eu.kanade.presentation.manga.components.MangaChapterListItem
import eu.kanade.presentation.manga.components.MangaInfoBox
import eu.kanade.presentation.manga.components.MangaToolbar
import eu.kanade.presentation.util.formatChapterNumber
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.download.model.Download
import eu.kanade.tachiyomi.source.getNameForMangaInfo
import eu.kanade.tachiyomi.ui.manga.ChapterItem
import eu.kanade.tachiyomi.ui.manga.MangaScreenState
import eu.kanade.tachiyomi.ui.manga.chapterDecimalFormat
import eu.kanade.tachiyomi.util.lang.toRelativeString
import eu.kanade.tachiyomi.util.system.copyToClipboard
import tachiyomi.domain.chapter.model.Chapter
@ -722,7 +722,7 @@ private fun LazyListScope.sharedChapterItems(
title = if (manga.displayMode == Manga.CHAPTER_DISPLAY_NUMBER) {
stringResource(
R.string.display_mode_chapter,
chapterDecimalFormat.format(chapterItem.chapter.chapterNumber.toDouble()),
formatChapterNumber(chapterItem.chapter.chapterNumber),
)
} else {
chapterItem.chapter.name

View File

@ -0,0 +1,13 @@
package eu.kanade.presentation.util
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
private val formatter = DecimalFormat(
"#.###",
DecimalFormatSymbols().apply { decimalSeparator = '.' },
)
fun formatChapterNumber(chapterNumber: Float): String {
return formatter.format(chapterNumber)
}

View File

@ -13,6 +13,7 @@ import androidx.core.app.NotificationManagerCompat
import coil.imageLoader
import coil.request.ImageRequest
import coil.transform.CircleCropTransformation
import eu.kanade.presentation.util.formatChapterNumber
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.core.security.SecurityPreferences
import eu.kanade.tachiyomi.data.download.Downloader
@ -29,8 +30,6 @@ import tachiyomi.core.util.lang.launchUI
import tachiyomi.domain.chapter.model.Chapter
import tachiyomi.domain.manga.model.Manga
import uy.kohesive.injekt.injectLazy
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
class LibraryUpdateNotifier(private val context: Context) {
@ -279,16 +278,10 @@ class LibraryUpdateNotifier(private val context: Context) {
}
private fun getNewChaptersDescription(chapters: Array<Chapter>): String {
val formatter = DecimalFormat(
"#.###",
DecimalFormatSymbols()
.apply { decimalSeparator = '.' },
)
val displayableChapterNumbers = chapters
.filter { it.isRecognizedNumber }
.sortedBy { it.chapterNumber }
.map { formatter.format(it.chapterNumber) }
.map { formatChapterNumber(it.chapterNumber) }
.toSet()
return when (displayableChapterNumbers.size) {

View File

@ -71,7 +71,7 @@ class MangaScreen(
val context = LocalContext.current
val haptic = LocalHapticFeedback.current
val scope = rememberCoroutineScope()
val screenModel = rememberScreenModel { MangaInfoScreenModel(context, mangaId, fromSource) }
val screenModel = rememberScreenModel { MangaScreenModel(context, mangaId, fromSource) }
val state by screenModel.state.collectAsState()
@ -136,7 +136,7 @@ class MangaScreen(
val onDismissRequest = { screenModel.dismissDialog() }
when (val dialog = successState.dialog) {
null -> {}
is MangaInfoScreenModel.Dialog.ChangeCategory -> {
is MangaScreenModel.Dialog.ChangeCategory -> {
ChangeCategoryDialog(
initialSelection = dialog.initialSelection,
onDismissRequest = onDismissRequest,
@ -146,7 +146,7 @@ class MangaScreen(
},
)
}
is MangaInfoScreenModel.Dialog.DeleteChapters -> {
is MangaScreenModel.Dialog.DeleteChapters -> {
DeleteChaptersDialog(
onDismissRequest = onDismissRequest,
onConfirm = {
@ -155,12 +155,12 @@ class MangaScreen(
},
)
}
is MangaInfoScreenModel.Dialog.DuplicateManga -> DuplicateMangaDialog(
is MangaScreenModel.Dialog.DuplicateManga -> DuplicateMangaDialog(
onDismissRequest = onDismissRequest,
onConfirm = { screenModel.toggleFavorite(onRemoved = {}, checkDuplicate = false) },
onOpenManga = { navigator.push(MangaScreen(dialog.duplicate.id)) },
)
MangaInfoScreenModel.Dialog.SettingsSheet -> ChapterSettingsDialog(
MangaScreenModel.Dialog.SettingsSheet -> ChapterSettingsDialog(
onDismissRequest = onDismissRequest,
manga = successState.manga,
onDownloadFilterChanged = screenModel::setDownloadedFilter,
@ -170,7 +170,7 @@ class MangaScreen(
onDisplayModeChanged = screenModel::setDisplayMode,
onSetAsDefault = screenModel::setCurrentSettingsAsDefault,
)
MangaInfoScreenModel.Dialog.TrackSheet -> {
MangaScreenModel.Dialog.TrackSheet -> {
NavigatorAdaptiveSheet(
screen = TrackInfoDialogHomeScreen(
mangaId = successState.manga.id,
@ -181,7 +181,7 @@ class MangaScreen(
onDismissRequest = onDismissRequest,
)
}
MangaInfoScreenModel.Dialog.FullCover -> {
MangaScreenModel.Dialog.FullCover -> {
val sm = rememberScreenModel { MangaCoverScreenModel(successState.manga.id) }
val manga by sm.state.collectAsState()
if (manga != null) {

View File

@ -74,10 +74,8 @@ import tachiyomi.domain.track.interactor.GetTracks
import tachiyomi.source.local.isLocal
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
class MangaInfoScreenModel(
class MangaScreenModel(
val context: Context,
val mangaId: Long,
private val isFromSource: Boolean,
@ -984,7 +982,7 @@ sealed class MangaScreenState {
val chapters: List<ChapterItem>,
val trackItems: List<TrackItem> = emptyList(),
val isRefreshingData: Boolean = false,
val dialog: MangaInfoScreenModel.Dialog? = null,
val dialog: MangaScreenModel.Dialog? = null,
val hasPromptedToAddBefore: Boolean = false,
) : MangaScreenState() {
@ -1025,9 +1023,3 @@ data class ChapterItem(
) {
val isDownloaded = downloadState == Download.State.DOWNLOADED
}
val chapterDecimalFormat = DecimalFormat(
"#.###",
DecimalFormatSymbols()
.apply { decimalSeparator = '.' },
)