Skip to content

Commit

Permalink
feat(new reviewer): immersive mode
Browse files Browse the repository at this point in the history
https://developer.android.com/develop/ui/views/layout/immersive

magic numbers are used for the preferences values because they can be reported in analytics
  • Loading branch information
BrayanDSO committed Jun 30, 2024
1 parent 8cd9c40 commit 976507e
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.ichi2.anki.previewer
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import com.ichi2.anki.SingleFragmentActivity
import kotlin.reflect.KClass
import kotlin.reflect.jvm.jvmName
Expand All @@ -27,6 +28,11 @@ import kotlin.reflect.jvm.jvmName
* @see TemplatePreviewerFragment
*/
class CardViewerActivity : SingleFragmentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
enableEdgeToEdge() // TODO assess moving this to SingleFragmentActivity
super.onCreate(savedInstanceState)
}

companion object {
fun getIntent(context: Context, fragmentClass: KClass<out CardViewerFragment>, arguments: Bundle? = null): Intent {
return Intent(context, CardViewerActivity::class.java).apply {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2024 Brayan Oliveira <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.ichi2.anki.ui.windows.reviewer

import android.content.Context
import androidx.annotation.StringRes
import com.ichi2.anki.R
import com.ichi2.anki.preferences.sharedPrefs

enum class HideSystemBars(@StringRes val valueRes: Int) {
NONE(R.string.hide_system_bars_none_value),
STATUS_BAR(R.string.hide_system_bars_status_bar_value),
NAVIGATION_BAR(R.string.hide_system_bars_navigation_bar_value),
ALL(R.string.hide_system_bars_all_value);

companion object {
fun defaultValue(context: Context): String = context.getString(NONE.valueRes)

fun from(context: Context): HideSystemBars {
val prefKey = context.getString(R.string.hide_system_bars_key)
val value = context.sharedPrefs().getString(prefKey, HideSystemBars.defaultValue(context))
return entries.first { value == context.getString(it.valueRes) }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ import androidx.appcompat.view.menu.MenuBuilder
import androidx.appcompat.widget.ThemeUtils
import androidx.appcompat.widget.Toolbar
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.core.view.isVisible
import androidx.core.view.updatePadding
import androidx.fragment.app.viewModels
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
Expand Down Expand Up @@ -83,6 +87,7 @@ class ReviewerFragment :

setupAnswerButtons(view)
setupCounts(view)
setupImmersiveMode(view)

view.findViewById<MaterialToolbar>(R.id.toolbar).apply {
setOnMenuItemClickListener(this@ReviewerFragment)
Expand Down Expand Up @@ -298,6 +303,33 @@ class ReviewerFragment :
}
}

private fun setupImmersiveMode(view: View) {
val hideSystemBarsSetting = HideSystemBars.from(requireContext())
val barsToHide = when (hideSystemBarsSetting) {
HideSystemBars.NONE -> return
HideSystemBars.STATUS_BAR -> WindowInsetsCompat.Type.statusBars()
HideSystemBars.NAVIGATION_BAR -> WindowInsetsCompat.Type.navigationBars()
HideSystemBars.ALL -> WindowInsetsCompat.Type.systemBars()
}

val window = requireActivity().window
with(WindowInsetsControllerCompat(window, window.decorView)) {
hide(barsToHide)
systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}

ViewCompat.setOnApplyWindowInsetsListener(view) { v, insets ->
val bars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.updatePadding(
left = bars.left,
top = bars.top,
right = bars.right,
bottom = bars.bottom
)
WindowInsetsCompat.CONSUMED
}
}

private val noteEditorLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.data?.getBooleanExtra(NoteEditor.RELOAD_REQUIRED_EXTRA_KEY, false) == true ||
Expand Down
13 changes: 12 additions & 1 deletion AnkiDroid/src/main/res/values/10-preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
<string name="gestures_corner_touch_summary">Allow touch gestures in screen corners</string>
<string name="gestures_full_screen_nav_drawer" maxLength="41">Full screen navigation drawer</string>
<string name="gestures_fullscreen_nav_drawer_summary">Open navigation drawer when swiped right from anywhere on the screen</string>
<string name="gestures_none" maxLength="41">None</string>
<string name="gestures_none" maxLength="41" comment="No gestures">None</string>
<string name="gestures_swipe_up" maxLength="41">Swipe up</string>
<string name="gestures_swipe_down" maxLength="41">Swipe down</string>
<string name="gestures_swipe_left" maxLength="41">Swipe left</string>
Expand Down Expand Up @@ -431,4 +431,15 @@ this formatter is used if the bind only applies to both the question and the ans
<string name="pref__etc__snackbar__no_collection"
comment="Generic snackbar message to be shown when user taps on a preference
that is not usable because the collection does not exist">Collection does not exist</string>

<string name="hide_system_bars" maxLength="41" comment="Title of a setting that allows to hide the system bars"
>Hide system bars</string>
<string name="hide_system_bars_none" comment="Setting option to not hide any of the system bars"
>None</string>
<string name="hide_system_bars_status_bar" comment="Setting option to hide the system status bar (the top one with clock, battery status, etc)"
>Status bar</string>
<string name="hide_system_bars_navigation_bar" comment="Setting option to hide the system navigation bar (the bottom one that can go back, to home, etc)"
>Navigation bar</string>
<string name="hide_system_bars_all_bars" comment="Setting option to hide all the system bars"
>All</string>
</resources>
19 changes: 19 additions & 0 deletions AnkiDroid/src/main/res/values/constants.xml
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,25 @@
<item>@string/sync_media_never_value</item>
</string-array>

<string-array name="hide_system_bars_entries">
<item>@string/hide_system_bars_none</item>
<item>@string/hide_system_bars_status_bar</item>
<item>@string/hide_system_bars_navigation_bar</item>
<item>@string/hide_system_bars_all_bars</item>
</string-array>

<string name="hide_system_bars_none_value">0</string>
<string name="hide_system_bars_status_bar_value">1</string>
<string name="hide_system_bars_navigation_bar_value">2</string>
<string name="hide_system_bars_all_value">3</string>

<string-array name="hide_system_bars_values">
<item>@string/hide_system_bars_none_value</item>
<item>@string/hide_system_bars_status_bar_value</item>
<item>@string/hide_system_bars_navigation_bar_value</item>
<item>@string/hide_system_bars_all_value</item>
</string-array>

<!-- Preferences headers summaries -->
<string-array name="general_summary_entries">
<item>@string/language</item>
Expand Down
3 changes: 3 additions & 0 deletions AnkiDroid/src/main/res/values/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,7 @@

<!-- Instant Editor -->
<string name="pref_open_instant_editor">openInstantEditor</string>

<!-- Reviewer options -->
<string name="hide_system_bars_key">hideSystemBars</string>
</resources>
10 changes: 9 additions & 1 deletion AnkiDroid/src/main/res/xml/preferences_reviewer.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<ListPreference
android:defaultValue="@string/hide_system_bars_none_value"
android:entries="@array/hide_system_bars_entries"
android:entryValues="@array/hide_system_bars_values"
android:key="@string/hide_system_bars_key"
android:title="@string/hide_system_bars"
app:useSimpleSummaryProvider="true"/>
</androidx.preference.PreferenceScreen>

0 comments on commit 976507e

Please sign in to comment.