From 69983108097c5999317196fcc4a7bcf96bda838a Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 26 Aug 2022 08:42:13 +0200 Subject: [PATCH 1/3] bug fixes --- .../java/com/github/libretube/Constants.kt | 7 ++- .../main/java/com/github/libretube/MyApp.kt | 2 +- .../libretube/preferences/PreferenceHelper.kt | 2 +- .../libretube/util/NotificationHelper.kt | 48 ++++++++----------- app/src/main/res/layout/dialog_slider.xml | 1 + 5 files changed, 27 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/com/github/libretube/Constants.kt b/app/src/main/java/com/github/libretube/Constants.kt index 05c68ed41..9cb03468f 100644 --- a/app/src/main/java/com/github/libretube/Constants.kt +++ b/app/src/main/java/com/github/libretube/Constants.kt @@ -39,10 +39,9 @@ const val PIPED_API_URL = "https://pipedapi.kavin.rocks/" * Notification IDs */ const val PLAYER_NOTIFICATION_ID = 1 -const val PUSH_NOTIFICATION_ID = 2 -const val DOWNLOAD_PENDING_NOTIFICATION_ID = 3 -const val DOWNLOAD_FAILURE_NOTIFICATION_ID = 4 -const val DOWNLOAD_SUCCESS_NOTIFICATION_ID = 5 +const val DOWNLOAD_PENDING_NOTIFICATION_ID = 2 +const val DOWNLOAD_FAILURE_NOTIFICATION_ID = 3 +const val DOWNLOAD_SUCCESS_NOTIFICATION_ID = 4 /** * Notification Channel IDs diff --git a/app/src/main/java/com/github/libretube/MyApp.kt b/app/src/main/java/com/github/libretube/MyApp.kt index 8f214834d..140298946 100644 --- a/app/src/main/java/com/github/libretube/MyApp.kt +++ b/app/src/main/java/com/github/libretube/MyApp.kt @@ -34,7 +34,7 @@ class MyApp : Application() { /** * Initialize the [PreferenceHelper] */ - PreferenceHelper.setContext(applicationContext) + PreferenceHelper.initialize(applicationContext) /** * Initialize the [DatabaseHolder] diff --git a/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt b/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt index 2bdbc6af0..a80148b67 100644 --- a/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt +++ b/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt @@ -23,7 +23,7 @@ object PreferenceHelper { /** * set the context that is being used to access the shared preferences */ - fun setContext(context: Context) { + fun initialize(context: Context) { settings = getDefaultSharedPreferences(context) editor = settings.edit() diff --git a/app/src/main/java/com/github/libretube/util/NotificationHelper.kt b/app/src/main/java/com/github/libretube/util/NotificationHelper.kt index e8c0df861..5b81cc838 100644 --- a/app/src/main/java/com/github/libretube/util/NotificationHelper.kt +++ b/app/src/main/java/com/github/libretube/util/NotificationHelper.kt @@ -1,5 +1,6 @@ package com.github.libretube.util +import android.app.NotificationManager import android.app.PendingIntent import android.content.Context import android.content.Intent @@ -25,7 +26,10 @@ import java.util.concurrent.TimeUnit class NotificationHelper( private val context: Context ) { - private var notificationId = 1 + val NotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + + // the id where notification channels start + private var notificationId = NotificationManager.activeNotifications.size + 5 /** * Enqueue the work manager task @@ -34,7 +38,7 @@ class NotificationHelper( existingPeriodicWorkPolicy: ExistingPeriodicWorkPolicy ) { // get the notification preferences - PreferenceHelper.setContext(context) + PreferenceHelper.initialize(context) val notificationsEnabled = PreferenceHelper.getBoolean( PreferenceKeys.NOTIFICATION_ENABLED, true @@ -90,7 +94,7 @@ class NotificationHelper( * check whether new streams are available in subscriptions */ fun checkForNewStreams(): Boolean { - var result = true + var success = true val token = PreferenceHelper.getToken() runBlocking { @@ -107,7 +111,7 @@ class NotificationHelper( val videoFeed = try { task.await() } catch (e: Exception) { - result = false + success = false return@runBlocking } @@ -135,9 +139,10 @@ class NotificationHelper( val channelGroups = newVideos.groupBy { it.uploaderUrl } // create a notification for each new stream channelGroups.forEach { (_, streams) -> - createGroupSummaryNotification( + createNotification( group = streams[0].uploaderUrl.toID(), - uploaderName = streams[0].uploaderName.toString() + title = streams[0].uploaderName.toString(), + isSummary = true ) streams.forEach { streamItem -> @@ -149,9 +154,11 @@ class NotificationHelper( ) } } + // save the latest streams that got notified about + PreferenceHelper.setLatestVideoId(videoFeed[0].url.toID()) } // return whether the work succeeded - return result + return success } /** @@ -159,8 +166,9 @@ class NotificationHelper( */ private fun createNotification( title: String, - description: String, - group: String + group: String, + description: String? = null, + isSummary: Boolean = false ) { val intent = Intent(context, MainActivity::class.java).apply { flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK @@ -174,33 +182,19 @@ class NotificationHelper( val builder = NotificationCompat.Builder(context, PUSH_CHANNEL_ID) .setContentTitle(title) - .setContentText(description) .setGroup(group) .setSmallIcon(R.drawable.ic_notification) .setPriority(NotificationCompat.PRIORITY_DEFAULT) // Set the intent that will fire when the user taps the notification .setContentIntent(pendingIntent) .setAutoCancel(true) + + if (isSummary) builder.setGroupSummary(true) + else builder.setContentText(description) + with(NotificationManagerCompat.from(context)) { // notificationId is a unique int for each notification that you must define notify(notificationId, builder.build()) } } - - private fun createGroupSummaryNotification( - uploaderName: String, - group: String - ) { - val summaryNotification = NotificationCompat.Builder(context, PUSH_CHANNEL_ID) - .setContentTitle(uploaderName) - .setSmallIcon(R.drawable.ic_notification) - .setGroup(group) - .setGroupSummary(true) - .build() - - with(NotificationManagerCompat.from(context)) { - // notificationId is a unique int for each notification that you must define - notify(notificationId, summaryNotification) - } - } } diff --git a/app/src/main/res/layout/dialog_slider.xml b/app/src/main/res/layout/dialog_slider.xml index 7e190af0f..fab99a531 100644 --- a/app/src/main/res/layout/dialog_slider.xml +++ b/app/src/main/res/layout/dialog_slider.xml @@ -9,6 +9,7 @@ android:layout_height="wrap_content" android:layout_marginHorizontal="10dp" android:layout_marginTop="10dp" + android:stepSize="0.25" android:value="1" android:valueFrom="0.25" android:valueTo="5" /> From f9cdd2da5fbb43787ed0ec672d7c2b918884ee43 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 26 Aug 2022 09:04:35 +0200 Subject: [PATCH 2/3] code improvements --- .../extensions/SetSliderRangeAndValue.kt | 14 ++++++++++++++ .../libretube/extensions/SliderPreference.kt | 18 ++++++++++++++++-- .../libretube/fragments/PlayerFragment.kt | 5 +++++ .../com/github/libretube/obj/SliderRange.kt | 8 ++++++++ .../libretube/preferences/PreferenceRanges.kt | 15 +++++++++++++++ .../libretube/util/NotificationHelper.kt | 7 +++++-- app/src/main/res/layout/dialog_slider.xml | 2 +- 7 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/com/github/libretube/extensions/SetSliderRangeAndValue.kt create mode 100644 app/src/main/java/com/github/libretube/obj/SliderRange.kt create mode 100644 app/src/main/java/com/github/libretube/preferences/PreferenceRanges.kt diff --git a/app/src/main/java/com/github/libretube/extensions/SetSliderRangeAndValue.kt b/app/src/main/java/com/github/libretube/extensions/SetSliderRangeAndValue.kt new file mode 100644 index 000000000..2ff773c82 --- /dev/null +++ b/app/src/main/java/com/github/libretube/extensions/SetSliderRangeAndValue.kt @@ -0,0 +1,14 @@ +package com.github.libretube.extensions + +import com.github.libretube.obj.SliderRange +import com.google.android.material.slider.Slider + +/** + * set the range of the slider preference + */ +fun Slider.setSliderRangeAndValue(range: SliderRange) { + this.valueFrom = range.valueFrom + this.valueTo = range.valueTo + this.stepSize = range.stepSize + this.value = range.defaultValue +} diff --git a/app/src/main/java/com/github/libretube/extensions/SliderPreference.kt b/app/src/main/java/com/github/libretube/extensions/SliderPreference.kt index 0ff432fc0..c37fb9dc7 100644 --- a/app/src/main/java/com/github/libretube/extensions/SliderPreference.kt +++ b/app/src/main/java/com/github/libretube/extensions/SliderPreference.kt @@ -7,6 +7,8 @@ import androidx.preference.Preference import com.github.libretube.R import com.github.libretube.databinding.DialogSliderBinding import com.github.libretube.preferences.PreferenceHelper +import com.github.libretube.preferences.PreferenceKeys +import com.github.libretube.preferences.PreferenceRanges import com.google.android.material.dialog.MaterialAlertDialogBuilder /** @@ -19,13 +21,25 @@ class SliderPreference( context, attributeSet ) { + private lateinit var sliderBinding: DialogSliderBinding + override fun onClick() { - val sliderBinding = DialogSliderBinding.inflate( + sliderBinding = DialogSliderBinding.inflate( LayoutInflater.from(context) ) + val range = when (key) { + PreferenceKeys.PLAYBACK_SPEED -> PreferenceRanges.playbackSpeed + PreferenceKeys.BACKGROUND_PLAYBACK_SPEED -> PreferenceRanges.playbackSpeed + else -> null + } + + if (range == null) return + + sliderBinding.slider.setSliderRangeAndValue(range) + sliderBinding.slider.value = PreferenceHelper.getString( key, - "1.0" + range.defaultValue.toString() ).toFloat() MaterialAlertDialogBuilder(context) diff --git a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt index 16dd19e3b..5526499d4 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -55,6 +55,7 @@ import com.github.libretube.dialogs.ShareDialog import com.github.libretube.extensions.BaseFragment import com.github.libretube.extensions.TAG import com.github.libretube.extensions.await +import com.github.libretube.extensions.setSliderRangeAndValue import com.github.libretube.interfaces.DoubleTapInterface import com.github.libretube.interfaces.PlayerOptionsInterface import com.github.libretube.models.PlayerViewModel @@ -64,6 +65,7 @@ import com.github.libretube.obj.Segments import com.github.libretube.obj.Streams import com.github.libretube.preferences.PreferenceHelper import com.github.libretube.preferences.PreferenceKeys +import com.github.libretube.preferences.PreferenceRanges import com.github.libretube.services.BackgroundMode import com.github.libretube.util.AutoPlayHelper import com.github.libretube.util.BackgroundHelper @@ -511,6 +513,9 @@ class PlayerFragment : BaseFragment() { override fun onPlaybackSpeedClicked() { val playbackSpeedBinding = DialogSliderBinding.inflate(layoutInflater) + playbackSpeedBinding.slider.setSliderRangeAndValue( + PreferenceRanges.playbackSpeed + ) playbackSpeedBinding.slider.value = exoPlayer.playbackParameters.speed // change playback speed dialog MaterialAlertDialogBuilder(requireContext()) diff --git a/app/src/main/java/com/github/libretube/obj/SliderRange.kt b/app/src/main/java/com/github/libretube/obj/SliderRange.kt new file mode 100644 index 000000000..3802ebecf --- /dev/null +++ b/app/src/main/java/com/github/libretube/obj/SliderRange.kt @@ -0,0 +1,8 @@ +package com.github.libretube.obj + +data class SliderRange( + val valueFrom: Float, + val valueTo: Float, + val stepSize: Float, + val defaultValue: Float +) diff --git a/app/src/main/java/com/github/libretube/preferences/PreferenceRanges.kt b/app/src/main/java/com/github/libretube/preferences/PreferenceRanges.kt new file mode 100644 index 000000000..36fffda11 --- /dev/null +++ b/app/src/main/java/com/github/libretube/preferences/PreferenceRanges.kt @@ -0,0 +1,15 @@ +package com.github.libretube.preferences + +import com.github.libretube.obj.SliderRange + +/** + * Stores the ranges for the [SliderPreference] + */ +object PreferenceRanges { + val playbackSpeed = SliderRange( + 0.25f, + 6.0f, + 0.25f, + 1.0f + ) +} diff --git a/app/src/main/java/com/github/libretube/util/NotificationHelper.kt b/app/src/main/java/com/github/libretube/util/NotificationHelper.kt index 5b81cc838..1f83f9c72 100644 --- a/app/src/main/java/com/github/libretube/util/NotificationHelper.kt +++ b/app/src/main/java/com/github/libretube/util/NotificationHelper.kt @@ -189,8 +189,11 @@ class NotificationHelper( .setContentIntent(pendingIntent) .setAutoCancel(true) - if (isSummary) builder.setGroupSummary(true) - else builder.setContentText(description) + if (isSummary) { + builder.setGroupSummary(true) + } else { + builder.setContentText(description) + } with(NotificationManagerCompat.from(context)) { // notificationId is a unique int for each notification that you must define diff --git a/app/src/main/res/layout/dialog_slider.xml b/app/src/main/res/layout/dialog_slider.xml index fab99a531..05f3bd91f 100644 --- a/app/src/main/res/layout/dialog_slider.xml +++ b/app/src/main/res/layout/dialog_slider.xml @@ -11,7 +11,7 @@ android:layout_marginTop="10dp" android:stepSize="0.25" android:value="1" - android:valueFrom="0.25" + android:valueFrom="0" android:valueTo="5" /> \ No newline at end of file From 7581985705d92b48f4cc70f3ffdc3319e6806bbe Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 26 Aug 2022 09:08:29 +0200 Subject: [PATCH 3/3] minor improvements --- .../main/java/com/github/libretube/fragments/PlayerFragment.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt index 5526499d4..b519833e0 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -604,7 +604,8 @@ class PlayerFragment : BaseFragment() { context.getString(R.string.none) } // set the playback speed - currentPlaybackSpeed = "${exoPlayer.playbackParameters.speed}x" + currentPlaybackSpeed = "${exoPlayer.playbackParameters.speed.toString() + .replace(".0", "")}x" // set the quality text val isAdaptive = exoPlayer.videoFormat?.codecs != null val quality = exoPlayer.videoSize.height