Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1762, #2704: Highlight Correct MenuItem after cancelling ExitProfileDialog #2671

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
c867ad0
Refactor the function markHomeMenuCloseDrawer() to restoreLastChecked…
prayutsu Feb 9, 2021
b99ce30
Fix lint errors.
prayutsu Feb 9, 2021
25ca17a
Make naming consistent.
prayutsu Feb 9, 2021
a0eb27e
Fix lint.
prayutsu Feb 9, 2021
7507d0d
Handle highlighting Administrator Controls separately.
prayutsu Feb 10, 2021
6cea308
Fix lint errors.
prayutsu Feb 10, 2021
251b2c6
Use let for null check.
prayutsu Feb 10, 2021
72c52ae
Fix proto lint errors.
prayutsu Feb 10, 2021
2b387bd
Make separate method to unhighlight Switch Profile.
prayutsu Feb 10, 2021
8888659
Rectify cases to set restoreLastCheckedItem
prayutsu Feb 10, 2021
af0a91c
Fix BUILD.bazel error.
prayutsu Feb 10, 2021
9625a00
Merge branch 'develop' of https://github.com/oppia/oppia-android into…
prayutsu Feb 11, 2021
f27526f
Fix Build Binary with bazel failing Github Check.
prayutsu Feb 11, 2021
a58aca5
Correct when statement to set `restoreLastCheckedItem`.
prayutsu Feb 11, 2021
50c21a8
Replace oneOf with enum and fiz nit in BUILD file.
prayutsu Feb 12, 2021
597ad4a
Merge branch 'develop' of https://github.com/oppia/oppia-android into…
prayutsu Feb 12, 2021
99df13f
Fix Highlighting Correct Item after pressing back button.
prayutsu Feb 12, 2021
c67d36b
Fix test cases.
prayutsu Feb 12, 2021
82402bb
Merge branch 'develop' of https://github.com/oppia/oppia-android into…
prayutsu Feb 12, 2021
aa9a1d4
Fix lint errors.
prayutsu Feb 12, 2021
ae251f2
Add explanation comment for enum HighlightItem.
prayutsu Feb 12, 2021
9d86ea5
Refactor enum HighlightItem.
prayutsu Feb 12, 2021
b3960af
Remove Logs from ExitProfileDialogFragment.kt.
prayutsu Feb 12, 2021
9201a6f
Add test cases for Landscape mode and MY_DOWNLOADS menuItem.
prayutsu Feb 13, 2021
6d411de
Merge branch 'develop' of https://github.com/oppia/oppia-android into…
prayutsu Feb 13, 2021
f96a35d
Remove `item` from the testcase.
prayutsu Feb 13, 2021
9515093
Restructure HighlightItem enum.
prayutsu Feb 14, 2021
dd67643
Merge branch 'develop' of https://github.com/oppia/oppia-android into…
prayutsu Feb 15, 2021
4121e21
Replace onResume() with onRestart() method.
prayutsu Feb 15, 2021
2e6acaa
Merge branch 'develop' of https://github.com/oppia/oppia-android into…
prayutsu Feb 15, 2021
83e99d1
Fix nits.
prayutsu Feb 15, 2021
237299e
Merge branch 'develop' into highlight-correct-item-after-cancelling-e…
BenHenning Feb 17, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
BenHenning marked this conversation as resolved.
Show resolved Hide resolved
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