Skip to content

Commit 85d3119

Browse files
committed
Refactor deck selection code in CardBrowser
Uses a simple TextView(and a dropdown drawable at its end) to display the selected deck instead of the more verbose current Spinner/ DeckSpinnerSelection code. Note for the different layout: CardBrowser was showing the deck name + the number of cards in that deck which was done through the DeckSpinnerSelection related Spinner. I replaced this(with the exact same behavior) by a deck name TextView + a subtitle TextView in the new layout.
1 parent 3c1c2ce commit 85d3119

5 files changed

Lines changed: 79 additions & 46 deletions

File tree

AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,6 @@ open class CardBrowser :
153153
*/
154154
private var noteEditorFrame: FragmentContainerView? = null
155155

156-
private var deckSpinnerSelection: DeckSpinnerSelection? = null
157-
158156
private var actionBarTitle: TextView? = null
159157

160158
private var searchView: CardBrowserSearchView? = null
@@ -364,18 +362,10 @@ open class CardBrowser :
364362
// initialize the lateinit variables
365363
// Load reference to action bar title
366364
actionBarTitle = findViewById(R.id.toolbar_title)
367-
368-
deckSpinnerSelection =
369-
DeckSpinnerSelection(
370-
this,
371-
findViewById(R.id.toolbar_spinner),
372-
showAllDecks = true,
373-
alwaysShowDefault = false,
374-
showFilteredDecks = true,
375-
subtitleProvider = this,
376-
)
377365
}
378366

367+
findViewById<LinearLayout>(R.id.toolbar_content).setOnClickListener { startDeckSelection(all = true, filtered = true) }
368+
379369
startLoadingCollection()
380370

381371
setupFlows()
@@ -506,10 +496,10 @@ open class CardBrowser :
506496
searchView?.setQuery(filterQuery, submit = false)
507497
}
508498

509-
suspend fun onDeckIdChanged(deckId: DeckId?) {
499+
fun onDeckIdChanged(deckId: DeckId?) {
510500
if (deckId == null) return
511501
// this handles ALL_DECKS_ID
512-
deckSpinnerSelection?.selectDeckById(deckId, false)
502+
updateAppBarInfo(deckId)
513503
}
514504

515505
fun onCanSaveChanged(canSave: Boolean) {
@@ -522,12 +512,14 @@ open class CardBrowser :
522512
Timber.d("load multiselect mode")
523513
// show title and hide spinner
524514
actionBarTitle?.visibility = View.VISIBLE
525-
deckSpinnerSelection?.setSpinnerVisibility(View.GONE)
515+
findViewById<TextView>(R.id.deck_name).isVisible = false
516+
findViewById<TextView>(R.id.subtitle).isVisible = false
526517
multiSelectOnBackPressedCallback.isEnabled = true
527518
} else {
528519
Timber.d("end multiselect mode")
529520
refreshSubtitle()
530-
deckSpinnerSelection?.setSpinnerVisibility(View.VISIBLE)
521+
findViewById<TextView>(R.id.deck_name).isVisible = true
522+
findViewById<TextView>(R.id.subtitle).isVisible = true
531523
actionBarTitle?.visibility = View.GONE
532524
multiSelectOnBackPressedCallback.isEnabled = false
533525
}
@@ -603,15 +595,7 @@ open class CardBrowser :
603595
registerReceiver()
604596

605597
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
606-
deckSpinnerSelection?.apply {
607-
initializeActionBarDeckSpinner(col, supportActionBar!!)
608-
launchCatchingTask {
609-
selectDeckById(
610-
viewModel.deckId ?: DeckSpinnerSelection.ALL_DECKS_ID,
611-
false,
612-
)
613-
}
614-
}
598+
updateAppBarInfo(viewModel.deckId ?: DeckSpinnerSelection.ALL_DECKS_ID)
615599
}
616600

