Skip to content

Commit

Permalink
Fix chapter names fallback (#1464)
Browse files Browse the repository at this point in the history
The chapter name used as a fallback evaluates to a long absolute path,
which is not easy to read. This commit shortens the name. See
#1447
  • Loading branch information
thezeroalpha authored Aug 26, 2022
1 parent 55586d1 commit b5d8fb8
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions scanner/src/main/kotlin/voice/app/scanner/MediaAnalyzer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package voice.app.scanner

import android.content.Context
import android.net.Uri
import androidx.documentfile.provider.DocumentFile
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.Json
import voice.data.MarkData
Expand Down Expand Up @@ -55,7 +56,7 @@ class MediaAnalyzer
return if (duration != null && duration > 0) {
Metadata(
duration = duration.seconds.inWholeMilliseconds,
chapterName = parsed.findTag(TagType.Title) ?: chapterNameFallback(uri),
chapterName = parsed.findTag(TagType.Title) ?: chapterNameFallback(uri, context),
author = parsed.findTag(TagType.Artist),
bookName = parsed.findTag(TagType.Album),
chapters = parsed.chapters.mapIndexed { index, metaDataChapter ->
Expand All @@ -80,8 +81,9 @@ class MediaAnalyzer
)
}

private fun chapterNameFallback(file: Uri): String {
val name = file.lastPathSegment ?: "Chapter"
private fun chapterNameFallback(uri: Uri, context: Context): String {
val file = DocumentFile.fromSingleUri(context, uri)
val name = file?.name ?: "Chapter"
return name.substringBeforeLast(".")
.trim()
.takeUnless { it.isEmpty() }
Expand Down

0 comments on commit b5d8fb8

Please sign in to comment.