Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 24 additions & 22 deletions AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,13 @@ import com.ichi2.anki.databinding.ActivityHomescreenBinding
import com.ichi2.anki.databinding.IncludeDeckPickerBinding
import com.ichi2.anki.databinding.IncludeFloatingAddButtonBinding
import com.ichi2.anki.deckpicker.BackgroundImage
import com.ichi2.anki.deckpicker.DeckDeletionResult
import com.ichi2.anki.deckpicker.DeckPickerViewModel
import com.ichi2.anki.deckpicker.DeckPickerViewModel.AnkiDroidEnvironment
import com.ichi2.anki.deckpicker.DeckPickerViewModel.FlattenedDeckList
import com.ichi2.anki.deckpicker.DeckPickerViewModel.StartupResponse
import com.ichi2.anki.deckpicker.EmptyCardsResult
import com.ichi2.anki.deckpicker.OptionsMenuState
import com.ichi2.anki.deckpicker.ShortcutData
import com.ichi2.anki.deckpicker.SyncIconState
import com.ichi2.anki.deckpicker.UiEvent
import com.ichi2.anki.dialogs.AsyncDialogFragment
import com.ichi2.anki.dialogs.BackupPromptDialog
import com.ichi2.anki.dialogs.CreateDeckDialog
Expand Down Expand Up @@ -609,15 +607,15 @@ open class DeckPicker :

@Suppress("UNUSED_PARAMETER")
private fun setupFlows() {
fun onDeckDeleted(result: DeckDeletionResult) {
fun onDeckDeleted(event: UiEvent.DeckDeleted) {
floatingActionButtonBinding.fabMain.isVisible = true
showSnackbar(result.toHumanReadableString(), Snackbar.LENGTH_SHORT) {
showSnackbar(event.toHumanReadableString(), Snackbar.LENGTH_SHORT) {
setAction(R.string.undo) { undo() }
}
}

fun onCardsEmptied(result: EmptyCardsResult) {
showSnackbar(result.toHumanReadableString(), Snackbar.LENGTH_SHORT) {
fun onCardsEmptied(event: UiEvent.EmptyCardsDeleted) {
showSnackbar(event.toHumanReadableString(), Snackbar.LENGTH_SHORT) {
setAction(R.string.undo) { undo() }
}
}
Expand Down Expand Up @@ -779,15 +777,20 @@ open class DeckPicker :
.show()
}

viewModel.deckDeletedNotification.launchCollectionInLifecycleScope(::onDeckDeleted)
viewModel.emptyCardsNotification.launchCollectionInLifecycleScope(::onCardsEmptied)
viewModel.flowOfDeckCountsChanged.launchCollectionInLifecycleScope(::onDeckCountsChanged)
viewModel.flowOfDestination.launchCollectionInLifecycleScope(::onDestinationChanged)
viewModel.flowOfExportDeck.launchCollectionInLifecycleScope(::onExportDeck)
viewModel.flowOfCreateShortcut.launchCollectionInLifecycleScope(::createIcon)
viewModel.flowOfDisableShortcuts.launchCollectionInLifecycleScope(::disableDeckAndChildrenShortcuts)
viewModel.onError.launchCollectionInLifecycleScope(::onError)
viewModel.flowOfPromptUserToUpdateScheduler.launchCollectionInLifecycleScope(::onPromptUserToUpdateScheduler)
viewModel.uiEvents.launchCollectionInLifecycleScope { event ->
when (event) {
is UiEvent.DeckDeleted -> onDeckDeleted(event)
is UiEvent.EmptyCardsDeleted -> onCardsEmptied(event)
is UiEvent.Navigate -> onDestinationChanged(event.destination)
is UiEvent.ShowError -> onError(event.message)
is UiEvent.ExportDeck -> onExportDeck(event.deckId)
is UiEvent.CreateShortcut -> createIcon(event)
is UiEvent.DisableShortcuts -> disableDeckAndChildrenShortcuts(event.deckIds)
UiEvent.PromptUpdateScheduler -> onPromptUserToUpdateScheduler(Unit)
UiEvent.DecksReloaded -> onDecksReloaded(Unit)
UiEvent.DeckCountsChanged -> onDeckCountsChanged(Unit)
}
}
viewModel.flowOfOptionsMenuState.filterNotNull().launchCollectionInLifecycleScope(::onOptionsMenuUpdated)
viewModel.flowOfStudiedTodayStats.launchCollectionInLifecycleScope(::onStudiedTodayChanged)
viewModel.flowOfDeckListInInitialState.filterNotNull().launchCollectionInLifecycleScope(::onCollectionStatusChanged)
Expand All @@ -796,7 +799,6 @@ open class DeckPicker :
viewModel.flowOfDeckList.launchCollectionInLifecycleScope(::onDeckListChanged)
viewModel.flowOfFocusedDeck.launchCollectionInLifecycleScope(::onFocusedDeckChanged)
viewModel.flowOfResizingDividerVisible.launchCollectionInLifecycleScope(::onResizingDividerVisibilityChanged)
viewModel.flowOfDecksReloaded.launchCollectionInLifecycleScope(::onDecksReloaded)
viewModel.flowOfStartupResponse.filterNotNull().launchCollectionInLifecycleScope(::onStartupResponse)
}

Expand Down Expand Up @@ -1983,16 +1985,16 @@ open class DeckPicker :
}
}

private fun createIcon(shortcutData: ShortcutData) {
private fun createIcon(event: UiEvent.CreateShortcut) {
// This code should not be reachable with lower versions
val shortcut =
ShortcutInfoCompat
.Builder(this, shortcutData.deckId.toString())
.Builder(this, event.deckId.toString())
.setIntent(
intentToReviewDeckFromShortcuts(this, shortcutData.deckId),
intentToReviewDeckFromShortcuts(this, event.deckId),
).setIcon(IconCompat.createWithResource(this, R.mipmap.ic_launcher))
.setShortLabel(shortcutData.shortLabel)
.setLongLabel(shortcutData.longLabel)
.setShortLabel(event.shortLabel)
.setLongLabel(event.longLabel)
.build()
try {
val success = ShortcutManagerCompat.requestPinShortcut(this, shortcut, null)
Expand Down
Loading
Loading