MyProfileActivity.java 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. package com.cygnusa.hyperm;
  2. import android.annotation.SuppressLint;
  3. import android.app.Activity;
  4. import android.app.DatePickerDialog;
  5. import android.content.ContentValues;
  6. import android.content.Intent;
  7. import android.content.res.ColorStateList;
  8. import android.graphics.Color;
  9. import android.os.Bundle;
  10. import android.os.Handler;
  11. import android.text.TextUtils;
  12. import android.view.LayoutInflater;
  13. import android.view.MenuItem;
  14. import android.view.MotionEvent;
  15. import android.view.View;
  16. import android.view.ViewGroup;
  17. import android.view.animation.AnimationUtils;
  18. import android.view.animation.TranslateAnimation;
  19. import android.view.inputmethod.InputMethodManager;
  20. import android.widget.Button;
  21. import android.widget.CheckBox;
  22. import android.widget.DatePicker;
  23. import android.widget.EditText;
  24. import android.widget.ImageButton;
  25. import android.widget.ImageView;
  26. import android.widget.LinearLayout;
  27. import android.widget.TextView;
  28. import android.widget.Toast;
  29. import androidx.appcompat.app.AlertDialog;
  30. import androidx.appcompat.app.AppCompatActivity;
  31. import androidx.appcompat.widget.Toolbar;
  32. import com.bumptech.glide.Glide;
  33. import com.google.android.material.appbar.AppBarLayout;
  34. import com.google.android.material.floatingactionbutton.FloatingActionButton;
  35. import com.google.android.material.textfield.TextInputLayout;
  36. import com.google.gson.Gson;
  37. import com.google.gson.JsonElement;
  38. import com.google.gson.reflect.TypeToken;
  39. import com.cygnusa.hyperm.fancyshowcase.FancyShowCaseView;
  40. import com.cygnusa.hyperm.fancyshowcase.FocusShape;
  41. import com.cygnusa.hyperm.pickimage.bean.PickResult;
  42. import com.cygnusa.hyperm.pickimage.bundle.PickSetup;
  43. import com.cygnusa.hyperm.pickimage.dialog.PickImageDialog;
  44. import com.cygnusa.hyperm.pickimage.listeners.IPickCancel;
  45. import com.cygnusa.hyperm.pickimage.listeners.IPickResult;
  46. import com.cygnusa.hyperm.util.ApiService;
  47. import com.cygnusa.hyperm.util.GlobalData;
  48. import com.cygnusa.hyperm.util.MainApplication;
  49. import com.cygnusa.hyperm.util.TransparentProgressDialog;
  50. import com.cygnusa.hyperm.util.UtilService;
  51. import com.cygnusa.hyperm.widget.SubtitleCollapsingToolbarLayout;
  52. import com.cygnusa.hyperm.wrappers.ErrorResponse;
  53. import com.cygnusa.hyperm.wrappers.UserResponse;
  54. import java.io.File;
  55. import java.lang.reflect.Field;
  56. import java.lang.reflect.Method;
  57. import java.text.ParseException;
  58. import java.text.SimpleDateFormat;
  59. import java.util.Calendar;
  60. import java.util.Date;
  61. import java.util.HashMap;
  62. import okhttp3.MediaType;
  63. import okhttp3.MultipartBody;
  64. import okhttp3.RequestBody;
  65. import retrofit2.Call;
  66. import retrofit2.Callback;
  67. import retrofit2.Response;
  68. public class MyProfileActivity extends AppCompatActivity implements IPickResult {
  69. public Calendar calendar;
  70. public Calendar calendarret;
  71. ApiService restService;
  72. TransparentProgressDialog progressDialog;
  73. AppBarLayout appBarLayout;
  74. Toolbar toolbar;
  75. ImageView userImage;
  76. FloatingActionButton editButton;
  77. FloatingActionButton cameraButton;
  78. LinearLayout bottomLayout;
  79. EditText firstName, lastName, dob, email, mobile, doj, dealer, zone, state, city, region, designation, weddingDate;
  80. Button submitButton;
  81. UserResponse MY_PROFILE = new UserResponse();
  82. SubtitleCollapsingToolbarLayout collapsingToolbar;
  83. String IMAGE_PATH = "";
  84. ImageButton toolEditIcon, helpIcon;
  85. CheckBox checkBoxMarried;
  86. AlertDialog.Builder alertDialogBuilder;
  87. AlertDialog alertDialog;
  88. String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
  89. private ImageView dobCalendar,weddingCalendar;
  90. private int year;
  91. private int month;
  92. private int day;
  93. boolean isDOB = false;
  94. private LinearLayout linearWeddingDate;
  95. private boolean isEditable = false;
  96. public TextInputLayout editInputDob,editInputEmail,editInputWedding,getEditInputMobile;
  97. public static void hideSoftKeyboard(Activity activity) {
  98. try {
  99. InputMethodManager inputMethodManager =
  100. (InputMethodManager) activity.getSystemService(
  101. Activity.INPUT_METHOD_SERVICE);
  102. inputMethodManager.hideSoftInputFromWindow(
  103. activity.getCurrentFocus().getWindowToken(), 0);
  104. } catch (Exception e) {
  105. e.printStackTrace();
  106. }
  107. }
  108. private static boolean isValidEmail(String email) {
  109. return !TextUtils.isEmpty(email) && android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
  110. }
  111. @SuppressLint("WrongViewCast")
  112. @Override
  113. protected void onCreate(Bundle savedInstanceState) {
  114. super.onCreate(savedInstanceState);
  115. setContentView(R.layout.activity_my_profile);
  116. setupWindowAnimations();
  117. setupUI(findViewById(R.id.main_content));
  118. calendar = Calendar.getInstance();
  119. year = calendar.get(Calendar.YEAR);
  120. month = calendar.get(Calendar.MONTH);
  121. day = calendar.get(Calendar.DAY_OF_MONTH);
  122. restService = ((MainApplication) getApplication()).getClient();
  123. appBarLayout = findViewById(R.id.appBarLayout);
  124. userImage = findViewById(R.id.userImage);
  125. toolbar = findViewById(R.id.toolbar);
  126. bottomLayout = findViewById(R.id.bottomLayout);
  127. toolEditIcon = findViewById(R.id.toolEditIcon);
  128. helpIcon = findViewById(R.id.helpIcon);
  129. helpIcon.setVisibility(View.VISIBLE);
  130. email = findViewById(R.id.email);
  131. editInputEmail = findViewById(R.id.txtemail);
  132. editInputDob = findViewById(R.id.txtInputDob);
  133. editInputWedding = findViewById(R.id.txtInputWedding);
  134. editInputWedding = findViewById(R.id.edit_mobile);
  135. mobile = findViewById(R.id.mobile);
  136. doj = findViewById(R.id.doj);
  137. dob = findViewById(R.id.dob);
  138. dealer = findViewById(R.id.dealer);
  139. zone = findViewById(R.id.zone);
  140. region = findViewById(R.id.region);
  141. state = findViewById(R.id.state);
  142. city = findViewById(R.id.city);
  143. designation = findViewById(R.id.designation);
  144. checkBoxMarried = findViewById(R.id.checkMarried);
  145. weddingDate = findViewById(R.id.weddingDate);
  146. dobCalendar = findViewById(R.id.dob_date);
  147. weddingCalendar = findViewById(R.id.weddding_date);
  148. linearWeddingDate = findViewById(R.id.linear_wedding_date);
  149. submitButton = findViewById(R.id.submitButton);
  150. email.setEnabled(false);
  151. weddingDate.setEnabled(false);
  152. dob.setEnabled(false);
  153. doj.setEnabled(false);
  154. dealer.setEnabled(false);
  155. designation.setEnabled(false);
  156. mobile.setEnabled(false);
  157. region.setEnabled(false);
  158. state.setEnabled(false);
  159. zone.setEnabled(false);
  160. city.setEnabled(false);
  161. weddingCalendar.setVisibility(View.VISIBLE);
  162. checkBoxMarried.setChecked(false);
  163. setSupportActionBar(toolbar);
  164. toolbar.setTitle("");
  165. getSupportActionBar().setTitle("");
  166. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  167. getSupportActionBar().setDisplayShowHomeEnabled(true);
  168. collapsingToolbar = findViewById(R.id.collapsing_toolbar);
  169. appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
  170. @Override
  171. public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
  172. if (Math.abs(verticalOffset) - appBarLayout.getTotalScrollRange() == 0) {
  173. toolbar.setBackgroundColor(getResources().getColor(R.color.bg_color));
  174. toolEditIcon.setVisibility(View.VISIBLE);
  175. helpIcon.setVisibility(View.GONE);
  176. } else {
  177. toolbar.setBackgroundColor(Color.TRANSPARENT);
  178. toolEditIcon.setVisibility(View.GONE);
  179. helpIcon.setVisibility(View.VISIBLE);
  180. }
  181. }
  182. });
  183. toolEditIcon.setOnClickListener(new View.OnClickListener() {
  184. @Override
  185. public void onClick(View v) {
  186. editButton.performClick();
  187. }
  188. });
  189. dobCalendar.setOnClickListener(new View.OnClickListener() {
  190. @Override
  191. public void onClick(View v) {
  192. isDOB = true;
  193. Calendar c = Calendar.getInstance();
  194. c.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - 18);
  195. Date date = new Date();
  196. if (dob.getText().toString().trim().isEmpty()
  197. || dob.getText().toString().trim().equalsIgnoreCase("0000-00-00")
  198. || dob.getText().toString().trim().equalsIgnoreCase("00-00-0000")
  199. || dob.getText().toString().trim().equalsIgnoreCase("00/00/0000")
  200. || dob.getText().toString().trim().equalsIgnoreCase("0000/00/00")) {
  201. date = c.getTime();
  202. } else {
  203. if (dob.getText().toString().trim().contains("-")) {
  204. try {
  205. date = new SimpleDateFormat("yyyy-MM-dd").parse(dob.getText().toString().trim());
  206. } catch (ParseException e) {
  207. e.printStackTrace();
  208. }
  209. } else {
  210. try {
  211. date = new SimpleDateFormat("dd-MMM-yyyy").parse(dob.getText().toString().trim());
  212. } catch (ParseException e) {
  213. e.printStackTrace();
  214. }
  215. }
  216. }
  217. c.setTime(date);
  218. DatePickerDialog dialog = new DatePickerDialog(MyProfileActivity.this, tolistener,
  219. c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
  220. try {
  221. c = Calendar.getInstance();
  222. c.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - 18);
  223. DatePicker datePicker = dialog.getDatePicker();
  224. datePicker.setMaxDate(c.getTime().getTime());
  225. } catch (Exception e) {
  226. e.printStackTrace();
  227. }
  228. dialog.show();
  229. }
  230. });
  231. weddingCalendar.setOnClickListener(new View.OnClickListener() {
  232. @Override
  233. public void onClick(View v) {
  234. isDOB = false;
  235. Calendar c = Calendar.getInstance();
  236. Date date = new Date();
  237. if (weddingDate.getText().toString().trim().isEmpty()
  238. || weddingDate.getText().toString().trim().equalsIgnoreCase("0000-00-00")
  239. || weddingDate.getText().toString().trim().equalsIgnoreCase("00-00-0000")
  240. || weddingDate.getText().toString().trim().equalsIgnoreCase("00/00/0000")
  241. || weddingDate.getText().toString().trim().equalsIgnoreCase("0000/00/00")) {
  242. date = c.getTime();
  243. } else {
  244. if (weddingDate.getText().toString().trim().contains("-")) {
  245. try {
  246. date = new SimpleDateFormat("yyyy-MM-dd").parse(weddingDate.getText().toString().trim());
  247. } catch (ParseException e) {
  248. e.printStackTrace();
  249. }
  250. } else {
  251. try {
  252. date = new SimpleDateFormat("dd-MMM-yyyy").parse(weddingDate.getText().toString().trim());
  253. } catch (ParseException e) {
  254. e.printStackTrace();
  255. }
  256. }
  257. }
  258. c.setTime(date);
  259. DatePickerDialog dialog = new DatePickerDialog(MyProfileActivity.this, tolistener,
  260. c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
  261. try {
  262. DatePicker datePicker = dialog.getDatePicker();
  263. datePicker.setMaxDate(c.getTime().getTime());
  264. } catch (Exception e) {
  265. e.printStackTrace();
  266. }
  267. dialog.show();
  268. }
  269. });
  270. editButton = findViewById(R.id.editButton);
  271. cameraButton = findViewById(R.id.cameraButton);
  272. cameraButton.hide();
  273. bottomLayout.setVisibility(View.INVISIBLE);
  274. linearWeddingDate.setVisibility(View.INVISIBLE);
  275. dobCalendar.setVisibility(View.INVISIBLE);
  276. checkBoxMarried.setEnabled(false);
  277. weddingCalendar.setVisibility(View.INVISIBLE);
  278. getMyProfile();
  279. checkBoxMarried.setOnClickListener(new View.OnClickListener() {
  280. @Override
  281. public void onClick(View v) {
  282. boolean checked = ((CheckBox) v).isChecked();
  283. if (checked) {
  284. linearWeddingDate.setVisibility(View.VISIBLE);
  285. if (!isEditable) {
  286. weddingCalendar.setVisibility(View.INVISIBLE);
  287. } else {
  288. weddingCalendar.setVisibility(View.VISIBLE);
  289. }
  290. } else {
  291. linearWeddingDate.setVisibility(View.INVISIBLE);
  292. if (!isEditable) {
  293. weddingCalendar.setVisibility(View.INVISIBLE);
  294. } else {
  295. weddingCalendar.setVisibility(View.VISIBLE);
  296. }
  297. }
  298. }
  299. });
  300. editButton.setOnClickListener(new View.OnClickListener() {
  301. @Override
  302. public void onClick(View v) {
  303. if (cameraButton.getVisibility() == View.GONE) {
  304. IMAGE_PATH = "";
  305. setUpperHintColor(getResources().getColor(R.color.bg_color_dark),editInputDob);
  306. setUpperHintColor(getResources().getColor(R.color.bg_color_dark),editInputEmail);
  307. setUpperHintColor(getResources().getColor(R.color.bg_color_dark),editInputWedding);
  308. mobile.setTextColor(getResources().getColor(R.color.material_grey300));
  309. designation.setTextColor(getResources().getColor(R.color.material_grey300));
  310. dealer.setTextColor(getResources().getColor(R.color.material_grey300));
  311. doj.setTextColor(getResources().getColor(R.color.material_grey300));
  312. zone.setTextColor(getResources().getColor(R.color.material_grey300));
  313. region.setTextColor(getResources().getColor(R.color.material_grey300));
  314. state.setTextColor(getResources().getColor(R.color.material_grey300));
  315. city.setTextColor(getResources().getColor(R.color.material_grey300));
  316. weddingDate.setHintTextColor(getResources().getColor(R.color.material_grey700));
  317. cameraButton.show();
  318. cameraButton.startAnimation(AnimationUtils.loadAnimation(MyProfileActivity.this, R.anim.zoomin));
  319. slideToTop(bottomLayout);
  320. email.setEnabled(true);
  321. checkBoxMarried.setEnabled(true);
  322. dobCalendar.setVisibility(View.VISIBLE);
  323. isEditable = true;
  324. } else {
  325. setUpperHintColor(getResources().getColor(R.color.btn_bg_color),editInputDob);
  326. setUpperHintColor(getResources().getColor(R.color.btn_bg_color),editInputEmail);
  327. setUpperHintColor(getResources().getColor(R.color.btn_bg_color),editInputWedding);
  328. mobile.setTextColor(getResources().getColor(R.color.btn_bg_color));
  329. designation.setTextColor(getResources().getColor(R.color.btn_bg_color));
  330. doj.setTextColor(getResources().getColor(R.color.btn_bg_color));
  331. zone.setTextColor(getResources().getColor(R.color.btn_bg_color));
  332. region.setTextColor(getResources().getColor(R.color.btn_bg_color));
  333. state.setTextColor(getResources().getColor(R.color.btn_bg_color));
  334. city.setTextColor(getResources().getColor(R.color.btn_bg_color));
  335. dealer.setTextColor(getResources().getColor(R.color.btn_bg_color));
  336. cameraButton.startAnimation(AnimationUtils.loadAnimation(MyProfileActivity.this, R.anim.zoomout));
  337. cameraButton.hide();
  338. slideToBottom(bottomLayout);
  339. email.setEnabled(false);
  340. checkBoxMarried.setEnabled(false);
  341. dobCalendar.setVisibility(View.INVISIBLE);
  342. weddingCalendar.setVisibility(View.INVISIBLE);
  343. IMAGE_PATH = "";
  344. isEditable = false;
  345. setMyProfile();
  346. }
  347. }
  348. });
  349. cameraButton.setOnClickListener(new View.OnClickListener() {
  350. @Override
  351. public void onClick(View v) {
  352. PickImageDialog.build(new PickSetup().setButtonOrientation(LinearLayout.HORIZONTAL)).setOnPickCancel(new IPickCancel() {
  353. @Override
  354. public void onCancelClick() {
  355. }
  356. }).show(MyProfileActivity.this);
  357. }
  358. });
  359. submitButton.setOnClickListener(new View.OnClickListener() {
  360. @Override
  361. public void onClick(View v) {
  362. updateProfile();
  363. }
  364. });
  365. helpIcon.setOnClickListener(new View.OnClickListener() {
  366. @Override
  367. public void onClick(View v) {
  368. showHelpScreen();
  369. }
  370. });
  371. }
  372. private static String displayMonth(String date) throws ParseException{
  373. Date d = new SimpleDateFormat("dd-MM-yyyy").parse(date);
  374. Calendar cal = Calendar.getInstance();
  375. cal.setTime(d);
  376. String monthName = new SimpleDateFormat("dd-MMM-yyyy").format(cal.getTime());
  377. return monthName;
  378. }
  379. private void setUpperHintColor(int color, TextInputLayout txtInputLayout) {
  380. try {
  381. Field field = txtInputLayout.getClass().getDeclaredField("mFocusedTextColor");
  382. field.setAccessible(true);
  383. int[][] states = new int[][]{
  384. new int[]{}
  385. };
  386. int[] colors = new int[]{
  387. color
  388. };
  389. ColorStateList myList = new ColorStateList(states, colors);
  390. field.set(txtInputLayout, myList);
  391. Method method = txtInputLayout.getClass().getDeclaredMethod("updateLabelState", boolean.class);
  392. method.setAccessible(true);
  393. method.invoke(txtInputLayout, true);
  394. } catch (Exception e) {
  395. e.printStackTrace();
  396. }
  397. }
  398. public DatePickerDialog.OnDateSetListener tolistener = new DatePickerDialog.OnDateSetListener() {
  399. @Override
  400. public void onDateSet(DatePicker view, int y, int monthOfYear, int dayOfMonth) {
  401. year = y;
  402. month = monthOfYear;
  403. day = dayOfMonth;
  404. String datevalue = String.valueOf(day);
  405. int mval = (month + 1);
  406. String monthvalue = String.valueOf(mval);
  407. String date = "0";
  408. String monthname = "0";
  409. if (datevalue.length() == 1) {
  410. date = "0" + datevalue;
  411. } else {
  412. date = datevalue;
  413. }
  414. if (monthvalue.length() == 1) {
  415. monthname = "0" + monthvalue;
  416. } else {
  417. monthname = monthvalue;
  418. }
  419. //dateValue = String.valueOf(year) + "-" + (month + 1) + "-" + day + " ";
  420. String dateValue = String.valueOf(year) + "-"+monthname+"-"+date;
  421. dob.setText(dateFormate(dateValue));
  422. if (isDOB) {
  423. dob.setText(dateFormate(dateValue));
  424. } else {
  425. weddingDate.setText(dateFormate(dateValue));
  426. }
  427. }
  428. };
  429. public String dateFormate(String dte) {
  430. String date = "";
  431. if (dte.equalsIgnoreCase("0000-00-00")) {
  432. date = "00/00/0000";
  433. return date;
  434. } else {
  435. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  436. Date newDate = null;
  437. try {
  438. newDate = format.parse(dte);
  439. } catch (ParseException e) {
  440. e.printStackTrace();
  441. }
  442. format = new SimpleDateFormat("dd-MMM-yyyy");
  443. date = format.format(newDate);
  444. }
  445. return date;
  446. }
  447. public String finalDateFormat(String date) {
  448. SimpleDateFormat format1 = new SimpleDateFormat("dd-MMM-yyyy");
  449. Date newDate = null;
  450. try {
  451. newDate = format1.parse(date);
  452. } catch (ParseException e) {
  453. e.printStackTrace();
  454. }
  455. format1 = new SimpleDateFormat("yyyy-MM-dd");
  456. date = format1.format(newDate);
  457. return date;
  458. }
  459. private void setupWindowAnimations() {
  460. this.overridePendingTransition(R.anim.animation_enter,
  461. R.anim.animation_leave);
  462. }
  463. @Override
  464. public boolean onOptionsItemSelected(MenuItem item) {
  465. if (item.getItemId() == android.R.id.home) {
  466. finish();
  467. overridePendingTransition(R.anim.animation_f_enter,
  468. R.anim.animation_f_leave);
  469. }
  470. return super.onOptionsItemSelected(item);
  471. }
  472. @Override
  473. public void onBackPressed() {
  474. finish();
  475. overridePendingTransition(R.anim.animation_f_enter,
  476. R.anim.animation_f_leave);
  477. }
  478. public void slideToBottom(View view) {
  479. TranslateAnimation animate = new TranslateAnimation(0, 0, 0, view.getHeight());
  480. animate.setDuration(500);
  481. animate.setFillAfter(true);
  482. view.startAnimation(animate);
  483. view.setVisibility(View.GONE);
  484. }
  485. public void slideToTop(View view) {
  486. TranslateAnimation animate = new TranslateAnimation(0, 0, view.getHeight(), 0);
  487. animate.setDuration(500);
  488. animate.setFillAfter(true);
  489. view.startAnimation(animate);
  490. view.setVisibility(View.VISIBLE);
  491. }
  492. private void getMyProfile() {
  493. if (new UtilService().isNetworkAvailable(MyProfileActivity.this)) {
  494. progressDialog = new TransparentProgressDialog(MyProfileActivity.this);
  495. progressDialog.show();
  496. try {
  497. restService.getProfile().enqueue(new Callback<UserResponse>() {
  498. @Override
  499. public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
  500. if (progressDialog != null && progressDialog.isShowing()) {
  501. progressDialog.dismiss();
  502. }
  503. if (response.body() != null && response.body().getStatus() == 200) {
  504. MY_PROFILE = response.body();
  505. if (MY_PROFILE.getData().getIs_married().equalsIgnoreCase("1")) {
  506. checkBoxMarried.setChecked(true);
  507. linearWeddingDate.setVisibility(View.VISIBLE);
  508. }
  509. setMyProfile();
  510. } else if (response.body() != null && response.body().getStatus() == 401) {
  511. GlobalData.canLogout = true;
  512. finish();
  513. } else if (response.body() != null) {
  514. GlobalData.showToast(getBaseContext(), response.body().getMessage(), Toast.LENGTH_SHORT).show();
  515. } else if (response.errorBody() != null) {
  516. Gson gson = new Gson();
  517. ErrorResponse message = gson.fromJson(response.errorBody().charStream(), ErrorResponse.class);
  518. GlobalData.showToast(getBaseContext(), message.getMessage(), Toast.LENGTH_SHORT).show();
  519. if (message.getStatus() == 401) {
  520. GlobalData.canLogout = true;
  521. finish();
  522. }
  523. }
  524. new Handler().postDelayed(new Runnable() {
  525. @Override
  526. public void run() {
  527. if (getPreferences(MODE_PRIVATE).getBoolean("MyProfileActivity", true)) {
  528. showHelpScreen();
  529. getPreferences(MODE_PRIVATE).edit().putBoolean("MyProfileActivity", false).apply();
  530. }
  531. }
  532. }, 1000);
  533. }
  534. @Override
  535. public void onFailure(Call<UserResponse> call, Throwable t) {
  536. if (progressDialog != null && progressDialog.isShowing()) {
  537. progressDialog.dismiss();
  538. }
  539. GlobalData.showToast(getBaseContext(), "Connection Failure, try again!", Toast.LENGTH_SHORT).show();
  540. }
  541. });
  542. } catch (Exception e) {
  543. if (progressDialog != null && progressDialog.isShowing()) {
  544. progressDialog.dismiss();
  545. }
  546. }
  547. } else {
  548. GlobalData.showToast(getBaseContext(), "Please check your internet connection", Toast.LENGTH_SHORT).show();
  549. }
  550. }
  551. protected void onResume() {
  552. super.onResume();
  553. if (GlobalData.canLogout) {
  554. finish();
  555. }
  556. }
  557. private void setMyProfile() {
  558. email.setText(MY_PROFILE.getData().getEmailid());
  559. mobile.setText(MY_PROFILE.getData().getMobileno());
  560. dealer.setText(MY_PROFILE.getData().getDealer().getDealer_name());
  561. designation.setText(MY_PROFILE.getData().getRolename());
  562. doj.setText(dateFormate(MY_PROFILE.getData().getDoj()));
  563. zone.setText(MY_PROFILE.getData().getZone());
  564. region.setText(MY_PROFILE.getData().getZone());
  565. state.setText(MY_PROFILE.getData().getState());
  566. city.setText(MY_PROFILE.getData().getCity());
  567. dob.setText(dateFormate(MY_PROFILE.getData().getDob()));
  568. weddingDate.setText(dateFormate(MY_PROFILE.getData().getMarriage_date()));
  569. collapsingToolbar.setTitle(MY_PROFILE.getData().getFname() + " " + MY_PROFILE.getData().getLname());
  570. collapsingToolbar.setSubtitle(MY_PROFILE.getData().getRolename());
  571. Glide.with(this).load(MY_PROFILE.getData().getProfile_photo())
  572. .thumbnail(Glide.with(this).load(R.drawable.gif_loader))
  573. .into(userImage);
  574. }
  575. @Override
  576. public void onPickResult(PickResult pickResult) {
  577. if (pickResult.getError() == null) {
  578. IMAGE_PATH = pickResult.getPath();
  579. if (IMAGE_PATH != null && !IMAGE_PATH.trim().isEmpty()) {
  580. Intent intent = new Intent(MyProfileActivity.this, CropActivity.class);
  581. CropActivity.mSourceUri = pickResult.getUri();
  582. startActivityForResult(intent, 2276);
  583. } else {
  584. userImage.setImageBitmap(pickResult.getBitmap());
  585. }
  586. } else {
  587. GlobalData.showToast(this, pickResult.getError().getMessage(), Toast.LENGTH_LONG).show();
  588. }
  589. }
  590. @Override
  591. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  592. super.onActivityResult(requestCode, resultCode, data);
  593. if (resultCode == RESULT_OK && requestCode == 2276) {
  594. IMAGE_PATH = data.getStringExtra("IMAGE_PATH");
  595. Glide.with(this).load(new File(IMAGE_PATH))
  596. .thumbnail(Glide.with(this).load(R.drawable.gif_loader))
  597. .into(userImage);
  598. } else if (resultCode == RESULT_CANCELED && requestCode == 2276) {
  599. IMAGE_PATH = "";
  600. }
  601. }
  602. private void showHelpScreen() {
  603. try {
  604. new FancyShowCaseView.Builder(this)
  605. .focusOn(editButton)
  606. .title("Tap here to edit profile details")
  607. .titleSize(22, 1)
  608. .closeOnTouch(true)
  609. .focusShape(FocusShape.CIRCLE)
  610. .backgroundColor(Color.parseColor("#99000000"))
  611. .build()
  612. .show();
  613. } catch (Exception e) {
  614. e.printStackTrace();
  615. }
  616. }
  617. public void updateProfile() {
  618. if (dob.getText().toString().trim().isEmpty() || dob.getText().toString().equalsIgnoreCase("00/00/0000")) {
  619. GlobalData.showToast(this, "Enter Date of Birth", Toast.LENGTH_SHORT).show();
  620. return;
  621. }
  622. if (email.getText().toString().trim().isEmpty()) {
  623. GlobalData.showToast(this, "Enter email", Toast.LENGTH_SHORT).show();
  624. return;
  625. }
  626. if (!isValidEmail(email.getText().toString())) {
  627. GlobalData.showToast(this, "Enter valid email address", Toast.LENGTH_SHORT).show();
  628. return;
  629. }
  630. if (mobile.getText().toString().trim().isEmpty()) {
  631. GlobalData.showToast(this, "Enter mobile number", Toast.LENGTH_SHORT).show();
  632. return;
  633. }
  634. if (checkBoxMarried.isChecked()) {
  635. if (weddingDate.getText().toString().trim().isEmpty() || weddingDate.getText().toString().equalsIgnoreCase("00/00/0000")) {
  636. GlobalData.showToast(this, "Enter wedding date", Toast.LENGTH_SHORT).show();
  637. return;
  638. }
  639. }
  640. if (new UtilService().isNetworkAvailable(MyProfileActivity.this)) {
  641. if (IMAGE_PATH.trim().isEmpty()) {
  642. try {
  643. HashMap<String, String> map = new HashMap<>();
  644. map.put("dob", finalDateFormat(dob.getText().toString().trim()));
  645. map.put("email", email.getText().toString().trim());
  646. map.put("is_married", checkBoxMarried.isChecked() ? "1" : "0");
  647. if (checkBoxMarried.isChecked()) {
  648. map.put("marriage_date", finalDateFormat(weddingDate.getText().toString().trim()));
  649. } else {
  650. map.put("marriage_date", "0000-00-00");
  651. }
  652. progressDialog = new TransparentProgressDialog(MyProfileActivity.this);
  653. progressDialog.show();
  654. restService.postProfile(map).enqueue(new Callback<UserResponse>() {
  655. @Override
  656. public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
  657. if (progressDialog != null && progressDialog.isShowing()) {
  658. progressDialog.dismiss();
  659. }
  660. if (response.body() != null && response.body().getStatus() == 200) {
  661. MY_PROFILE = response.body();
  662. GlobalData.showToast(MyProfileActivity.this, "Profile updated successfully", Toast.LENGTH_SHORT).show();
  663. setMyProfile();
  664. GlobalData.user.setData(response.body().getData());
  665. saveUserData();
  666. new Handler(HomeActivity.profileUpdateCallback).sendEmptyMessage(0);
  667. } else if (response.body() != null && response.body().getStatus() == 401) {
  668. GlobalData.canLogout = true;
  669. finish();
  670. } else if (response.body() != null) {
  671. GlobalData.showToast(getBaseContext(), response.body().getMessage(), Toast.LENGTH_SHORT).show();
  672. } else if (response.errorBody() != null) {
  673. Gson gson = new Gson();
  674. ErrorResponse message = gson.fromJson(response.errorBody().charStream(), ErrorResponse.class);
  675. GlobalData.showToast(getBaseContext(), message.getMessage(), Toast.LENGTH_SHORT).show();
  676. if (message.getStatus() == 401) {
  677. GlobalData.canLogout = true;
  678. finish();
  679. }
  680. }
  681. }
  682. @Override
  683. public void onFailure(Call<UserResponse> call, Throwable t) {
  684. if (progressDialog != null && progressDialog.isShowing()) {
  685. progressDialog.dismiss();
  686. }
  687. GlobalData.showToast(MyProfileActivity.this, "Connection Failure, try again!", Toast.LENGTH_SHORT).show();
  688. }
  689. });
  690. } catch (Exception e) {
  691. e.printStackTrace();
  692. }
  693. } else {
  694. File file = new File(IMAGE_PATH);
  695. RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file);
  696. MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), reqFile);
  697. RequestBody emailtxt = RequestBody.create(MediaType.parse("text/plain"), email.getText().toString().trim());
  698. RequestBody dobtxt = RequestBody.create(MediaType.parse("text/plain"), finalDateFormat(dob.getText().toString().trim()));
  699. RequestBody isMarried = RequestBody.create(MediaType.parse("text/plain"), checkBoxMarried.isChecked() ? "1" : "0");
  700. RequestBody isMarriedDate = RequestBody.create(MediaType.parse("text/plain"), "0000-00-00");
  701. if (checkBoxMarried.isChecked()) {
  702. isMarriedDate = RequestBody.create(MediaType.parse("text/plain"), finalDateFormat(weddingDate.getText().toString().trim()));
  703. }
  704. progressDialog = new TransparentProgressDialog(MyProfileActivity.this);
  705. progressDialog.show();
  706. restService.postProfileWithImage(body, emailtxt, dobtxt, isMarried, isMarriedDate).enqueue(new Callback<UserResponse>() {
  707. @Override
  708. public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
  709. if (progressDialog != null && progressDialog.isShowing()) {
  710. progressDialog.dismiss();
  711. }
  712. if (response.body() != null && response.body().getStatus() == 200) {
  713. MY_PROFILE = response.body();
  714. GlobalData.showToast(MyProfileActivity.this, "Profile updated successfully", Toast.LENGTH_SHORT).show();
  715. setMyProfile();
  716. GlobalData.user.setData(response.body().getData());
  717. saveUserData();
  718. new Handler(HomeActivity.profileUpdateCallback).sendEmptyMessage(0);
  719. } else if (response.body() != null && response.body().getStatus() == 401) {
  720. GlobalData.canLogout = true;
  721. finish();
  722. } else if (response.body() != null) {
  723. GlobalData.showToast(getBaseContext(), response.body().getMessage(), Toast.LENGTH_SHORT).show();
  724. } else if (response.errorBody() != null) {
  725. Gson gson = new Gson();
  726. ErrorResponse message = gson.fromJson(response.errorBody().charStream(), ErrorResponse.class);
  727. GlobalData.showToast(getBaseContext(), message.getMessage(), Toast.LENGTH_SHORT).show();
  728. if (message.getStatus() == 401) {
  729. GlobalData.canLogout = true;
  730. finish();
  731. }
  732. }
  733. }
  734. @Override
  735. public void onFailure(Call<UserResponse> call, Throwable t) {
  736. if (progressDialog != null && progressDialog.isShowing()) {
  737. progressDialog.dismiss();
  738. }
  739. t.printStackTrace();
  740. GlobalData.showToast(MyProfileActivity.this, "Connection Failure, try again!", Toast.LENGTH_SHORT).show();
  741. }
  742. });
  743. }
  744. } else {
  745. GlobalData.showToast(MyProfileActivity.this, "Please check your internet connection", Toast.LENGTH_SHORT).show();
  746. }
  747. }
  748. private void showInstructions() {
  749. View view = LayoutInflater.from(MyProfileActivity.this).inflate(
  750. R.layout.profile_success, null);
  751. TextView textHead = (TextView) view.findViewById(R.id.textHead);
  752. TextView text1 = (TextView) view.findViewById(R.id.text1);
  753. TextView text2 = (TextView) view.findViewById(R.id.text2);
  754. TextView text3 = (TextView) view.findViewById(R.id.text3);
  755. view.findViewById(R.id.btn_ok).setOnClickListener(
  756. new View.OnClickListener() {
  757. @Override
  758. public void onClick(View v) {
  759. alertDialog.dismiss();
  760. startActivity(new Intent(MyProfileActivity.this, HomeActivity.class));
  761. }
  762. });
  763. alertDialogBuilder = new AlertDialog.Builder(MyProfileActivity.this);
  764. alertDialogBuilder.setCancelable(false);
  765. alertDialogBuilder.setView(view);
  766. alertDialog = alertDialogBuilder.create();
  767. alertDialog.show();
  768. }
  769. private void saveUserData() {
  770. editButton.performClick();
  771. GlobalData.sdb.delete("session", null, null);
  772. Gson gson = new Gson();
  773. JsonElement jsonElement = gson.toJsonTree(
  774. GlobalData.user, new TypeToken<UserResponse>() {
  775. }.getType());
  776. ContentValues values = new ContentValues();
  777. values.put("user", jsonElement.toString());
  778. GlobalData.sdb.insert("session", null, values);
  779. }
  780. public void setupUI(View view) {
  781. try {
  782. if (!(view instanceof EditText)) {
  783. view.setOnTouchListener(new View.OnTouchListener() {
  784. @SuppressLint("ClickableViewAccessibility")
  785. public boolean onTouch(View v, MotionEvent event) {
  786. hideSoftKeyboard(MyProfileActivity.this);
  787. return false;
  788. }
  789. });
  790. }
  791. if (view instanceof ViewGroup) {
  792. for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
  793. View innerView = ((ViewGroup) view).getChildAt(i);
  794. setupUI(innerView);
  795. }
  796. }
  797. } catch (Exception e) {
  798. e.printStackTrace();
  799. }
  800. }
  801. }