Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
xenonnn4w committed Aug 15, 2024
1 parent 58151c9 commit f384f89
Showing 1 changed file with 49 additions and 9 deletions.
58 changes: 49 additions & 9 deletions AnkiDroid/src/main/java/com/ichi2/widget/DeckPickerWidgetConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,51 @@ class DeckPickerWidgetConfig : AnkiActivity(), DeckSelectionListener {

/** Updates the visibility of the FloatingActionButton based on the number of selected decks */
private fun updateFabVisibility() {
val fab = findViewById<FloatingActionButton>(R.id.fabWidgetDeckPicker)
fab.visibility = if (deckAdapter.itemCount >= MAX_DECKS_ALLOWED) {
View.GONE
} else {
View.VISIBLE
lifecycleScope.launch {
// Check if the default deck is empty
val defaultDeckEmpty = isDefaultDeckEmpty()

// Fetch the total number of selectable decks
val totalSelectableDecks = getTotalSelectableDecks()

// Adjust totalSelectableDecks if the default deck is empty
var adjustedTotalSelectableDecks = totalSelectableDecks
if (defaultDeckEmpty) {
adjustedTotalSelectableDecks -= 1
}

// Get the number of selected decks
val selectedDeckCount = deckAdapter.itemCount
Timber.d("Selected decks count: $selectedDeckCount")

// Determine FAB visibility
val fab = findViewById<FloatingActionButton>(R.id.fabWidgetDeckPicker)
fab.visibility = if (selectedDeckCount >= MAX_DECKS_ALLOWED || selectedDeckCount >= adjustedTotalSelectableDecks) {
View.GONE
} else {
View.VISIBLE
}
}
}

/** Returns the total number of selectable decks. */
private suspend fun getTotalSelectableDecks(): Int {
return withContext(Dispatchers.IO) {
SelectableDeck.fromCollection(includeFiltered = false).size
}
}

private fun isDefaultDeckEmpty(): Boolean {
val tree = dueTree ?: run {
Timber.d("Due tree is null")
return true
}
val defaultDeck = tree.children.find { it.did == 1L }
if (defaultDeck == null) {
return true
}
val defaultDeckHasCards = defaultDeck.hasCardsReadyToStudy()
return !defaultDeckHasCards
}

/** Loads saved preferences and updates the RecyclerView */
Expand Down Expand Up @@ -360,10 +399,11 @@ class DeckPickerWidgetConfig : AnkiActivity(), DeckSelectionListener {
* `false` otherwise.
*/
private fun isCollectionEmpty(tree: DeckNode): Boolean {
val isEmpty = tree.children.size == 1 && tree.children[0].did == 1L
Timber.d("isEmpty: $isEmpty")

return isEmpty
if (tree.children.size == 1 && tree.children[0].did == 1L) {
val defaultDeckHasCards = tree.children[0].hasCardsReadyToStudy()
return !defaultDeckHasCards
}
return false
}

/**
Expand Down

0 comments on commit f384f89

Please sign in to comment.