增加一键定位, 修复当前列表点击切曲弹窗消失的问题
This commit is contained in:
parent
1f34785bde
commit
9f3f5a69af
10
.gitignore
vendored
10
.gitignore
vendored
@ -1,12 +1,10 @@
|
||||
*.iml
|
||||
.gradle
|
||||
/local.properties
|
||||
/.idea/caches
|
||||
/.idea/libraries
|
||||
/.idea/modules.xml
|
||||
/.idea/workspace.xml
|
||||
/.idea/navEditor.xml
|
||||
/.idea/assetWizardSettings.xml
|
||||
.idea/
|
||||
.superpowers/
|
||||
app/src/test/
|
||||
app/src/androidTest/
|
||||
.DS_Store
|
||||
/build
|
||||
/captures
|
||||
|
||||
@ -73,7 +73,7 @@ class MediaStoreMusicScannerRepository(
|
||||
config = PagingConfig(
|
||||
pageSize = MUSIC_PAGE_SIZE,
|
||||
prefetchDistance = MUSIC_PAGE_SIZE,
|
||||
enablePlaceholders = false
|
||||
enablePlaceholders = true
|
||||
),
|
||||
pagingSourceFactory = {
|
||||
when (sort) {
|
||||
@ -152,6 +152,15 @@ class MediaStoreMusicScannerRepository(
|
||||
ids.toLongArray()
|
||||
}
|
||||
|
||||
override suspend fun playbackQueueIndex(
|
||||
trackId: Long,
|
||||
query: String,
|
||||
sort: MusicLibrarySort
|
||||
): Int? {
|
||||
val index = playbackQueueIds(query, sort).indexOf(trackId)
|
||||
return index.takeIf { it >= 0 }
|
||||
}
|
||||
|
||||
override suspend fun playbackTrack(id: Long): PlaybackTrack? = withContext(Dispatchers.IO) {
|
||||
musicTrackDao.playbackTrackById(id)?.toDomain()
|
||||
}
|
||||
|
||||
@ -13,6 +13,7 @@ interface MusicScannerRepository {
|
||||
suspend fun loadCachedAudio(): List<ScannedAudioTrack>
|
||||
suspend fun loadLibrarySummary(query: String = ""): MusicLibrarySummary
|
||||
suspend fun playbackQueueIds(query: String, sort: MusicLibrarySort): LongArray
|
||||
suspend fun playbackQueueIndex(trackId: Long, query: String, sort: MusicLibrarySort): Int?
|
||||
suspend fun playbackTrack(id: Long): PlaybackTrack?
|
||||
suspend fun scanAudio(onProgress: (MusicScanProgress) -> Unit): List<ScannedAudioTrack>
|
||||
suspend fun scanFolder(
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package com.audio.vibe.ui.components
|
||||
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.Canvas
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.border
|
||||
@ -29,6 +30,7 @@ import androidx.compose.material.icons.filled.Mic
|
||||
import androidx.compose.material.icons.filled.MusicNote
|
||||
import androidx.compose.material.icons.filled.Person
|
||||
import androidx.compose.material.icons.filled.Sell
|
||||
import androidx.compose.material3.FloatingActionButton
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.Text
|
||||
@ -43,12 +45,14 @@ import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.StrokeCap
|
||||
import androidx.compose.ui.graphics.drawscope.Stroke
|
||||
import androidx.compose.ui.graphics.vector.ImageVector
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.Dp
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import com.audio.vibe.R
|
||||
import com.audio.vibe.ui.i18n.LocalAppStrings
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.sin
|
||||
@ -189,6 +193,28 @@ fun RoundIcon(
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun LocateCurrentTrackButton(
|
||||
onClick: () -> Unit,
|
||||
modifier: Modifier = Modifier,
|
||||
containerColor: Color = AudioVibeColors.Primary,
|
||||
size: Dp = 52.dp,
|
||||
iconSize: Dp = 28.dp
|
||||
) {
|
||||
FloatingActionButton(
|
||||
onClick = onClick,
|
||||
containerColor = containerColor,
|
||||
contentColor = Color.White,
|
||||
modifier = modifier.size(size)
|
||||
) {
|
||||
Image(
|
||||
painter = painterResource(R.drawable.img_locate_current_track),
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(iconSize)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun FeatureTile(
|
||||
title: String,
|
||||
|
||||
@ -13,6 +13,7 @@ import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
@ -22,6 +23,7 @@ import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.rememberLazyListState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Album
|
||||
@ -45,9 +47,11 @@ import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.derivedStateOf
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
@ -69,6 +73,7 @@ import com.audio.vibe.data.playback.AudioPlaybackService
|
||||
import com.audio.vibe.data.repository.MusicScannerRepositoryProvider
|
||||
import com.audio.vibe.domain.model.ScannedAudioTrack
|
||||
import com.audio.vibe.ui.components.AudioVibeColors
|
||||
import com.audio.vibe.ui.components.LocateCurrentTrackButton
|
||||
import com.audio.vibe.ui.components.PrimaryButton
|
||||
import com.audio.vibe.ui.components.RoundIcon
|
||||
import com.audio.vibe.ui.components.SecondaryButton
|
||||
@ -79,6 +84,7 @@ import com.audio.vibe.ui.i18n.LocalAppStrings
|
||||
import com.audio.vibe.ui.player.MiniPlayerBar
|
||||
import com.audio.vibe.ui.theme.AudioVibeTheme
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
@Composable
|
||||
@ -221,6 +227,8 @@ private fun MusicManagerContent(
|
||||
query = uiState.query,
|
||||
category = uiState.category,
|
||||
playingId = uiState.playingId,
|
||||
currentTrackId = uiState.miniPlayerState.currentId,
|
||||
locateTrackIndex = uiState.locateTrackIndex,
|
||||
onEvent = onEvent,
|
||||
onPlayTrack = onPlayTrack,
|
||||
toEditor = toEditor,
|
||||
@ -236,6 +244,8 @@ private fun MusicManagerContent(
|
||||
query = uiState.query,
|
||||
category = uiState.category,
|
||||
playingId = uiState.playingId,
|
||||
currentTrackId = uiState.miniPlayerState.currentId,
|
||||
locateTrackIndex = uiState.locateTrackIndex,
|
||||
onEvent = onEvent,
|
||||
onPlayTrack = onPlayTrack,
|
||||
toEditor = toEditor,
|
||||
@ -281,6 +291,8 @@ private fun LibrarySearchAndList(
|
||||
query: String,
|
||||
category: MusicCategory,
|
||||
playingId: Long?,
|
||||
currentTrackId: Long?,
|
||||
locateTrackIndex: Int?,
|
||||
onEvent: (MusicManagerEvent) -> Unit,
|
||||
onPlayTrack: (Long) -> Unit,
|
||||
toEditor: () -> Unit,
|
||||
@ -298,6 +310,8 @@ private fun LibrarySearchAndList(
|
||||
TrackList(
|
||||
tracks = tracks,
|
||||
playingId = playingId,
|
||||
currentTrackId = currentTrackId,
|
||||
locateTrackIndex = locateTrackIndex,
|
||||
onPlay = onPlayTrack,
|
||||
toEditor = toEditor,
|
||||
bottomPadding = bottomPadding
|
||||
@ -312,6 +326,8 @@ private fun ScanningLibrary(
|
||||
query: String,
|
||||
category: MusicCategory,
|
||||
playingId: Long?,
|
||||
currentTrackId: Long?,
|
||||
locateTrackIndex: Int?,
|
||||
onEvent: (MusicManagerEvent) -> Unit,
|
||||
onPlayTrack: (Long) -> Unit,
|
||||
toEditor: () -> Unit,
|
||||
@ -325,6 +341,8 @@ private fun ScanningLibrary(
|
||||
query = query,
|
||||
category = category,
|
||||
playingId = playingId,
|
||||
currentTrackId = currentTrackId,
|
||||
locateTrackIndex = locateTrackIndex,
|
||||
onEvent = onEvent,
|
||||
onPlayTrack = onPlayTrack,
|
||||
toEditor = toEditor,
|
||||
@ -530,14 +548,33 @@ private fun StatCard(
|
||||
private fun TrackList(
|
||||
tracks: LazyPagingItems<ScannedAudioTrack>,
|
||||
playingId: Long?,
|
||||
currentTrackId: Long?,
|
||||
locateTrackIndex: Int?,
|
||||
onPlay: (Long) -> Unit,
|
||||
toEditor: () -> Unit,
|
||||
bottomPadding: Dp = 0.dp
|
||||
) {
|
||||
val listState = rememberLazyListState()
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
val currentTrackVisible by remember(currentTrackId, tracks.itemCount) {
|
||||
derivedStateOf {
|
||||
currentTrackId != null &&
|
||||
listState.layoutInfo.visibleItemsInfo.any { itemInfo ->
|
||||
tracks.peek(itemInfo.index)?.id == currentTrackId
|
||||
}
|
||||
}
|
||||
}
|
||||
val canLocateCurrentTrack = currentTrackId != null &&
|
||||
locateTrackIndex != null &&
|
||||
locateTrackIndex in 0 until tracks.itemCount &&
|
||||
!currentTrackVisible
|
||||
|
||||
Box(modifier = Modifier.fillMaxSize()) {
|
||||
LazyColumn(
|
||||
state = listState,
|
||||
verticalArrangement = Arrangement.spacedBy(10.dp),
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
contentPadding = androidx.compose.foundation.layout.PaddingValues(bottom = bottomPadding)
|
||||
contentPadding = PaddingValues(bottom = bottomPadding)
|
||||
) {
|
||||
items(
|
||||
count = tracks.itemCount,
|
||||
@ -554,6 +591,62 @@ private fun TrackList(
|
||||
onPlay = { onPlay(track.id) },
|
||||
toEditor = toEditor,
|
||||
)
|
||||
} else {
|
||||
TrackPlaceholderRow()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (canLocateCurrentTrack) {
|
||||
LocateCurrentTrackButton(
|
||||
onClick = {
|
||||
coroutineScope.launch {
|
||||
listState.animateScrollToItem(locateTrackIndex)
|
||||
}
|
||||
},
|
||||
modifier = Modifier
|
||||
.align(Alignment.BottomEnd)
|
||||
.padding(end = 16.dp, bottom = bottomPadding + 16.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun TrackPlaceholderRow() {
|
||||
VibeCard(modifier = Modifier.fillMaxWidth()) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(14.dp)
|
||||
) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.size(48.dp)
|
||||
.clip(RoundedCornerShape(12.dp))
|
||||
.background(AudioVibeColors.Line)
|
||||
)
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.padding(start = 12.dp)
|
||||
.weight(1f)
|
||||
) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth(0.58f)
|
||||
.height(16.dp)
|
||||
.clip(RoundedCornerShape(8.dp))
|
||||
.background(AudioVibeColors.Line)
|
||||
)
|
||||
Spacer(Modifier.height(8.dp))
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth(0.42f)
|
||||
.height(12.dp)
|
||||
.clip(RoundedCornerShape(8.dp))
|
||||
.background(AudioVibeColors.Line)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -40,6 +40,8 @@ class MusicManagerViewModel(
|
||||
private var scanJob: Job? = null
|
||||
private var observedPlaybackController: PlaybackServiceController? = null
|
||||
private var playbackStateJob: Job? = null
|
||||
private var locateIndexJob: Job? = null
|
||||
private var locateIndexRequest: LocateIndexRequest? = null
|
||||
|
||||
init {
|
||||
loadCachedLibrary()
|
||||
@ -53,8 +55,12 @@ class MusicManagerViewModel(
|
||||
is MusicManagerEvent.QueryChanged -> {
|
||||
_uiState.update { it.copy(query = event.query) }
|
||||
refreshLibrarySummaryForQuery(event.query)
|
||||
refreshLocateTrackIndex()
|
||||
}
|
||||
is MusicManagerEvent.CategoryChanged -> {
|
||||
_uiState.update { it.copy(category = event.category) }
|
||||
refreshLocateTrackIndex()
|
||||
}
|
||||
is MusicManagerEvent.CategoryChanged -> _uiState.update { it.copy(category = event.category) }
|
||||
is MusicManagerEvent.PlayToggled -> togglePlayback(event.trackId)
|
||||
MusicManagerEvent.MiniPlayerPlayPauseToggled -> toggleMiniPlayer()
|
||||
MusicManagerEvent.MiniPlayerSkipNext -> skipNext()
|
||||
@ -92,6 +98,46 @@ class MusicManagerViewModel(
|
||||
miniPlayerState = MiniPlayerState.from(playbackState)
|
||||
)
|
||||
}
|
||||
refreshLocateTrackIndex()
|
||||
}
|
||||
}
|
||||
|
||||
private fun refreshLocateTrackIndex() {
|
||||
val state = _uiState.value
|
||||
val currentId = state.miniPlayerState.currentId
|
||||
if (currentId == null) {
|
||||
locateIndexRequest = null
|
||||
locateIndexJob?.cancel()
|
||||
locateIndexJob = null
|
||||
_uiState.update { it.copy(locateTrackIndex = null) }
|
||||
return
|
||||
}
|
||||
|
||||
val request = LocateIndexRequest(
|
||||
trackId = currentId,
|
||||
query = state.query,
|
||||
category = state.category
|
||||
)
|
||||
if (request == locateIndexRequest) return
|
||||
locateIndexRequest = request
|
||||
locateIndexJob?.cancel()
|
||||
locateIndexJob = viewModelScope.launch {
|
||||
val index = repository.playbackQueueIndex(
|
||||
trackId = request.trackId,
|
||||
query = request.query,
|
||||
sort = request.category.toLibrarySort()
|
||||
)
|
||||
_uiState.update { latest ->
|
||||
if (
|
||||
latest.miniPlayerState.currentId == request.trackId &&
|
||||
latest.query == request.query &&
|
||||
latest.category == request.category
|
||||
) {
|
||||
latest.copy(locateTrackIndex = index)
|
||||
} else {
|
||||
latest
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -244,6 +290,7 @@ class MusicManagerViewModel(
|
||||
it.copy(
|
||||
playingId = null,
|
||||
miniPlayerState = MiniPlayerState.Hidden,
|
||||
locateTrackIndex = null,
|
||||
query = "",
|
||||
libraryState = MusicLibraryState.Empty
|
||||
)
|
||||
@ -328,9 +375,16 @@ data class MusicManagerUiState(
|
||||
val category: MusicCategory = MusicCategory.Tracks,
|
||||
val playingId: Long? = null,
|
||||
val miniPlayerState: MiniPlayerState = MiniPlayerState.Hidden,
|
||||
val locateTrackIndex: Int? = null,
|
||||
val libraryState: MusicLibraryState = MusicLibraryState.Loading
|
||||
)
|
||||
|
||||
private data class LocateIndexRequest(
|
||||
val trackId: Long,
|
||||
val query: String,
|
||||
val category: MusicCategory
|
||||
)
|
||||
|
||||
sealed interface MusicLibraryState {
|
||||
data object Loading : MusicLibraryState
|
||||
|
||||
|
||||
@ -5,6 +5,7 @@ import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
@ -27,9 +28,11 @@ import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.rememberModalBottomSheetState
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.derivedStateOf
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
@ -39,6 +42,8 @@ import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import com.audio.vibe.ui.components.LocateCurrentTrackButton
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
@ -51,7 +56,15 @@ fun PlayerQueueSheet(
|
||||
val sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)
|
||||
val currentIndex = state.queueIndex ?: -1
|
||||
val listState = rememberLazyListState()
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
var didInitialScroll by remember { mutableStateOf(false) }
|
||||
val currentTrackVisible by remember(currentIndex, state.queueItems.size) {
|
||||
derivedStateOf {
|
||||
currentIndex in state.queueItems.indices &&
|
||||
listState.layoutInfo.visibleItemsInfo.any { it.index == currentIndex }
|
||||
}
|
||||
}
|
||||
val canLocateCurrentTrack = currentIndex in state.queueItems.indices && !currentTrackVisible
|
||||
|
||||
LaunchedEffect(currentIndex, state.queueItems.size, didInitialScroll) {
|
||||
if (!didInitialScroll && currentIndex in state.queueItems.indices) {
|
||||
@ -83,11 +96,15 @@ fun PlayerQueueSheet(
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
|
||||
LazyColumn(
|
||||
state = listState,
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.heightIn(max = 420.dp)
|
||||
) {
|
||||
LazyColumn(
|
||||
state = listState,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
contentPadding = PaddingValues(bottom = if (canLocateCurrentTrack) 68.dp else 0.dp)
|
||||
) {
|
||||
itemsIndexed(state.queueItems, key = { index, item -> "${index}-${item.id}" }) { index, item ->
|
||||
QueueTrackRow(
|
||||
@ -102,6 +119,23 @@ fun PlayerQueueSheet(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (canLocateCurrentTrack) {
|
||||
LocateCurrentTrackButton(
|
||||
onClick = {
|
||||
coroutineScope.launch {
|
||||
listState.animateScrollToItem(currentIndex)
|
||||
}
|
||||
},
|
||||
containerColor = QueueSheetColors.Action,
|
||||
size = 50.dp,
|
||||
iconSize = 27.dp,
|
||||
modifier = Modifier
|
||||
.align(Alignment.BottomEnd)
|
||||
.padding(end = 10.dp, bottom = 12.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -39,9 +39,7 @@ class PlayerViewModel(
|
||||
}
|
||||
|
||||
fun onPlayQueueItem(index: Int) {
|
||||
if (controller.playQueueIndex(index)) {
|
||||
_uiState.update { it.copy(showQueueSheet = false) }
|
||||
}
|
||||
controller.playQueueIndex(index)
|
||||
}
|
||||
|
||||
fun onSeekTo(positionMs: Long) {
|
||||
|
||||
BIN
app/src/main/res/drawable-nodpi/img_locate_current_track.png
Normal file
BIN
app/src/main/res/drawable-nodpi/img_locate_current_track.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 327 KiB |
@ -1,202 +0,0 @@
|
||||
package com.audio.vibe.common.text
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
import java.nio.charset.Charset
|
||||
import java.nio.charset.StandardCharsets
|
||||
|
||||
class MetadataTextRepairTest {
|
||||
@Test
|
||||
fun repairsJapaneseUtf8ReadAsLatin1() {
|
||||
val broken = mojibake("さくら", StandardCharsets.UTF_8, StandardCharsets.ISO_8859_1)
|
||||
|
||||
assertEquals("さくら", MetadataTextRepair.repair(broken))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun repairsKoreanUtf8ReadAsLatin1() {
|
||||
val broken = mojibake("안녕하세요", StandardCharsets.UTF_8, StandardCharsets.ISO_8859_1)
|
||||
|
||||
assertEquals("안녕하세요", MetadataTextRepair.repair(broken))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun repairsRussianWindows1251ReadAsLatin1() {
|
||||
val broken = mojibake("Привет", Charset.forName("windows-1251"), StandardCharsets.ISO_8859_1)
|
||||
|
||||
assertEquals("Привет", MetadataTextRepair.repair(broken))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun keepsAlreadyValidText() {
|
||||
assertEquals("Beyonce - Halo", MetadataTextRepair.repair("Beyonce - Halo"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun keepsJapaneseSongInfoWithoutMojibake() {
|
||||
val samples = listOf(
|
||||
"夜に駆ける",
|
||||
"残酷な天使のテーゼ",
|
||||
"世界に一つだけの花",
|
||||
"君の知らない物語",
|
||||
"紅蓮華",
|
||||
"春よ、来い",
|
||||
"海の声",
|
||||
"恋するフォーチュンクッキー",
|
||||
"Pretender - Official髭男dism",
|
||||
"打上花火",
|
||||
"ありがとう",
|
||||
"さよならエレジー"
|
||||
)
|
||||
|
||||
samples.assertRepairKeepsOriginal()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun keepsKoreanSongInfoWithoutMojibake() {
|
||||
val samples = listOf(
|
||||
"밤편지",
|
||||
"좋은 날",
|
||||
"봄날",
|
||||
"너의 모든 순간",
|
||||
"사랑했지만",
|
||||
"헤어지자 말해요",
|
||||
"첫눈처럼 너에게 가겠다",
|
||||
"어떻게 이별까지 사랑하겠어",
|
||||
"사건의 지평선",
|
||||
"Ditto - NewJeans",
|
||||
"퀸카",
|
||||
"너를 만나"
|
||||
)
|
||||
|
||||
samples.assertRepairKeepsOriginal()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun keepsRussianSongInfoWithoutMojibake() {
|
||||
val samples = listOf(
|
||||
"Звезда по имени Солнце",
|
||||
"Группа крови",
|
||||
"Кукушка",
|
||||
"Мой рок-н-ролл",
|
||||
"Позови меня с собой",
|
||||
"Сансара",
|
||||
"Небо славян",
|
||||
"Комета",
|
||||
"До скорой встречи",
|
||||
"Вахтерам",
|
||||
"Трава у дома",
|
||||
"Я свободен"
|
||||
)
|
||||
|
||||
samples.assertRepairKeepsOriginal()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun repairsJapaneseSongInfoMojibake() {
|
||||
val samples = listOf(
|
||||
"夜に駆ける",
|
||||
"残酷な天使のテーゼ",
|
||||
"世界に一つだけの花",
|
||||
"君の知らない物語",
|
||||
"紅蓮華",
|
||||
"春よ、来い",
|
||||
"海の声",
|
||||
"恋するフォーチュンクッキー",
|
||||
"打上花火",
|
||||
"ありがとう"
|
||||
)
|
||||
|
||||
samples.assertUtf8ReadAsLatin1Repairs()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun repairsKoreanSongInfoMojibake() {
|
||||
val samples = listOf(
|
||||
"밤편지",
|
||||
"좋은 날",
|
||||
"봄날",
|
||||
"너의 모든 순간",
|
||||
"사랑했지만",
|
||||
"헤어지자 말해요",
|
||||
"첫눈처럼 너에게 가겠다",
|
||||
"어떻게 이별까지 사랑하겠어",
|
||||
"사건의 지평선",
|
||||
"너를 만나"
|
||||
)
|
||||
|
||||
samples.assertUtf8ReadAsLatin1Repairs()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun repairsRussianSongInfoMojibake() {
|
||||
val samples = listOf(
|
||||
"Звезда по имени Солнце",
|
||||
"Группа крови",
|
||||
"Кукушка",
|
||||
"Мой рок-н-ролл",
|
||||
"Позови меня с собой",
|
||||
"Сансара",
|
||||
"Небо славян",
|
||||
"Комета",
|
||||
"До скорой встречи",
|
||||
"Я свободен"
|
||||
)
|
||||
|
||||
samples.forEach { sample ->
|
||||
val broken = mojibake(sample, Charset.forName("windows-1251"), StandardCharsets.ISO_8859_1)
|
||||
assertEquals(sample, MetadataTextRepair.repair(broken))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun keepsCommonGooglePlayLocaleSongInfoWithoutMojibake() {
|
||||
val samples = listOf(
|
||||
"月亮代表我的心",
|
||||
"晴天 - 周杰倫",
|
||||
"รักแรกพบ",
|
||||
"คิดถึงเธอ",
|
||||
"दिल से रे",
|
||||
"तुम ही हो",
|
||||
"يا ليل يا عين",
|
||||
"حبيبي يا نور العين",
|
||||
"Trái tim bên lề",
|
||||
"Có chắc yêu là đây",
|
||||
"Canción bonita",
|
||||
"Corazón partío",
|
||||
"Garota de Ipanema",
|
||||
"Águas de Março",
|
||||
"Ne me quitte pas",
|
||||
"Dernière danse",
|
||||
"Über den Wolken",
|
||||
"99 Luftballons",
|
||||
"Güzel günler",
|
||||
"Seni seviyorum",
|
||||
"Selamat tinggal",
|
||||
"Buwan - Juan Karlos"
|
||||
)
|
||||
|
||||
samples.assertRepairKeepsOriginal()
|
||||
}
|
||||
|
||||
private fun mojibake(
|
||||
value: String,
|
||||
realCharset: Charset,
|
||||
wrongCharset: Charset
|
||||
): String {
|
||||
return String(value.toByteArray(realCharset), wrongCharset)
|
||||
}
|
||||
|
||||
private fun List<String>.assertRepairKeepsOriginal() {
|
||||
forEach { sample ->
|
||||
assertEquals(sample, MetadataTextRepair.repair(sample))
|
||||
}
|
||||
}
|
||||
|
||||
private fun List<String>.assertUtf8ReadAsLatin1Repairs() {
|
||||
forEach { sample ->
|
||||
val broken = mojibake(sample, StandardCharsets.UTF_8, StandardCharsets.ISO_8859_1)
|
||||
assertEquals(sample, MetadataTextRepair.repair(broken))
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user