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
*/
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

View File

@ -34,7 +34,7 @@ class MyApp : Application() {
/**
* Initialize the [PreferenceHelper]
*/
PreferenceHelper.setContext(applicationContext)
PreferenceHelper.initialize(applicationContext)
/**
* 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.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)

View File

@ -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())
@ -599,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

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
*/
fun setContext(context: Context) {
fun initialize(context: Context) {
settings = getDefaultSharedPreferences(context)
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
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,22 @@ 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)
}
}
}

View File

@ -9,8 +9,9 @@
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:valueFrom="0"
android:valueTo="5" />
</LinearLayout>