Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
xenonnn4w committed Aug 14, 2024
1 parent 99aaf2b commit 870ef4f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<RecyclerView>(R.id.recyclerViewSelectedDecks).apply {
Expand Down Expand Up @@ -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<FloatingActionButton>(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 {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
<TextView
android:id="@+id/deckName1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_height="45dp"
android:layout_weight="3"
android:height="48dp"
android:paddingBottom="4dp"
android:paddingStart="15dp"
android:paddingTop="10dp"
android:text="@string/deck1Name_deck_picker_widget"
Expand Down Expand Up @@ -68,10 +67,10 @@
<TextView
android:id="@+id/deckName2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_height="45dp"
android:layout_weight="3"
android:height="48dp"
android:paddingBottom="4dp"
android:paddingBottom="1dp"
android:paddingStart="15dp"
android:paddingTop="10dp"
android:text="@string/deck2Name_deck_picker_widget"
Expand Down Expand Up @@ -121,7 +120,7 @@
<TextView
android:id="@+id/deckName3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_height="45dp"
android:layout_weight="3"
android:height="48dp"
android:paddingBottom="4dp"
Expand Down
2 changes: 1 addition & 1 deletion AnkiDroid/src/main/res/layout/widget_item_deck_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
android:layout_height="wrap_content"
android:layout_weight="3"
android:height="48dp"
android:paddingBottom="12dp"
android:paddingBottom="2dp"
android:paddingEnd="12dp"
android:paddingStart="15dp"
android:paddingTop="10dp"
Expand Down

0 comments on commit 870ef4f

Please sign in to comment.