Skip to content

Commit

Permalink
Put persistent menu behind cc-peristent-menu setting
Browse files Browse the repository at this point in the history
  • Loading branch information
shubham1g5 committed Jul 18, 2024
1 parent 326721d commit f774919
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,16 @@ public MenuSession buildSession(String username,
boolean preview) throws Exception {
return new MenuSession(username, domain, appId, locale,
installService, restoreFactory, host, oneQuestionPerScreen, asUser, preview,
new FormplayerRemoteInstanceFetcher(caseSearchHelper, virtualDataInstanceService));
new FormplayerRemoteInstanceFetcher(caseSearchHelper, virtualDataInstanceService),
storageFactory.getPropertyManager().isPersistentMenuEnabled());
}

@Trace
public MenuSession buildSession(SerializableMenuSession serializableMenuSession, FormplayerConfigEngine engine,
CommCareSession commCareSession) throws Exception {
return new MenuSession(serializableMenuSession, engine, commCareSession, restoreFactory,
new FormplayerRemoteInstanceFetcher(caseSearchHelper, virtualDataInstanceService));
new FormplayerRemoteInstanceFetcher(caseSearchHelper, virtualDataInstanceService),
storageFactory.getPropertyManager().isPersistentMenuEnabled());
}

@Trace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.commcare.formplayer.beans.menus.EntityDetailResponse;
import org.commcare.formplayer.beans.menus.EntityListResponse;
import org.commcare.formplayer.beans.menus.MenuBean;
import org.commcare.formplayer.beans.menus.PeristentCommand;
import org.commcare.formplayer.beans.menus.QueryResponseBean;
import org.commcare.formplayer.exceptions.ApplicationConfigException;
import org.commcare.formplayer.exceptions.SyncRestoreException;
Expand Down Expand Up @@ -75,6 +76,7 @@
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -214,10 +216,16 @@ public BaseResponseBean getNextMenu(@Nullable Screen nextScreen, MenuSession men
menuSession.getCommCareVersionString() + ", App Version: " + menuSession.getAppVersion());
menuResponseBean.setPersistentCaseTile(
getPersistentDetail(menuSession, storageFactory.getPropertyManager().isFuzzySearchEnabled()));
menuResponseBean.setPersistentMenu(menuSession.getPersistentMenu());
setPeristenMenuToBean(menuResponseBean, menuSession.getPersistentMenu());
return menuResponseBean;
}

private void setPeristenMenuToBean(BaseResponseBean menuResponseBean, ArrayList<PeristentCommand> persistentMenu) {
if (storageFactory.getPropertyManager().isPersistentMenuEnabled()) {
menuResponseBean.setPersistentMenu(persistentMenu);
}
}

