Skip to content

Commit

Permalink
Merge pull request #105 from trufla-technology/translate-form-schema
Browse files Browse the repository at this point in the history
Translate truforms views and handle Json schema to display views with…
  • Loading branch information
marina-waged authored Nov 29, 2020
2 parents 65affbe + 5708111 commit 8f647bc
Show file tree
Hide file tree
Showing 19 changed files with 919 additions and 418 deletions.
3 changes: 3 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
package com.trufla.androidtruforms;

import com.trufla.androidtruforms.utils.TruUtils;

/**
* Created by Mohamed Salah on 10,March,2020
* Trufla Technology,
* Cairo, Egypt.
*/
public class SharedData {

public class SharedData
{
private static SharedData instance;

// Global variable
private String data;
private String defaultLanguage;

// Restrict the constructor from being instantiated
private SharedData() {
}

public static synchronized SharedData getInstance() {
if (instance == null) {
instance = new SharedData();
}
return instance;
}

public void setData(String d) {
this.data = d;
}
Expand All @@ -24,10 +35,15 @@ public String getData() {
return this.data;
}

public static synchronized SharedData getInstance() {
if (instance == null) {
instance = new SharedData();
}
return instance;
public void setLanguage(String language)
{
this.defaultLanguage = language;
}

public String getDefaultLanguage()
{
//Check if default language is null or empty I'll return "en" as a default language
return TruUtils.setDefaultUserLanguage(this.defaultLanguage);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
* Created by ohefny on 8/13/18.
*/

public class TruFormActivity extends AppCompatActivity implements FormContract {
public class TruFormActivity extends AppCompatActivity implements FormContract
{
private static final int PICK_IMAGE_CODE = 1;
private static final int CAPTURE_IMAGE_CODE = 2;
private static final int PERMISSION_REQUEST_CODE = 1;
Expand All @@ -61,7 +62,6 @@ public class TruFormActivity extends AppCompatActivity implements FormContract {
private boolean isHistory = false;

BottomSheetDialog dialog;

String currentCameraPhotoPath;

//create a single thread pool to our image compression class.
Expand All @@ -76,10 +76,14 @@ public static void startActivityForFormResult(Activity context, String jsonStr)
context.startActivityForResult(intent, SchemaBuilder.REQUEST_CODE);
}

public static void startActivityForFormResult(Fragment hostFragment, String jsonStr) {
public static void startActivityForFormResult(Fragment hostFragment, String jsonStr)
{
Intent intent = new Intent(hostFragment.getActivity(), TruFormActivity.class);
intent.putExtra(SCHEMA_KEY, jsonStr);
sharedData = null;
// sharedData = SharedData.getInstance();
// sharedData.setLanguage("fr");

hostFragment.startActivityForResult(intent, SchemaBuilder.REQUEST_CODE);
}

Expand Down Expand Up @@ -113,9 +117,11 @@ protected void onCreate(Bundle savedInstanceState) {
try {

String jsonVal = "";
if (sharedData != null) {
if (sharedData != null)
jsonVal = sharedData.getData();
}

sharedData = SharedData.getInstance();
sharedData.setLanguage("fr");

if (TextUtils.isEmpty(jsonVal))
truFormView = SchemaBuilder.getInstance().buildSchemaView(Objects.requireNonNull(getIntent().getExtras()).getString(SCHEMA_KEY), this);
Expand Down Expand Up @@ -322,4 +328,4 @@ protected void onDestroy() {
mExecutorService = null;
imageCompressTask = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public class TruFormFragment extends Fragment implements FormContract, CollectDa
private static SharedData sharedData;
private String currentCameraPhotoPath;


//create a single thread pool to our image compression class.
private ExecutorService mExecutorService = Executors.newFixedThreadPool(1);

Expand All @@ -87,12 +86,16 @@ public class TruFormFragment extends Fragment implements FormContract, CollectDa
public TruFormFragment() {
}

public static TruFormFragment newInstance(int schemaType, String schemaString) {
public static TruFormFragment newInstance(int schemaType, String schemaString)
{
TruFormFragment fragment = new TruFormFragment();
Bundle args = new Bundle();
args.putString(SCHEMA_KEY, schemaString);
args.putInt(SCHEMA_TYPE, schemaType);
sharedData = null;
// sharedData = SharedData.getInstance();
// sharedData.setLanguage("fr");

fragment.setArguments(args);
return fragment;
}
Expand Down Expand Up @@ -127,9 +130,12 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
try {

String jsonVal = "";
if (sharedData != null) {
if (sharedData != null)
jsonVal = sharedData.getData();
}

//Will Change those lines when upload the new schema
sharedData = SharedData.getInstance();
sharedData.setLanguage("fr");

if (TextUtils.isEmpty(jsonVal))
truFormView = SchemaBuilder.getInstance().buildSchemaView(schemaString, getContext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,53 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.trufla.androidtruforms.SharedData;
import com.trufla.androidtruforms.models.DataEnumNames;

import org.json.JSONStringer;

import java.lang.reflect.Type;
import java.util.ArrayList;

public class DataEnumNamesDeserializer implements JsonDeserializer<DataEnumNames> {


public class DataEnumNamesDeserializer implements JsonDeserializer<DataEnumNames>
{
@Override
public DataEnumNames deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
ArrayList<String> names = new ArrayList<>();
Gson gson = new Gson();
if (json instanceof JsonArray) {
names = gson.fromJson(json, names.getClass());
} else if (json instanceof JsonPrimitive) {
if (json instanceof JsonArray)
{
names = getEnumList(json);
// names = gson.fromJson(json, names.getClass());

} else if (json instanceof JsonPrimitive)
names.add(gson.fromJson(json, String.class));
}

return new DataEnumNames(names);
}

public ArrayList<String> getEnumList(JsonElement json)
{
SharedData sharedData = SharedData.getInstance();
String myLanguage = sharedData.getDefaultLanguage();

ArrayList<String> newEnumList = new ArrayList<>();
JsonArray parentArray = json.getAsJsonArray();

for (int x= 0; x<parentArray.size(); x++)
{
JsonArray childArray = parentArray.get(x).getAsJsonArray();
for (int i=0; i<childArray.size(); i++)
{
JsonObject myObj = childArray.get(i).getAsJsonObject();

String lang = myObj.get("language").getAsString();
String value = myObj.get("value").getAsString();

if(lang.equals(myLanguage))
newEnumList.add(value);
}
}
return newEnumList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,12 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.trufla.androidtruforms.models.ArrayInstance;
import com.trufla.androidtruforms.models.ObjectInstance;
import com.trufla.androidtruforms.models.ObjectProperties;
import com.trufla.androidtruforms.models.SchemaInstance;
import com.trufla.androidtruforms.models.TitleInstance;
import com.trufla.androidtruforms.utils.TruUtils;
import com.trufla.androidtruforms.utils.ValueToSchemaMapper;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


/**
* Created by ohefny on 6/27/18.
Expand All @@ -38,8 +29,14 @@ public ObjectProperties deserialize(JsonElement json, Type typeOfT, JsonDeserial

public SchemaInstance getPropertyItem(String key, JsonObject jsonObject, JsonDeserializationContext context) {
SchemaInstance schemaInstance = context.deserialize(jsonObject, SchemaInstance.class);
if (TruUtils.isEmpty(schemaInstance.getTitle()))
schemaInstance.setTitle(key);
if (TruUtils.isEmpty(schemaInstance.getTittleValue()))
{
TitleInstance titleInstance = new TitleInstance();
titleInstance.setTitleValue(key);
ArrayList<TitleInstance> schemaTitle = new ArrayList<>();
schemaTitle.add(titleInstance);
schemaInstance.setTitle(schemaTitle);
}
schemaInstance.setKey(key);
return schemaInstance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import androidx.annotation.Keep;

import com.google.gson.annotations.SerializedName;
import com.trufla.androidtruforms.SharedData;
import com.trufla.androidtruforms.truviews.TruEnumDataView;
import com.trufla.androidtruforms.truviews.TruEnumView;

Expand All @@ -21,11 +22,19 @@ public class EnumInstance<T> extends SchemaInstance
protected ArrayList<T> enumVals; //instance of String or Number or Boolean

@SerializedName("enumNames")
protected ArrayList<String> enumNames; //instance of String or Number or Boolean
protected ArrayList<ArrayList<TitleInstance>> enumNamesList; //instance of String or Number or Boolean

@SerializedName("_data")
protected DataInstance dataInstance;

protected ArrayList<String> myEnumNa;

private static SharedData sharedData;

// @SerializedName("enumNames")
// protected ArrayList<String> enumNames; //instance of String or Number or Boolean


public EnumInstance(){
}

Expand All @@ -38,18 +47,20 @@ public EnumInstance(EnumInstance<T> copyInstance)
{
super(copyInstance);
this.enumVals= new ArrayList<>(copyInstance.enumVals);
this.enumNames=new ArrayList<>(copyInstance.enumNames);
this.myEnumNa =new ArrayList<>(copyInstance.myEnumNa);
this.dataInstance=new DataInstance(copyInstance.getDataInstance());
}

public boolean enumExists() {
public boolean enumExists()
{
myEnumNa = getEnumList(enumNamesList);
return enumVals != null && !enumVals.isEmpty();
}

@Override
public TruEnumView getViewBuilder(Context context)
{
if (dataInstance == null)
if (dataInstance == null)
return new TruEnumView(context, this);
else
return new TruEnumDataView(context, this);
Expand All @@ -58,8 +69,8 @@ public TruEnumView getViewBuilder(Context context)
public ArrayList<String> getEnumDisplayedNames()
{
ArrayList<String> displayedNames = new ArrayList<>();
if (enumNames != null && !enumNames.isEmpty())
return enumNames;
if (myEnumNa != null && !myEnumNa.isEmpty())
return myEnumNa;

if(enumVals.size() > 0)
{
Expand All @@ -77,6 +88,28 @@ public ArrayList<String> getEnumDisplayedNames()
return displayedNames;
}

public ArrayList<String> getEnumList(ArrayList<ArrayList<TitleInstance>> myEnumsList)
{
sharedData = SharedData.getInstance();
String myLanguage = sharedData.getDefaultLanguage();

ArrayList<String> newEnumList = new ArrayList<>();

if(myEnumsList != null)
{
for(int listIndex = 0; listIndex< myEnumsList.size(); listIndex++)
{
for (TitleInstance titleInstance: myEnumsList.get(listIndex))
{
if(titleInstance.getLanguage().equals(myLanguage))
newEnumList.add(titleInstance.getTitleValue());
}
}
}

return newEnumList;
}

public DataInstance getDataInstance() {
return dataInstance;
}
Expand All @@ -89,11 +122,11 @@ public ArrayList<T> getEnumVals() {
return enumVals;
}

public void setEnumNames(ArrayList<String> enumNames) {
this.enumNames = enumNames;
public void setMyEnumNa(ArrayList<String> myEnumNa) {
this.myEnumNa = myEnumNa;
}

public ArrayList<String> getEnumNames() {
return enumNames;
public ArrayList<String> getMyEnumNa() {
return myEnumNa;
}
}
Loading

0 comments on commit 8f647bc

Please sign in to comment.