Skip to content

Commit

Permalink
Refactored the chapter flattener and remove the inner function
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulWoitaschek committed Jul 19, 2017
1 parent a10987d commit 88959ab
Showing 1 changed file with 18 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,26 @@ import android.util.SparseArray

object MatroskaChapterFlattener {

fun toSparseArray(list: List<MatroskaChapter>, preferredLanguages: List<String>): SparseArray<String> {
val res = SparseArray<String>()

fun addChapter(chapters: List<MatroskaChapter>, depth: Int) {
chapters.forEachIndexed { i, chapter ->
val duration = (chapter.startTime / 1000000).toInt() +
if (i == 0) depth else 0
// Simple hack with adding depth is needed because chapter
// and it's first sub-chapter have usually the same starting time.
val name = "+ ".repeat(depth) + (chapter.getName(preferredLanguages) ?: "Chapter ${i + 1}")
res.put(duration, name)
addChapter(chapter.children, depth + 1)
}
}
private lateinit var target: SparseArray<String>
private lateinit var preferredLanguages: List<String>

@Synchronized
fun toSparseArray(list: List<MatroskaChapter>, preferredLanguages: List<String>): SparseArray<String> {
target = SparseArray()
this.preferredLanguages = preferredLanguages
addChapter(list, 0)
return target
}

return res
private fun addChapter(chapters: List<MatroskaChapter>, depth: Int) {
chapters.forEachIndexed { i, chapter ->
val duration = (chapter.startTime / 1000000).toInt() +
if (i == 0) depth else 0
// Simple hack with adding depth is needed because chapter
// and it's first sub-chapter have usually the same starting time.
val name = "+ ".repeat(depth) + (chapter.getName(preferredLanguages) ?: "Chapter ${i + 1}")
target.put(duration, name)
addChapter(chapter.children, depth + 1)
}
}
}

0 comments on commit 88959ab

Please sign in to comment.