add the notifcation text and settings

This commit is contained in:
Bnyro 2022-07-28 14:31:35 +02:00
parent 2ed9f2385e
commit 9756193f36
11 changed files with 191 additions and 41 deletions

View File

@ -6,16 +6,8 @@ import android.app.NotificationManager
import android.os.Build import android.os.Build
import android.os.StrictMode import android.os.StrictMode
import android.os.StrictMode.VmPolicy import android.os.StrictMode.VmPolicy
import androidx.work.Constraints
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.NetworkType
import androidx.work.PeriodicWorkRequest
import androidx.work.WorkManager
import com.github.libretube.preferences.PreferenceHelper import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.util.NotificationHelper import com.github.libretube.util.NotificationHelper
import com.github.libretube.util.NotificationWorker
import java.util.concurrent.TimeUnit
class MyApp : Application() { class MyApp : Application() {
override fun onCreate() { override fun onCreate() {

View File

@ -62,6 +62,13 @@ class MainSettings : PreferenceFragmentCompat() {
true true
} }
val notifications = findPreference<Preference>("notifications")
notifications?.setOnPreferenceClickListener {
val newFragment = NotificationSettings()
navigateToSettingsFragment(newFragment)
true
}
val advanced = findPreference<Preference>("advanced") val advanced = findPreference<Preference>("advanced")
advanced?.setOnPreferenceClickListener { advanced?.setOnPreferenceClickListener {
val newFragment = AdvancedSettings() val newFragment = AdvancedSettings()

View File

@ -0,0 +1,32 @@
package com.github.libretube.preferences
import android.os.Bundle
import androidx.preference.ListPreference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreferenceCompat
import com.github.libretube.R
import com.github.libretube.activities.SettingsActivity
import com.github.libretube.util.NotificationHelper
class NotificationSettings : PreferenceFragmentCompat() {
val TAG = "SettingsFragment"
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.notification_settings, rootKey)
val settingsActivity = activity as SettingsActivity
settingsActivity.changeTopBarText(getString(R.string.notifications))
val notificationsEnabled = findPreference<SwitchPreferenceCompat>(PreferenceKeys.NOTIFICATION_ENABLED)
notificationsEnabled?.setOnPreferenceChangeListener { _, _ ->
NotificationHelper.enqueueWork(requireContext())
true
}
val checkingFrequency = findPreference<ListPreference>(PreferenceKeys.CHECKING_FREQUENCY)
checkingFrequency?.setOnPreferenceChangeListener { _, _ ->
NotificationHelper.enqueueWork(requireContext())
true
}
}
}

View File

@ -246,6 +246,14 @@ object PreferenceHelper {
} }
} }
fun setLatestVideoId(videoId: String) {
setString(PreferenceKeys.LAST_STREAM_VIDEO_ID, videoId)
}
fun getLatestVideoId(): String {
return getString(PreferenceKeys.LAST_STREAM_VIDEO_ID, "")
}
private fun getDefaultSharedPreferences(context: Context): SharedPreferences { private fun getDefaultSharedPreferences(context: Context): SharedPreferences {
return PreferenceManager.getDefaultSharedPreferences(context) return PreferenceManager.getDefaultSharedPreferences(context)
} }

View File

