Skip to content

Commit

Permalink
Merge branch 'ankidroid:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
xenonnn4w committed Aug 13, 2024
2 parents f82a109 + 35cfd4d commit 4e7ddad
Show file tree
Hide file tree
Showing 15 changed files with 312 additions and 120 deletions.
6 changes: 0 additions & 6 deletions AnkiDroid/src/main/java/com/ichi2/anki/AnkiFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package com.ichi2.anki

import android.content.BroadcastReceiver
import android.net.Uri
import android.os.Bundle
import android.view.Menu
import android.view.View
import androidx.annotation.AttrRes
Expand Down Expand Up @@ -67,11 +66,6 @@ open class AnkiFragment(@LayoutRes layout: Int) : Fragment(layout) {
hideProgressBar()
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
requireActivity().window.statusBarColor = ThemeUtils.getThemeAttrColor(requireContext(), R.attr.appBarColor)
super.onViewCreated(view, savedInstanceState)
}

// Helper functions: These make fragment code shorter

/**
Expand Down
277 changes: 195 additions & 82 deletions AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateEditor.kt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@ package com.ichi2.anki.previewer
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.webkit.WebView
import androidx.appcompat.widget.ThemeUtils
import androidx.appcompat.widget.Toolbar
import androidx.core.os.BundleCompat
import androidx.core.os.bundleOf
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.google.android.material.appbar.MaterialToolbar
import com.google.android.material.button.MaterialButton
import com.google.android.material.card.MaterialCardView
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import com.ichi2.anki.CardTemplateEditor
import com.ichi2.anki.R
import com.ichi2.anki.cardviewer.CardMediaPlayer
import com.ichi2.anki.snackbar.BaseSnackbarBuilderProvider
Expand All @@ -43,11 +45,18 @@ import timber.log.Timber

class TemplatePreviewerFragment :
CardViewerFragment(R.layout.template_previewer),
BaseSnackbarBuilderProvider {
BaseSnackbarBuilderProvider,
Toolbar.OnMenuItemClickListener {
/**
* Whether this is displayed in a fragment view.
* If true, this fragment is on the trailing side of the card template editor.
*/
private var inFragmentedActivity = false
private lateinit var templatePreviewerArguments: TemplatePreviewerArguments

override val viewModel: TemplatePreviewerViewModel by viewModels {
val arguments = BundleCompat.getParcelable(requireArguments(), ARGS_KEY, TemplatePreviewerArguments::class.java)!!
TemplatePreviewerViewModel.factory(arguments, CardMediaPlayer())
templatePreviewerArguments = BundleCompat.getParcelable(requireArguments(), ARGS_KEY, TemplatePreviewerArguments::class.java)!!
TemplatePreviewerViewModel.factory(templatePreviewerArguments, CardMediaPlayer())
}
override val webView: WebView
get() = requireView().findViewById(R.id.webview)
Expand All @@ -58,8 +67,17 @@ class TemplatePreviewerFragment :
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

view.findViewById<MaterialToolbar>(R.id.toolbar).setNavigationOnClickListener {
requireActivity().onBackPressedDispatcher.onBackPressed()
val toolbar = view.findViewById<Toolbar>(R.id.toolbar)
// Retrieve the boolean argument "inFragmentedActivity" from the fragment's arguments bundle
inFragmentedActivity = templatePreviewerArguments.inFragmentedActivity
// If this fragment is a part of fragmented activity, then there is already a navigation icon an the activity.
// We hide the one specific to this fragment
if (inFragmentedActivity) {
toolbar.navigationIcon = null
} else {
toolbar.setNavigationOnClickListener {
requireActivity().onBackPressedDispatcher.onBackPressed()
}
}

val showAnswerButton = view.findViewById<MaterialButton>(R.id.show_answer).apply {
Expand Down Expand Up @@ -107,12 +125,28 @@ class TemplatePreviewerFragment :
if (!navBarNeedsScrim) {
window.navigationBarColor = ThemeUtils.getThemeAttrColor(this, R.attr.alternativeBackgroundColor)
}
window.statusBarColor = ThemeUtils.getThemeAttrColor(this, R.attr.appBarColor)
}
if (inFragmentedActivity) {
toolbar.setOnMenuItemClickListener(this)
toolbar.inflateMenu(R.menu.card_template_editor)
(activity as CardTemplateEditor).currentFragment?.setupCommonMenu(toolbar.menu)
}
}

