Guest User

Untitled

a guest
Jul 20th, 2018
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.75 KB | None | 0 0
  1. package com.signzy.indusind;
  2.  
  3. import android.app.AlertDialog;
  4. import android.content.DialogInterface;
  5. import android.content.Intent;
  6. import android.content.SharedPreferences;
  7. import android.graphics.Color;
  8. import android.support.design.widget.NavigationView;
  9. import android.support.design.widget.TextInputLayout;
  10. import android.support.v4.view.GravityCompat;
  11. import android.support.v4.widget.DrawerLayout;
  12. import android.support.v7.app.ActionBarDrawerToggle;
  13. import android.support.v7.app.AppCompatActivity;
  14. import android.os.Bundle;
  15. import android.support.v7.widget.Toolbar;
  16. import android.text.InputType;
  17. import android.util.Log;
  18. import android.view.ContextThemeWrapper;
  19. import android.view.Gravity;
  20. import android.view.MenuItem;
  21. import android.view.View;
  22. import android.widget.Button;
  23. import android.widget.CheckBox;
  24. import android.widget.EditText;
  25. import android.widget.ImageView;
  26. import android.widget.LinearLayout;
  27. import android.widget.RadioButton;
  28. import android.widget.RadioGroup;
  29. import android.widget.TextView;
  30.  
  31. import com.google.gson.Gson;
  32. import com.jaredrummler.materialspinner.MaterialSpinner;
  33. import com.signzy.indusind.model.FormData;
  34. import com.signzy.indusind.network.TestAPI;
  35. import com.signzy.indusind.network.response.ResUI;
  36. import com.signzy.indusind.network.response.customer.ResCusLogin;
  37. import com.signzy.indusind.util.ILoader;
  38.  
  39. import java.util.ArrayList;
  40. import java.util.Arrays;
  41. import java.util.HashMap;
  42. import java.util.List;
  43. import java.util.Map;
  44.  
  45. import retrofit2.Call;
  46. import retrofit2.Callback;
  47. import retrofit2.Response;
  48.  
  49. public class Form extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
  50.  
  51. LinearLayout bottomBar;
  52. SharedPreferences sh;
  53. SharedPreferences.Editor ed;
  54. AlertDialog ad;
  55.  
  56. TestAPI.ApiTest apiTest;
  57.  
  58. TextView signzy_id;
  59. ILoader iLoader;
  60. LinearLayout formLL;
  61. ImageView drop;
  62.  
  63. ResUI.Widgets widgets;
  64. ArrayList<FormData> data = new ArrayList<>();
  65.  
  66. Map<String, EditText> eds = new HashMap<>();
  67. Map<String, CheckBox> cbs = new HashMap<>();
  68. Map<String, RadioGroup> rbs = new HashMap<>();
  69. Map<String, MaterialSpinner> mbs = new HashMap<>();
  70. Map<String, LinearLayout> lls = new HashMap<>();
  71. Button save;
  72.  
  73. public void updateStep(int step)
  74. {
  75. if(sh.getString("entity","").equals("Individual"))
  76. signzy_id.setText("Step: "+step+"/"+getResources().getString(R.string.stepsIndividual));
  77. else
  78. signzy_id.setText("Step: "+step+"/"+getResources().getString(R.string.steps));
  79. }
  80.  
  81. @Override
  82. protected void onCreate(Bundle savedInstanceState) {
  83. super.onCreate(savedInstanceState);
  84. setContentView(R.layout.activity_form);
  85.  
  86. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  87. setSupportActionBar(toolbar);
  88. getSupportActionBar().setTitle("Forms");
  89.  
  90. DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  91. ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
  92. this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
  93. drawer.addDrawerListener(toggle);
  94. toggle.syncState();
  95.  
  96. NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
  97. navigationView.setNavigationItemSelectedListener(this);
  98.  
  99. signzy_id = (TextView)findViewById(R.id.signzy_id);
  100. save = (Button)findViewById(R.id.form_save);
  101. formLL = (LinearLayout)findViewById(R.id.formLL);
  102. iLoader = new ILoader(Form.this);
  103. drop = (ImageView)findViewById(R.id.drop);
  104.  
  105.  
  106. save.setVisibility(View.GONE);
  107. apiTest = TestAPI.getApiClient().create(TestAPI.ApiTest.class);
  108. iLoader.showPD("Building Elements...");
  109. Call<ResUI> resUICall = apiTest.fetchUI();
  110. resUICall.enqueue(new Callback<ResUI>() {
  111. @Override
  112. public void onResponse(Call<ResUI> call, Response<ResUI> response) {
  113. iLoader.hidePD();
  114. save.setVisibility(View.VISIBLE);
  115. if(response.body()!=null)
  116. {
  117. Log.d("resp",response.body().toString());
  118. ResUI resUI = response.body();
  119. widgets = resUI.getWidgets();
  120. Log.d("Widget Size",""+widgets.getData().size());
  121. bindWidget(widgets,formLL);
  122. lls.get(widgets.getName()).setVisibility(View.VISIBLE);
  123.  
  124. }
  125. }
  126.  
  127. @Override
  128. public void onFailure(Call<ResUI> call, Throwable t) {
  129. iLoader.hidePD();
  130. }
  131. });
  132.  
  133. sh = getSharedPreferences("entity",MODE_PRIVATE);
  134.  
  135. if(sh.getString("entity","").equals("Proprietor"))
  136. updateStep(13);
  137. else if(sh.getString("entity","").equals("Individual"))
  138. updateStep(11);
  139. else
  140. updateStep(14);
  141.  
  142. save.setOnClickListener((View v)->{
  143. startActivity(new Intent(this, Declaration.class));
  144. finish();
  145. dataSave(widgets);
  146. });
  147.  
  148. drop.setOnClickListener((View v)->{
  149.  
  150. AlertDialog.Builder adb = new AlertDialog.Builder(Form.this);
  151. adb.setMessage("Do you want to drop the current application ?")
  152. .setPositiveButton("No", new DialogInterface.OnClickListener() {
  153. @Override
  154. public void onClick(DialogInterface dialog, int which) {
  155. ad.dismiss();
  156. }
  157. })
  158. .setNegativeButton("Yes", new DialogInterface.OnClickListener() {
  159. @Override
  160. public void onClick(DialogInterface dialog, int which) {
  161. ad.dismiss();
  162. ResCusLogin resCusLogin = new Gson().fromJson(sh.getString("customer_login", ""), ResCusLogin.class);
  163. ed = sh.edit();
  164. ed.clear();
  165. ed.putString("customer_login", new Gson().toJson(resCusLogin));
  166. ed.commit();
  167. startActivity(new Intent(Form.this, Dashboard.class));
  168. finishAffinity();
  169. }
  170. }).setCancelable(false);
  171.  
  172. ad = adb.create();
  173. ad.show();
  174.  
  175. });
  176. }
  177.  
  178. @Override
  179. public void onBackPressed() {
  180. DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  181. if (drawer.isDrawerOpen(GravityCompat.START)) {
  182. drawer.closeDrawer(GravityCompat.START);
  183. } else {
  184. super.onBackPressed();
  185. }
  186. }
  187.  
  188. @SuppressWarnings("StatementWithEmptyBody")
  189. @Override
  190. public boolean onNavigationItemSelected(MenuItem item) {
  191. // Handle navigation view item clicks here.
  192. int id = item.getItemId();
  193.  
  194. if (id == R.id.nav_dash) {
  195. // Dialog Goes here
  196. android.app.AlertDialog.Builder adb = new android.app.AlertDialog.Builder(Form.this);
  197. adb.setMessage("Do you want to close the current application ?")
  198. .setPositiveButton("No", new DialogInterface.OnClickListener() {
  199. @Override
  200. public void onClick(DialogInterface dialog, int which) {
  201. ad.dismiss();
  202. }
  203. })
  204. .setNegativeButton("Yes", new DialogInterface.OnClickListener() {
  205. @Override
  206. public void onClick(DialogInterface dialog, int which) {
  207. ad.dismiss();
  208. ResCusLogin resCusLogin = new Gson().fromJson(sh.getString("customer_login", ""), ResCusLogin.class);
  209. ed = sh.edit();
  210. ed.clear();
  211. ed.putString("customer_login", new Gson().toJson(resCusLogin));
  212. ed.commit();
  213. startActivity(new Intent(Form.this, Dashboard.class));
  214. finish();
  215. }
  216. }).setCancelable(false);
  217.  
  218. ad = adb.create();
  219. ad.show();
  220. // Handle the camera action
  221.  
  222. }
  223.  
  224. else if (id == R.id.nav_opened) {
  225. // Handle the camera action
  226. startActivity(new Intent(getApplicationContext(), AccountsOpened.class));
  227. //finish();
  228. }
  229.  
  230. else if (id == R.id.nav_check)
  231. {
  232. startActivity(new Intent(getApplicationContext(), DeliveryStatus.class));
  233. }
  234.  
  235. DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  236. drawer.closeDrawer(GravityCompat.START);
  237. return true;
  238. }
  239.  
  240. private void dataSave(ResUI.Widgets widgets) {
  241. for(int i=0;i<widgets.getData().size();i++)
  242. {
  243. ResUI.Widgets.Data widget = widgets.getData().get(i);
  244.  
  245. FormData fd;
  246.  
  247. //Input Fields
  248. if(widget.getType().equals("inputText") || widget.getType().equals("inputNumber") || widget.getType().equals("inputDate") || widget.getType().equals("inputEmail"))
  249. {
  250. fd = new FormData();
  251. fd.setName(widget.getName());
  252. fd.setData(eds.get(fd.getName()).getText().toString());
  253. Log.d("Data ",fd.getName()+" - "+fd.getData());
  254. data.add(fd);
  255. }
  256.  
  257. //Checkbox Fields
  258. else if(widget.getType().equals("checkbox"))
  259. {
  260. fd = new FormData();
  261. fd.setName(widget.getName());
  262. CheckBox cb = cbs.get(fd.getName());
  263. if(cb.isChecked())
  264. fd.setData("yes");
  265. else
  266. fd.setData("no");
  267. Log.d("Data ",fd.getName()+" - "+fd.getData());
  268. data.add(fd);
  269.  
  270. //Route
  271. if(widget.getIfYes()!=null && cb.isChecked())
  272. {
  273. dataSave(widget.getIfYes());
  274. }
  275.  
  276. }
  277.  
  278. //Spinner Fields
  279. else if(widget.getType().equals("dropdown"))
  280. {
  281. fd = new FormData();
  282. fd.setName(widget.getName());
  283. fd.setData(mbs.get(fd.getName()).getText().toString());
  284. Log.d("Data ",fd.getName()+" - "+fd.getData());
  285. data.add(fd);
  286. }
  287.  
  288. //Radio Button
  289. else if(widget.getType().equals("radio"))
  290. {
  291. fd = new FormData();
  292. fd.setName(widget.getName());
  293. if(rbs.get(fd.getName()).getCheckedRadioButtonId()!= -1) {
  294. RadioButton rb = findViewById(rbs.get(fd.getName()).getCheckedRadioButtonId());
  295. fd.setData(rb.getText().toString());
  296. Log.d("Data ", fd.getName() + " - " + fd.getData());
  297. data.add(fd);
  298.  
  299. //Route
  300. if (widget.getIfYes() != null && rb.getText().toString().equals("Yes")) {
  301. dataSave(widget.getIfYes());
  302. }
  303. }
  304. }
  305.  
  306. //Custom Radio Buttons
  307. else if(widget.getType().equals("customRadio"))
  308. {
  309. fd = new FormData();
  310. fd.setName(widget.getName());
  311. if(rbs.get(fd.getName()).getCheckedRadioButtonId()!=-1)
  312. {
  313. RadioButton rb = findViewById(rbs.get(fd.getName()).getCheckedRadioButtonId());
  314. fd.setData(rb.getText().toString());
  315. Log.d("Data ",fd.getName()+" - "+fd.getData());
  316. data.add(fd);
  317.  
  318. //Route
  319. if(widget.getIfYes()!=null && rb.getText().toString().equals(widget.getPositive()))
  320. {
  321. dataSave(widget.getIfYes());
  322. }
  323. }
  324. }
  325. }
  326. }
  327.  
  328.  
  329. public void bindWidget(ResUI.Widgets widgets, LinearLayout form)
  330. {
  331. LinearLayout ll = new LinearLayout(Form.this);
  332. ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
  333. ll.setOrientation(LinearLayout.VERTICAL);
  334. ll.setVisibility(View.GONE);
  335. lls.put(widgets.getName(),ll);
  336. form.addView(ll);
  337.  
  338. for(int i=0;i<widgets.getData().size();i++)
  339. {
  340. ResUI.Widgets.Data widget = widgets.getData().get(i);
  341. // if(widget.getIfYes()!=null)
  342. // {
  343. // bindWidget(widget.getIfYes(),ll);
  344. // }
  345. // else
  346. // {
  347. Log.d("Type: ",widget.getName()+" & "+widget.getType());
  348. //Input Fields
  349. if(widget.getType().equals("inputText") || widget.getType().equals("inputNumber") || widget.getType().equals("inputDate") || widget.getType().equals("inputEmail"))
  350. {
  351. TextInputLayout textInputLayout = new TextInputLayout(new ContextThemeWrapper(Form.this, R.style.editText));
  352. textInputLayout.setHint(widget.getValue());
  353. EditText editText = new EditText(Form.this);
  354. if(widget.getType().equals("inputText"))
  355. editText.setInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
  356. else if(widget.getType().equals("inputDate"))
  357. editText.setInputType(InputType.TYPE_CLASS_DATETIME);
  358. else if(widget.getType().equals("inputNumber"))
  359. editText.setInputType(InputType.TYPE_CLASS_PHONE);
  360. else if(widget.getType().equals("inputEmail"))
  361. editText.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
  362. //editText.setId(i);
  363. eds.put(widget.getName(),editText);
  364. textInputLayout.addView(editText);
  365. ll.addView(textInputLayout);
  366. }
  367.  
  368. //Checkbox Fields
  369. else if(widget.getType().equals("checkbox"))
  370. {
  371. CheckBox checkBox = new CheckBox(Form.this);
  372. checkBox.setText(widget.getValue());
  373. LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
  374. LinearLayout.LayoutParams.WRAP_CONTENT,
  375. LinearLayout.LayoutParams.WRAP_CONTENT
  376. );
  377. params.setMargins(0, 15, 0, 15);
  378. checkBox.setLayoutParams(params);
  379. //checkBox.setId(i);
  380. cbs.put(widget.getName(),checkBox);
  381. ll.addView(checkBox);
  382.  
  383. if(widget.getIfYes()!=null)
  384. {
  385. checkBox.setOnClickListener((View v)->{
  386. lls.get(widget.getIfYes().getName()).setVisibility(View.VISIBLE);
  387. });
  388. }
  389. }
  390.  
  391. //Spinner Fields
  392. else if(widget.getType().equals("dropdown"))
  393. {
  394. MaterialSpinner materialSpinner = new MaterialSpinner(Form.this);
  395. materialSpinner.setHint(widget.getHint());
  396. LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
  397. LinearLayout.LayoutParams.MATCH_PARENT,
  398. LinearLayout.LayoutParams.WRAP_CONTENT
  399. );
  400. params.setMargins(0, 10, 0, 10);
  401. materialSpinner.setLayoutParams(params);
  402. List<String> options = widget.getOptions();
  403. options.add(0,widget.getHint());
  404. materialSpinner.setItems(options);
  405. materialSpinner.setBackgroundColor(Color.parseColor("#e0e0e0"));
  406. mbs.put(widget.getName(), materialSpinner);
  407. //materialSpinner.setId(i);
  408. ll.addView(materialSpinner);
  409. }
  410.  
  411. //Headings
  412. else if(widget.getType().equals("heading"))
  413. {
  414. TextView textView = new TextView(Form.this);
  415. textView.setPadding(0,15,0,15);
  416. textView.setTextColor(Color.parseColor("#000000"));
  417. textView.setText(widget.getValue());
  418. textView.setTextSize(20);
  419. textView.setBackgroundColor(Color.parseColor("#e0e0e0"));
  420. textView.setGravity(Gravity.CENTER);
  421. ll.addView(textView);
  422. }
  423.  
  424. //Radio Button
  425. else if(widget.getType().equals("radio"))
  426. {
  427. TextView textView = new TextView(Form.this);
  428. LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
  429. LinearLayout.LayoutParams.WRAP_CONTENT,
  430. LinearLayout.LayoutParams.WRAP_CONTENT
  431. );
  432. params.setMargins(10, 15, 0, 15);
  433. textView.setLayoutParams(params);
  434. textView.setTextColor(Color.parseColor("#000000"));
  435. textView.setText(widget.getValue());
  436. ll.addView(textView);
  437.  
  438. RadioGroup radioGroup = new RadioGroup(Form.this);
  439. radioGroup.setOrientation(LinearLayout.HORIZONTAL);
  440. radioGroup.setWeightSum(2);
  441. rbs.put(widget.getName(), radioGroup);
  442. //radioGroup.setId((i * 91) + 1);
  443. RadioButton yes = new RadioButton(Form.this);
  444. yes.setText("Yes");
  445. yes.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.MATCH_PARENT, (float) 1.0));
  446. radioGroup.addView(yes);
  447. RadioButton no = new RadioButton(Form.this);
  448. no.setText("No");
  449. no.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.MATCH_PARENT, (float) 1.0));
  450. radioGroup.addView(no);
  451. ll.addView(radioGroup);
  452.  
  453. if(widget.getIfYes()!=null)
  454. {
  455. yes.setOnClickListener((View v)->{
  456. lls.get(widget.getIfYes().getName()).setVisibility(View.VISIBLE);
  457. });
  458.  
  459. no.setOnClickListener((View v)->{
  460. lls.get(widget.getIfYes().getName()).setVisibility(View.GONE);
  461. });
  462. }
  463. }
  464.  
  465. //Custom Radio Buttons
  466. else if(widget.getType().equals("customRadio"))
  467. {
  468. Log.d("customRadio","Yes");
  469. RadioGroup radioGroup = new RadioGroup(Form.this);
  470. radioGroup.setOrientation(LinearLayout.VERTICAL);
  471. LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
  472. LinearLayout.LayoutParams.MATCH_PARENT,
  473. LinearLayout.LayoutParams.WRAP_CONTENT
  474. );
  475. params.setMargins(0, 10, 0, 10);
  476. radioGroup.setLayoutParams(params);
  477. rbs.put(widget.getName(), radioGroup);
  478. //radioGroup.setId((i * 91) + 1);
  479. RadioButton pos = new RadioButton(Form.this);
  480. pos.setText(widget.getPositive());
  481. pos.setLayoutParams(params);
  482. //pos.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.MATCH_PARENT, (float) 1.0));
  483. radioGroup.addView(pos);
  484.  
  485. RadioButton no = new RadioButton(Form.this);
  486. no.setText(widget.getNegative());
  487. no.setLayoutParams(params);
  488. //no.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.MATCH_PARENT, (float) 1.0));
  489. radioGroup.addView(no);
  490.  
  491. ll.addView(radioGroup);
  492.  
  493. if(widget.getIfYes()!=null)
  494. {
  495. pos.setOnClickListener((View v)->{
  496. lls.get(widget.getIfYes().getName()).setVisibility(View.VISIBLE);
  497. });
  498.  
  499. no.setOnClickListener((View v)->{
  500. lls.get(widget.getIfYes().getName()).setVisibility(View.GONE);
  501. });
  502. }
  503. }
  504.  
  505. if(widget.getIfYes()!=null)
  506. {
  507. bindWidget(widget.getIfYes(),ll);
  508. }
  509.  
  510. }
  511. }
  512. }
Add Comment
Please, Sign In to add comment