@ -69,7 +69,8 @@ object PreferenceKeys {
* Notifications * Notifications
*/ */
const val NOTIFICATION_ENABLED = "notification_toggle" const val NOTIFICATION_ENABLED = "notification_toggle"
const val NOTIFICATION_DELAY = "notifcation_delay" const val CHECKING_FREQUENCY = "checking_frequency"
const val LAST_STREAM_VIDEO_ID = "last_stream_video_id"
/** /**
* Advanced * Advanced

View File

@ -6,62 +6,105 @@ import android.content.Intent
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat import androidx.core.app.NotificationManagerCompat
import androidx.work.Constraints import androidx.work.Constraints
import androidx.work.CoroutineWorker
import androidx.work.ExistingPeriodicWorkPolicy import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.NetworkType import androidx.work.NetworkType
import androidx.work.PeriodicWorkRequest import androidx.work.PeriodicWorkRequest
import androidx.work.WorkManager import androidx.work.WorkManager
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.activities.MainActivity import com.github.libretube.activities.MainActivity
import com.github.libretube.obj.StreamItem
import com.github.libretube.preferences.PreferenceHelper import com.github.libretube.preferences.PreferenceHelper
import kotlinx.coroutines.CoroutineScope import com.github.libretube.preferences.PreferenceKeys
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import java.util.stream.Stream
object NotificationHelper { object NotificationHelper {
fun enqueueWork( fun enqueueWork(
context: Context context: Context
) { ) {
val constraints = Constraints.Builder() PreferenceHelper.setContext(context)
.setRequiredNetworkType(NetworkType.CONNECTED) val notificationsEnabled = PreferenceHelper.getBoolean(
.build() PreferenceKeys.NOTIFICATION_ENABLED,
true
val myWorkBuilder = PeriodicWorkRequest.Builder(
NotificationWorker::class.java,
1,
TimeUnit.SECONDS
) )
.setConstraints(constraints)
val myWork = myWorkBuilder.build() val checkingFrequency = PreferenceHelper.getString(
WorkManager.getInstance(context) PreferenceKeys.CHECKING_FREQUENCY,
.enqueueUniquePeriodicWork( "60"
"NotificationService", ).toLong()
ExistingPeriodicWorkPolicy.REPLACE,
myWork val uniqueWorkName = "NotificationService"
if (notificationsEnabled) {
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
val myWorkBuilder = PeriodicWorkRequest.Builder(
NotificationWorker::class.java,
checkingFrequency,
TimeUnit.MINUTES
) )
.setConstraints(constraints)
val myWork = myWorkBuilder.build()
WorkManager.getInstance(context)
.enqueueUniquePeriodicWork(
uniqueWorkName,
ExistingPeriodicWorkPolicy.REPLACE,
myWork
)
} else {
WorkManager.getInstance(context)
.cancelUniqueWork(uniqueWorkName)
}
} }
/**
* check whether new streams are available in subscriptions
*/
fun checkForNewStreams(context: Context) { fun checkForNewStreams(context: Context) {
val token = PreferenceHelper.getToken() val token = PreferenceHelper.getToken()
var response: List<StreamItem>
runBlocking { runBlocking {
val task = async { val task = async {
RetrofitInstance.authApi.getFeed(token) RetrofitInstance.authApi.getFeed(token)
} }
response = task.await() val videoFeed = task.await()
} val lastSeenStreamId = PreferenceHelper.getLatestVideoId()
createNotification( val latestFeedStreamId = videoFeed[0].url?.replace("/watch?v=", "")
context, // first time notifications enabled
response[0].title.toString(), if (lastSeenStreamId == "") PreferenceHelper.setLatestVideoId(lastSeenStreamId)
response[0].uploaderName.toString() else if (lastSeenStreamId != latestFeedStreamId) {
) // get the index of the last user-seen video
var newStreamIndex = -1
videoFeed.forEachIndexed { index, stream ->
if (stream.url?.replace("/watch?v=", "") == lastSeenStreamId) {
newStreamIndex = index
}
}
val (title, description) = when (newStreamIndex) {
// only one new stream available
1 -> {
Pair(videoFeed[0].title, videoFeed[0].uploaderName)
}
else -> {
Pair(
// return the amount of new streams as title
context.getString(
R.string.new_streams_count,
newStreamIndex.toString()
),
// return the first few uploader as description
context.getString(
R.string.new_streams_by,
videoFeed[0].uploaderName + ", " + videoFeed[1].uploaderName + ", " + videoFeed[2].uploaderName
)
)
}
}
createNotification(context, title!!, description!!)
}
}
} }
fun createNotification(context: Context, title: String, description: String) { fun createNotification(context: Context, title: String, description: String) {
@ -82,7 +125,5 @@ object NotificationHelper {
// 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(2, builder.build()) notify(2, builder.build())
} }
} }
} }

View File