private void addHereFuncHandler(EntityScreen nextScreen, MenuSession menuSession) {
EvaluationContext ec = nextScreen.getEvalContext();
ec.addFunctionHandler(
Expand Down Expand Up @@ -670,7 +678,7 @@ private NewFormResponse startFormEntry(MenuSession menuSession) throws Exception
formResponseBean.setPersistentCaseTile(
getPersistentDetail(menuSession, storageFactory.getPropertyManager().isFuzzySearchEnabled()));
formResponseBean.setBreadcrumbs(menuSession.getBreadcrumbs());
formResponseBean.setPersistentMenu(menuSession.getPersistentMenu());
setPeristenMenuToBean(formResponseBean, menuSession.getPersistentMenu());
// update datadog/sentry metrics
datadog.addRequestScopedTag(Constants.MODULE_TAG, "form");
Sentry.setTag(Constants.MODULE_TAG, "form");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
*/
public class MenuSession implements HereFunctionHandlerListener {
private final SerializableMenuSession session;
private final boolean isPersistentMenuEnabled;
private FormplayerConfigEngine engine;
private UserSqlSandbox sandbox;
private SessionWrapper sessionWrapper;
Expand All @@ -92,7 +93,7 @@ public class MenuSession implements HereFunctionHandlerListener {

public MenuSession(SerializableMenuSession session,
FormplayerConfigEngine engine, CommCareSession commCareSession, RestoreFactory restoreFactory,
FormplayerRemoteInstanceFetcher instanceFetcher) throws Exception {
FormplayerRemoteInstanceFetcher instanceFetcher, boolean isPersistentMenuEnabled) throws Exception {
this.instanceFetcher = instanceFetcher;
this.session = session;
this.engine = engine;
Expand All @@ -101,13 +102,14 @@ public MenuSession(SerializableMenuSession session,
commCareSession, engine.getPlatform(), sandbox, instanceFetcher);
SessionUtils.setLocale(session.getLocale());
sessionWrapper.syncState();
this.isPersistentMenuEnabled = isPersistentMenuEnabled;
initializeBreadcrumbs();
}

public MenuSession(String username, String domain, String appId, String locale,
InstallService installService, RestoreFactory restoreFactory, String host,
boolean oneQuestionPerScreen, String asUser, boolean preview,
FormplayerRemoteInstanceFetcher instanceFetcher)
FormplayerRemoteInstanceFetcher instanceFetcher, boolean isPersistentMenuEnabled)
throws Exception {
this.oneQuestionPerScreen = oneQuestionPerScreen;
this.instanceFetcher = instanceFetcher;
Expand All @@ -131,6 +133,7 @@ public MenuSession(String username, String domain, String appId, String locale,
this.sessionWrapper = new FormplayerSessionWrapper(engine.getPlatform(), sandbox,
instanceFetcher);
SessionUtils.setLocale(locale);
this.isPersistentMenuEnabled = isPersistentMenuEnabled;
initializeBreadcrumbs();
}

Expand All @@ -145,7 +148,7 @@ public void resetSession() throws RemoteInstanceFetcher.RemoteInstanceException
private void initializeBreadcrumbs() {
this.breadcrumbs = new ArrayList<>();
this.breadcrumbs.add(ScreenUtils.getAppTitle());
persistentMenuHelper = new PersistentMenuHelper();
persistentMenuHelper = new PersistentMenuHelper(isPersistentMenuEnabled);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,47 @@ import org.commcare.util.screen.Screen
/**
* Utility methods related to persistent menu evaluation
*/
class PersistentMenuHelper {
class PersistentMenuHelper(val isPersistentMenuEnabled: Boolean) {

var persistentMenu: ArrayList<PeristentCommand> = ArrayList()
private var currentMenu: PeristentCommand? = null

fun addEntitySelection(input: String, entityText: String) {
if (!StringUtils.isEmpty(input) && !input.contentEquals(MultiSelectEntityScreen.USE_SELECTED_VALUES)) {
if (isPersistentMenuEnabled && !StringUtils.isEmpty(input) && !input.contentEquals(MultiSelectEntityScreen.USE_SELECTED_VALUES)) {
val command = PeristentCommand(input, entityText)
addPersistentCommand(command)
}
}

fun addMenusToPeristentMenu(menuScreen: MenuScreen) {
val options = menuScreen.getOptions()
for (i in options.indices) {
val command = PeristentCommand(i.toString(), options[i])
addPersistentCommand(command)
if (isPersistentMenuEnabled) {
val options = menuScreen.getOptions()
for (i in options.indices) {
val command = PeristentCommand(i.toString(), options[i])
addPersistentCommand(command)
}
}
}

/**
* Identifies and sets current menu based on the input give to the current screen
*/
fun advanceCurrentMenuWithInput(screen: Screen, input: String) {
if (screen is MenuScreen) {
val index = input.toInt()
val parentMenu = if (currentMenu == null) persistentMenu else currentMenu!!.commands
if (index < parentMenu.size) {
currentMenu = parentMenu[index]
}
} else if (screen is EntityScreen) {
check(currentMenu != null) { "Current menu can't be null for Entity screen" }
check(currentMenu!!.commands.size <= 1) { "Current menu can't have more than one commands for Entity screen" }
if (isPersistentMenuEnabled) {
if (screen is MenuScreen) {
val index = input.toInt()
val parentMenu = if (currentMenu == null) persistentMenu else currentMenu!!.commands
if (index < parentMenu.size) {
currentMenu = parentMenu[index]
}
} else if (screen is EntityScreen) {
check(currentMenu != null) { "Current menu can't be null for Entity screen" }
check(currentMenu!!.commands.size <= 1) { "Current menu can't have more than one commands for Entity screen" }

// if it's the last input, we would not have yet added entity screen menu to peristent Menu, so just
// return current menu otherwise return the only command entry for entity screen
currentMenu = if (currentMenu!!.commands.size == 0) currentMenu else currentMenu!!.commands[0]
// if it's the last input, we would not have yet added entity screen menu to peristent Menu, so just
// return current menu otherwise return the only command entry for entity screen
currentMenu = if (currentMenu!!.commands.size == 0) currentMenu else currentMenu!!.commands[0]
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class FormplayerPropertyManager extends PropertyManager {

public static final String INDEX_CASE_SEARCH_RESULTS = "cc-index-case-search-results";

public static final String PERSISTENT_MENU = "cc-persistent-menu";

/**
* Constructor for this PropertyManager
*
Expand Down Expand Up @@ -68,4 +70,8 @@ public boolean isAutoAdvanceMenu() {
public boolean isIndexCaseSearchResults() {
return doesPropertyMatch(INDEX_CASE_SEARCH_RESULTS, NO, YES);
}

public boolean isPersistentMenuEnabled() {
return doesPropertyMatch(PERSISTENT_MENU, NO, YES);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public boolean isIndexCaseSearchResults() {
return indexCaseSearchResults;
}

@Override
public boolean isPersistentMenuEnabled() {
return true;
}

// convenience method to set auto advance menu as true
public static void mockAutoAdvanceMenu(FormplayerStorageFactory storageFactoryMock) {
SQLiteDB db = storageFactoryMock.getSQLiteDB();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;

import org.commcare.formplayer.beans.NewFormResponse;
import org.commcare.formplayer.beans.menus.CommandListResponseBean;
import org.commcare.formplayer.beans.menus.PeristentCommand;
import org.commcare.formplayer.mocks.FormPlayerPropertyManagerMock;
import org.commcare.formplayer.sandbox.SqlStorage;
import org.commcare.formplayer.services.BrowserValuesProvider;
import org.commcare.formplayer.sqlitedb.SQLiteDB;
import org.commcare.formplayer.utils.TestContext;
import org.commcare.formplayer.utils.WithHqUser;
import org.javarosa.core.services.PropertyManager;
import org.javarosa.core.services.properties.Property;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -34,6 +40,8 @@ public class EndpointLaunchTest extends BaseTestClass {
public void setUp() throws Exception {
super.setUp();
configureRestoreFactory("endpointdomain", "endpointusername");
storageFactoryMock.configure("endpointusername", "endpointdomain", "app_id", "asUser");
FormPlayerPropertyManagerMock.mockAutoAdvanceMenu(storageFactoryMock);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class MultiSelectCaseListTest extends BaseTestClass {
public void setUp() throws Exception {
super.setUp();
configureRestoreFactory("caseclaimdomain", "caseclaimusername");
storageFactoryMock.configure("user", "domain", "app_id", "asUser");
FormPlayerPropertyManagerMock.mockAutoAdvanceMenu(storageFactoryMock);
}

@Override
Expand Down Expand Up @@ -91,7 +93,6 @@ public void testConfirmedSelectionsForMultiSelectCaseList() {

@Test
public void testAutoAdvanceWithMultiSelect() throws Exception {
FormPlayerPropertyManagerMock.mockAutoAdvanceMenu(storageFactoryMock);
String[] selections = new String[]{"1"};
sessionNavigate(selections, APP,
EntityListResponse.class);
Expand Down

0 comments on commit f774919

Please sign in to comment.