From 59385b0c734bb78305c3521db28b2792320b9be9 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Mon, 29 Jan 2024 05:27:39 +0530 Subject: [PATCH] refactor: Avoid boxing for segment pair --- app/build.gradle.kts | 1 + .../java/com/github/libretube/api/obj/Segment.kt | 7 +++++-- .../com/github/libretube/helpers/PlayerHelper.kt | 15 +++++---------- .../libretube/ui/dialogs/VoteForSegmentDialog.kt | 9 ++++----- .../libretube/ui/fragments/PlayerFragment.kt | 2 +- .../github/libretube/ui/views/MarkableTimeBar.kt | 2 +- gradle/libs.versions.toml | 2 ++ 7 files changed, 19 insertions(+), 19 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 071352e67..12c91eec7 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -98,6 +98,7 @@ dependencies { implementation(libs.androidx.navigation.ui) implementation(libs.androidx.preference) implementation(libs.androidx.work.runtime) + implementation(libs.androidx.collection) /* Android Lifecycle */ implementation(libs.lifecycle.viewmodel) diff --git a/app/src/main/java/com/github/libretube/api/obj/Segment.kt b/app/src/main/java/com/github/libretube/api/obj/Segment.kt index 7e122cade..74a871dff 100644 --- a/app/src/main/java/com/github/libretube/api/obj/Segment.kt +++ b/app/src/main/java/com/github/libretube/api/obj/Segment.kt @@ -1,7 +1,9 @@ package com.github.libretube.api.obj +import androidx.collection.FloatFloatPair import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable +import kotlinx.serialization.Transient @Serializable data class Segment( @@ -10,11 +12,12 @@ data class Segment( val category: String? = null, val description: String? = null, val locked: Int? = null, - val segment: List = listOf(), + private val segment: List = listOf(), val userID: String? = null, val videoDuration: Double? = null, val votes: Int? = null, var skipped: Boolean = false ) { - val segmentStartAndEnd = segment[0] to segment[1] + @Transient + val segmentStartAndEnd = FloatFloatPair(segment[0], segment[1]) } diff --git a/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt b/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt index c260a902f..9434d6301 100644 --- a/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt @@ -550,11 +550,9 @@ object PlayerHelper { if ((duration - currentPosition).absoluteValue < 500) continue if (currentPosition in segmentStart until segmentEnd) { - if (sponsorBlockConfig[segment.category] == SbSkipOptions.AUTOMATIC || - ( - sponsorBlockConfig[segment.category] == SbSkipOptions.AUTOMATIC_ONCE && - !segment.skipped - ) + val key = sponsorBlockConfig[segment.category] + if (key == SbSkipOptions.AUTOMATIC || + (key == SbSkipOptions.AUTOMATIC_ONCE && !segment.skipped) ) { if (sponsorBlockNotifications) { runCatching { @@ -564,11 +562,8 @@ object PlayerHelper { } seekTo(segmentEnd) segment.skipped = true - } else if (sponsorBlockConfig[segment.category] == SbSkipOptions.MANUAL || - ( - sponsorBlockConfig[segment.category] == SbSkipOptions.AUTOMATIC_ONCE && - segment.skipped - ) + } else if (key == SbSkipOptions.MANUAL || + (key == SbSkipOptions.AUTOMATIC_ONCE && segment.skipped) ) { return segment } diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/VoteForSegmentDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/VoteForSegmentDialog.kt index 8e2dabcfd..728bd6c20 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/VoteForSegmentDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/VoteForSegmentDialog.kt @@ -98,11 +98,10 @@ class VoteForSegmentDialog : DialogFragment() { } binding.segmentsDropdown.items = segments.map { - "${it.category} (${ - DateUtils.formatElapsedTime(it.segmentStartAndEnd.first.toLong()) - } - ${ - DateUtils.formatElapsedTime(it.segmentStartAndEnd.second.toLong()) - })" + val (start, end) = it.segmentStartAndEnd + val (startStr, endStr) = DateUtils.formatElapsedTime(start.toLong()) to + DateUtils.formatElapsedTime(end.toLong()) + "${it.category} ($startStr - $endStr)" } } } diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index 4bba4b69a..125b94b00 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -1142,7 +1142,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { val durationWithoutSegments = duration - segments.sumOf { val (start, end) = it.segmentStartAndEnd - end - start + end.toDouble() - start.toDouble() }.toLong() val durationString = DateUtils.formatElapsedTime(duration) diff --git a/app/src/main/java/com/github/libretube/ui/views/MarkableTimeBar.kt b/app/src/main/java/com/github/libretube/ui/views/MarkableTimeBar.kt index 66d34eade..5649c03a6 100644 --- a/app/src/main/java/com/github/libretube/ui/views/MarkableTimeBar.kt +++ b/app/src/main/java/com/github/libretube/ui/views/MarkableTimeBar.kt @@ -67,7 +67,7 @@ class MarkableTimeBar( canvas.restore() } - private fun Double.toLength(): Int { + private fun Float.toLength(): Int { return (this * 1000 / player!!.duration * length).toInt() } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index b61d1c23d..9bb671b76 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -30,6 +30,7 @@ benchmarkMacroJunit4 = "1.2.3" baselineprofile = "1.2.3" profileinstaller = "1.3.1" paging = "3.2.1" +collection = "1.4.0" [libraries] androidx-activity = { group = "androidx.activity", name = "activity-ktx", version.ref = "activity" } @@ -74,6 +75,7 @@ androidx-uiautomator = { group = "androidx.test.uiautomator", name = "uiautomato androidx-benchmark-macro-junit4 = { group = "androidx.benchmark", name = "benchmark-macro-junit4", version.ref = "benchmarkMacroJunit4" } androidx-profileinstaller = { group = "androidx.profileinstaller", name = "profileinstaller", version.ref = "profileinstaller" } androidx-paging = { group = "androidx.paging", name = "paging-runtime-ktx", version.ref = "paging" } +androidx-collection = { group = "androidx.collection", name = "collection", version.ref = "collection" } [plugins] androidTest = { id = "com.android.test", version.ref = "gradle" }