0
0
mirror of https://github.com/ankidroid/Anki-Android.git synced 2024-09-19 19:42:17 +02:00

Refactor isCollectionEmpty method to DeckUtils for reuse in multiple classes

This commit is contained in:
Anoop 2024-08-31 16:31:42 +05:30 committed by David Allison
parent 9d3cb64a5b
commit b9be27f168
3 changed files with 20 additions and 33 deletions

View File

@ -52,4 +52,22 @@ object DeckUtils {
}
}
}
/**
* Returns whether the deck picker displays any deck.
* Technically, it means that there is a non default deck, or that the default deck is non-empty.
*
* This function is specifically implemented to address an issue where the default deck
* isn't handled correctly when a second deck is added to the
* collection. In this case, the deck tree may incorrectly appear as non-empty when it contains
* only the default deck and no other cards.
*
*/
suspend fun isCollectionEmpty(): Boolean {
val tree = withCol { sched.deckDueTree() }
if (tree.children.size == 1 && tree.children[0].did == 1L) {
return isDefaultDeckEmpty()
}
return false
}
}

View File

@ -33,8 +33,7 @@ import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.snackbar.Snackbar
import com.ichi2.anki.AnkiActivity
import com.ichi2.anki.CollectionManager.withCol
import com.ichi2.anki.DeckUtils
import com.ichi2.anki.DeckUtils.isCollectionEmpty
import com.ichi2.anki.R
import com.ichi2.anki.dialogs.DeckSelectionDialog
import com.ichi2.anki.dialogs.DeckSelectionDialog.DeckSelectionListener
@ -214,10 +213,6 @@ class CardAnalysisWidgetConfig : AnkiActivity(), DeckSelectionListener, BaseSnac
}
}
private suspend fun isDefaultDeckEmpty(): Boolean {
return DeckUtils.isDefaultDeckEmpty()
}
/** Updates the view according to the saved preference for appWidgetId.*/
fun updateViewWithSavedPreferences() {
val selectedDeckId = cardAnalysisWidgetPreferences.getSelectedDeckIdFromPreferences(appWidgetId) ?: return
@ -336,14 +331,6 @@ class CardAnalysisWidgetConfig : AnkiActivity(), DeckSelectionListener, BaseSnac
context?.let { cardAnalysisWidgetPreferences.deleteDeckData(appWidgetId) }
}
}
private suspend fun isCollectionEmpty(): Boolean {
val tree = withCol { sched.deckDueTree() }
if (tree.children.size == 1 && tree.children[0].did == 1L) {
return isDefaultDeckEmpty()
}
return false
}
}
fun ContextWrapper.unregisterReceiverSilently(receiver: BroadcastReceiver) {

View File

@ -34,8 +34,8 @@ import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.snackbar.Snackbar
import com.ichi2.anki.AnkiActivity
import com.ichi2.anki.CollectionManager.withCol
import com.ichi2.anki.DeckUtils
import com.ichi2.anki.DeckUtils.isCollectionEmpty
import com.ichi2.anki.R
import com.ichi2.anki.dialogs.DeckSelectionDialog
import com.ichi2.anki.dialogs.DeckSelectionDialog.DeckSelectionListener
@ -444,24 +444,6 @@ class DeckPickerWidgetConfig : AnkiActivity(), DeckSelectionListener, BaseSnackb
context?.let { deckPickerWidgetPreferences.deleteDeckData(appWidgetId) }
}
}
/**
* Returns whether the deck picker displays any deck.
* Technically, it means that there is a non default deck, or that the default deck is non-empty.
*
* This function is specifically implemented to address an issue where the default deck
* isn't handled correctly when a second deck is added to the
* collection. In this case, the deck tree may incorrectly appear as non-empty when it contains
* only the default deck and no other cards.
*
*/
private suspend fun isCollectionEmpty(): Boolean {
val tree = withCol { sched.deckDueTree() }
if (tree.children.size == 1 && tree.children[0].did == 1L) {
return isDefaultDeckEmpty()
}
return false
}
}
/**