Skip to content

Commit

Permalink
refactor: improve logs related to deckPicker in StudyOptionsFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
criticalAY committed Oct 22, 2024
1 parent f013c2e commit 26f1393
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions AnkiDroid/src/main/java/com/ichi2/anki/StudyOptionsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ class StudyOptionsFragment : Fragment(), ChangeManager.Subscriber, Toolbar.OnMen
private var createMenuJob: Job? = null

/** Current activity.*/
// TODO: nullability is temporary while #17284 is being diagnosed
private val deckPicker: DeckPicker?
get() = activity as? DeckPicker
get() = requireActivity() as? DeckPicker

// Flag to indicate if the fragment should load the deck options immediately after it loads
private var loadWithDeckOptions = false
Expand Down Expand Up @@ -145,6 +146,11 @@ class StudyOptionsFragment : Fragment(), ChangeManager.Subscriber, Toolbar.OnMen
}
}

/** Executes [action] if [DeckPicker] is not null, otherwise report the calling site **/
private inline fun DeckPicker?.executeDeckAction(actionName: String, action: DeckPicker.() -> Unit) {
this?.action() ?: CrashReportService.sendExceptionReport("Failed to $actionName: deckPicker is null", "StudyOptionsFragment")
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
Timber.i("onCreateView()")
val studyOptionsView = inflater.inflate(R.layout.studyoptions_fragment, container, false)
Expand Down Expand Up @@ -285,20 +291,20 @@ class StudyOptionsFragment : Fragment(), ChangeManager.Subscriber, Toolbar.OnMen
return true
}
R.id.action_rename -> {
deckPicker?.renameDeckDialog(col!!.decks.selected()) ?: run {
Timber.w("Failed to rename deck: deckPicker is null")
deckPicker.executeDeckAction("rename deck") {
renameDeckDialog(col!!.decks.selected())
}
return true
}
R.id.action_delete -> {
deckPicker?.confirmDeckDeletion(col!!.decks.selected()) ?: run {
Timber.w("StudyOptionsFragment:: Failed to delete deck: deckPicker is null")
deckPicker.executeDeckAction("delete deck") {
confirmDeckDeletion(col!!.decks.selected())
}
return true
}
R.id.action_export_deck -> {
deckPicker?.exportDeck(col!!.decks.selected()) ?: run {
Timber.w("StudyOptionsFragment:: Failed to export deck: deckPicker is null")
deckPicker.executeDeckAction("export deck") {
exportDeck(col!!.decks.selected())
}
return true
}
Expand Down Expand Up @@ -376,20 +382,18 @@ class StudyOptionsFragment : Fragment(), ChangeManager.Subscriber, Toolbar.OnMen
}
menu.removeItem(R.id.action_export_collection)

deckPicker?.let {
it.setupMediaSyncMenuItem(menu)
it.updateMenuFromState(menu)
deckPicker.executeDeckAction("configureToolbarInternal:: unable to set up menu items") {
setupMediaSyncMenuItem(menu)
updateMenuFromState(menu)

/**
* Launch a task to possibly update the visible icons.
* Save the job in a member variable to manage the lifecycle
*/
createMenuJob = launchCatchingTask {
it.updateMenuState()
it.updateMenuFromState(menu)
updateMenuState()
updateMenuFromState(menu)
}
} ?: run {
Timber.w("deckPicker is null, unable to set up menu items")
}
} else {
menu.setGroupVisible(R.id.commonItems, false)
Expand Down Expand Up @@ -441,6 +445,13 @@ class StudyOptionsFragment : Fragment(), ChangeManager.Subscriber, Toolbar.OnMen

private var onRequestReviewActivityResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
Timber.i("StudyOptionsFragment::mOnRequestReviewActivityResult")

if (!isAdded) {
Timber.d("Fragment not added to the activity")
CrashReportService.sendExceptionReport("Fragment is not added to activity", "StudyOptionsFragment")
return@registerForActivityResult
}

Timber.d("Handling onActivityResult for StudyOptionsFragment (openReview, resultCode = %d)", result.resultCode)
if (toolbar != null) {
configureToolbar() // FIXME we were crashing here because mToolbar is null #8913
Expand All @@ -461,6 +472,13 @@ class StudyOptionsFragment : Fragment(), ChangeManager.Subscriber, Toolbar.OnMen
}
private var onDeckOptionsActivityResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
Timber.i("StudyOptionsFragment::mOnDeckOptionsActivityResult")

if (!isAdded) {
Timber.d("Fragment not added to the activity")
CrashReportService.sendExceptionReport("Fragment is not added to activity", "StudyOptionsFragment")
return@registerForActivityResult
}

Timber.d("Handling onActivityResult for StudyOptionsFragment (deckOptions/filteredDeckOptions, resultCode = %d)", result.resultCode)
configureToolbar()
if (result.resultCode == DeckPicker.RESULT_DB_ERROR || result.resultCode == DeckPicker.RESULT_MEDIA_EJECTED) {
Expand Down

0 comments on commit 26f1393

Please sign in to comment.