Skip to content

Commit

Permalink
Issue 8602, Reviewer startActivityForResult Migration (#14842)
Browse files Browse the repository at this point in the history
* Issue 8602, Reviewer startActivityForResult Migration

- In AbstractFlashcardViewer,
-- Created common FlashCardViewerResultCallback inner class to pass to registerForActivityResult function. This will capture the common result code check behavior that was being done in onActivityResult and apply it to every ActivityResultLauncher when using the new registerForActivityResult API. There was only one request code being processed, but the pattern is consistent with the other classes that were updated.
-- In FlashCardViewerResultCallback, gave the lambda a default do-nothing implementation. The ADD_NOTE request code used in Reviewer doesn't specifically do anything with a result, but the common checks that FlashCardViewerResultCallback does still needs to be done for every activity result.
-- Replaced startActivityForResult for EDIT_CURRENT_CARD with registerForActivityResult API implementation.
-- Removed the now unused onActivityResult function override

- In Reviewer,
-- Replaced startActivityForResult for ADD_NOTE with registerForActivityResult API implementation.

* - removed unused const
- formatting
  • Loading branch information
nfragiskatos authored Dec 6, 2023
1 parent da2c694 commit 8718479
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 34 deletions.
73 changes: 41 additions & 32 deletions AnkiDroid/src/main/java/com/ichi2/anki/AbstractFlashcardViewer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import android.view.inputmethod.InputMethodManager
import android.webkit.*
import android.webkit.WebView.HitTestResult
import android.widget.*
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.CheckResult
import androidx.annotation.IdRes
Expand Down Expand Up @@ -281,6 +283,44 @@ abstract class AbstractFlashcardViewer :
* @see refreshIfRequired
*/
private var refreshRequired: ViewerRefresh? = null

private val editCurrentCardLauncher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult(),
FlashCardViewerResultCallback { result, reloadRequired ->
if (result.resultCode == RESULT_OK) {
// content of note was changed so update the note and current card
Timber.i("AbstractFlashcardViewer:: Saving card...")
launchCatchingTask { saveEditedCard() }
onEditedNoteChanged()
} else if (result.resultCode == RESULT_CANCELED && !reloadRequired) {
// nothing was changed by the note editor so just redraw the card
redrawCard()
}
}
)
protected inner class FlashCardViewerResultCallback(
private val callback: (result: ActivityResult, reloadRequired: Boolean) -> Unit = { _, _ -> }
) : ActivityResultCallback<ActivityResult> {
override fun onActivityResult(result: ActivityResult) {
if (result.resultCode == DeckPicker.RESULT_DB_ERROR) {
closeReviewer(DeckPicker.RESULT_DB_ERROR)
}
if (result.resultCode == DeckPicker.RESULT_MEDIA_EJECTED) {
finishNoStorageAvailable()
}

/* Reset the schedule and reload the latest card off the top of the stack if required.
The card could have been rescheduled, the deck could have changed, or a change of
note type could have lead to the card being deleted */
val reloadRequired = result.data?.getBooleanExtra(NoteEditor.RELOAD_REQUIRED_EXTRA_KEY, false) == true
if (reloadRequired) {
performReload()
}

callback(result, reloadRequired)
}
}

init {
ChangeManager.subscribe(this)
}
Expand Down Expand Up @@ -707,36 +747,6 @@ abstract class AbstractFlashcardViewer :
performReload()
}

// super.onActivityResult
public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == DeckPicker.RESULT_DB_ERROR) {
closeReviewer(DeckPicker.RESULT_DB_ERROR)
}
if (resultCode == DeckPicker.RESULT_MEDIA_EJECTED) {
finishNoStorageAvailable()
}

/* Reset the schedule and reload the latest card off the top of the stack if required.
The card could have been rescheduled, the deck could have changed, or a change of
note type could have lead to the card being deleted */
val reloadRequired = data?.getBooleanExtra(NoteEditor.RELOAD_REQUIRED_EXTRA_KEY, false) == true
if (reloadRequired) {
performReload()
}
if (requestCode == EDIT_CURRENT_CARD) {
if (resultCode == RESULT_OK) {
// content of note was changed so update the note and current card
Timber.i("AbstractFlashcardViewer:: Saving card...")
launchCatchingTask { saveEditedCard() }
onEditedNoteChanged()
} else if (resultCode == RESULT_CANCELED && !reloadRequired) {
// nothing was changed by the note editor so just redraw the card
redrawCard()
}
}
}

/**
* Whether the class should use collection.getSched() when performing tasks.
* The aim of this method is to completely distinguish FlashcardViewer from Reviewer
Expand Down Expand Up @@ -816,7 +826,7 @@ abstract class AbstractFlashcardViewer :
editCard.putExtra(NoteEditor.EXTRA_CALLER, NoteEditor.CALLER_REVIEWER_EDIT)
editCard.putExtra(FINISH_ANIMATION_EXTRA, getInverseTransition(animation) as Parcelable)
editorCard = currentCard
startActivityForResultWithAnimation(editCard, EDIT_CURRENT_CARD, animation)
launchActivityForResultWithAnimation(editCard, editCurrentCardLauncher, animation)
}

fun generateQuestionSoundList() {
Expand Down Expand Up @@ -2576,7 +2586,6 @@ abstract class AbstractFlashcardViewer :
/**
* Available options performed by other activities.
*/
const val EDIT_CURRENT_CARD = 0
const val EASE_1 = 1
const val EASE_2 = 2
const val EASE_3 = 3
Expand Down
9 changes: 7 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/anki/Reviewer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import android.text.style.UnderlineSpan
import android.view.*
import android.webkit.JavascriptInterface
import android.widget.*
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.*
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.view.menu.MenuBuilder
Expand Down Expand Up @@ -151,6 +152,11 @@ open class Reviewer :
protected val mProcessor = PeripheralKeymap(this, this)
private val mOnboarding = Onboarding.Reviewer(this)

private val addNoteLauncher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult(),
FlashCardViewerResultCallback()
)

override fun onCreate(savedInstanceState: Bundle?) {
if (showedActivityFailedScreen(savedInstanceState)) {
return
Expand Down Expand Up @@ -660,7 +666,7 @@ open class Reviewer :
val animation = getAnimationTransitionFromGesture(fromGesture)
intent.putExtra(NoteEditor.EXTRA_CALLER, NoteEditor.CALLER_REVIEWER_ADD)
intent.putExtra(FINISH_ANIMATION_EXTRA, getInverseTransition(animation) as Parcelable)
startActivityForResultWithAnimation(intent, ADD_NOTE, animation)
launchActivityForResultWithAnimation(intent, addNoteLauncher, animation)
}

@NeedsTest("Starting animation from swipe is inverse to the finishing one")
Expand Down Expand Up @@ -1641,7 +1647,6 @@ open class Reviewer :
}

companion object {
private const val ADD_NOTE = 12
private const val REQUEST_AUDIO_PERMISSION = 0
private const val ANIMATION_DURATION = 200
private const val TRANSPARENCY = 0.90f
Expand Down

0 comments on commit 8718479

Please sign in to comment.