Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.signzy.indusind;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.graphics.Color;
- import android.support.design.widget.NavigationView;
- import android.support.design.widget.TextInputLayout;
- import android.support.v4.view.GravityCompat;
- import android.support.v4.widget.DrawerLayout;
- import android.support.v7.app.ActionBarDrawerToggle;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.support.v7.widget.Toolbar;
- import android.text.InputType;
- import android.util.Log;
- import android.view.ContextThemeWrapper;
- import android.view.Gravity;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import android.widget.TextView;
- import com.google.gson.Gson;
- import com.jaredrummler.materialspinner.MaterialSpinner;
- import com.signzy.indusind.model.FormData;
- import com.signzy.indusind.network.TestAPI;
- import com.signzy.indusind.network.response.ResUI;
- import com.signzy.indusind.network.response.customer.ResCusLogin;
- import com.signzy.indusind.util.ILoader;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import retrofit2.Call;
- import retrofit2.Callback;
- import retrofit2.Response;
- public class Form extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
- LinearLayout bottomBar;
- SharedPreferences sh;
- SharedPreferences.Editor ed;
- AlertDialog ad;
- TestAPI.ApiTest apiTest;
- TextView signzy_id;
- ILoader iLoader;
- LinearLayout formLL;
- ImageView drop;
- ResUI.Widgets widgets;
- ArrayList<FormData> data = new ArrayList<>();
- Map<String, EditText> eds = new HashMap<>();
- Map<String, CheckBox> cbs = new HashMap<>();
- Map<String, RadioGroup> rbs = new HashMap<>();
- Map<String, MaterialSpinner> mbs = new HashMap<>();
- Map<String, LinearLayout> lls = new HashMap<>();
- Button save;
- public void updateStep(int step)
- {
- if(sh.getString("entity","").equals("Individual"))
- signzy_id.setText("Step: "+step+"/"+getResources().getString(R.string.stepsIndividual));
- else
- signzy_id.setText("Step: "+step+"/"+getResources().getString(R.string.steps));
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_form);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- getSupportActionBar().setTitle("Forms");
- DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
- ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
- this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
- drawer.addDrawerListener(toggle);
- toggle.syncState();
- NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
- navigationView.setNavigationItemSelectedListener(this);
- signzy_id = (TextView)findViewById(R.id.signzy_id);
- save = (Button)findViewById(R.id.form_save);
- formLL = (LinearLayout)findViewById(R.id.formLL);
- iLoader = new ILoader(Form.this);
- drop = (ImageView)findViewById(R.id.drop);
- save.setVisibility(View.GONE);
- apiTest = TestAPI.getApiClient().create(TestAPI.ApiTest.class);
- iLoader.showPD("Building Elements...");
- Call<ResUI> resUICall = apiTest.fetchUI();
- resUICall.enqueue(new Callback<ResUI>() {
- @Override
- public void onResponse(Call<ResUI> call, Response<ResUI> response) {
- iLoader.hidePD();
- save.setVisibility(View.VISIBLE);
- if(response.body()!=null)
- {
- Log.d("resp",response.body().toString());
- ResUI resUI = response.body();
- widgets = resUI.getWidgets();
- Log.d("Widget Size",""+widgets.getData().size());
- bindWidget(widgets,formLL);
- lls.get(widgets.getName()).setVisibility(View.VISIBLE);
- }
- }
- @Override
- public void onFailure(Call<ResUI> call, Throwable t) {
- iLoader.hidePD();
- }
- });
- sh = getSharedPreferences("entity",MODE_PRIVATE);
- if(sh.getString("entity","").equals("Proprietor"))
- updateStep(13);
- else if(sh.getString("entity","").equals("Individual"))
- updateStep(11);
- else
- updateStep(14);
- save.setOnClickListener((View v)->{
- startActivity(new Intent(this, Declaration.class));
- finish();
- dataSave(widgets);
- });
- drop.setOnClickListener((View v)->{
- AlertDialog.Builder adb = new AlertDialog.Builder(Form.this);
- adb.setMessage("Do you want to drop the current application ?")
- .setPositiveButton("No", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- ad.dismiss();
- }
- })
- .setNegativeButton("Yes", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- ad.dismiss();
- ResCusLogin resCusLogin = new Gson().fromJson(sh.getString("customer_login", ""), ResCusLogin.class);
- ed = sh.edit();
- ed.clear();
- ed.putString("customer_login", new Gson().toJson(resCusLogin));
- ed.commit();
- startActivity(new Intent(Form.this, Dashboard.class));
- finishAffinity();
- }
- }).setCancelable(false);
- ad = adb.create();
- ad.show();
- });
- }
- @Override
- public void onBackPressed() {
- DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
- if (drawer.isDrawerOpen(GravityCompat.START)) {
- drawer.closeDrawer(GravityCompat.START);
- } else {
- super.onBackPressed();
- }
- }
- @SuppressWarnings("StatementWithEmptyBody")
- @Override
- public boolean onNavigationItemSelected(MenuItem item) {
- // Handle navigation view item clicks here.
- int id = item.getItemId();
- if (id == R.id.nav_dash) {
- // Dialog Goes here
- android.app.AlertDialog.Builder adb = new android.app.AlertDialog.Builder(Form.this);
- adb.setMessage("Do you want to close the current application ?")
- .setPositiveButton("No", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- ad.dismiss();
- }
- })
- .setNegativeButton("Yes", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- ad.dismiss();
- ResCusLogin resCusLogin = new Gson().fromJson(sh.getString("customer_login", ""), ResCusLogin.class);
- ed = sh.edit();
- ed.clear();
- ed.putString("customer_login", new Gson().toJson(resCusLogin));
- ed.commit();
- startActivity(new Intent(Form.this, Dashboard.class));
- finish();
- }
- }).setCancelable(false);
- ad = adb.create();
- ad.show();
- // Handle the camera action
- }
- else if (id == R.id.nav_opened) {
- // Handle the camera action
- startActivity(new Intent(getApplicationContext(), AccountsOpened.class));
- //finish();
- }
- else if (id == R.id.nav_check)
- {
- startActivity(new Intent(getApplicationContext(), DeliveryStatus.class));
- }
- DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
- drawer.closeDrawer(GravityCompat.START);
- return true;
- }
- private void dataSave(ResUI.Widgets widgets) {
- for(int i=0;i<widgets.getData().size();i++)
- {
- ResUI.Widgets.Data widget = widgets.getData().get(i);
- FormData fd;
- //Input Fields
- if(widget.getType().equals("inputText") || widget.getType().equals("inputNumber") || widget.getType().equals("inputDate") || widget.getType().equals("inputEmail"))
- {
- fd = new FormData();
- fd.setName(widget.getName());
- fd.setData(eds.get(fd.getName()).getText().toString());
- Log.d("Data ",fd.getName()+" - "+fd.getData());
- data.add(fd);
- }
- //Checkbox Fields
- else if(widget.getType().equals("checkbox"))
- {
- fd = new FormData();
- fd.setName(widget.getName());
- CheckBox cb = cbs.get(fd.getName());
- if(cb.isChecked())
- fd.setData("yes");
- else
- fd.setData("no");
- Log.d("Data ",fd.getName()+" - "+fd.getData());
- data.add(fd);
- //Route
- if(widget.getIfYes()!=null && cb.isChecked())
- {
- dataSave(widget.getIfYes());
- }
- }
- //Spinner Fields
- else if(widget.getType().equals("dropdown"))
- {
- fd = new FormData();
- fd.setName(widget.getName());
- fd.setData(mbs.get(fd.getName()).getText().toString());
- Log.d("Data ",fd.getName()+" - "+fd.getData());
- data.add(fd);
- }
- //Radio Button
- else if(widget.getType().equals("radio"))
- {
- fd = new FormData();
- fd.setName(widget.getName());
- if(rbs.get(fd.getName()).getCheckedRadioButtonId()!= -1) {
- RadioButton rb = findViewById(rbs.get(fd.getName()).getCheckedRadioButtonId());
- fd.setData(rb.getText().toString());
- Log.d("Data ", fd.getName() + " - " + fd.getData());
- data.add(fd);
- //Route
- if (widget.getIfYes() != null && rb.getText().toString().equals("Yes")) {
- dataSave(widget.getIfYes());
- }
- }
- }
- //Custom Radio Buttons
- else if(widget.getType().equals("customRadio"))
- {
- fd = new FormData();
- fd.setName(widget.getName());
- if(rbs.get(fd.getName()).getCheckedRadioButtonId()!=-1)
- {
- RadioButton rb = findViewById(rbs.get(fd.getName()).getCheckedRadioButtonId());
- fd.setData(rb.getText().toString());
- Log.d("Data ",fd.getName()+" - "+fd.getData());
- data.add(fd);
- //Route
- if(widget.getIfYes()!=null && rb.getText().toString().equals(widget.getPositive()))
- {
- dataSave(widget.getIfYes());
- }
- }
- }
- }
- }
- public void bindWidget(ResUI.Widgets widgets, LinearLayout form)
- {
- LinearLayout ll = new LinearLayout(Form.this);
- ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
- ll.setOrientation(LinearLayout.VERTICAL);
- ll.setVisibility(View.GONE);
- lls.put(widgets.getName(),ll);
- form.addView(ll);
- for(int i=0;i<widgets.getData().size();i++)
- {
- ResUI.Widgets.Data widget = widgets.getData().get(i);
- // if(widget.getIfYes()!=null)
- // {
- // bindWidget(widget.getIfYes(),ll);
- // }
- // else
- // {
- Log.d("Type: ",widget.getName()+" & "+widget.getType());
- //Input Fields
- if(widget.getType().equals("inputText") || widget.getType().equals("inputNumber") || widget.getType().equals("inputDate") || widget.getType().equals("inputEmail"))
- {
- TextInputLayout textInputLayout = new TextInputLayout(new ContextThemeWrapper(Form.this, R.style.editText));
- textInputLayout.setHint(widget.getValue());
- EditText editText = new EditText(Form.this);
- if(widget.getType().equals("inputText"))
- editText.setInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
- else if(widget.getType().equals("inputDate"))
- editText.setInputType(InputType.TYPE_CLASS_DATETIME);
- else if(widget.getType().equals("inputNumber"))
- editText.setInputType(InputType.TYPE_CLASS_PHONE);
- else if(widget.getType().equals("inputEmail"))
- editText.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
- //editText.setId(i);
- eds.put(widget.getName(),editText);
- textInputLayout.addView(editText);
- ll.addView(textInputLayout);
- }
- //Checkbox Fields
- else if(widget.getType().equals("checkbox"))
- {
- CheckBox checkBox = new CheckBox(Form.this);
- checkBox.setText(widget.getValue());
- LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.WRAP_CONTENT,
- LinearLayout.LayoutParams.WRAP_CONTENT
- );
- params.setMargins(0, 15, 0, 15);
- checkBox.setLayoutParams(params);
- //checkBox.setId(i);
- cbs.put(widget.getName(),checkBox);
- ll.addView(checkBox);
- if(widget.getIfYes()!=null)
- {
- checkBox.setOnClickListener((View v)->{
- lls.get(widget.getIfYes().getName()).setVisibility(View.VISIBLE);
- });
- }
- }
- //Spinner Fields
- else if(widget.getType().equals("dropdown"))
- {
- MaterialSpinner materialSpinner = new MaterialSpinner(Form.this);
- materialSpinner.setHint(widget.getHint());
- LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.MATCH_PARENT,
- LinearLayout.LayoutParams.WRAP_CONTENT
- );
- params.setMargins(0, 10, 0, 10);
- materialSpinner.setLayoutParams(params);
- List<String> options = widget.getOptions();
- options.add(0,widget.getHint());
- materialSpinner.setItems(options);
- materialSpinner.setBackgroundColor(Color.parseColor("#e0e0e0"));
- mbs.put(widget.getName(), materialSpinner);
- //materialSpinner.setId(i);
- ll.addView(materialSpinner);
- }
- //Headings
- else if(widget.getType().equals("heading"))
- {
- TextView textView = new TextView(Form.this);
- textView.setPadding(0,15,0,15);
- textView.setTextColor(Color.parseColor("#000000"));
- textView.setText(widget.getValue());
- textView.setTextSize(20);
- textView.setBackgroundColor(Color.parseColor("#e0e0e0"));
- textView.setGravity(Gravity.CENTER);
- ll.addView(textView);
- }
- //Radio Button
- else if(widget.getType().equals("radio"))
- {
- TextView textView = new TextView(Form.this);
- LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.WRAP_CONTENT,
- LinearLayout.LayoutParams.WRAP_CONTENT
- );
- params.setMargins(10, 15, 0, 15);
- textView.setLayoutParams(params);
- textView.setTextColor(Color.parseColor("#000000"));
- textView.setText(widget.getValue());
- ll.addView(textView);
- RadioGroup radioGroup = new RadioGroup(Form.this);
- radioGroup.setOrientation(LinearLayout.HORIZONTAL);
- radioGroup.setWeightSum(2);
- rbs.put(widget.getName(), radioGroup);
- //radioGroup.setId((i * 91) + 1);
- RadioButton yes = new RadioButton(Form.this);
- yes.setText("Yes");
- yes.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.MATCH_PARENT, (float) 1.0));
- radioGroup.addView(yes);
- RadioButton no = new RadioButton(Form.this);
- no.setText("No");
- no.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.MATCH_PARENT, (float) 1.0));
- radioGroup.addView(no);
- ll.addView(radioGroup);
- if(widget.getIfYes()!=null)
- {
- yes.setOnClickListener((View v)->{
- lls.get(widget.getIfYes().getName()).setVisibility(View.VISIBLE);
- });
- no.setOnClickListener((View v)->{
- lls.get(widget.getIfYes().getName()).setVisibility(View.GONE);
- });
- }
- }
- //Custom Radio Buttons
- else if(widget.getType().equals("customRadio"))
- {
- Log.d("customRadio","Yes");
- RadioGroup radioGroup = new RadioGroup(Form.this);
- radioGroup.setOrientation(LinearLayout.VERTICAL);
- LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.MATCH_PARENT,
- LinearLayout.LayoutParams.WRAP_CONTENT
- );
- params.setMargins(0, 10, 0, 10);
- radioGroup.setLayoutParams(params);
- rbs.put(widget.getName(), radioGroup);
- //radioGroup.setId((i * 91) + 1);
- RadioButton pos = new RadioButton(Form.this);
- pos.setText(widget.getPositive());
- pos.setLayoutParams(params);
- //pos.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.MATCH_PARENT, (float) 1.0));
- radioGroup.addView(pos);
- RadioButton no = new RadioButton(Form.this);
- no.setText(widget.getNegative());
- no.setLayoutParams(params);
- //no.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.MATCH_PARENT, (float) 1.0));
- radioGroup.addView(no);
- ll.addView(radioGroup);
- if(widget.getIfYes()!=null)
- {
- pos.setOnClickListener((View v)->{
- lls.get(widget.getIfYes().getName()).setVisibility(View.VISIBLE);
- });
- no.setOnClickListener((View v)->{
- lls.get(widget.getIfYes().getName()).setVisibility(View.GONE);
- });
- }
- }
- if(widget.getIfYes()!=null)
- {
- bindWidget(widget.getIfYes(),ll);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment