Merge pull request #1165 from Bnyro/master

Slider and notification improvements
This commit is contained in:
Bnyro 2022-08-26 09:08:50 +02:00 committed by GitHub
commit 3f861ca074
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 91 additions and 37 deletions

View File

@ -39,10 +39,9 @@ const val PIPED_API_URL = "https://pipedapi.kavin.rocks/"
* Notification IDs * Notification IDs
*/ */
const val PLAYER_NOTIFICATION_ID = 1 const val PLAYER_NOTIFICATION_ID = 1
const val PUSH_NOTIFICATION_ID = 2 const val DOWNLOAD_PENDING_NOTIFICATION_ID = 2
const val DOWNLOAD_PENDING_NOTIFICATION_ID = 3 const val DOWNLOAD_FAILURE_NOTIFICATION_ID = 3
const val DOWNLOAD_FAILURE_NOTIFICATION_ID = 4 const val DOWNLOAD_SUCCESS_NOTIFICATION_ID = 4
const val DOWNLOAD_SUCCESS_NOTIFICATION_ID = 5
/** /**
* Notification Channel IDs * Notification Channel IDs

View File

@ -34,7 +34,7 @@ class MyApp : Application() {
/** /**
* Initialize the [PreferenceHelper] * Initialize the [PreferenceHelper]
*/ */
PreferenceHelper.setContext(applicationContext) PreferenceHelper.initialize(applicationContext)
/** /**
* Initialize the [DatabaseHolder] * Initialize the [DatabaseHolder]

View File

@ -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
}

View File

@ -7,6 +7,8 @@ import androidx.preference.Preference
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.databinding.DialogSliderBinding import com.github.libretube.databinding.DialogSliderBinding
import com.github.libretube.preferences.PreferenceHelper 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 import com.google.android.material.dialog.MaterialAlertDialogBuilder
/** /**
@ -19,13 +21,25 @@ class SliderPreference(
context, context,
attributeSet attributeSet
) { ) {
private lateinit var sliderBinding: DialogSliderBinding
override fun onClick() { override fun onClick() {
val sliderBinding = DialogSliderBinding.inflate( sliderBinding = DialogSliderBinding.inflate(
LayoutInflater.from(context) 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( sliderBinding.slider.value = PreferenceHelper.getString(
key, key,
"1.0" range.defaultValue.toString()
).toFloat() ).toFloat()
MaterialAlertDialogBuilder(context) MaterialAlertDialogBuilder(context)

View File

@ -55,6 +55,7 @@ import com.github.libretube.dialogs.ShareDialog
import com.github.libretube.extensions.BaseFragment import com.github.libretube.extensions.BaseFragment
import com.github.libretube.extensions.TAG import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.await import com.github.libretube.extensions.await
import com.github.libretube.extensions.setSliderRangeAndValue
import com.github.libretube.interfaces.DoubleTapInterface import com.github.libretube.interfaces.DoubleTapInterface
import com.github.libretube.interfaces.PlayerOptionsInterface import com.github.libretube.interfaces.PlayerOptionsInterface
import com.github.libretube.models.PlayerViewModel 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.obj.Streams
import com.github.libretube.preferences.PreferenceHelper import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.preferences.PreferenceKeys import com.github.libretube.preferences.PreferenceKeys
import com.github.libretube.preferences.PreferenceRanges
import com.github.libretube.services.BackgroundMode import com.github.libretube.services.BackgroundMode
import com.github.libretube.util.AutoPlayHelper import com.github.libretube.util.AutoPlayHelper
import com.github.libretube.util.BackgroundHelper import com.github.libretube.util.BackgroundHelper
@ -511,6 +513,9 @@ class PlayerFragment : BaseFragment() {
override fun onPlaybackSpeedClicked() { override fun onPlaybackSpeedClicked() {
val playbackSpeedBinding = DialogSliderBinding.inflate(layoutInflater) val playbackSpeedBinding = DialogSliderBinding.inflate(layoutInflater)
playbackSpeedBinding.slider.setSliderRangeAndValue(
PreferenceRanges.playbackSpeed
)
playbackSpeedBinding.slider.value = exoPlayer.playbackParameters.speed playbackSpeedBinding.slider.value = exoPlayer.playbackParameters.speed
// change playback speed dialog // change playback speed dialog
MaterialAlertDialogBuilder(requireContext()) MaterialAlertDialogBuilder(requireContext())
@ -599,7 +604,8 @@ class PlayerFragment : BaseFragment() {
context.getString(R.string.none) context.getString(R.string.none)
} }
// set the playback speed // set the playback speed
currentPlaybackSpeed = "${exoPlayer.playbackParameters.speed}x" currentPlaybackSpeed = "${exoPlayer.playbackParameters.speed.toString()
.replace(".0", "")}x"
// set the quality text // set the quality text
val isAdaptive = exoPlayer.videoFormat?.codecs != null val isAdaptive = exoPlayer.videoFormat?.codecs != null
val quality = exoPlayer.videoSize.height val quality = exoPlayer.videoSize.height

View File

@ -0,0 +1,8 @@
package com.github.libretube.obj
data class SliderRange(
val valueFrom: Float,
val valueTo: Float,
val stepSize: Float,
val defaultValue: Float
)

View File

@ -23,7 +23,7 @@ object PreferenceHelper {
/** /**
* set the context that is being used to access the shared preferences * set the context that is being used to access the shared preferences
*/ */
fun setContext(context: Context) { fun initialize(context: Context) {
settings = getDefaultSharedPreferences(context) settings = getDefaultSharedPreferences(context)
editor = settings.edit() editor = settings.edit()

View File

@ -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
)
}

View File

@ -1,5 +1,6 @@
package com.github.libretube.util package com.github.libretube.util
import android.app.NotificationManager
import android.app.PendingIntent import android.app.PendingIntent
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
@ -25,7 +26,10 @@ import java.util.concurrent.TimeUnit
class NotificationHelper( class NotificationHelper(
private val context: Context 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 * Enqueue the work manager task
@ -34,7 +38,7 @@ class NotificationHelper(
existingPeriodicWorkPolicy: ExistingPeriodicWorkPolicy existingPeriodicWorkPolicy: ExistingPeriodicWorkPolicy
) { ) {
// get the notification preferences // get the notification preferences
PreferenceHelper.setContext(context) PreferenceHelper.initialize(context)
val notificationsEnabled = PreferenceHelper.getBoolean( val notificationsEnabled = PreferenceHelper.getBoolean(
PreferenceKeys.NOTIFICATION_ENABLED, PreferenceKeys.NOTIFICATION_ENABLED,
true true
@ -90,7 +94,7 @@ class NotificationHelper(
* check whether new streams are available in subscriptions * check whether new streams are available in subscriptions
*/ */
fun checkForNewStreams(): Boolean { fun checkForNewStreams(): Boolean {
var result = true var success = true
val token = PreferenceHelper.getToken() val token = PreferenceHelper.getToken()
runBlocking { runBlocking {
@ -107,7 +111,7 @@ class NotificationHelper(
val videoFeed = try { val videoFeed = try {
task.await() task.await()
} catch (e: Exception) { } catch (e: Exception) {
result = false success = false
return@runBlocking return@runBlocking
} }
@ -135,9 +139,10 @@ class NotificationHelper(
val channelGroups = newVideos.groupBy { it.uploaderUrl } val channelGroups = newVideos.groupBy { it.uploaderUrl }
// create a notification for each new stream // create a notification for each new stream
channelGroups.forEach { (_, streams) -> channelGroups.forEach { (_, streams) ->
createGroupSummaryNotification( createNotification(
group = streams[0].uploaderUrl.toID(), group = streams[0].uploaderUrl.toID(),
uploaderName = streams[0].uploaderName.toString() title = streams[0].uploaderName.toString(),
isSummary = true
) )
streams.forEach { streamItem -> 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 whether the work succeeded
return result return success
} }
/** /**
@ -159,8 +166,9 @@ class NotificationHelper(
*/ */
private fun createNotification( private fun createNotification(
title: String, title: String,
description: String, group: String,
group: String description: String? = null,
isSummary: Boolean = false
) { ) {
val intent = Intent(context, MainActivity::class.java).apply { val intent = Intent(context, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
@ -174,33 +182,22 @@ class NotificationHelper(
val builder = NotificationCompat.Builder(context, PUSH_CHANNEL_ID) val builder = NotificationCompat.Builder(context, PUSH_CHANNEL_ID)
.setContentTitle(title) .setContentTitle(title)
.setContentText(description)
.setGroup(group) .setGroup(group)
.setSmallIcon(R.drawable.ic_notification) .setSmallIcon(R.drawable.ic_notification)
.setPriority(NotificationCompat.PRIORITY_DEFAULT) .setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Set the intent that will fire when the user taps the notification // Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent) .setContentIntent(pendingIntent)
.setAutoCancel(true) .setAutoCancel(true)
if (isSummary) {
builder.setGroupSummary(true)
} else {
builder.setContentText(description)
}
with(NotificationManagerCompat.from(context)) { with(NotificationManagerCompat.from(context)) {
// notificationId is a unique int for each notification that you must define // notificationId is a unique int for each notification that you must define
notify(notificationId, builder.build()) 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)
}
}
} }

View File

@ -9,8 +9,9 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp" android:layout_marginHorizontal="10dp"
android:layout_marginTop="10dp" android:layout_marginTop="10dp"
android:stepSize="0.25"
android:value="1" android:value="1"
android:valueFrom="0.25" android:valueFrom="0"
android:valueTo="5" /> android:valueTo="5" />
</LinearLayout> </LinearLayout>