Skip to content

Commit

Permalink
feat: Allow deck selection in statistics screen
Browse files Browse the repository at this point in the history
A Spinner was introduced in the statistics screen which can be used by
the user to change the current selected deck and also trigger a reload
of the statistics UI.
  • Loading branch information
criticalAY authored and lukstbit committed Jun 19, 2024
1 parent 5c8919f commit d8106cb
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 2 deletions.
3 changes: 3 additions & 0 deletions AnkiDroid/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,9 @@
android:exported="false"
android:configChanges="orientation|screenSize"
/>
<activity android:name="com.ichi2.anki.StatisticsActivity"
android:exported="false"
android:configChanges="orientation|screenSize" />
<activity
android:name="com.ichi2.anki.previewer.CardViewerActivity"
android:exported="false"
Expand Down
20 changes: 20 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/anki/DeckSpinnerSelection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@ class DeckSpinnerSelection(
setSpinnerListener()
}

@MainThread // spinner.adapter
fun initializeStatsBarDeckSpinner(col: Collection) {
// compute decks doesn't handle Default deck
dropDownDecks = col.decks.allNamesAndIds(includeFiltered = showFilteredDecks, skipEmptyDefault = true)
.toMutableList()
// custom implementation as DeckDropDownAdapter automatically includes a ALL_DECKS entry
spinner.adapter = object : ArrayAdapter<DeckNameId>(
context,
android.R.layout.simple_spinner_item,
dropDownDecks
) {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val rowView = super.getView(position, convertView, parent) as TextView
rowView.text = getItem(position)!!.name
return rowView
}
}.apply { setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) }
setSpinnerListener()
}

@MainThread // spinner.adapter
fun initializeNoteEditorDeckSpinner(col: Collection, @LayoutRes layoutResource: Int = R.layout.multiline_spinner_item) {
dropDownDecks = computeDropDownDecks(col, includeFiltered = false).toMutableList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ abstract class NavigationDrawerActivity :

R.id.nav_stats -> {
Timber.i("Navigating to stats")
val intent = com.ichi2.anki.pages.Statistics.getIntent(this)
val intent = Intent(this, StatisticsActivity::class.java)
startActivity(intent)
}

Expand Down
83 changes: 83 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/anki/StatisticsActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2024 Ashish Yadav <mailtoashish693@gmail.com>
*
* 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

import android.os.Bundle
import androidx.fragment.app.commit
import com.ichi2.anki.dialogs.DeckSelectionDialog
import com.ichi2.anki.pages.Statistics
import com.ichi2.libanki.Collection

/**
* Shows the statistics for the current selected deck and offers the possibility of changing the deck.
*
* Note: uses `configChanges="orientation|screenSize"` to avoid activity recreations as it doesn't
* play well with the embedded WebView.
*
* @see Statistics
*/
class StatisticsActivity :
AnkiActivity(),
DeckSelectionDialog.DeckSelectionListener {

private lateinit var deckSpinnerSelection: DeckSpinnerSelection

override fun onCreate(savedInstanceState: Bundle?) {
if (showedActivityFailedScreen(savedInstanceState)) {
return
}
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_statistics)
if (savedInstanceState == null) {
supportFragmentManager.commit {
replace(
R.id.fragment_container,
Statistics.newInstance(path = "graphs", title = getString(R.string.statistics)),
STATS_FRAGMENT_TAG
)
}
}
startLoadingCollection()
}

override fun onCollectionLoaded(col: Collection) {
super.onCollectionLoaded(col)
deckSpinnerSelection = DeckSpinnerSelection(
this,
findViewById(R.id.deck_selector),
showAllDecks = false,
alwaysShowDefault = false,
showFilteredDecks = false
)
deckSpinnerSelection.initializeStatsBarDeckSpinner(col)
launchCatchingTask {
deckSpinnerSelection.selectDeckById(col.decks.selected(), true)
}
}

override fun onDeckSelected(deck: DeckSelectionDialog.SelectableDeck?) {
if (deck == null) return
(supportFragmentManager.findFragmentByTag(STATS_FRAGMENT_TAG) as? Statistics)?.webView?.reload()
launchCatchingTask {
deckSpinnerSelection.selectDeckById(deck.deckId, true)
}
}

companion object {
private const val STATS_FRAGMENT_TAG = "stats_fragment_tag"
}
}
8 changes: 8 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/anki/pages/Statistics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import android.print.PrintAttributes
import android.print.PrintManager
import android.view.View
import androidx.core.content.ContextCompat.getSystemService
import androidx.core.os.bundleOf
import com.google.android.material.appbar.AppBarLayout
import com.google.android.material.appbar.MaterialToolbar
import com.ichi2.anki.CollectionManager
Expand Down Expand Up @@ -69,5 +70,12 @@ class Statistics : PageFragment(R.layout.statistics) {
fun getIntent(context: Context): Intent {
return getIntent(context, "graphs", context.getString(R.string.statistics), Statistics::class)
}

fun newInstance(path: String, title: String) = Statistics().apply {
arguments = bundleOf(
PATH_ARG_KEY to path,
TITLE_ARG_KEY to title
)
}
}
}
35 changes: 35 additions & 0 deletions AnkiDroid/src/main/res/layout/activity_statistics.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StatisticsActivity">

<FrameLayout
android:id="@+id/deck_selector_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/listPreferredItemHeightSmall"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:paddingHorizontal="8dp"
android:background="?attr/currentDeckBackground"
android:paddingTop="8dp"
app:layout_constraintStart_toStartOf="parent">

<Spinner
android:id="@+id/deck_selector"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>

<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/deck_selector_layout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
3 changes: 2 additions & 1 deletion AnkiDroid/src/test/java/com/ichi2/testutils/ActivityList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ object ActivityList {
get(PermissionsActivity::class.java),
get(SingleFragmentActivity::class.java),
get(CardViewerActivity::class.java),
get(InstantNoteEditorActivity::class.java)
get(InstantNoteEditorActivity::class.java),
get(StatisticsActivity::class.java)
)
}

Expand Down

0 comments on commit d8106cb

Please sign in to comment.