diff --git a/app/src/main/java/com/github/libretube/activities/MainActivity.kt b/app/src/main/java/com/github/libretube/activities/MainActivity.kt index 0fa212023..724c13462 100644 --- a/app/src/main/java/com/github/libretube/activities/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/activities/MainActivity.kt @@ -16,6 +16,7 @@ import android.view.WindowInsets import android.view.WindowInsetsController import android.view.WindowManager import android.widget.LinearLayout +import android.widget.Toast import androidx.appcompat.widget.SearchView import androidx.constraintlayout.motion.widget.MotionLayout import androidx.constraintlayout.widget.ConstraintLayout @@ -39,6 +40,7 @@ import com.github.libretube.util.ConnectionHelper import com.github.libretube.util.CronetHelper import com.github.libretube.util.LocaleHelper import com.github.libretube.util.ThemeHelper +import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.elevation.SurfaceColors import com.google.android.material.navigation.NavigationBarView @@ -163,6 +165,40 @@ class MainActivity : BaseActivity() { */ val log = PreferenceHelper.getErrorLog() if (log != "") ErrorDialog().show(supportFragmentManager, null) + + setupBreakReminder() + } + + /** + * Show a break reminder when watched too long + */ + private fun setupBreakReminder() { + val breakReminderPref = PreferenceHelper.getString( + PreferenceKeys.BREAK_REMINDER, + "disabled" + ) + if (breakReminderPref == "disabled") return + Handler(Looper.getMainLooper()).postDelayed( + { + try { + MaterialAlertDialogBuilder(this) + .setTitle(getString(R.string.share_with_time)) + .setMessage( + getString( + R.string.already_spent_time, + breakReminderPref + ) + ) + .setPositiveButton(R.string.okay, null) + .show() + } catch (e: Exception) { + kotlin.runCatching { + Toast.makeText(this, R.string.take_a_break, Toast.LENGTH_LONG).show() + } + } + }, + breakReminderPref.toLong() * 60 * 1000 + ) } private fun removeSearchFocus() { diff --git a/app/src/main/java/com/github/libretube/preferences/GeneralSettings.kt b/app/src/main/java/com/github/libretube/preferences/GeneralSettings.kt index f9d15e56d..fe88d4504 100644 --- a/app/src/main/java/com/github/libretube/preferences/GeneralSettings.kt +++ b/app/src/main/java/com/github/libretube/preferences/GeneralSettings.kt @@ -37,5 +37,12 @@ class GeneralSettings : MaterialPreferenceFragment() { restartDialog.show(childFragmentManager, RequireRestartDialog::class.java.name) true } + + val breakReminder = findPreference(PreferenceKeys.BREAK_REMINDER) + breakReminder?.setOnPreferenceChangeListener { _, _ -> + val restartDialog = RequireRestartDialog() + restartDialog.show(childFragmentManager, RequireRestartDialog::class.java.name) + true + } } } diff --git a/app/src/main/java/com/github/libretube/preferences/PreferenceKeys.kt b/app/src/main/java/com/github/libretube/preferences/PreferenceKeys.kt index 3b4c4ad55..9dd059dbe 100644 --- a/app/src/main/java/com/github/libretube/preferences/PreferenceKeys.kt +++ b/app/src/main/java/com/github/libretube/preferences/PreferenceKeys.kt @@ -10,6 +10,7 @@ object PreferenceKeys { const val LANGUAGE = "language" const val REGION = "region" const val AUTO_ROTATION = "auto_rotation" + const val BREAK_REMINDER = "break_reminder" /** * Appearance diff --git a/app/src/main/res/values/array.xml b/app/src/main/res/values/array.xml index b5b54c098..33a47a5c5 100644 --- a/app/src/main/res/values/array.xml +++ b/app/src/main/res/values/array.xml @@ -828,4 +828,22 @@ unlimited + + @string/disabled + 15 min + 30 min + 60 min + 90 min + 120 min + + + + disabled + 15 + 30 + 60 + 90 + 120 + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6cc64f7a6..2f3ec6ca5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -298,4 +298,8 @@ Unlimited Background mode Add to queue + Misc + Break reminder + Time to take a break + You already spent %1$s minutes in the app, time to take a break. diff --git a/app/src/main/res/xml/general_settings.xml b/app/src/main/res/xml/general_settings.xml index 91ba8bfd5..e97a86801 100644 --- a/app/src/main/res/xml/general_settings.xml +++ b/app/src/main/res/xml/general_settings.xml @@ -61,4 +61,17 @@ + + + + + + \ No newline at end of file