override fun onMenuItemClick(item: MenuItem): Boolean {
return (activity as CardTemplateEditor).currentFragment?.handleCommonMenuItemSelected(item) == true
}

companion object {
const val ARGS_KEY = "templatePreviewerArgs"

fun newInstance(arguments: TemplatePreviewerArguments): TemplatePreviewerFragment {
return TemplatePreviewerFragment().apply {
this.arguments = bundleOf(ARGS_KEY to arguments)
}
}

fun getIntent(context: Context, arguments: TemplatePreviewerArguments): Intent {
return CardViewerActivity.getIntent(
context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ data class TemplatePreviewerArguments(
val tags: MutableList<String>,
val id: Long = 0,
val ord: Int = 0,
val fillEmpty: Boolean = false
val fillEmpty: Boolean = false,
val inFragmentedActivity: Boolean = false
) : Parcelable {
val notetype: NotetypeJson get() = notetypeFile.getNotetype()
}
22 changes: 22 additions & 0 deletions AnkiDroid/src/main/res/layout-xlarge/card_template_editor.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/card_template_editor_xl_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/colorBackground"
android:orientation="horizontal">
<include layout="@layout/card_template_editor_activity"
android:layout_width="1dip"
android:layout_weight="1"
android:layout_height="match_parent"/>
<androidx.fragment.app.FragmentContainerView
android:id="@+id/template_previewer_fragment"
android:layout_weight="1"
android:layout_width="1dip"
android:layout_height="match_parent"/>
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

6 changes: 6 additions & 0 deletions AnkiDroid/src/main/res/layout/card_template_editor.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/card_template_editor_activity" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
android:layout_height="wrap_content"
android:layout_gravity="bottom"
style="@style/BottomNavigationViewStyle"
android:background="?attr/appBarColor"
android:background="?attr/alternativeBackgroundColor"
app:menu="@menu/card_template_editor_navigation_bar_menu"
/>

Expand Down
28 changes: 8 additions & 20 deletions AnkiDroid/src/main/res/layout/template_previewer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,20 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent">

<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
<include layout="@layout/toolbar" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
style="@style/TabLayoutStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:navigationContentDescription="@string/abc_action_bar_up_description"
app:navigationIcon="?attr/homeAsUpIndicator"
android:background="?attr/alternativeBackgroundColor">

<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:background="@color/transparent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
/>

</com.google.android.material.appbar.MaterialToolbar>

app:tabMode="scrollable"
/>
</com.google.android.material.appbar.AppBarLayout>

<com.google.android.material.card.MaterialCardView
android:id="@+id/webview_container"
android:layout_width="match_parent"
android:layout_marginHorizontal="8dp"
android:layout_margin="8dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@id/appbar"
app:layout_constraintBottom_toTopOf="@id/show_answer"
Expand All @@ -62,7 +50,7 @@
<com.google.android.material.button.MaterialButton
android:id="@+id/show_answer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_height="80dp"
style="@style/Widget.Material3.Button.TextButton"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
Expand Down
1 change: 1 addition & 0 deletions AnkiDroid/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@
The latter is set by colorPrimary, but theme overlay can't use it,
as it itself overrides colorPrimary, which is used by the view directly. -->
<attr name="tabLayoutBackgroundColor" format="color"/>
<attr name="tabActiveIconColor" format="color"/>

<attr name="checkMediaListBackground" format="color" />
<attr name="checkMediaListForeground" format="color" />
Expand Down
8 changes: 4 additions & 4 deletions AnkiDroid/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@
<item name="materialThemeOverlay">@style/ThemeOverlay.App.BottomNavigationView</item>
</style>
<style name="ThemeOverlay.App.BottomNavigationView" parent="">
<item name="colorSecondaryContainer">#fff</item> <!-- Selected item indicator -->
<item name="colorOnSecondaryContainer">?attr/tabLayoutBackgroundColor</item> <!-- Active icon -->
<item name="colorOnSurface">#fff</item> <!-- Active icon -->
<item name="colorOnSurfaceVariant">#fff</item> <!-- Inactive icon & label -->
<item name="colorSecondaryContainer">?attr/tabLayoutBackgroundColor</item> <!-- Selected item indicator -->
<item name="colorOnSecondaryContainer">?attr/tabActiveIconColor</item> <!-- Active icon -->
<item name="colorOnSurface">?attr/colorPrimary</item> <!-- Active icon -->
<item name="colorOnSurfaceVariant">?attr/colorPrimary</item> <!-- Inactive icon & label -->
<item name="textAppearanceTitleSmall">@style/TextAppearance.Material3.TitleSmall</item>
<item name="textAppearanceLabelLarge">@style/TextAppearance.Material3.LabelLarge</item>
</style>
Expand Down
1 change: 1 addition & 0 deletions AnkiDroid/src/main/res/values/theme_black.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<item name="actionModeBackground">@color/theme_black_primary_dark</item>
<!-- Tab layout -->
<item name="tabLayoutBackgroundColor">@color/theme_black_primary_dark</item>
<item name="tabActiveIconColor">@color/material_blue_500</item>
<!-- Deck list colors and divider -->
<item name="currentDeckBackground">@drawable/item_background_selected</item>
<item name="currentDeckBackgroundColor">@color/theme_black_row_current</item>
Expand Down
1 change: 1 addition & 0 deletions AnkiDroid/src/main/res/values/theme_dark.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<item name="actionModeBackground">@color/theme_dark_primary</item>
<!-- Tab layout -->
<item name="tabLayoutBackgroundColor">@color/theme_dark_primary</item>
<item name="tabActiveIconColor">@color/material_blue_500</item>
<!-- Deck list colors and divider -->
<item name="currentDeckBackground">@drawable/item_background_selected</item>
<item name="currentDeckBackgroundColor">@color/theme_dark_row_current</item>
Expand Down
1 change: 1 addition & 0 deletions AnkiDroid/src/main/res/values/theme_light.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<item name="actionModeBackground">?attr/colorPrimary</item>
<!-- Tab layout -->
<item name="tabLayoutBackgroundColor">?attr/colorPrimary</item>
<item name="tabActiveIconColor">@color/white</item>
<!-- Deck list colors and divider -->
<item name="currentDeckBackground">@drawable/item_background_selected</item>
<item name="currentDeckBackgroundColor">#ececec</item>
Expand Down
1 change: 1 addition & 0 deletions AnkiDroid/src/main/res/values/theme_plain.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<item name="actionModeBackground">@color/theme_plain_primary</item>
<!-- Tab layout -->
<item name="tabLayoutBackgroundColor">@color/theme_plain_primary</item>
<item name="tabActiveIconColor">@color/white</item>
<!-- Reviewer colors -->
<item name="topBarColor">@color/theme_plain_primary_light</item>
<!-- Reviewer button drawables -->
Expand Down
29 changes: 29 additions & 0 deletions AnkiDroid/src/test/java/com/ichi2/utils/BundleUtilsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package com.ichi2.utils

import android.os.Bundle
import com.ichi2.utils.BundleUtils.getNullableLong
import com.ichi2.utils.BundleUtils.requireLong
import org.junit.Assert.assertEquals
import org.junit.Test
import org.mockito.Mockito.anyString
Expand All @@ -26,6 +27,7 @@ import org.mockito.Mockito.times
import org.mockito.Mockito.verify
import org.mockito.kotlin.whenever
import kotlin.random.Random
import kotlin.test.assertFailsWith
import kotlin.test.assertNull

class BundleUtilsTest {
Expand Down Expand Up @@ -58,6 +60,33 @@ class BundleUtilsTest {
assertEquals(expected, value)
}

@Test
fun test_RequireLong_NotFound_ThrowsException() {
val mockedBundle = mock(Bundle::class.java)

whenever(mockedBundle.containsKey(anyString())).thenReturn(false)

assertFailsWith<IllegalStateException> { mockedBundle.requireLong(KEY) }

verify(mockedBundle).containsKey(eq(KEY))
}

@Test
fun test_RequireLong_Found_ReturnIt() {
val expected = Random.Default.nextLong()
val mockedBundle = mock(Bundle::class.java)

whenever(mockedBundle.containsKey(anyString())).thenReturn(true)
whenever(mockedBundle.getLong(anyString())).thenReturn(expected)

val value = mockedBundle.requireLong(KEY)

verify(mockedBundle).containsKey(eq(KEY))
verify(mockedBundle).getLong(eq(KEY))

assertEquals(expected, value)
}

companion object {
const val KEY = "KEY"
}
Expand Down

0 comments on commit 4e7ddad

Please sign in to comment.