Replace the break reminder with a sleep timer

This commit is contained in:
Bnyro 2023-01-17 18:21:59 +01:00
parent 6735e0c72d
commit 2bf5b6e6f7
7 changed files with 85 additions and 75 deletions

View File

@ -18,8 +18,8 @@ object PreferenceKeys {
const val LANGUAGE = "language" const val LANGUAGE = "language"
const val REGION = "region" const val REGION = "region"
const val AUTO_ROTATION = "auto_rotation" const val AUTO_ROTATION = "auto_rotation"
const val BREAK_REMINDER_TOGGLE = "break_reminder_toggle" const val SLEEP_TIMER = "sleep_timer_toggle"
const val BREAK_REMINDER = "break_reminder" const val SLEEP_TIMER_DELAY = "sleep_timer_delay"
const val SAVE_FEED = "save_feed" const val SAVE_FEED = "save_feed"
const val NAVBAR_ITEMS = "navbar_items" const val NAVBAR_ITEMS = "navbar_items"
const val START_FRAGMENT = "start_fragment" const val START_FRAGMENT = "start_fragment"

View File

@ -35,7 +35,7 @@ import com.github.libretube.ui.fragments.PlayerFragment
import com.github.libretube.ui.models.PlayerViewModel import com.github.libretube.ui.models.PlayerViewModel
import com.github.libretube.ui.models.SearchViewModel import com.github.libretube.ui.models.SearchViewModel
import com.github.libretube.ui.models.SubscriptionsViewModel 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.NavBarHelper
import com.github.libretube.util.NavigationHelper import com.github.libretube.util.NavigationHelper
import com.github.libretube.util.NetworkHelper import com.github.libretube.util.NetworkHelper
@ -132,13 +132,12 @@ class MainActivity : BaseActivity() {
binding.toolbar.title = ThemeHelper.getStyledAppName(this) binding.toolbar.title = ThemeHelper.getStyledAppName(this)
/** // handle error logs
* handle error logs PreferenceHelper.getErrorLog().ifBlank { null }?.let {
*/ ErrorDialog().show(supportFragmentManager, null)
val log = PreferenceHelper.getErrorLog() }
if (log != "") ErrorDialog().show(supportFragmentManager, null)
BreakReminder.setupBreakReminder(applicationContext) SleepTimer.setup(this)
setupSubscriptionsBadge() setupSubscriptionsBadge()
@ -157,11 +156,9 @@ class MainActivity : BaseActivity() {
} }
if (binding.mainMotionLayout.progress == 0F) { if (binding.mainMotionLayout.progress == 0F) {
try { runCatching {
minimizePlayer() minimizePlayer()
return return
} catch (e: Exception) {
// current fragment isn't the player fragment
} }
} }

View File

@ -30,21 +30,26 @@ class GeneralSettings : BasePreferenceFragment() {
val autoRotation = findPreference<SwitchPreferenceCompat>(PreferenceKeys.AUTO_ROTATION) val autoRotation = findPreference<SwitchPreferenceCompat>(PreferenceKeys.AUTO_ROTATION)
autoRotation?.setOnPreferenceChangeListener { _, _ -> autoRotation?.setOnPreferenceChangeListener { _, _ ->
val restartDialog = RequireRestartDialog() RequireRestartDialog().show(childFragmentManager, RequireRestartDialog::class.java.name)
restartDialog.show(childFragmentManager, RequireRestartDialog::class.java.name)
true true
} }
val breakReminder = val breakReminder =
findPreference<SwitchPreferenceCompat>(PreferenceKeys.BREAK_REMINDER_TOGGLE) findPreference<SwitchPreferenceCompat>(PreferenceKeys.SLEEP_TIMER)
val breakReminderTime = findPreference<EditTextPreference>(PreferenceKeys.BREAK_REMINDER) val breakReminderTime = findPreference<EditTextPreference>(PreferenceKeys.SLEEP_TIMER_DELAY)
breakReminderTime?.isEnabled = PreferenceHelper.getBoolean( breakReminderTime?.isEnabled = PreferenceHelper.getBoolean(
PreferenceKeys.BREAK_REMINDER_TOGGLE, PreferenceKeys.SLEEP_TIMER,
false false
) )
breakReminder?.setOnPreferenceChangeListener { _, newValue -> breakReminder?.setOnPreferenceChangeListener { _, newValue ->
breakReminderTime?.isEnabled = newValue as Boolean breakReminderTime?.isEnabled = newValue as Boolean
RequireRestartDialog().show(childFragmentManager, RequireRestartDialog::class.java.name)
true
}
breakReminderTime?.setOnPreferenceChangeListener { _, _ ->
RequireRestartDialog().show(childFragmentManager, RequireRestartDialog::class.java.name)
true true
} }
} }

View File

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

View File

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

View File

@ -440,6 +440,7 @@
<string name="audio_player">Audio player</string> <string name="audio_player">Audio player</string>
<string name="audio_only_mode">Audio only mode</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="audio_only_mode_summary">Turn LibreTube into a music player.</string>
<string name="sleep_timer">Sleep timer</string>
<!-- Notification channel strings --> <!-- Notification channel strings -->
<string name="download_channel_name">Download Service</string> <string name="download_channel_name">Download Service</string>

View File

@ -50,14 +50,14 @@
<SwitchPreferenceCompat <SwitchPreferenceCompat
android:defaultValue="false" android:defaultValue="false"
android:icon="@drawable/ic_notification" android:icon="@drawable/ic_notification"
app:key="break_reminder_toggle" app:key="sleep_timer_toggle"
app:title="@string/break_reminder" /> app:title="@string/sleep_timer" />
<EditTextPreference <EditTextPreference
android:enabled="false" android:enabled="false"
android:icon="@drawable/ic_time" android:icon="@drawable/ic_time"
app:defaultValue="60" app:defaultValue="60"
app:key="break_reminder" app:key="sleep_timer_delay"
app:title="@string/break_reminder_time" app:title="@string/break_reminder_time"
app:useSimpleSummaryProvider="true" /> app:useSimpleSummaryProvider="true" />