Skip to content

Commit 4f8b4d8

Browse files
lukstbitdavid-allison
authored andcommitted
Refactor deck selection code in NoteEditorFragment
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.
1 parent c26cd50 commit 4f8b4d8

File tree

3 files changed

+28
-67
lines changed

3 files changed

+28
-67
lines changed

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

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@ package com.ichi2.anki
1717

1818
import android.view.MotionEvent
1919
import android.view.View
20-
import android.view.ViewGroup
21-
import android.widget.ArrayAdapter
2220
import android.widget.Spinner
23-
import android.widget.TextView
24-
import androidx.annotation.LayoutRes
25-
import androidx.annotation.MainThread
2621
import androidx.appcompat.app.AppCompatActivity
2722
import androidx.fragment.app.Fragment
2823
import com.ichi2.anki.CollectionManager.withCol
@@ -73,40 +68,6 @@ class DeckSpinnerSelection(
7368
// but this class also handles initializeNoteEditorDeckSpinner, so this can't happen yet
7469
private var dropDownDecks: MutableList<DeckNameId>? = null
7570

76-
@MainThread // spinner.adapter
77-
fun initializeNoteEditorDeckSpinner(
78-
col: Collection,
79-
@LayoutRes layoutResource: Int = R.layout.multiline_spinner_item,
80-
) {
81-
computeDropDownDecks(col, includeFiltered = false).toMutableList().let {
82-
dropDownDecks = it
83-
val deckNames = it.map { it.name }
84-
val noteDeckAdapter: ArrayAdapter<String?> =
85-
object :
86-
ArrayAdapter<String?>(context, layoutResource, deckNames as List<String?>) {
87-
override fun getDropDownView(
88-
position: Int,
89-
convertView: View?,
90-
parent: ViewGroup,
91-
): View {
92-
// Cast the drop down items (popup items) as text view
93-
val tv = super.getDropDownView(position, convertView, parent) as TextView
94-
95-
// If this item is selected
96-
if (position == spinner.selectedItemPosition) {
97-
tv.setBackgroundColor(context.getColor(R.color.note_editor_selected_item_background))
98-
tv.setTextColor(context.getColor(R.color.note_editor_selected_item_text))
99-
}
100-
101-
// Return the modified view
102-
return tv
103-
}
104-
}
105-
spinner.adapter = noteDeckAdapter
106-
setSpinnerListener()
107-
}
108-
}
109-
11071
/** @return All decks. */
11172
private fun computeDropDownDecks(
11273
col: Collection,

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

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ import androidx.annotation.CheckResult
5858
import androidx.annotation.DrawableRes
5959
import androidx.annotation.VisibleForTesting
6060
import androidx.appcompat.app.AlertDialog
61-
import androidx.appcompat.app.AppCompatActivity
6261
import androidx.appcompat.widget.AppCompatButton
6362
import androidx.appcompat.widget.PopupMenu
6463
import androidx.core.content.FileProvider
@@ -239,7 +238,6 @@ class NoteEditorFragment :
239238

240239
@VisibleForTesting
241240
internal var noteTypeSpinner: Spinner? = null
242-
private var deckSpinnerSelection: DeckSpinnerSelection? = null
243241
private var imageOcclusionButtonsContainer: LinearLayout? = null
244242
private var selectImageForOcclusionButton: Button? = null
245243
private var editOcclusionsButton: Button? = null
@@ -433,11 +431,7 @@ class NoteEditorFragment :
433431
}
434432
require(deck is SelectableDeck.Deck)
435433
deckId = deck.deckId
436-
// this is called because DeckSpinnerSelection.onDeckAdded doesn't update the list
437-
deckSpinnerSelection!!.initializeNoteEditorDeckSpinner(getColUnsafe)
438-
launchCatchingTask {
439-
deckSpinnerSelection!!.selectDeckById(deckId, false)
440-
}
434+
view?.findViewById<TextView>(R.id.note_deck_name)?.text = deck.name
441435
}
442436

