-
Notifications
You must be signed in to change notification settings - Fork 551
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: NavigationDrawer cancel switch profile always mark home #1816
Changes from all commits
d415788
519c36a
803ce83
e3f336e
1931f22
c56d9c4
5098c10
0799f75
245014b
80a5a8f
f9a8600
2f9d2b7
d271dfe
99215f8
cf488c5
f5d2fcb
b071e2b
56d5250
78e2d62
7eccac7
8e4fdc0
0ef2f5a
bfe6192
5d4f8ee
6639420
0c8b43d
007407d
05f48f0
a1777e5
f9f086c
6c477fc
0733811
72a3fe2
544c814
b23869f
b03b57e
38c09c8
52ae75d
de003c6
08f59d3
6fba236
cc45211
72d243c
3774af2
4c7f798
08deb4b
91d75b1
6ffd6ed
caf0f48
93828e2
4ff38db
d65710b
381509d
8ec8a2d
d9a4608
fe5191f
b29f8e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package org.oppia.android.app.drawer | ||
|
||
sealed class Argument { | ||
class LastCheckedMenuItem(val navigationDrawerItem: NavigationDrawerItem) : Argument() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need suggestion for a better name. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, please suggest a good KDoc. |
||
class IsAdministratorControlsSelected(val value: Boolean) : Argument() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,42 +8,76 @@ 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.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_ARGUMENTS_PROTO" | ||
|
||
/** | ||
* This function is responsible for displaying content in DialogFragment. | ||
* | ||
* @return [ExitProfileDialogFragment]: DialogFragment | ||
*/ | ||
fun newInstance(isFromNavigationDrawer: Boolean): ExitProfileDialogFragment { | ||
fun newInstance( | ||
restoreLastCheckedMenuItem: Boolean, | ||
argument: Argument | ||
): ExitProfileDialogFragment { | ||
val exitProfileDialogFragment = ExitProfileDialogFragment() | ||
val args = Bundle() | ||
args.putBoolean(BOOL_IS_FROM_NAVIGATION_DRAWER_EXTRA_KEY, isFromNavigationDrawer) | ||
val exitProfileDialogArguments = createExitProfileDialogFragmentProto( | ||
argument = argument, | ||
restoreLastCheckedMenuItem = restoreLastCheckedMenuItem | ||
) | ||
|
||
args.putProto(EXIT_PROFILE_DIALOG_ARGUMENTS_PROTO, exitProfileDialogArguments) | ||
|
||
exitProfileDialogFragment.arguments = args | ||
return exitProfileDialogFragment | ||
} | ||
|
||
private fun createExitProfileDialogFragmentProto( | ||
argument: Argument, | ||
restoreLastCheckedMenuItem: Boolean | ||
): ExitProfileDialogArguments { | ||
return when (argument) { | ||
is Argument.IsAdministratorControlsSelected -> { | ||
ExitProfileDialogArguments.newBuilder() | ||
.setRestoreLastCheckedMenuItem(restoreLastCheckedMenuItem) | ||
.setIsAdministratorControlsSelected(argument.value) | ||
.build() | ||
} | ||
is Argument.LastCheckedMenuItem -> { | ||
ExitProfileDialogArguments.newBuilder() | ||
.setRestoreLastCheckedMenuItem(restoreLastCheckedMenuItem) | ||
.setLastCheckedMenuItemValue(argument.navigationDrawerItem.value) | ||
.build() | ||
} | ||
} | ||
} | ||
} | ||
|
||
lateinit var exitProfileDialogInterface: ExitProfileDialogInterface | ||
private lateinit var exitProfileDialogInterface: ExitProfileDialogInterface | ||
|
||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { | ||
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) { | ||
val restoreLastCheckedMenuItem = exitProfileDialogArguments.restoreLastCheckedMenuItem | ||
val argument = exitProfileDialogArguments.adminControlsOrNavDrawerItemsCase | ||
|
||
if (restoreLastCheckedMenuItem) { | ||
exitProfileDialogInterface = | ||
parentFragment as ExitProfileDialogInterface | ||
} | ||
|
@@ -52,19 +86,42 @@ class ExitProfileDialogFragment : DialogFragment() { | |
.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() | ||
prayutsu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (restoreLastCheckedMenuItem) { | ||
exitProfileDialogInterface.checkLastCheckedItemAndCloseDrawer( | ||
when (argument.number) { | ||
2 -> Argument.IsAdministratorControlsSelected( | ||
exitProfileDialogArguments.isAdministratorControlsSelected | ||
) | ||
else -> Argument.LastCheckedMenuItem( | ||
getNavigationDrawerItem( | ||
exitProfileDialogArguments.lastCheckedMenuItemValue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we create a constant with value - |
||
) | ||
) | ||
} | ||
) | ||
exitProfileDialogInterface.unCheckSwitchProfileItemAndCloseDrawer() | ||
} | ||
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 (!restoreLastCheckedMenuItem) { | ||
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) | ||
} | ||
activity!!.startActivity(intent) | ||
} | ||
.create() | ||
} | ||
|
||
private fun getNavigationDrawerItem(value: Int): NavigationDrawerItem { | ||
return when (value) { | ||
0 -> NavigationDrawerItem.HOME | ||
1 -> NavigationDrawerItem.OPTIONS | ||
2 -> NavigationDrawerItem.HELP | ||
3 -> NavigationDrawerItem.DOWNLOADS | ||
4 -> NavigationDrawerItem.SWITCH_PROFILE | ||
else -> NavigationDrawerItem.HOME | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How should we handle else branch here? |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,7 +59,7 @@ class NavigationDrawerFragmentPresenter @Inject constructor( | |
private var previousMenuItemId: Int? = null | ||
private var internalProfileId: Int = -1 | ||
|
||
fun handleCreateView(inflater: LayoutInflater, container: ViewGroup?): View? { | ||
fun handleCreateView(inflater: LayoutInflater, container: ViewGroup?): View { | ||
binding = DrawerFragmentBinding.inflate(inflater, container, /* attachToRoot= */ false) | ||
binding.fragmentDrawerNavView.setNavigationItemSelectedListener(this) | ||
|
||
|
@@ -105,9 +105,7 @@ class NavigationDrawerFragmentPresenter @Inject constructor( | |
return@setOnClickListener | ||
} | ||
|
||
binding.fragmentDrawerNavView.menu.forEach { menuItem -> | ||
menuItem.isCheckable = false | ||
} | ||
uncheckAllMenuItemsWhenAdministratorControlsIsChecked() | ||
|
||
drawerLayout.closeDrawers() | ||
getFooterViewModel().isAdministratorControlsSelected.set(true) | ||
|
@@ -199,7 +197,6 @@ class NavigationDrawerFragmentPresenter @Inject constructor( | |
} | ||
|
||
private fun openActivityByMenuItemId(menuItemId: Int) { | ||
getFooterViewModel().isAdministratorControlsSelected.set(false) | ||
if (previousMenuItemId != menuItemId) { | ||
when (NavigationDrawerItem.valueFromNavId(menuItemId)) { | ||
NavigationDrawerItem.HOME -> { | ||
|
@@ -244,8 +241,30 @@ class NavigationDrawerFragmentPresenter @Inject constructor( | |
if (previousFragment != null) { | ||
fragment.childFragmentManager.beginTransaction().remove(previousFragment).commitNow() | ||
} | ||
val argument: Argument = | ||
if (getFooterViewModel().isAdministratorControlsSelected.get() == true) { | ||
getFooterViewModel().isAdministratorControlsSelected.set(false) | ||
Argument.IsAdministratorControlsSelected(value = true) | ||
} else { | ||
Argument.LastCheckedMenuItem( | ||
navigationDrawerItem = when (previousMenuItemId) { | ||
0 -> NavigationDrawerItem.HOME | ||
1 -> NavigationDrawerItem.OPTIONS | ||
2 -> NavigationDrawerItem.HELP | ||
3 -> NavigationDrawerItem.DOWNLOADS | ||
4 -> NavigationDrawerItem.SWITCH_PROFILE | ||
else -> NavigationDrawerItem.HOME | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How should we handle else branch here? |
||
) | ||
} | ||
binding.fragmentDrawerNavView.menu.getItem( | ||
NavigationDrawerItem.SWITCH_PROFILE.ordinal | ||
).isChecked = true | ||
val dialogFragment = ExitProfileDialogFragment | ||
.newInstance(isFromNavigationDrawer = true) | ||
.newInstance( | ||
restoreLastCheckedMenuItem = true, | ||
argument = argument | ||
) | ||
dialogFragment.showNow(fragment.childFragmentManager, TAG_SWITCH_PROFILE_DIALOG) | ||
} | ||
} | ||
|
@@ -263,12 +282,31 @@ class NavigationDrawerFragmentPresenter @Inject constructor( | |
) | ||
} | ||
|
||
fun markHomeMenuCloseDrawer() { | ||
fun checkLastCheckedItemAndCloseDrawer(argument: Argument) { | ||
when (argument) { | ||
is Argument.IsAdministratorControlsSelected -> { | ||
getFooterViewModel().isAdministratorControlsSelected.set(argument.value) | ||
uncheckAllMenuItemsWhenAdministratorControlsIsChecked() | ||
} | ||
is Argument.LastCheckedMenuItem -> { | ||
binding.fragmentDrawerNavView.menu | ||
.getItem(argument.navigationDrawerItem.ordinal).isChecked = true | ||
} | ||
} | ||
drawerLayout.closeDrawers() | ||
} | ||
|
||
fun uncheckSwitchProfileItemAndCloseDrawer() { | ||
binding.fragmentDrawerNavView.menu.getItem( | ||
NavigationDrawerItem.HOME.ordinal | ||
NavigationDrawerItem.SWITCH_PROFILE.ordinal | ||
).isChecked = | ||
true | ||
drawerLayout.closeDrawers() | ||
false | ||
} | ||
|
||
private fun uncheckAllMenuItemsWhenAdministratorControlsIsChecked() { | ||
binding.fragmentDrawerNavView.menu.forEach { item -> | ||
item.isCheckable = false | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -345,6 +383,7 @@ class NavigationDrawerFragmentPresenter @Inject constructor( | |
} else { | ||
// For showing navigation drawer in AdministratorControlsActivity | ||
getFooterViewModel().isAdministratorControlsSelected.set(true) | ||
uncheckAllMenuItemsWhenAdministratorControlsIsChecked() | ||
this.drawerLayout = drawerLayout | ||
drawerToggle = object : ActionBarDrawerToggle( | ||
fragment.activity, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need a sealed class if we have a proto with a oneof? That should satisfy the same need.