mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-27 23:40:33 +05:30
Replace the break reminder with a sleep timer
This commit is contained in:
parent
6735e0c72d
commit
2bf5b6e6f7
@ -18,8 +18,8 @@ object PreferenceKeys {
|
||||
const val LANGUAGE = "language"
|
||||
const val REGION = "region"
|
||||
const val AUTO_ROTATION = "auto_rotation"
|
||||
const val BREAK_REMINDER_TOGGLE = "break_reminder_toggle"
|
||||
const val BREAK_REMINDER = "break_reminder"
|
||||
const val SLEEP_TIMER = "sleep_timer_toggle"
|
||||
const val SLEEP_TIMER_DELAY = "sleep_timer_delay"
|
||||
const val SAVE_FEED = "save_feed"
|
||||
const val NAVBAR_ITEMS = "navbar_items"
|
||||
const val START_FRAGMENT = "start_fragment"
|
||||
|
@ -35,7 +35,7 @@ import com.github.libretube.ui.fragments.PlayerFragment
|
||||
import com.github.libretube.ui.models.PlayerViewModel
|
||||
import com.github.libretube.ui.models.SearchViewModel
|
||||
import com.github.libretube.ui.models.SubscriptionsViewModel
|
||||
import com.github.libretube.ui.tools.BreakReminder
|
||||
import com.github.libretube.ui.tools.SleepTimer
|
||||
import com.github.libretube.util.NavBarHelper
|
||||
import com.github.libretube.util.NavigationHelper
|
||||
import com.github.libretube.util.NetworkHelper
|
||||
@ -132,13 +132,12 @@ class MainActivity : BaseActivity() {
|
||||
|
||||
binding.toolbar.title = ThemeHelper.getStyledAppName(this)
|
||||
|
||||
/**
|
||||
* handle error logs
|
||||
*/
|
||||
val log = PreferenceHelper.getErrorLog()
|
||||
if (log != "") ErrorDialog().show(supportFragmentManager, null)
|
||||
// handle error logs
|
||||
PreferenceHelper.getErrorLog().ifBlank { null }?.let {
|
||||
ErrorDialog().show(supportFragmentManager, null)
|
||||
}
|
||||
|
||||
BreakReminder.setupBreakReminder(applicationContext)
|
||||
SleepTimer.setup(this)
|
||||
|
||||
setupSubscriptionsBadge()
|
||||
|
||||
@ -157,11 +156,9 @@ class MainActivity : BaseActivity() {
|
||||
}
|
||||
|
||||
if (binding.mainMotionLayout.progress == 0F) {
|
||||
try {
|
||||
runCatching {
|
||||
minimizePlayer()
|
||||
return
|
||||
} catch (e: Exception) {
|
||||
// current fragment isn't the player fragment
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -30,21 +30,26 @@ class GeneralSettings : BasePreferenceFragment() {
|
||||
|
||||
val autoRotation = findPreference<SwitchPreferenceCompat>(PreferenceKeys.AUTO_ROTATION)
|
||||
autoRotation?.setOnPreferenceChangeListener { _, _ ->
|
||||
val restartDialog = RequireRestartDialog()
|
||||
restartDialog.show(childFragmentManager, RequireRestartDialog::class.java.name)
|
||||
RequireRestartDialog().show(childFragmentManager, RequireRestartDialog::class.java.name)
|
||||
true
|
||||
}
|
||||
|
||||
val breakReminder =
|
||||
findPreference<SwitchPreferenceCompat>(PreferenceKeys.BREAK_REMINDER_TOGGLE)
|
||||
val breakReminderTime = findPreference<EditTextPreference>(PreferenceKeys.BREAK_REMINDER)
|
||||
findPreference<SwitchPreferenceCompat>(PreferenceKeys.SLEEP_TIMER)
|
||||
val breakReminderTime = findPreference<EditTextPreference>(PreferenceKeys.SLEEP_TIMER_DELAY)
|
||||
breakReminderTime?.isEnabled = PreferenceHelper.getBoolean(
|
||||
PreferenceKeys.BREAK_REMINDER_TOGGLE,
|
||||
PreferenceKeys.SLEEP_TIMER,
|
||||
false
|
||||
)
|
||||
|
||||
breakReminder?.setOnPreferenceChangeListener { _, newValue ->
|
||||
breakReminderTime?.isEnabled = newValue as Boolean
|
||||
RequireRestartDialog().show(childFragmentManager, RequireRestartDialog::class.java.name)
|
||||
true
|
||||
}
|
||||
|
||||
breakReminderTime?.setOnPreferenceChangeListener { _, _ ->
|
||||
RequireRestartDialog().show(childFragmentManager, RequireRestartDialog::class.java.name)
|
||||
true
|
||||
}
|
||||
}
|
||||
|
@ -1,55 +0,0 @@
|
||||
package com.github.libretube.ui.tools
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.widget.Toast
|
||||
import com.github.libretube.R
|
||||
import com.github.libretube.constants.PreferenceKeys
|
||||
import com.github.libretube.util.PreferenceHelper
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
|
||||
object BreakReminder {
|
||||
/**
|
||||
* Show a break reminder when watched too long
|
||||
*/
|
||||
fun setupBreakReminder(context: Context) {
|
||||
if (!PreferenceHelper.getBoolean(
|
||||
PreferenceKeys.BREAK_REMINDER_TOGGLE,
|
||||
false
|
||||
)
|
||||
) {
|
||||
return
|
||||
}
|
||||
val breakReminderPref = PreferenceHelper.getString(
|
||||
PreferenceKeys.BREAK_REMINDER,
|
||||
"0"
|
||||
)
|
||||
if (!breakReminderPref.all { Character.isDigit(it) } ||
|
||||
breakReminderPref == "" || breakReminderPref == "0"
|
||||
) {
|
||||
return
|
||||
}
|
||||
Handler(Looper.getMainLooper()).postDelayed(
|
||||
{
|
||||
try {
|
||||
MaterialAlertDialogBuilder(context)
|
||||
.setTitle(R.string.take_a_break)
|
||||
.setMessage(
|
||||
context.getString(
|
||||
R.string.already_spent_time,
|
||||
breakReminderPref
|
||||
)
|
||||
)
|
||||
.setPositiveButton(R.string.okay, null)
|
||||
.show()
|
||||
} catch (e: Exception) {
|
||||
runCatching {
|
||||
Toast.makeText(context, R.string.take_a_break, Toast.LENGTH_LONG).show()
|
||||
}
|
||||
}
|
||||
},
|
||||
breakReminderPref.toLong() * 60 * 1000
|
||||
)
|
||||
}
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
package com.github.libretube.ui.tools
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import com.github.libretube.R
|
||||
import com.github.libretube.constants.PreferenceKeys
|
||||
import com.github.libretube.ui.activities.MainActivity
|
||||
import com.github.libretube.util.PreferenceHelper
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
|
||||
object SleepTimer {
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
private const val REACTION_INTERVAL = 5L
|
||||
|
||||
/**
|
||||
* Kill the app after showing a warning after a certain amount of time
|
||||
* @param context This must not be the applicationContext!
|
||||
*/
|
||||
fun setup(context: Context) {
|
||||
if (!PreferenceHelper.getBoolean(PreferenceKeys.SLEEP_TIMER, false)) return
|
||||
|
||||
val breakReminderPref = PreferenceHelper.getString(
|
||||
PreferenceKeys.SLEEP_TIMER_DELAY,
|
||||
""
|
||||
).ifEmpty { return }
|
||||
|
||||
handler.postDelayed(
|
||||
{
|
||||
var killApp = true
|
||||
val mainActivity = context as? MainActivity ?: return@postDelayed
|
||||
val snackBar = Snackbar.make(
|
||||
mainActivity.binding.root,
|
||||
R.string.take_a_break,
|
||||
Snackbar.LENGTH_INDEFINITE
|
||||
)
|
||||
.setAction(R.string.cancel) {
|
||||
killApp = false
|
||||
}
|
||||
snackBar.show()
|
||||
(0..REACTION_INTERVAL).forEach {
|
||||
handler.postDelayed({
|
||||
val remainingTime = " (${REACTION_INTERVAL - it})"
|
||||
snackBar.setText(context.getString(R.string.take_a_break) + remainingTime)
|
||||
}, it * 1000)
|
||||
}
|
||||
handler.postDelayed(
|
||||
killApp@{
|
||||
if (!killApp) return@killApp
|
||||
|
||||
// kill the application
|
||||
mainActivity.finishAffinity()
|
||||
mainActivity.finish()
|
||||
android.os.Process.killProcess(android.os.Process.myPid())
|
||||
},
|
||||
REACTION_INTERVAL * 1000
|
||||
)
|
||||
},
|
||||
breakReminderPref.toLong() * 60 * 1000
|
||||
)
|
||||
}
|
||||
}
|
@ -440,6 +440,7 @@
|
||||
<string name="audio_player">Audio player</string>
|
||||
<string name="audio_only_mode">Audio only mode</string>
|
||||
<string name="audio_only_mode_summary">Turn LibreTube into a music player.</string>
|
||||
<string name="sleep_timer">Sleep timer</string>
|
||||
|
||||
<!-- Notification channel strings -->
|
||||
<string name="download_channel_name">Download Service</string>
|
||||
|
@ -50,14 +50,14 @@
|
||||
<SwitchPreferenceCompat
|
||||
android:defaultValue="false"
|
||||
android:icon="@drawable/ic_notification"
|
||||
app:key="break_reminder_toggle"
|
||||
app:title="@string/break_reminder" />
|
||||
app:key="sleep_timer_toggle"
|
||||
app:title="@string/sleep_timer" />
|
||||
|
||||
<EditTextPreference
|
||||
android:enabled="false"
|
||||
android:icon="@drawable/ic_time"
|
||||
app:defaultValue="60"
|
||||
app:key="break_reminder"
|
||||
app:key="sleep_timer_delay"
|
||||
app:title="@string/break_reminder_time"
|
||||
app:useSimpleSummaryProvider="true" />
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user