Tweak code

This commit is contained in:
vfsfitvnm
2022-10-03 12:52:24 +02:00
parent 5f5a763675
commit 0ac516b39b
16 changed files with 699 additions and 1085 deletions

View File

@@ -1,24 +1,25 @@
package it.vfsfitvnm.youtubemusic.utils
import io.ktor.utils.io.CancellationException
import it.vfsfitvnm.youtubemusic.Innertube
import it.vfsfitvnm.youtubemusic.models.SectionListRenderer
internal fun SectionListRenderer.findSectionByTitle(text: String): SectionListRenderer.Content? {
return contents?.find { content ->
val title = content
.musicCarouselShelfRenderer
?.header
?.musicCarouselShelfBasicHeaderRenderer
val title = content
.musicCarouselShelfRenderer
?.header
?.musicCarouselShelfBasicHeaderRenderer
?.title
?: content
.musicShelfRenderer
?.title
?: content
.musicShelfRenderer
?.title
title
?.runs
?.firstOrNull()
?.text == text
}
title
?.runs
?.firstOrNull()
?.text == text
}
}
internal fun SectionListRenderer.findSectionByStrapline(text: String): SectionListRenderer.Content? {
@@ -31,14 +32,19 @@ internal fun SectionListRenderer.findSectionByStrapline(text: String): SectionLi
?.runs
?.firstOrNull()
?.text == text
}
}
}
internal inline fun <R> runCatchingNonCancellable(block: () -> R): Result<R>? {
return Result.success(block())
// val result = runCatching(block)
// return when (val ex = result.exceptionOrNull()) {
// is CancellationException -> null
// else -> result
// }
val result = runCatching(block)
return when (result.exceptionOrNull()) {
is CancellationException -> null
else -> result
}
}
infix operator fun <T : Innertube.Item> Innertube.ItemsPage<T>?.plus(other: Innertube.ItemsPage<T>) =
other.copy(
items = this?.items?.plus(other.items ?: emptyList())?.distinctBy(Innertube.Item::key)
?: other.items
)