Skip to content

Commit

Permalink
Merge pull request #586 from opensrp/585-get-from-db-no-locale
Browse files Browse the repository at this point in the history
allow getting forms from db without appending locale
  • Loading branch information
LZRS authored Jul 26, 2021
2 parents c9c6c7e + a787630 commit f6553e6
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.vijay.jsonwizard.utils.AppExecutors;
import com.vijay.jsonwizard.utils.FormUtils;
import com.vijay.jsonwizard.utils.NativeFormLangUtils;
import com.vijay.jsonwizard.utils.NoLocaleFormUtils;

import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -38,11 +39,22 @@ public class FormConfigurationJsonFormActivity extends JsonFormActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

formUtils = new FormUtils();
formUtils = getFormUtils();
JSONObject jsonObject = getmJSONObject();
checkIfFormUpdate(jsonObject);
}

private FormUtils getFormUtils() {
if (!this.supportsLocaleBasedForms()) {
return new NoLocaleFormUtils();
}
return new FormUtils();
}

protected boolean supportsLocaleBasedForms() {
return true;
}

private void checkIfFormUpdate(@NonNull JSONObject formJsonObject) {
if (FormUtils.isFormNew(formJsonObject)) {
showFormVersionUpdateDialog(formJsonObject, getString(R.string.form_update_title), getString(R.string.form_update_message));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.vijay.jsonwizard.activities;

import com.vijay.jsonwizard.delegates.JsonWizardFormDelegate;

import org.json.JSONException;

public class FormConfigurationJsonWizardFormActivity extends FormConfigurationJsonFormActivity {

private final JsonWizardFormDelegate delegate = new JsonWizardFormDelegate();

@Override
public synchronized void initializeFormFragment() {
initializeFormFragmentCore();
}

@Override
public void writeValue(String stepName, String key, String value, String openMrsEntityParent, String openMrsEntity, String openMrsEntityId) throws JSONException {
callSuperWriteValue(stepName, key, value, openMrsEntityParent, openMrsEntity, openMrsEntityId);
}

@Override
public void onFormFinish() {
callSuperFinish();
}

protected void callSuperFinish() {
super.onFormFinish();
}

protected void callSuperWriteValue(String stepName, String key, String value, String openMrsEntityParent, String openMrsEntity, String openMrsEntityId) throws JSONException {
super.writeValue(stepName, key, value, openMrsEntityParent, openMrsEntity, openMrsEntityId);
}

protected void initializeFormFragmentCore() {
delegate.initializeFormFragmentCore(this.getSupportFragmentManager());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.vijay.jsonwizard.activities;

import com.vijay.jsonwizard.constants.JsonFormConstants;
import com.vijay.jsonwizard.fragments.JsonWizardFormFragment;
import com.vijay.jsonwizard.delegates.JsonWizardFormDelegate;

import org.json.JSONException;

Expand All @@ -10,8 +9,10 @@
*/
public class JsonWizardFormActivity extends JsonFormActivity {

private final JsonWizardFormDelegate delegate = new JsonWizardFormDelegate();

@Override
public void initializeFormFragment() {
public synchronized void initializeFormFragment() {
initializeFormFragmentCore();
}

Expand All @@ -34,8 +35,7 @@ protected void callSuperWriteValue(String stepName, String key, String value, St
}

protected void initializeFormFragmentCore() {
JsonWizardFormFragment jsonWizardFormFragment = JsonWizardFormFragment.getFormFragment(JsonFormConstants.FIRST_STEP_NAME);
getSupportFragmentManager().beginTransaction().add(com.vijay.jsonwizard.R.id.container, jsonWizardFormFragment).commit();
delegate.initializeFormFragmentCore(this.getSupportFragmentManager());
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.vijay.jsonwizard.activities;

public class NoLocaleFormConfigurationJsonWizardFormActivity extends FormConfigurationJsonWizardFormActivity {
@Override
protected boolean supportsLocaleBasedForms() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.vijay.jsonwizard.delegates;

import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;

import com.vijay.jsonwizard.constants.JsonFormConstants;
import com.vijay.jsonwizard.fragments.JsonWizardFormFragment;

public class JsonWizardFormDelegate {

public void initializeFormFragmentCore(@NonNull FragmentManager supportFragmentManager){
JsonWizardFormFragment jsonWizardFormFragment = JsonWizardFormFragment.getFormFragment(JsonFormConstants.FIRST_STEP_NAME);
supportFragmentManager.beginTransaction()
.add(com.vijay.jsonwizard.R.id.container, jsonWizardFormFragment)
.commit();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1979,13 +1979,17 @@ public JSONObject getFormJson(@NonNull Context context, @NonNull String formIden
}
}

private ClientFormContract.Model getClientFormFromRepository(@NonNull Context context, @NonNull ClientFormContract.Dao clientFormRepository, String formIdentity) {
//Check the current locale of the app to load the correct version of the form in the desired language
protected String getLocaleFormIdentity(final Context context, final String formIdentity){
String locale = context.getResources().getConfiguration().locale.getLanguage();
String localeFormIdentity = formIdentity;
if (!Locale.ENGLISH.getLanguage().equals(locale)) {
localeFormIdentity = localeFormIdentity + "-" + locale;
return formIdentity + "-" + locale;
}
return formIdentity;
}

private ClientFormContract.Model getClientFormFromRepository(@NonNull Context context, @NonNull ClientFormContract.Dao clientFormRepository, String formIdentity) {
//Check the current locale of the app to load the correct version of the form in the desired language
String localeFormIdentity = getLocaleFormIdentity(context, formIdentity);

ClientFormContract.Model clientForm = clientFormRepository.getActiveClientFormByIdentifier(localeFormIdentity);

Expand Down Expand Up @@ -2061,13 +2065,8 @@ public void onCancelClicked() {

@Nullable
public JSONObject getSubFormJsonFromRepository(@NonNull Context context, @NonNull ClientFormContract.Dao clientFormDao, String formIdentity, String subFormsLocation, boolean translateSubForm) throws JSONException {
String locale = context.getResources().getConfiguration().locale.getLanguage();

//Check the current locale of the app to load the correct version of the form in the desired language
String localeFormIdentity = formIdentity;
if (!Locale.ENGLISH.getLanguage().equals(locale)) {
localeFormIdentity = localeFormIdentity + "-" + locale;
}
String localeFormIdentity = getLocaleFormIdentity(context, formIdentity);

String dbFormName = StringUtils.isBlank(subFormsLocation) ? localeFormIdentity : subFormsLocation + "/" + localeFormIdentity;
ClientFormContract.Model clientForm = clientFormDao.getActiveClientFormByIdentifier(dbFormName);
Expand Down Expand Up @@ -2098,13 +2097,8 @@ public JSONObject getSubFormJsonFromRepository(@NonNull Context context, @NonNul

@Nullable
public BufferedReader getRulesFromRepository(@NonNull Context context, @NonNull ClientFormContract.Dao clientFormDao, @NonNull String fileName) {
String locale = context.getResources().getConfiguration().locale.getLanguage();

//Check the current locale of the app to load the correct version of the form in the desired language
String localeFormIdentity = fileName;
if (!Locale.ENGLISH.getLanguage().equals(locale)) {
localeFormIdentity = localeFormIdentity + "-" + locale;
}
String localeFormIdentity = getLocaleFormIdentity(context, fileName);

ClientFormContract.Model clientForm = clientFormDao.getActiveClientFormByIdentifier(localeFormIdentity);
if (clientForm == null && StringUtils.isNotBlank(fileName) && fileName.contains("/") && !fileName.endsWith("/")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.vijay.jsonwizard.utils;

import android.content.Context;

public class NoLocaleFormUtils extends FormUtils{
@Override
protected String getLocaleFormIdentity(Context context, String formIdentity) {
return formIdentity;
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=2.1.0-SNAPSHOT
VERSION_NAME=2.1.1-SNAPSHOT
VERSION_CODE=1
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Native Form Json Wizard
Expand Down

0 comments on commit f6553e6

Please sign in to comment.