617601
override fun onKeyUp(
@@ -1224,7 +1208,7 @@ open class CardBrowser :
12241208
private fun updateList() {
12251209
if (!colIsOpenUnsafe()) return
12261210
Timber.d("updateList")
1227-
deckSpinnerSelection?.notifyDataSetChanged()
1211+
updateAppBarInfo(viewModel.deckId)
12281212
onSelectionChanged()
12291213
refreshMenuItems()
12301214
}
@@ -1363,6 +1347,23 @@ open class CardBrowser :
13631347
override val shortcuts
13641348
get() = cardBrowserFragment.shortcuts
13651349

1350+
/**
1351+
* Sets the selected deck name and current selection count based on [deckDropDownSubtitle] in
1352+
* the topbar.
1353+
*/
1354+
private fun updateAppBarInfo(deckId: DeckId?) {
1355+
if (deckId == null || useSearchView) return
1356+
findViewById<TextView>(R.id.subtitle)?.text = deckDropDownSubtitle
1357+
launchCatchingTask {
1358+
val deckName =
1359+
when (deckId) {
1360+
DeckSpinnerSelection.ALL_DECKS_ID -> getString(R.string.card_browser_all_decks)
1361+
else -> withCol { decks.getLegacy(deckId)?.name }
1362+
}
1363+
findViewById<TextView>(R.id.deck_name)?.text = deckName
1364+
}
1365+
}
1366+
13661367
companion object {
13671368
// Keys for saving pane weights in SharedPreferences
13681369
private const val PREF_CARD_BROWSER_PANE_WEIGHT = "cardBrowserPaneWeight"

AnkiDroid/src/main/java/com/ichi2/anki/DeckSpinnerSelection.kt

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import android.widget.Spinner
2323
import android.widget.TextView
2424
import androidx.annotation.LayoutRes
2525
import androidx.annotation.MainThread
26-
import androidx.appcompat.app.ActionBar
2726
import androidx.appcompat.app.AppCompatActivity
2827
import androidx.fragment.app.Fragment
2928
import com.ichi2.anki.CollectionManager.withCol
@@ -66,7 +65,6 @@ class DeckSpinnerSelection(
6665
private val showAllDecks: Boolean,
6766
private val alwaysShowDefault: Boolean,
6867
private val showFilteredDecks: Boolean,
69-
private val subtitleProvider: DeckDropDownAdapter.SubtitleProvider? = null,
7068
private val fragmentManagerSupplier: FragmentManagerSupplier = context.asFragmentManagerSupplier(),
7169
) {
7270
private var deckDropDownAdapter: DeckDropDownAdapter? = null
@@ -75,22 +73,6 @@ class DeckSpinnerSelection(
7573
// but this class also handles initializeNoteEditorDeckSpinner, so this can't happen yet
7674
private var dropDownDecks: MutableList<DeckNameId>? = null
7775

78-
@MainThread // spinner.adapter
79-
fun initializeActionBarDeckSpinner(
80-
col: Collection,
81-
actionBar: ActionBar,
82-
) {
83-
actionBar.setDisplayShowTitleEnabled(false)
84-
85-
// Add drop-down menu to select deck to action bar.
86-
computeDropDownDecks(col, includeFiltered = showFilteredDecks).toMutableList().let {
87-
dropDownDecks = it
88-
deckDropDownAdapter = DeckDropDownAdapter(context, subtitleProvider, it)
89-
spinner.adapter = deckDropDownAdapter
90-
setSpinnerListener()
91-
}
92-
}
93-
9476
@MainThread // spinner.adapter
9577
fun initializeNoteEditorDeckSpinner(
9678
col: Collection,

AnkiDroid/src/main/res/layout-xlarge/card_browser.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
android:layout_width="match_parent"
88
android:layout_height="match_parent"
99
android:orientation="vertical">
10-
<include layout="@layout/toolbar" />
10+
<include layout="@layout/view_browser_toolbar" />
1111
<LinearLayout
1212
android:id="@+id/card_browser_xl_view"
1313
android:layout_width="match_parent"

AnkiDroid/src/main/res/layout/card_browser.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
android:layout_width="match_parent"
77
android:layout_height="match_parent"
88
android:orientation="vertical">
9-
<include layout="@layout/toolbar" />
9+
<include layout="@layout/view_browser_toolbar" />
1010
<androidx.fragment.app.FragmentContainerView
1111
android:id="@+id/card_browser_frame"
1212
android:layout_width="match_parent"
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:id="@+id/toolbar"
6+
android:layout_width="match_parent"
7+
android:layout_height="wrap_content"
8+
android:background="?attr/appBarColor"
9+
android:minHeight="?attr/actionBarSize"
10+
android:theme="@style/ActionBarStyle"
11+
app:navigationContentDescription="@string/abc_action_bar_up_description"
12+
app:navigationIcon="?attr/homeAsUpIndicator">
13+
14+
<com.ichi2.ui.FixedTextView
15+
android:id="@+id/toolbar_title"
16+
android:layout_width="wrap_content"
17+
android:layout_height="match_parent"
18+
android:textColor="@color/white"
19+
android:textSize="20sp"
20+
android:visibility="gone" />
21+
22+
<LinearLayout
23+
android:id="@+id/toolbar_content"
24+
android:layout_width="match_parent"
25+
android:layout_height="wrap_content"
26+
android:background="?attr/selectableItemBackground"
27+
android:orientation="vertical">
28+
29+
<com.ichi2.ui.FixedTextView
30+
android:id="@+id/deck_name"
31+
android:layout_width="match_parent"
32+
android:layout_height="wrap_content"
33+
android:gravity="center_vertical"
34+
android:drawableEnd="@drawable/id_arrow_drop_down"
35+
android:textAppearance="?attr/textAppearanceListItem"
36+
android:textColor="@color/white"
37+
android:maxLines="1"
38+
android:ellipsize="end"
39+
tools:text="Deck name here"
40+
/>
41+
42+
<TextView
43+
android:id="@+id/subtitle"
44+
android:layout_width="match_parent"
45+
android:layout_height="wrap_content"
46+
android:textColor="@color/white"
47+
android:textAppearance="?attr/textAppearanceListItemSecondary"
48+
tools:text="2 cards shown"/>
49+
</LinearLayout>
50+
</androidx.appcompat.widget.Toolbar>

0 commit comments

Comments
 (0)