Skip to content

Commit

Permalink
Fix part of oppia#5404: Migrate away from onBackPressed (oppia#5521)
Browse files Browse the repository at this point in the history
<!-- READ ME FIRST: Please fill in the explanation section below and
check off every point from the Essential Checklist! -->

## Explanation
<!-- 
- Explain what your PR does. If this PR fixes an existing bug, please
include
- "Fixes #bugnum:" in the explanation so that GitHub can auto-close the
issue
  - when this PR is merged.
  -->
Fixes part of oppia#5404 

This PR migrates deprecated `onBackPressed` usage to
`OnBackPressedDispatcher` callback in the following activities and
presenters.

- AdministratorControlsActivity
- AppVersionActivity
- ProfileAndDeviceIdActivity
- MarkChaptersCompletedActivity
- MarkStoriesCompletedActivity
- MarkTopicsCompletedActivity
- ReadingTextSizeActivityPresenter
- ExplorationActivityPresenter
- ResumeLessonActivityPresenter


## Essential Checklist
<!-- Please tick the relevant boxes by putting an "x" in them. -->
- [x] The PR title and explanation each start with "Fix #bugnum: " (If
this PR fixes part of an issue, prefix the title with "Fix part of
#bugnum: ...".)
- [x] Any changes to
[scripts/assets](https://github.com/oppia/oppia-android/tree/develop/scripts/assets)
files have their rationale included in the PR explanation.
- [x] The PR follows the [style
guide](https://github.com/oppia/oppia-android/wiki/Coding-style-guide).
- [x] The PR does not contain any unnecessary code changes from Android
Studio
([reference](https://github.com/oppia/oppia-android/wiki/Guidance-on-submitting-a-PR#undo-unnecessary-changes)).
- [x] The PR is made from a branch that's **not** called "develop" and
is up-to-date with "develop".
- [x] The PR is **assigned** to the appropriate reviewers
([reference](https://github.com/oppia/oppia-android/wiki/Guidance-on-submitting-a-PR#clarification-regarding-assignees-and-reviewers-section)).

## For UI-specific PRs only
<!-- Delete these section if this PR does not include UI-related
changes. -->
If your PR includes UI-related changes, then:
- Add screenshots for portrait/landscape for both a tablet & phone of
the before & after UI changes
- For the screenshots above, include both English and pseudo-localized
(RTL) screenshots (see [RTL
guide](https://github.com/oppia/oppia-android/wiki/RTL-Guidelines))
- Add a video showing the full UX flow with a screen reader enabled (see
[accessibility
guide](https://github.com/oppia/oppia-android/wiki/Accessibility-A11y-Guide))
- For PRs introducing new UI elements or color changes, both light and
dark mode screenshots must be included
- Add a screenshot demonstrating that you ran affected Espresso tests
locally & that they're passing

---------

Co-authored-by: Adhiambo Peres <59600948+adhiamboperes@users.noreply.github.com>
  • Loading branch information
dattasneha and adhiamboperes authored Sep 19, 2024
1 parent dfb9a30 commit 8d44bf7
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.oppia.android.app.administratorcontrols
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.OnBackPressedCallback
import org.oppia.android.R
import org.oppia.android.app.activity.ActivityComponentImpl
import org.oppia.android.app.activity.InjectableAutoLocalizedAppCompatActivity
Expand Down Expand Up @@ -83,6 +84,15 @@ class AdministratorControlsActivity :
isProfileDeletionDialogVisible
)
title = resourceHandler.getStringInLocale(R.string.administrator_controls)

onBackPressedDispatcher.addCallback(
this,
object : OnBackPressedCallback(/* enabled = */ true) {
override fun handleOnBackPressed() {
this@AdministratorControlsActivity.handleBackPress()
}
}
)
}

override fun routeToAppVersion() {
Expand Down Expand Up @@ -121,7 +131,7 @@ class AdministratorControlsActivity :
}
}

override fun onBackPressed() {
private fun handleBackPress() {
val fragment =
supportFragmentManager.findFragmentById(
R.id.administrator_controls_fragment_multipane_placeholder
Expand All @@ -134,8 +144,7 @@ class AdministratorControlsActivity :
if (fragment is ProfileEditFragment) {
administratorControlsActivityPresenter.handleOnBackPressed()
} else {
@Suppress("DEPRECATION") // TODO(#5404): Migrate to a back pressed dispatcher.
super.onBackPressed()
finish()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import androidx.activity.OnBackPressedCallback
import org.oppia.android.app.activity.ActivityComponentImpl
import org.oppia.android.app.activity.InjectableAutoLocalizedAppCompatActivity
import org.oppia.android.app.model.ScreenName.APP_VERSION_ACTIVITY
Expand All @@ -19,12 +20,20 @@ class AppVersionActivity : InjectableAutoLocalizedAppCompatActivity() {
super.onCreate(savedInstanceState)
(activityComponent as ActivityComponentImpl).inject(this)
appVersionActivityPresenter.handleOnCreate()

onBackPressedDispatcher.addCallback(
this,
object : OnBackPressedCallback(/* enabled = */ true) {
override fun handleOnBackPressed() {
finish()
}
}
)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == android.R.id.home) {
@Suppress("DEPRECATION") // TODO(#5404): Migrate to a back pressed dispatcher.
onBackPressed()
onBackPressedDispatcher.onBackPressed()
}
return super.onOptionsItemSelected(item)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import androidx.activity.OnBackPressedCallback
import org.oppia.android.app.activity.ActivityComponentImpl
import org.oppia.android.app.activity.InjectableAutoLocalizedAppCompatActivity
import org.oppia.android.app.model.ScreenName.PROFILE_AND_DEVICE_ID_ACTIVITY
Expand All @@ -18,18 +19,27 @@ import javax.inject.Inject
* a particular user or group.
*/
class ProfileAndDeviceIdActivity : InjectableAutoLocalizedAppCompatActivity() {
@Inject lateinit var profileAndDeviceIdActivityPresenter: ProfileAndDeviceIdActivityPresenter
@Inject
lateinit var profileAndDeviceIdActivityPresenter: ProfileAndDeviceIdActivityPresenter

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
(activityComponent as ActivityComponentImpl).inject(this)
profileAndDeviceIdActivityPresenter.handleOnCreate()

onBackPressedDispatcher.addCallback(
this,
object : OnBackPressedCallback(/* enabled = */ true) {
override fun handleOnBackPressed() {
finish()
}
}
)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == android.R.id.home) {
@Suppress("DEPRECATION") // TODO(#5404): Migrate to a back pressed dispatcher.
onBackPressed()
onBackPressedDispatcher.onBackPressed()
}
return super.onOptionsItemSelected(item)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import androidx.activity.OnBackPressedCallback
import org.oppia.android.R
import org.oppia.android.app.activity.ActivityComponentImpl
import org.oppia.android.app.activity.InjectableAutoLocalizedAppCompatActivity
Expand Down Expand Up @@ -37,12 +38,20 @@ class MarkChaptersCompletedActivity : InjectableAutoLocalizedAppCompatActivity()
val showConfirmationNotice = args?.showConfirmationNotice ?: false
markChaptersCompletedActivityPresenter.handleOnCreate(internalProfileId, showConfirmationNotice)
title = resourceHandler.getStringInLocale(R.string.mark_chapters_completed_activity_title)

onBackPressedDispatcher.addCallback(
this,
object : OnBackPressedCallback(/* enabled = */ true) {
override fun handleOnBackPressed() {
finish()
}
}
)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == android.R.id.home) {
@Suppress("DEPRECATION") // TODO(#5404): Migrate to a back pressed dispatcher.
onBackPressed()
onBackPressedDispatcher.onBackPressed()
}
return super.onOptionsItemSelected(item)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import androidx.activity.OnBackPressedCallback
import org.oppia.android.R
import org.oppia.android.app.activity.ActivityComponentImpl
import org.oppia.android.app.activity.InjectableAutoLocalizedAppCompatActivity
Expand Down Expand Up @@ -34,12 +35,20 @@ class MarkStoriesCompletedActivity : InjectableAutoLocalizedAppCompatActivity()
internalProfileId = profileId?.internalId ?: -1
markStoriesCompletedActivityPresenter.handleOnCreate(internalProfileId)
title = resourceHandler.getStringInLocale(R.string.mark_stories_completed_activity_title)

onBackPressedDispatcher.addCallback(
this,
object : OnBackPressedCallback(/* enabled = */ true) {
override fun handleOnBackPressed() {
finish()
}
}
)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == android.R.id.home) {
@Suppress("DEPRECATION") // TODO(#5404): Migrate to a back pressed dispatcher.
onBackPressed()
onBackPressedDispatcher.onBackPressed()
}
return super.onOptionsItemSelected(item)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import androidx.activity.OnBackPressedCallback
import org.oppia.android.R
import org.oppia.android.app.activity.ActivityComponentImpl
import org.oppia.android.app.activity.InjectableAutoLocalizedAppCompatActivity
Expand Down Expand Up @@ -34,12 +35,20 @@ class MarkTopicsCompletedActivity : InjectableAutoLocalizedAppCompatActivity() {
internalProfileId = profileId?.internalId ?: -1
markTopicsCompletedActivityPresenter.handleOnCreate(internalProfileId)
title = resourceHandler.getStringInLocale(R.string.mark_topics_completed_activity_title)

onBackPressedDispatcher.addCallback(
this,
object : OnBackPressedCallback(/* enabled = */ true) {
override fun handleOnBackPressed() {
finish()
}
}
)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == android.R.id.home) {
@Suppress("DEPRECATION") // TODO(#5404): Migrate to a back pressed dispatcher.
onBackPressed()
onBackPressedDispatcher.onBackPressed()
}
return super.onOptionsItemSelected(item)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.oppia.android.app.options
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.OnBackPressedCallback
import org.oppia.android.app.activity.ActivityComponentImpl
import org.oppia.android.app.activity.InjectableAutoLocalizedAppCompatActivity
import org.oppia.android.app.model.ReadingTextSize
Expand Down Expand Up @@ -34,6 +35,22 @@ class ReadingTextSizeActivity : InjectableAutoLocalizedAppCompatActivity() {
savedInstanceState?.retrieveStateBundle()?.selectedReadingTextSize
?: retrieveActivityParams().readingTextSize
readingTextSizeActivityPresenter.handleOnCreate(readingTextSize)

onBackPressedDispatcher.addCallback(
this,
object : OnBackPressedCallback(/* enabled = */ true) {
override fun handleOnBackPressed() {
val resultBundle = ReadingTextSizeActivityResultBundle.newBuilder().apply {
selectedReadingTextSize = readingTextSizeActivityPresenter.getSelectedReadingTextSize()
}.build()
val intent = Intent().apply {
putProtoExtra(MESSAGE_READING_TEXT_SIZE_RESULTS_KEY, resultBundle)
}
setResult(RESULT_OK, intent)
finish()
}
}
)
}

companion object {
Expand All @@ -60,17 +77,6 @@ class ReadingTextSizeActivity : InjectableAutoLocalizedAppCompatActivity() {
outState.putProto(ACTIVITY_SAVED_STATE_KEY, stateBundle)
}

override fun onBackPressed() {
val resultBundle = ReadingTextSizeActivityResultBundle.newBuilder().apply {
selectedReadingTextSize = readingTextSizeActivityPresenter.getSelectedReadingTextSize()
}.build()
val intent = Intent().apply {
putProtoExtra(MESSAGE_READING_TEXT_SIZE_RESULTS_KEY, resultBundle)
}
setResult(RESULT_OK, intent)
finish()
}

private fun retrieveActivityParams() =
intent.getProtoExtra(ACTIVITY_PARAMS_KEY, ReadingTextSizeActivityParams.getDefaultInstance())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ class ReadingTextSizeActivityPresenter @Inject constructor(
private fun setToolbar() {
val readingTextSizeToolbar: Toolbar = activity.findViewById(R.id.reading_text_size_toolbar)
readingTextSizeToolbar.setNavigationOnClickListener {
@Suppress("DEPRECATION") // TODO(#5404): Migrate to a back pressed dispatcher.
activity.onBackPressed()
activity.onBackPressedDispatcher.onBackPressed()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.oppia.android.app.player.exploration
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.OnBackPressedCallback
import org.oppia.android.app.activity.ActivityComponentImpl
import org.oppia.android.app.activity.InjectableAutoLocalizedAppCompatActivity
import org.oppia.android.app.hintsandsolution.HintsAndSolutionDialogFragment
Expand Down Expand Up @@ -48,7 +49,8 @@ class ExplorationActivity :
BottomSheetOptionsMenuItemClickListener,
RequestVoiceOverIconSpotlightListener {

@Inject lateinit var explorationActivityPresenter: ExplorationActivityPresenter
@Inject
lateinit var explorationActivityPresenter: ExplorationActivityPresenter
private lateinit var state: State
private lateinit var writtenTranslationContext: WrittenTranslationContext

Expand All @@ -67,6 +69,14 @@ class ExplorationActivity :
params.parentScreen,
params.isCheckpointingEnabled
)
onBackPressedDispatcher.addCallback(
this,
object : OnBackPressedCallback(/* enabled = */ true) {
override fun handleOnBackPressed() {
explorationActivityPresenter.backButtonPressed()
}
}
)
}

// TODO(#1655): Re-restrict access to fields in tests post-Gradle.
Expand Down Expand Up @@ -114,10 +124,6 @@ class ExplorationActivity :
getProtoExtra(PARAMS_KEY, ExplorationActivityParams.getDefaultInstance())
}

override fun onBackPressed() {
explorationActivityPresenter.backButtonPressed()
}

override fun deleteCurrentProgressAndStopSession(isCompletion: Boolean) {
explorationActivityPresenter.deleteCurrentProgressAndStopExploration(isCompletion)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ class ExplorationActivityPresenter @Inject constructor(
}

binding.explorationToolbar.setNavigationOnClickListener {
@Suppress("DEPRECATION") // TODO(#5404): Migrate to a back pressed dispatcher.
activity.onBackPressed()
activity.onBackPressedDispatcher.onBackPressed()
}

binding.actionAudioPlayer.setOnClickListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.oppia.android.app.resumelesson
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.OnBackPressedCallback
import org.oppia.android.app.activity.ActivityComponentImpl
import org.oppia.android.app.activity.InjectableAutoLocalizedAppCompatActivity
import org.oppia.android.app.home.RouteToExplorationListener
Expand Down Expand Up @@ -41,6 +42,15 @@ class ResumeLessonActivity :
params.parentScreen,
params.checkpoint
)
onBackPressedDispatcher.addCallback(
this,
object : OnBackPressedCallback(/* enabled = */ true) {
override fun handleOnBackPressed() {
resumeLessonActivityPresenter.setReadingTextSizeNormal()
finish()
}
}
)
}

// TODO(#1655): Re-restrict access to fields in tests post-Gradle.
Expand Down Expand Up @@ -113,9 +123,4 @@ class ResumeLessonActivity :
override fun onDefaultFontSizeLoaded(readingTextSize: ReadingTextSize) {
resumeLessonActivityPresenter.loadResumeLessonFragment(readingTextSize)
}

override fun onBackPressed() {
resumeLessonActivityPresenter.setReadingTextSizeNormal()
finish()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ class ResumeLessonActivityPresenter @Inject constructor(
context = activity,
ReadingTextSize.MEDIUM_TEXT_SIZE
)
@Suppress("DEPRECATION") // TODO(#5404): Migrate to a back pressed dispatcher.
activity.onBackPressed()
activity.onBackPressedDispatcher.onBackPressed()
}
}

Expand Down

0 comments on commit 8d44bf7

Please sign in to comment.