From dca38bf4df1da04f42fe55c5472d2486f3f8e867 Mon Sep 17 00:00:00 2001 From: Tejas-67 Date: Mon, 17 Jun 2024 00:07:37 +0530 Subject: [PATCH] refractored: ProfileEditActivity, ProfileEditActivityPresenter --- app/src/main/AndroidManifest.xml | 1 + .../settings/profile/ProfileEditActivity.kt | 41 +++++++++++++++---- .../profile/ProfileEditActivityPresenter.kt | 12 ++---- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 41d1ce55918..97a3e32beda 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -14,6 +14,7 @@ android:icon="@mipmap/launcher_icon" android:label="@string/app_name" android:supportsRtl="true" + android:enableOnBackInvokedCallback="true" android:theme="@style/OppiaTheme"> diff --git a/app/src/main/java/org/oppia/android/app/settings/profile/ProfileEditActivity.kt b/app/src/main/java/org/oppia/android/app/settings/profile/ProfileEditActivity.kt index 2e42ba5bbff..65c1a36876c 100644 --- a/app/src/main/java/org/oppia/android/app/settings/profile/ProfileEditActivity.kt +++ b/app/src/main/java/org/oppia/android/app/settings/profile/ProfileEditActivity.kt @@ -2,7 +2,9 @@ package org.oppia.android.app.settings.profile import android.content.Context import android.content.Intent +import android.os.Build import android.os.Bundle +import android.util.Log import org.oppia.android.app.activity.ActivityComponentImpl import org.oppia.android.app.activity.InjectableAutoLocalizedAppCompatActivity import org.oppia.android.app.model.ScreenName.PROFILE_EDIT_ACTIVITY @@ -41,19 +43,44 @@ class ProfileEditActivity : InjectableAutoLocalizedAppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + onBackInvokedDispatcher.registerOnBackInvokedCallback(1000){ + Log.w("profile-edit", "dispatch") + val isMultipane: Boolean = intent.extras!!.getBoolean(IS_MULTIPANE_EXTRA_KEY, false) + if (isMultipane) { + onBackPressedDispatcher.onBackPressed() + } else { + val intent = Intent(this, ProfileListActivity::class.java) + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) + startActivity(intent) + } + } + } (activityComponent as ActivityComponentImpl).inject(this) profileEditActivityPresenter.handleOnCreate() } override fun onBackPressed() { - val isMultipane = intent.extras!!.getBoolean(IS_MULTIPANE_EXTRA_KEY, false) - if (isMultipane) { - @Suppress("DEPRECATION") // TODO(#5404): Migrate to a back pressed dispatcher. - super.onBackPressed() + Log.w("profile-edit", "back pressed") + 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(){ + Log.w("profile-edit", "here") + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + onBackPressedDispatcher.onBackPressed() } else { - val intent = Intent(this, ProfileListActivity::class.java) - intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) - startActivity(intent) + onBackPressed() } } } diff --git a/app/src/main/java/org/oppia/android/app/settings/profile/ProfileEditActivityPresenter.kt b/app/src/main/java/org/oppia/android/app/settings/profile/ProfileEditActivityPresenter.kt index dd6b0f39e78..3316b266c98 100644 --- a/app/src/main/java/org/oppia/android/app/settings/profile/ProfileEditActivityPresenter.kt +++ b/app/src/main/java/org/oppia/android/app/settings/profile/ProfileEditActivityPresenter.kt @@ -25,14 +25,7 @@ class ProfileEditActivityPresenter @Inject constructor( val isMultipane = activity.intent.getBooleanExtra(IS_MULTIPANE_EXTRA_KEY, false) toolbar.setNavigationOnClickListener { - if (isMultipane) { - @Suppress("DEPRECATION") // TODO(#5404): Migrate to a back pressed dispatcher. - activity.onBackPressed() - } else { - val intent = Intent(activity, ProfileListActivity::class.java) - intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) - activity.startActivity(intent) - } + getProfileEditActivity()?.handleBackPressFromPresenter() } if (getProfileEditFragment() == null) { @@ -43,6 +36,9 @@ class ProfileEditActivityPresenter @Inject constructor( } } + private fun getProfileEditActivity(): ProfileEditActivity? { + return activity as ProfileEditActivity? + } private fun setUpToolbar() { toolbar = activity.findViewById(R.id.profile_edit_toolbar) as Toolbar activity.setSupportActionBar(toolbar)