Skip to content

Commit

Permalink
refractor: AdministratorControlActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
Tejas-67 committed Jun 17, 2024
1 parent cd2cf5d commit 53efd31
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package org.oppia.android.app.administratorcontrols

import android.content.Context
import android.content.Intent
import android.os.Build
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 @@ -86,6 +88,46 @@ class AdministratorControlsActivity :
isProfileDeletionDialogVisible
)
title = resourceHandler.getStringInLocale(R.string.administrator_controls)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
onBackInvokedDispatcher.registerOnBackInvokedCallback(1000) {
val fragment =
supportFragmentManager.findFragmentById(
R.id.administrator_controls_fragment_multipane_placeholder
)
/*
* If the current fragment is ProfileListFragment then the activity should end on back press.
* If it's instead ProfileEditFragment then profileListFragment should be inflated via
* handleOnBackPressed.
*/
if (fragment is ProfileEditFragment) {
administratorControlsActivityPresenter.handleOnBackPressed()
} else {
onBackPressedDispatcher.onBackPressed()
}
}
} else {
onBackPressedDispatcher.addCallback(
this@AdministratorControlsActivity, object: OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
val fragment =
supportFragmentManager.findFragmentById(
R.id.administrator_controls_fragment_multipane_placeholder
)
/*
* If the current fragment is ProfileListFragment then the activity should end on back press.
* If it's instead ProfileEditFragment then profileListFragment should be inflated via
* handleOnBackPressed.
*/
if (fragment is ProfileEditFragment) {
administratorControlsActivityPresenter.handleOnBackPressed()
} else {
isEnabled = false
onBackPressedDispatcher.onBackPressed()
isEnabled = true
}
}
})
}
}

override fun routeToAppVersion() {
Expand Down Expand Up @@ -125,24 +167,6 @@ class AdministratorControlsActivity :
}
}

override fun onBackPressed() {
val fragment =
supportFragmentManager.findFragmentById(
R.id.administrator_controls_fragment_multipane_placeholder
)
/*
* If the current fragment is ProfileListFragment then the activity should end on back press.
* If it's instead ProfileEditFragment then profileListFragment should be inflated via
* handleOnBackPressed.
*/
if (fragment is ProfileEditFragment) {
administratorControlsActivityPresenter.handleOnBackPressed()
} else {
@Suppress("DEPRECATION") // TODO(#5404): Migrate to a back pressed dispatcher.
super.onBackPressed()
}
}

override fun loadProfileList() {
lastLoadedFragment = PROFILE_LIST_FRAGMENT
administratorControlsActivityPresenter
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.Build
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.ScreenName.PROFILE_EDIT_ACTIVITY
Expand Down Expand Up @@ -53,30 +54,22 @@ class ProfileEditActivity : InjectableAutoLocalizedAppCompatActivity() {
startActivity(intent)
}
}
} else {
onBackPressedDispatcher.addCallback(
this@ProfileEditActivity, object: OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
val isMultipane: Boolean = intent.extras!!.getBoolean(IS_MULTIPANE_EXTRA_KEY, false)
if (isMultipane) {
onBackPressedDispatcher.onBackPressed()
} else {
val intent = Intent(this@ProfileEditActivity, ProfileListActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(intent)
}
}
})
}
(activityComponent as ActivityComponentImpl).inject(this)
profileEditActivityPresenter.handleOnCreate()
}

override fun onBackPressed() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
val isMultipane: Boolean = intent.extras!!.getBoolean(IS_MULTIPANE_EXTRA_KEY, false)
if (isMultipane) {
@Suppress("DEPRECATION")
super.onBackPressed()
} else {
val intent = Intent(this, ProfileListActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(intent)
}
}
}

fun handleBackPressFromPresenter() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
onBackPressedDispatcher.onBackPressed()
} else {
onBackPressed()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ProfileEditActivityPresenter @Inject constructor(
val isMultipane = activity.intent.getBooleanExtra(IS_MULTIPANE_EXTRA_KEY, false)

toolbar.setNavigationOnClickListener {
getProfileEditActivity()?.handleBackPressFromPresenter()
activity.onBackPressedDispatcher.onBackPressed()
}

if (getProfileEditFragment() == null) {
Expand Down

0 comments on commit 53efd31

Please sign in to comment.