Skip to content

Commit

Permalink
Fix #1762, #2704: Highlight Correct MenuItem after cancelling ExitPro…
Browse files Browse the repository at this point in the history
…fileDialog (#2671)

* Refactor the function markHomeMenuCloseDrawer() to restoreLastCheckedMenuItem()

* Fix lint errors.

* Make naming consistent.

* Fix lint.

* Handle highlighting Administrator Controls separately.

* Fix lint errors.

* Use let for null check.

* Fix proto lint errors.

* Make separate method to unhighlight Switch Profile.

* Rectify cases to set restoreLastCheckedItem

* Fix BUILD.bazel error.

* Fix Build Binary with bazel failing Github Check.

* Correct when statement to set `restoreLastCheckedItem`.

* Replace oneOf with enum and fiz nit in BUILD file.

* Fix Highlighting Correct Item after pressing back button.

* Fix test cases.

* Fix lint errors.

* Add explanation comment for enum HighlightItem.

* Refactor enum HighlightItem.

* Remove Logs from ExitProfileDialogFragment.kt.

* Add test cases for Landscape mode and MY_DOWNLOADS menuItem.

* Remove `item` from the testcase.

* Restructure HighlightItem enum.

* Replace onResume() with onRestart() method.

* Fix nits.

Co-authored-by: Ben Henning <henning.benmax@gmail.com>
  • Loading branch information
prayutsu and BenHenning authored Feb 17, 2021
1 parent dbb8ae7 commit e7ed2c0
Show file tree
Hide file tree
Showing 10 changed files with 601 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,37 @@ package org.oppia.android.app.drawer

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.view.ContextThemeWrapper
import androidx.fragment.app.DialogFragment
import org.oppia.android.R
import org.oppia.android.app.model.ExitProfileDialogArguments
import org.oppia.android.app.model.HighlightItem
import org.oppia.android.app.profile.ProfileChooserActivity
import org.oppia.android.util.extensions.getProto
import org.oppia.android.util.extensions.putProto

/** [DialogFragment] that gives option to either cancel or exit current profile. */
class ExitProfileDialogFragment : DialogFragment() {

companion object {
// TODO(#1655): Re-restrict access to fields in tests post-Gradle.
const val BOOL_IS_FROM_NAVIGATION_DRAWER_EXTRA_KEY =
"BOOL_IS_FROM_NAVIGATION_DRAWER_EXTRA_KEY"
const val EXIT_PROFILE_DIALOG_ARGUMENTS_PROTO = "EXIT_PROFILE_DIALOG_ARGUMENT_PROTO"

/**
* This function is responsible for displaying content in DialogFragment.
*
* @return [ExitProfileDialogFragment]: DialogFragment
*/
fun newInstance(isFromNavigationDrawer: Boolean): ExitProfileDialogFragment {
fun newInstance(
exitProfileDialogArguments: ExitProfileDialogArguments
): ExitProfileDialogFragment {
val exitProfileDialogFragment = ExitProfileDialogFragment()
val args = Bundle()
args.putBoolean(BOOL_IS_FROM_NAVIGATION_DRAWER_EXTRA_KEY, isFromNavigationDrawer)
args.putProto(EXIT_PROFILE_DIALOG_ARGUMENTS_PROTO, exitProfileDialogArguments)
exitProfileDialogFragment.arguments = args
return exitProfileDialogFragment
}
Expand All @@ -38,33 +44,61 @@ class ExitProfileDialogFragment : DialogFragment() {
val args =
checkNotNull(arguments) { "Expected arguments to be pass to ExitProfileDialogFragment" }

val isFromNavigationDrawer = args.getBoolean(
BOOL_IS_FROM_NAVIGATION_DRAWER_EXTRA_KEY,
false
val exitProfileDialogArguments = args.getProto(
EXIT_PROFILE_DIALOG_ARGUMENTS_PROTO,
ExitProfileDialogArguments.getDefaultInstance()
)

if (isFromNavigationDrawer) {
exitProfileDialogInterface =
parentFragment as ExitProfileDialogInterface
val restoreLastCheckedItem = when (exitProfileDialogArguments.highlightItem) {
HighlightItem.ADMINISTRATOR_CONTROLS_ITEM,
HighlightItem.LAST_CHECKED_MENU_ITEM -> true
else -> false
}

return AlertDialog
val alertDialog = AlertDialog
.Builder(ContextThemeWrapper(activity as Context, R.style.AlertDialogTheme))
.setMessage(R.string.home_activity_back_dialog_message)
.setNegativeButton(R.string.home_activity_back_dialog_cancel) { dialog, _ ->
if (isFromNavigationDrawer) {
exitProfileDialogInterface.markHomeMenuCloseDrawer()
}
dialog.dismiss()
}
.setPositiveButton(R.string.home_activity_back_dialog_exit) { _, _ ->
// TODO(#322): Need to start intent for ProfileChooserActivity to get update. Change to finish when live data bug is fixed.
val intent = ProfileChooserActivity.createProfileChooserActivity(activity!!)
if (!isFromNavigationDrawer) {
if (!restoreLastCheckedItem) {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
}
activity!!.startActivity(intent)
}
.create()
alertDialog.setCanceledOnTouchOutside(false)
return alertDialog
}

override fun onDismiss(dialog: DialogInterface) {
super.onDismiss(dialog)
val args =
checkNotNull(arguments) { "Expected arguments to be pass to ExitProfileDialogFragment" }

val exitProfileDialogArguments = args.getProto(
EXIT_PROFILE_DIALOG_ARGUMENTS_PROTO,
ExitProfileDialogArguments.getDefaultInstance()
)

val restoreLastCheckedItem = when (exitProfileDialogArguments.highlightItem) {
HighlightItem.ADMINISTRATOR_CONTROLS_ITEM,
HighlightItem.LAST_CHECKED_MENU_ITEM -> true
else -> false
}

if (restoreLastCheckedItem) {
exitProfileDialogInterface =
parentFragment as ExitProfileDialogInterface
exitProfileDialogInterface.unhighlightSwitchProfileMenuItem()
if (exitProfileDialogArguments.highlightItem == HighlightItem.LAST_CHECKED_MENU_ITEM) {
exitProfileDialogInterface.highlightLastCheckedMenuItem()
} else {
exitProfileDialogInterface.highlightAdministratorControlsItem()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ package org.oppia.android.app.drawer

/** Interface to handle option selection in [ExitProfileDialogFragment]. */
interface ExitProfileDialogInterface {
fun markHomeMenuCloseDrawer()
fun highlightLastCheckedMenuItem()
fun highlightAdministratorControlsItem()
fun unhighlightSwitchProfileMenuItem()
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ class NavigationDrawerFragment :
navigationDrawerFragmentPresenter.openProfileProgress(profileId)
}

override fun markHomeMenuCloseDrawer() {
navigationDrawerFragmentPresenter.markHomeMenuCloseDrawer()
override fun highlightLastCheckedMenuItem() {
navigationDrawerFragmentPresenter.highlightLastCheckedMenuItem()
}

override fun highlightAdministratorControlsItem() {
navigationDrawerFragmentPresenter.highlightAdministratorControlsItem()
}

override fun unhighlightSwitchProfileMenuItem() {
navigationDrawerFragmentPresenter.unhighlightSwitchProfileMenuItem()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import org.oppia.android.app.fragment.FragmentScope
import org.oppia.android.app.help.HelpActivity
import org.oppia.android.app.home.HomeActivity
import org.oppia.android.app.model.CompletedStoryList
import org.oppia.android.app.model.ExitProfileDialogArguments
import org.oppia.android.app.model.HighlightItem
import org.oppia.android.app.model.OngoingTopicList
import org.oppia.android.app.model.Profile
import org.oppia.android.app.model.ProfileId
Expand Down Expand Up @@ -105,9 +107,7 @@ class NavigationDrawerFragmentPresenter @Inject constructor(
return@setOnClickListener
}

binding.fragmentDrawerNavView.menu.forEach { menuItem ->
menuItem.isCheckable = false
}
uncheckAllMenuItemsWhenAdministratorControlsIsSelected()

drawerLayout.closeDrawers()
getFooterViewModel().isAdministratorControlsSelected.set(true)
Expand Down Expand Up @@ -199,7 +199,6 @@ class NavigationDrawerFragmentPresenter @Inject constructor(
}

private fun openActivityByMenuItemId(menuItemId: Int) {
getFooterViewModel().isAdministratorControlsSelected.set(false)
if (previousMenuItemId != menuItemId) {
when (NavigationDrawerItem.valueFromNavId(menuItemId)) {
NavigationDrawerItem.HOME -> {
Expand Down Expand Up @@ -244,8 +243,25 @@ class NavigationDrawerFragmentPresenter @Inject constructor(
if (previousFragment != null) {
fragment.childFragmentManager.beginTransaction().remove(previousFragment).commitNow()
}
val exitProfileDialogArguments =
if (getFooterViewModel().isAdministratorControlsSelected.get() == true) {
ExitProfileDialogArguments
.newBuilder()
.setHighlightItem(HighlightItem.ADMINISTRATOR_CONTROLS_ITEM)
.build()
} else {
ExitProfileDialogArguments
.newBuilder()
.setHighlightItem(HighlightItem.LAST_CHECKED_MENU_ITEM)
.build()
}
getFooterViewModel().isAdministratorControlsSelected.set(false)
binding.fragmentDrawerNavView.menu.getItem(
NavigationDrawerItem.SWITCH_PROFILE.ordinal
).isChecked =
true
val dialogFragment = ExitProfileDialogFragment
.newInstance(isFromNavigationDrawer = true)
.newInstance(exitProfileDialogArguments = exitProfileDialogArguments)
dialogFragment.showNow(fragment.childFragmentManager, TAG_SWITCH_PROFILE_DIALOG)
}
}
Expand All @@ -263,12 +279,37 @@ class NavigationDrawerFragmentPresenter @Inject constructor(
)
}

fun markHomeMenuCloseDrawer() {
fun highlightLastCheckedMenuItem() {
previousMenuItemId?.let { itemId ->
if (itemId != 0) {
binding.fragmentDrawerNavView.menu.getItem(
NavigationDrawerItem.valueFromNavId(
itemId
).ordinal
).isChecked =
true
}
drawerLayout.closeDrawers()
}
}

fun highlightAdministratorControlsItem() {
getFooterViewModel().isAdministratorControlsSelected.set(true)
uncheckAllMenuItemsWhenAdministratorControlsIsSelected()
drawerLayout.closeDrawers()
}

fun unhighlightSwitchProfileMenuItem() {
binding.fragmentDrawerNavView.menu.getItem(
NavigationDrawerItem.HOME.ordinal
NavigationDrawerItem.SWITCH_PROFILE.ordinal
).isChecked =
true
drawerLayout.closeDrawers()
false
}

private fun uncheckAllMenuItemsWhenAdministratorControlsIsSelected() {
binding.fragmentDrawerNavView.menu.forEach {
it.isCheckable = false
}
}

/**
Expand Down Expand Up @@ -345,6 +386,7 @@ class NavigationDrawerFragmentPresenter @Inject constructor(
} else {
// For showing navigation drawer in AdministratorControlsActivity
getFooterViewModel().isAdministratorControlsSelected.set(true)
uncheckAllMenuItemsWhenAdministratorControlsIsSelected()
this.drawerLayout = drawerLayout
drawerToggle = object : ActionBarDrawerToggle(
fragment.activity,
Expand Down
14 changes: 13 additions & 1 deletion app/src/main/java/org/oppia/android/app/home/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import org.oppia.android.app.drawer.ExitProfileDialogFragment
import org.oppia.android.app.drawer.KEY_NAVIGATION_PROFILE_ID
import org.oppia.android.app.drawer.TAG_SWITCH_PROFILE_DIALOG
import org.oppia.android.app.home.recentlyplayed.RecentlyPlayedActivity
import org.oppia.android.app.model.ExitProfileDialogArguments
import org.oppia.android.app.model.HighlightItem
import org.oppia.android.app.topic.TopicActivity
import javax.inject.Inject

Expand Down Expand Up @@ -38,6 +40,11 @@ class HomeActivity :
title = getString(R.string.menu_home)
}

override fun onRestart() {
super.onRestart()
homeActivityPresenter.handleOnRestart()
}

override fun routeToTopic(internalProfileId: Int, topicId: String) {
startActivity(TopicActivity.createTopicActivityIntent(this, internalProfileId, topicId))
}
Expand All @@ -48,8 +55,13 @@ class HomeActivity :
if (previousFragment != null) {
supportFragmentManager.beginTransaction().remove(previousFragment).commitNow()
}
val exitProfileDialogArguments =
ExitProfileDialogArguments
.newBuilder()
.setHighlightItem(HighlightItem.NONE)
.build()
val dialogFragment = ExitProfileDialogFragment
.newInstance(isFromNavigationDrawer = false)
.newInstance(exitProfileDialogArguments = exitProfileDialogArguments)
dialogFragment.showNow(supportFragmentManager, TAG_SWITCH_PROFILE_DIALOG)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class HomeActivityPresenter @Inject constructor(private val activity: AppCompatA
}
}

fun handleOnRestart() {
setUpNavigationDrawer()
}

private fun setUpNavigationDrawer() {
val toolbar = activity.findViewById<View>(R.id.home_activity_toolbar) as Toolbar
activity.setSupportActionBar(toolbar)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class NavigationDrawerTestActivity : InjectableAppCompatActivity(), RouteToTopic
title = getString(R.string.menu_home)
}

override fun onRestart() {
super.onRestart()
homeActivityPresenter.handleOnRestart()
}

override fun routeToTopic(internalProfileId: Int, topicId: String) {
startActivity(TopicActivity.createTopicActivityIntent(this, internalProfileId, topicId))
}
Expand Down
Loading

0 comments on commit e7ed2c0

Please sign in to comment.