Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dearrow for playing video and watch history #5983

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,6 @@ object SubscriptionHelper {
subscriptions.joinToString(",")
)
}
}.deArrow()
}
}
}
4 changes: 2 additions & 2 deletions app/src/main/java/com/github/libretube/api/obj/Streams.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import kotlinx.serialization.Serializable

@Serializable
data class Streams(
val title: String,
var title: String,
val description: String,

@Serializable(SafeInstantSerializer::class)
Expand All @@ -24,7 +24,7 @@ data class Streams(
val uploader: String,
val uploaderUrl: String,
val uploaderAvatar: String? = null,
val thumbnailUrl: String,
var thumbnailUrl: String,
val category: String,
val license: String = "YouTube licence",
val visibility: String = "public",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
private suspend fun loadFeed(subscriptionsViewModel: SubscriptionsViewModel) {
runSafely(
onSuccess = { videos -> feed.updateIfChanged(videos) },
ioBlock = { tryLoadFeed(subscriptionsViewModel) }
ioBlock = { tryLoadFeed(subscriptionsViewModel).deArrow().take(20) }

Check failure on line 88 in app/src/main/java/com/github/libretube/ui/models/HomeViewModel.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Missing trailing comma before ")" Raw Output: app/src/main/java/com/github/libretube/ui/models/HomeViewModel.kt:88:81: error: Missing trailing comma before ")" (standard:trailing-comma-on-call-site)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ class PlayerViewModel : ViewModel() {
if (isOrientationChangeInProgress && streamsInfo != null) return@withContext streamsInfo to null

streamsInfo = try {
RetrofitInstance.api.getStreams(videoId).apply {
relatedStreams = relatedStreams.deArrow()
}
RetrofitInstance.api.getStreams(videoId).deArrow(videoId)
} catch (e: IOException) {
return@withContext null to context.getString(R.string.unknown_error)
} catch (e: HttpException) {
Expand Down
76 changes: 54 additions & 22 deletions app/src/main/java/com/github/libretube/util/DeArrowUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.github.libretube.api.obj.ContentItem
import com.github.libretube.api.obj.DeArrowContent
import com.github.libretube.api.obj.StreamItem
import com.github.libretube.api.obj.Streams
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.extensions.toID
import com.github.libretube.helpers.PreferenceHelper
Expand All @@ -19,26 +20,58 @@
return newTitle to newThumbnail
}

private suspend fun fetchDeArrowContent(videoIds: List<String>): Map<String, DeArrowContent>? {
val videoIdsString = videoIds.mapTo(TreeSet()) { it }.joinToString(",")

return try {
RetrofitInstance.api.getDeArrowContent(videoIdsString)
} catch (e: Exception) {
Log.e(this::class.java.name, e.toString())
null
}
}

/**
* Apply the new titles and thumbnails generated by DeArrow to the streams item
*/
suspend fun deArrowStreams(streams: Streams, vidId: String): Streams {

Check failure on line 37 in app/src/main/java/com/github/libretube/util/DeArrowUtil.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Newline expected after opening parenthesis Raw Output: app/src/main/java/com/github/libretube/util/DeArrowUtil.kt:37:32: error: Newline expected after opening parenthesis (standard:function-signature)

Check failure on line 37 in app/src/main/java/com/github/libretube/util/DeArrowUtil.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Parameter should start on a newline Raw Output: app/src/main/java/com/github/libretube/util/DeArrowUtil.kt:37:50: error: Parameter should start on a newline (standard:function-signature)

Check failure on line 37 in app/src/main/java/com/github/libretube/util/DeArrowUtil.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Newline expected before closing parenthesis Raw Output: app/src/main/java/com/github/libretube/util/DeArrowUtil.kt:37:63: error: Newline expected before closing parenthesis (standard:function-signature)
if (!PreferenceHelper.getBoolean(PreferenceKeys.DEARROW, false)) return streams

val videoIds = listOf(vidId) + streams.relatedStreams.map { it.url!!.toID() }
val response = fetchDeArrowContent(videoIds) ?: return streams

for ((videoId, data) in response.entries) {
val (newTitle, newThumbnail) = extractTitleAndThumbnail(data)

if (videoId == vidId) {
if (newTitle != null) streams.title = newTitle
if (newThumbnail != null) streams.thumbnailUrl = newThumbnail
} else {
val streamItem = streams.relatedStreams

Check failure on line 50 in app/src/main/java/com/github/libretube/util/DeArrowUtil.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 A multiline expression should start on a new line Raw Output: app/src/main/java/com/github/libretube/util/DeArrowUtil.kt:50:34: error: A multiline expression should start on a new line (standard:multiline-expression-wrapping)
.firstOrNull { it.url?.toID() == videoId } ?: continue

if (newTitle != null) streamItem.title = newTitle
if (newThumbnail != null) streamItem.thumbnail = newThumbnail
}
}

return streams
}

/**
* Apply the new titles and thumbnails generated by DeArrow to the stream items
*/
suspend fun deArrowStreamItems(streamItems: List<StreamItem>): List<StreamItem> {
if (!PreferenceHelper.getBoolean(PreferenceKeys.DEARROW, false)) return streamItems

val videoIds = streamItems.mapNotNullTo(TreeSet()) { it.url?.toID() }
.joinToString(",")
val response = fetchDeArrowContent(streamItems.map{ it.url!!.toID() }) ?: return streamItems

Check failure on line 67 in app/src/main/java/com/github/libretube/util/DeArrowUtil.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Missing spacing before "{" Raw Output: app/src/main/java/com/github/libretube/util/DeArrowUtil.kt:67:59: error: Missing spacing before "{" (standard:curly-spacing)

val response = try {
RetrofitInstance.api.getDeArrowContent(videoIds)
} catch (e: Exception) {
Log.e(this::class.java.name, e.toString())
return streamItems
}
for ((videoId, data) in response.entries) {
val (newTitle, newThumbnail) = extractTitleAndThumbnail(data)
val streamItem = streamItems.firstOrNull { it.url?.toID() == videoId }
newTitle?.let { streamItem?.title = newTitle }
newThumbnail?.let { streamItem?.thumbnail = newThumbnail }
val streamItem = streamItems.firstOrNull { it.url?.toID() == videoId } ?: continue

if (newTitle != null) streamItem.title = newTitle
if (newThumbnail != null) streamItem.thumbnail = newThumbnail
}
return streamItems
}
Expand All @@ -50,22 +83,18 @@
if (!PreferenceHelper.getBoolean(PreferenceKeys.DEARROW, false)) return contentItems

val videoIds = contentItems.filter { it.type == "stream" }
.mapTo(TreeSet()) { it.url.toID() }
.joinToString(",")
.map { it.url.toID() }

if (videoIds.isEmpty()) return contentItems

val response = try {
RetrofitInstance.api.getDeArrowContent(videoIds)
} catch (e: Exception) {
Log.e(this::class.java.name, e.toString())
return contentItems
}
val response = fetchDeArrowContent(videoIds) ?: return contentItems

for ((videoId, data) in response.entries) {
val (newTitle, newThumbnail) = extractTitleAndThumbnail(data)
val contentItem = contentItems.firstOrNull { it.url.toID() == videoId }
newTitle?.let { contentItem?.title = newTitle }
newThumbnail?.let { contentItem?.thumbnail = newThumbnail }
val contentItem = contentItems.firstOrNull { it.url.toID() == videoId } ?: continue

if (newTitle != null) { contentItem.title = newTitle }

Check failure on line 96 in app/src/main/java/com/github/libretube/util/DeArrowUtil.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 A single line if-statement should be kept simple. The 'THEN' may not be wrapped in a block. Raw Output: app/src/main/java/com/github/libretube/util/DeArrowUtil.kt:96:35: error: A single line if-statement should be kept simple. The 'THEN' may not be wrapped in a block. (standard:if-else-wrapping)

Check failure on line 96 in app/src/main/java/com/github/libretube/util/DeArrowUtil.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Missing newline after '{' Raw Output: app/src/main/java/com/github/libretube/util/DeArrowUtil.kt:96:37: error: Missing newline after '{' (standard:statement-wrapping)

Check failure on line 96 in app/src/main/java/com/github/libretube/util/DeArrowUtil.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Missing newline before '}' Raw Output: app/src/main/java/com/github/libretube/util/DeArrowUtil.kt:96:66: error: Missing newline before '}' (standard:statement-wrapping)
if (newThumbnail != null) { contentItem.thumbnail = newThumbnail }
}
return contentItems
}
Expand All @@ -84,3 +113,6 @@
*/
@JvmName("deArrowContentItems")
suspend fun List<ContentItem>.deArrow() = DeArrowUtil.deArrowContentItems(this)

@JvmName("deArrowStreams")
suspend fun Streams.deArrow(videoId: String) = DeArrowUtil.deArrowStreams(this, videoId)
Loading