diff --git a/AnkiDroid/src/main/java/com/ichi2/widget/DeckPickerWidgetConfig.kt b/AnkiDroid/src/main/java/com/ichi2/widget/DeckPickerWidgetConfig.kt index b21987440adf..efacb544b14b 100644 --- a/AnkiDroid/src/main/java/com/ichi2/widget/DeckPickerWidgetConfig.kt +++ b/AnkiDroid/src/main/java/com/ichi2/widget/DeckPickerWidgetConfig.kt @@ -82,11 +82,11 @@ class DeckPickerWidgetConfig : AnkiActivity(), DeckSelectionListener { } deckAdapter = WidgetConfigScreenAdapter { deck, position -> - deckAdapter.removeDeck(position) + deckAdapter.removeDeck(deck.deckId) showSnackbar(R.string.deck_removed_from_widget) updateViewVisibility() updateFabVisibility() - hasUnsavedChanges = true // Set flag when deck is removed + hasUnsavedChanges = true } findViewById(R.id.recyclerViewSelectedDecks).apply { @@ -121,12 +121,13 @@ class DeckPickerWidgetConfig : AnkiActivity(), DeckSelectionListener { // Load the due tree loadDueTree() + // TODO: Implement multi-select functionality so that user can select desired decks in once. findViewById(R.id.fabWidgetDeckPicker).setOnClickListener { lifecycleScope.launch { val tree = dueTree if (tree != null) { // Check if the default deck is the only available deck and there are no cards - val isEmpty = isCollectionEmpty(tree, collectionIsEmpty = true) + val isEmpty = isCollectionEmpty(tree) if (isEmpty) { showSnackbar(R.string.no_decks_available_message) } else { @@ -355,12 +356,11 @@ class DeckPickerWidgetConfig : AnkiActivity(), DeckSelectionListener { * only the default deck and no other cards. * * @param tree The root node of the deck tree, representing the hierarchy of decks. - * @param collectionIsEmpty A flag indicating whether the collection is externally considered empty. * @return `true` if the collection is empty based on the deck tree and the external flag; * `false` otherwise. */ - private fun isCollectionEmpty(tree: DeckNode, collectionIsEmpty: Boolean): Boolean { - val isEmpty = tree.children.size == 1 && tree.children[0].did == 1L && collectionIsEmpty + private fun isCollectionEmpty(tree: DeckNode): Boolean { + val isEmpty = tree.children.size == 1 && tree.children[0].did == 1L Timber.d("isEmpty: $isEmpty") return isEmpty diff --git a/AnkiDroid/src/main/java/com/ichi2/widget/WidgetConfigScreenAdapter.kt b/AnkiDroid/src/main/java/com/ichi2/widget/WidgetConfigScreenAdapter.kt index 80575747ba57..a3bec893eb92 100644 --- a/AnkiDroid/src/main/java/com/ichi2/widget/WidgetConfigScreenAdapter.kt +++ b/AnkiDroid/src/main/java/com/ichi2/widget/WidgetConfigScreenAdapter.kt @@ -53,6 +53,7 @@ class WidgetConfigScreenAdapter( /** Creates and inflates the view for each item in the RecyclerView * @param parent the parent ViewGroup + * @param viewType the type of the view */ override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DeckViewHolder { val view = LayoutInflater.from(parent.context) @@ -77,15 +78,18 @@ class WidgetConfigScreenAdapter( override fun getItemCount(): Int = decks.size - // Adds a new deck to the list and notifies the adapter fun addDeck(deck: SelectableDeck) { decks.add(deck) notifyItemInserted(decks.size - 1) } - fun removeDeck(position: Int) { - decks.removeAt(position) - notifyItemRemoved(position) + fun removeDeck(deckId: Long) { + // Find the position of the deck with the given ID + val position = decks.indexOfFirst { it.deckId == deckId } + if (position != -1) { + decks.removeAt(position) + notifyItemRemoved(position) + } } fun moveDeck(fromPosition: Int, toPosition: Int) { diff --git a/AnkiDroid/src/main/res/layout/widget_deck_picker_drawable_v31.xml b/AnkiDroid/src/main/res/layout/widget_deck_picker_drawable_v31.xml index 47f8f2a8fd3d..85ec97789c67 100644 --- a/AnkiDroid/src/main/res/layout/widget_deck_picker_drawable_v31.xml +++ b/AnkiDroid/src/main/res/layout/widget_deck_picker_drawable_v31.xml @@ -15,10 +15,9 @@