2019-05-08 19:17:54 +02:00
|
|
|
package org.schabi.newpipe.settings
|
|
|
|
|
|
|
|
import android.content.SharedPreferences
|
|
|
|
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
|
|
|
|
import android.graphics.Color
|
|
|
|
import android.os.Bundle
|
|
|
|
import androidx.preference.Preference
|
|
|
|
import com.google.android.material.snackbar.Snackbar
|
|
|
|
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
|
|
|
|
import io.reactivex.rxjava3.disposables.Disposable
|
|
|
|
import org.schabi.newpipe.R
|
|
|
|
import org.schabi.newpipe.database.subscription.NotificationMode
|
|
|
|
import org.schabi.newpipe.database.subscription.SubscriptionEntity
|
|
|
|
import org.schabi.newpipe.error.ErrorActivity
|
|
|
|
import org.schabi.newpipe.error.ErrorInfo
|
|
|
|
import org.schabi.newpipe.error.UserAction
|
2021-07-20 12:20:51 +02:00
|
|
|
import org.schabi.newpipe.local.feed.notifications.NotificationHelper
|
|
|
|
import org.schabi.newpipe.local.feed.notifications.NotificationWorker
|
|
|
|
import org.schabi.newpipe.local.feed.notifications.ScheduleOptions
|
2019-05-08 19:17:54 +02:00
|
|
|
import org.schabi.newpipe.local.subscription.SubscriptionManager
|
|
|
|
|
|
|
|
class NotificationsSettingsFragment : BasePreferenceFragment(), OnSharedPreferenceChangeListener {
|
|
|
|
|
|
|
|
private var notificationWarningSnackbar: Snackbar? = null
|
|
|
|
private var loader: Disposable? = null
|
|
|
|
|
|
|
|
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
|
|
|
addPreferencesFromResource(R.xml.notifications_settings)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onStart() {
|
|
|
|
super.onStart()
|
|
|
|
defaultPreferences.registerOnSharedPreferenceChangeListener(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onStop() {
|
|
|
|
defaultPreferences.unregisterOnSharedPreferenceChangeListener(this)
|
|
|
|
super.onStop()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
|
|
|
|
val context = context ?: return
|
|
|
|
if (key == getString(R.string.streams_notifications_interval_key) || key == getString(R.string.streams_notifications_network_key)) {
|
|
|
|
NotificationWorker.schedule(context, ScheduleOptions.from(context), true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
2021-11-02 23:22:52 +01:00
|
|
|
|
2021-10-22 21:24:22 +02:00
|
|
|
val enabled = NotificationHelper.areNotificationsEnabledOnDevice(requireContext())
|
2019-05-08 19:17:54 +02:00
|
|
|
preferenceScreen.isEnabled = enabled
|
|
|
|
if (!enabled) {
|
|
|
|
if (notificationWarningSnackbar == null) {
|
|
|
|
notificationWarningSnackbar = Snackbar.make(
|
|
|
|
listView,
|
|
|
|
R.string.notifications_disabled,
|
|
|
|
Snackbar.LENGTH_INDEFINITE
|
|
|
|
).apply {
|
2021-10-22 21:24:22 +02:00
|
|
|
setAction(R.string.settings) {
|
2021-10-25 13:59:49 +02:00
|
|
|
NotificationHelper.openNewPipeSystemNotificationSettings(it.context)
|
2019-05-08 19:17:54 +02:00
|
|
|
}
|
|
|
|
setActionTextColor(Color.YELLOW)
|
|
|
|
addCallback(object : Snackbar.Callback() {
|
|
|
|
override fun onDismissed(transientBottomBar: Snackbar, event: Int) {
|
|
|
|
super.onDismissed(transientBottomBar, event)
|
|
|
|
notificationWarningSnackbar = null
|
|
|
|
}
|
|
|
|
})
|
|
|
|
show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
notificationWarningSnackbar?.dismiss()
|
|
|
|
notificationWarningSnackbar = null
|
|
|
|
}
|
2021-11-02 23:22:52 +01:00
|
|
|
|
|
|
|
// (Re-)Create loader
|
2019-05-08 19:17:54 +02:00
|
|
|
loader?.dispose()
|
|
|
|
loader = SubscriptionManager(requireContext())
|
|
|
|
.subscriptions()
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
.subscribe(this::updateSubscriptions, this::onError)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onPause() {
|
|
|
|
loader?.dispose()
|
|
|
|
loader = null
|
2021-11-02 23:22:52 +01:00
|
|
|
|
2019-05-08 19:17:54 +02:00
|
|
|
super.onPause()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun updateSubscriptions(subscriptions: List<SubscriptionEntity>) {
|
2021-09-04 14:28:11 +02:00
|
|
|
val notified = subscriptions.count { it.notificationMode != NotificationMode.DISABLED }
|
2019-05-08 19:17:54 +02:00
|
|
|
val preference = findPreference<Preference>(getString(R.string.streams_notifications_channels_key))
|
|
|
|
if (preference != null) {
|
|
|
|
preference.summary = preference.context.getString(
|
|
|
|
R.string.streams_notifications_channels_summary,
|
|
|
|
notified,
|
|
|
|
subscriptions.size
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun onError(e: Throwable) {
|
|
|
|
ErrorActivity.reportErrorInSnackbar(
|
|
|
|
this,
|
|
|
|
ErrorInfo(e, UserAction.SUBSCRIPTION_GET, "Get subscriptions list")
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|