@ -0,0 +1,14 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:width="22dp"
android:height="24dp"
android:tint="?android:attr/colorControlNormal"
android:viewportWidth="18"
android:viewportHeight="20"
tools:ignore="VectorRaster">
<path
android:fillColor="#E6E1E5"
android:fillType="evenOdd"
android:pathData="M8.625,0H9.375C9.5821,0 9.75,0.1679 9.75,0.375C9.75,0.5821 9.9179,0.75 10.125,0.75C10.3321,0.75 10.5,0.9179 10.5,1.125V1.875C10.5,2.4963 11.0088,3.0566 11.5685,3.3265C12.4353,3.7446 13.5132,4.5489 14.0889,5.6503C14.2067,5.8758 14.25,6.1307 14.25,6.3852V12.0512C14.25,12.9156 14.8488,13.6151 15.5614,14.1044C15.8681,14.315 16.102,14.5525 16.3642,14.8462C16.4522,14.9448 16.5,15.0728 16.5,15.2049C16.5,15.506 16.256,15.75 15.955,15.75H2.051C1.7467,15.75 1.5,15.5033 1.5,15.199C1.5,15.0704 1.5445,14.9452 1.6287,14.848C1.8916,14.5448 2.1396,14.3062 2.4619,14.0946C3.1737,13.6273 3.75,12.9293 3.75,12.0778V6.3852C3.75,6.1307 3.7933,5.8758 3.9111,5.6503C4.4868,4.5489 5.5648,3.7446 6.4316,3.3265C6.9912,3.0566 7.5,2.4963 7.5,1.875V1.125C7.5,0.9179 7.6679,0.75 7.875,0.75C8.0821,0.75 8.25,0.5821 8.25,0.375C8.25,0.1679 8.4179,0 8.625,0ZM9,19.4998C7.7574,19.4998 6.7501,18.4925 6.75,17.2499H11.25C11.2499,18.4925 10.2426,19.4998 9,19.4998ZM0.0558,7.5132C-0.1246,6.2857 0.1356,5.034 0.7902,3.9801C1.4447,2.9262 2.4513,2.1382 3.6315,1.7557L3.9552,2.7546C3.0111,3.0605 2.2058,3.691 1.6821,4.5341C1.1585,5.3772 0.9503,6.3786 1.0946,7.3605L0.0558,7.5132ZM17.1808,3.934C16.5153,2.8869 15.5006,2.1094 14.3165,1.7392L14.0032,2.7414C14.9504,3.0375 15.7623,3.6596 16.2946,4.4972C16.827,5.3348 17.0455,6.334 16.9115,7.3174L17.9519,7.4592C18.1194,6.2299 17.8463,4.981 17.1808,3.934Z"
tools:ignore="VectorPath" />
</vector>

View File

@ -760,4 +760,22 @@
<item>worst</item> <item>worst</item>
</string-array> </string-array>
<string-array name="checkingFrequency">
<item>15 minutes</item>
<item>30 minutes</item>
<item>60 minutes</item>
<item>2 hours</item>
<item>6 hours</item>
<item>12 hours</item>
</string-array>
<string-array name="checkingFrequencyValues">
<item>15</item>
<item>30</item>
<item>60</item>
<item>120</item>
<item>360</item>
<item>720</item>
</string-array>
</resources> </resources>

View File

@ -265,4 +265,10 @@
<string name="best_quality">Best quality</string> <string name="best_quality">Best quality</string>
<string name="worst_quality">Worst quality</string> <string name="worst_quality">Worst quality</string>
<string name="default_subtitle_language">Default subtitle language</string> <string name="default_subtitle_language">Default subtitle language</string>
<string name="notifications">Notifications</string>
<string name="notify_new_streams">New streams notifications</string>
<string name="notify_new_streams_summary">Notify when new streams from subscriptions are available</string>
<string name="checking_frequency">Checking frequency</string>
<string name="new_streams_count">%1$s new streams are available</string>
<string name="new_streams_by">New streams by %1$s …</string>
</resources> </resources>

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory app:title="@string/notifications">
<SwitchPreferenceCompat
android:icon="@drawable/ic_notification"
app:defaultValue="true"
app:key="notification_toggle"
app:title="@string/notify_new_streams"
android:summary="@string/notify_new_streams_summary"/>
<ListPreference
android:icon="@drawable/ic_time"
app:defaultValue="60"
app:entries="@array/checkingFrequency"
app:entryValues="@array/checkingFrequencyValues"
app:key="checking_frequency"
app:title="@string/checking_frequency"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>
</PreferenceScreen>

View File

@ -40,6 +40,12 @@
app:key="history" app:key="history"
app:title="@string/history" /> app:title="@string/history" />
<Preference
android:icon="@drawable/ic_notification"
android:summary="@string/notify_new_streams"
app:key="notifications"
app:title="@string/notifications" />
<Preference <Preference
android:icon="@drawable/ic_list" android:icon="@drawable/ic_list"
app:key="advanced" app:key="advanced"