443437
private enum class AddClozeType {
@@ -825,17 +819,14 @@ class NoteEditorFragment :
825819
if (!addNote && editorNote!!.notetype.templates.length() > 1) {
826820
deckTextView.setText(R.string.CardEditorCardDeck)
827821
}
828-
deckSpinnerSelection =
829-
DeckSpinnerSelection(
830-
requireContext() as AppCompatActivity,
831-
requireView().findViewById(R.id.note_deck_spinner),
832-
showAllDecks = false,
833-
alwaysShowDefault = true,
834-
showFilteredDecks = false,
835-
fragmentManagerSupplier = { childFragmentManager },
836-
)
837-
deckSpinnerSelection!!.initializeNoteEditorDeckSpinner(col)
822+
838823
deckId = requireArguments().getLong(EXTRA_DID, deckId)
824+
view?.findViewById<TextView>(R.id.note_deck_name)?.apply {
825+
text = col.decks.name(deckId)
826+
setOnClickListener {
827+
startDeckSelection(all = false, filtered = false)
828+
}
829+
}
839830
val getTextFromSearchView = requireArguments().getString(EXTRA_TEXT_FROM_SEARCH_VIEW)
840831
setDid(editorNote)
841832
setNote(editorNote, FieldChangeType.onActivityCreation(shouldReplaceNewlines()))
@@ -1055,7 +1046,7 @@ class NoteEditorFragment :
10551046
}
10561047
KeyEvent.KEYCODE_D -> // null check in case Spinner is moved into options menu in the future
10571048
if (event.isCtrlPressed) {
1058-
launchCatchingTask { deckSpinnerSelection!!.displayDeckSelectionDialog() }
1049+
launchCatchingTask { startDeckSelection(all = false, filtered = false) }
10591050
return true
10601051
}
10611052
KeyEvent.KEYCODE_L ->
@@ -1847,7 +1838,7 @@ class NoteEditorFragment :
18471838
)
18481839
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
18491840
if (i == 0) {
1850-
requireView().findViewById<View>(R.id.note_deck_spinner).nextFocusForwardId = newEditText.id
1841+
requireView().findViewById<View>(R.id.note_deck_name).nextFocusForwardId = newEditText.id
18511842
}
18521843
if (previous != null) {
18531844
previous.lastViewInTabOrder.nextFocusForwardId = newEditText.id
@@ -2379,7 +2370,10 @@ class NoteEditorFragment :
23792370
}
23802371

23812372
deckId = calculateDeckId()
2382-
launchCatchingTask { deckSpinnerSelection!!.selectDeckById(deckId, false) }
2373+
launchCatchingTask {
2374+
val selectedDeckName = withCol { decks.name(deckId) }
2375+
view?.findViewById<TextView>(R.id.note_deck_name)?.text = selectedDeckName
2376+
}
23832377
}
23842378

23852379
/** Refreshes the UI using the currently selected note type as a template */
@@ -2393,7 +2387,7 @@ class NoteEditorFragment :
23932387
changeType: FieldChangeType,
23942388
) {
23952389
requireView().findViewById<TextView>(R.id.CardEditorDeckText).isVisible = !currentNotetypeIsImageOcclusion()
2396-
requireView().findViewById<View>(R.id.note_deck_spinner).isVisible = !currentNotetypeIsImageOcclusion()
2390+
requireView().findViewById<View>(R.id.note_deck_name).isVisible = !currentNotetypeIsImageOcclusion()
23972391
editorNote =
23982392
if (note == null || addNote) {
23992393
getColUnsafe.run {
@@ -2784,7 +2778,6 @@ class NoteEditorFragment :
27842778

27852779
refreshNoteData(FieldChangeType.changeFieldCount(shouldReplaceNewlines()))
27862780
setDuplicateFieldStyles()
2787-
deckSpinnerSelection!!.updateDeckPosition(deckId)
27882781
}
27892782

27902783
// ----------------------------------------------------------------------------
@@ -2858,15 +2851,14 @@ class NoteEditorFragment :
28582851
updateTags()
28592852
requireView().findViewById<View>(R.id.CardEditorTagButton).isEnabled = false
28602853
// ((LinearLayout) findViewById(R.id.CardEditorCardsButton)).setEnabled(false);
2861-
deckSpinnerSelection!!.setEnabledActionBarSpinner(false)
2862-
deckSpinnerSelection!!.updateDeckPosition(currentEditedCard!!.did)
2854+
view?.findViewById<TextView>(R.id.note_deck_name)?.isEnabled = false
28632855
updateFieldsFromStickyText()
28642856
} else {
28652857
populateEditFields(FieldChangeType.refresh(shouldReplaceNewlines()), false)
28662858
updateCards(currentEditedCard!!.noteType(getColUnsafe))
28672859
requireView().findViewById<View>(R.id.CardEditorTagButton).isEnabled = true
28682860
// ((LinearLayout) findViewById(R.id.CardEditorCardsButton)).setEnabled(false);
2869-
deckSpinnerSelection!!.setEnabledActionBarSpinner(true)
2861+
view?.findViewById<TextView>(R.id.note_deck_name)?.isEnabled = true
28702862
}
28712863
}
28722864

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,19 @@
6161
android:gravity="start|center_vertical"
6262
android:textStyle="bold"
6363
android:text="@string/CardEditorNoteDeck" />
64-
<Spinner
65-
android:id="@+id/note_deck_spinner"
64+
<!-- different horizontal padding to match note_type_spinner drop down alignment -->
65+
<com.ichi2.ui.FixedTextView
66+
android:id="@+id/note_deck_name"
6667
android:layout_width="match_parent"
6768
android:layout_height="wrap_content"
68-
app:popupTheme="@style/ActionBar.Popup"/>
69+
android:paddingStart="8dp"
70+
android:paddingEnd="12dp"
71+
android:drawableEnd="@drawable/id_arrow_drop_down"
72+
android:background="?attr/selectableItemBackground"
73+
android:minHeight="?attr/listPreferredItemHeightSmall"
74+
android:gravity="center_vertical"
75+
tools:text="Deck name here"
76+
/>
6977
</LinearLayout>
7078

7179
<!-- Front/Back/Attach views added in NoteEditor.populateEditFields(..) -->

0 commit comments

Comments
 (0)