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.cygnusa.hyperm.fancyshowcase.FancyShowCaseView;
  34. import com.cygnusa.hyperm.fancyshowcase.FocusShape;
  35. import com.cygnusa.hyperm.pickimage.bean.PickResult;
  36. import com.cygnusa.hyperm.pickimage.bundle.PickSetup;
  37. import com.cygnusa.hyperm.pickimage.dialog.PickImageDialog;
  38. import com.cygnusa.hyperm.pickimage.listeners.IPickCancel;
  39. import com.cygnusa.hyperm.pickimage.listeners.IPickResult;
  40. import com.cygnusa.hyperm.util.ApiService;
  41. import com.cygnusa.hyperm.util.GlobalData;
  42. import com.cygnusa.hyperm.util.MainApplication;
  43. import com.cygnusa.hyperm.util.TransparentProgressDialog;
  44. import com.cygnusa.hyperm.util.UtilService;
  45. import com.cygnusa.hyperm.widget.SubtitleCollapsingToolbarLayout;
  46. import com.cygnusa.hyperm.wrappers.ErrorResponse;
  47. import com.cygnusa.hyperm.wrappers.UserResponse;
  48. import com.google.android.material.appbar.AppBarLayout;
  49. import com.google.android.material.floatingactionbutton.FloatingActionButton;
  50. import com.google.android.material.textfield.TextInputLayout;
  51. import com.google.gson.Gson;
  52. import com.google.gson.JsonElement;
  53. import com.google.gson.reflect.TypeToken;
  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. public TextInputLayout editInputDob, editInputEmail, editInputWedding, getEditInputMobile;
  72. ApiService restService;
  73. TransparentProgressDialog progressDialog;
  74. AppBarLayout appBarLayout;
  75. Toolbar toolbar;
  76. ImageView userImage;
  77. FloatingActionButton editButton;
  78. FloatingActionButton cameraButton;
  79. LinearLayout bottomLayout;
  80. EditText firstName, lastName, dob, email, mobile, doj, dealer, zone, state, city, region, designation, weddingDate;
  81. Button submitButton;
  82. UserResponse MY_PROFILE = new UserResponse();
  83. SubtitleCollapsingToolbarLayout collapsingToolbar;
  84. String IMAGE_PATH = "";
  85. ImageButton toolEditIcon, helpIcon;
  86. CheckBox checkBoxMarried;
  87. AlertDialog.Builder alertDialogBuilder;
  88. AlertDialog alertDialog;
  89. String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
  90. boolean isDOB = false;
  91. private ImageView dobCalendar, weddingCalendar;
  92. private int year;
  93. private int month;
  94. private int day;
  95. public DatePickerDialog.OnDateSetListener tolistener = new DatePickerDialog.OnDateSetListener() {
  96. @Override
  97. public void onDateSet(DatePicker view, int y, int monthOfYear, int dayOfMonth) {
  98. year = y;
  99. month = monthOfYear;
  100. day = dayOfMonth;
  101. String datevalue = String.valueOf(day);
  102. int mval = (month + 1);
  103. String monthvalue = String.valueOf(mval);
  104. String date = "0";
  105. String monthname = "0";
  106. if (datevalue.length() == 1) {
  107. date = "0" + datevalue;
  108. } else {
  109. date = datevalue;
  110. }
  111. if (monthvalue.length() == 1) {
  112. monthname = "0" + monthvalue;
  113. } else {
  114. monthname = monthvalue;
  115. }
  116. //dateValue = String.valueOf(year) + "-" + (month + 1) + "-" + day + " ";
  117. String dateValue = String.valueOf(year) + "-" + monthname + "-" + date;
  118. dob.setText(dateFormate(dateValue));
  119. if (isDOB) {
  120. dob.setText(dateFormate(dateValue));
  121. } else {
  122. weddingDate.setText(dateFormate(dateValue));
  123. }
  124. }
  125. };
  126. private LinearLayout linearWeddingDate;
  127. private boolean isEditable = false;
  128. public static void hideSoftKeyboard(Activity activity) {
  129. try {
  130. InputMethodManager inputMethodManager =
  131. (InputMethodManager) activity.getSystemService(
  132. Activity.INPUT_METHOD_SERVICE);
  133. inputMethodManager.hideSoftInputFromWindow(
  134. activity.getCurrentFocus().getWindowToken(), 0);
  135. } catch (Exception e) {
  136. //e.printStackTrace();
  137. }
  138. }
  139. private static boolean isValidEmail(String email) {
  140. return !TextUtils.isEmpty(email) && android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
  141. }
  142. private static String displayMonth(String date) throws ParseException {
  143. Date d = new SimpleDateFormat("dd-MM-yyyy").parse(date);
  144. Calendar cal = Calendar.getInstance();
  145. cal.setTime(d);
  146. String monthName = new SimpleDateFormat("dd-MMM-yyyy").format(cal.getTime());
  147. return monthName;
  148. }
  149. @SuppressLint("WrongViewCast")
  150. @Override
  151. protected void onCreate(Bundle savedInstanceState) {
  152. super.onCreate(savedInstanceState);
  153. setContentView(R.layout.activity_my_profile);
  154. setupWindowAnimations();
  155. setupUI(findViewById(R.id.main_content));
  156. calendar = Calendar.getInstance();
  157. year = calendar.get(Calendar.YEAR);
  158. month = calendar.get(Calendar.MONTH);
  159. day = calendar.get(Calendar.DAY_OF_MONTH);
  160. restService = ((MainApplication) getApplication()).getClient();
  161. appBarLayout = findViewById(R.id.appBarLayout);
  162. userImage = findViewById(R.id.userImage);
  163. toolbar = findViewById(R.id.toolbar);
  164. bottomLayout = findViewById(R.id.bottomLayout);
  165. toolEditIcon = findViewById(R.id.toolEditIcon);
  166. helpIcon = findViewById(R.id.helpIcon);
  167. helpIcon.setVisibility(View.VISIBLE);
  168. email = findViewById(R.id.email);
  169. editInputEmail = findViewById(R.id.txtemail);
  170. editInputDob = findViewById(R.id.txtInputDob);
  171. editInputWedding = findViewById(R.id.txtInputWedding);
  172. editInputWedding = findViewById(R.id.edit_mobile);
  173. mobile = findViewById(R.id.mobile);
  174. doj = findViewById(R.id.doj);
  175. dob = findViewById(R.id.dob);
  176. dealer = findViewById(R.id.dealer);
  177. zone = findViewById(R.id.zone);
  178. region = findViewById(R.id.region);
  179. state = findViewById(R.id.state);
  180. city = findViewById(R.id.city);
  181. designation = findViewById(R.id.designation);
  182. checkBoxMarried = findViewById(R.id.checkMarried);
  183. weddingDate = findViewById(R.id.weddingDate);
  184. dobCalendar = findViewById(R.id.dob_date);
  185. weddingCalendar = findViewById(R.id.weddding_date);
  186. linearWeddingDate = findViewById(R.id.linear_wedding_date);
  187. submitButton = findViewById(R.id.submitButton);
  188. email.setEnabled(false);
  189. weddingDate.setEnabled(false);
  190. dob.setEnabled(false);
  191. doj.setEnabled(false);
  192. dealer.setEnabled(false);
  193. designation.setEnabled(false);
  194. mobile.setEnabled(false);
  195. region.setEnabled(false);
  196. state.setEnabled(false);
  197. zone.setEnabled(false);
  198. city.setEnabled(false);
  199. weddingCalendar.setVisibility(View.VISIBLE);
  200. checkBoxMarried.setChecked(false);
  201. setSupportActionBar(toolbar);
  202. toolbar.setTitle("");
  203. getSupportActionBar().setTitle("");
  204. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  205. getSupportActionBar().setDisplayShowHomeEnabled(true);
  206. collapsingToolbar = findViewById(R.id.collapsing_toolbar);
  207. appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
  208. @Override
  209. public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
  210. if (Math.abs(verticalOffset) - appBarLayout.getTotalScrollRange() == 0) {
  211. toolbar.setBackgroundColor(getResources().getColor(R.color.bg_color));
  212. toolEditIcon.setVisibility(View.VISIBLE);
  213. helpIcon.setVisibility(View.GONE);
  214. } else {
  215. toolbar.setBackgroundColor(Color.TRANSPARENT);
  216. toolEditIcon.setVisibility(View.GONE);
  217. helpIcon.setVisibility(View.VISIBLE);
  218. }
  219. }
  220. });
  221. toolEditIcon.setOnClickListener(new View.OnClickListener() {
  222. @Override
  223. public void onClick(View v) {
  224. editButton.performClick();
  225. }
  226. });
  227. dobCalendar.setOnClickListener(new View.OnClickListener() {
  228. @Override
  229. public void onClick(View v) {
  230. isDOB = true;
  231. Calendar c = Calendar.getInstance();
  232. c.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - 18);
  233. Date date = new Date();
  234. if (dob.getText().toString().trim().isEmpty()
  235. || dob.getText().toString().trim().equalsIgnoreCase("0000-00-00")
  236. || dob.getText().toString().trim().equalsIgnoreCase("00-00-0000")
  237. || dob.getText().toString().trim().equalsIgnoreCase("00/00/0000")
  238. || dob.getText().toString().trim().equalsIgnoreCase("0000/00/00")) {
  239. date = c.getTime();
  240. } else {
  241. if (dob.getText().toString().trim().contains("-")) {
  242. try {
  243. date = new SimpleDateFormat("yyyy-MM-dd").parse(dob.getText().toString().trim());
  244. } catch (ParseException e) {
  245. e.printStackTrace();
  246. }
  247. } else {
  248. try {
  249. date = new SimpleDateFormat("dd-MMM-yyyy").parse(dob.getText().toString().trim());
  250. } catch (ParseException e) {
  251. e.printStackTrace();
  252. }
  253. }
  254. }
  255. c.setTime(date);
  256. DatePickerDialog dialog = new DatePickerDialog(MyProfileActivity.this, tolistener,
  257. c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
  258. try {
  259. c = Calendar.getInstance();
  260. c.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - 18);
  261. DatePicker datePicker = dialog.getDatePicker();
  262. datePicker.setMaxDate(c.getTime().getTime());
  263. } catch (Exception e) {
  264. e.printStackTrace();
  265. }
  266. dialog.show();
  267. }
  268. });
  269. weddingCalendar.setOnClickListener(new View.OnClickListener() {
  270. @Override
  271. public void onClick(View v) {
  272. isDOB = false;
  273. Calendar c = Calendar.getInstance();
  274. Date date = new Date();
  275. if (weddingDate.getText().toString().trim().isEmpty()
  276. || weddingDate.getText().toString().trim().equalsIgnoreCase("0000-00-00")
  277. || weddingDate.getText().toString().trim().equalsIgnoreCase("00-00-0000")
  278. || weddingDate.getText().toString().trim().equalsIgnoreCase("00/00/0000")
  279. || weddingDate.getText().toString().trim().equalsIgnoreCase("0000/00/00")) {
  280. date = c.getTime();
  281. } else {
  282. if (weddingDate.getText().toString().trim().contains("-")) {
  283. try {
  284. date = new SimpleDateFormat("yyyy-MM-dd").parse(weddingDate.getText().toString().trim());
  285. } catch (ParseException e) {
  286. e.printStackTrace();
  287. }
  288. } else {
  289. try {
  290. date = new SimpleDateFormat("dd-MMM-yyyy").parse(weddingDate.getText().toString().trim());
  291. } catch (ParseException e) {
  292. e.printStackTrace();
  293. }
  294. }
  295. }
  296. c.setTime(date);
  297. DatePickerDialog dialog = new DatePickerDialog(MyProfileActivity.this, tolistener,
  298. c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
  299. try {
  300. DatePicker datePicker = dialog.getDatePicker();
  301. datePicker.setMaxDate(c.getTime().getTime());
  302. } catch (Exception e) {
  303. e.printStackTrace();
  304. }
  305. dialog.show();
  306. }
  307. });
  308. editButton = findViewById(R.id.editButton);
  309. cameraButton = findViewById(R.id.cameraButton);
  310. cameraButton.hide();
  311. bottomLayout.setVisibility(View.INVISIBLE);
  312. linearWeddingDate.setVisibility(View.GONE);
  313. dobCalendar.setVisibility(View.INVISIBLE);
  314. checkBoxMarried.setEnabled(false);
  315. weddingCalendar.setVisibility(View.INVISIBLE);
  316. getMyProfile();
  317. checkBoxMarried.setOnClickListener(new View.OnClickListener() {
  318. @Override
  319. public void onClick(View v) {
  320. boolean checked = ((CheckBox) v).isChecked();
  321. if (checked) {
  322. linearWeddingDate.setVisibility(View.GONE);
  323. if (!isEditable) {
  324. weddingCalendar.setVisibility(View.INVISIBLE);
  325. } else {
  326. weddingCalendar.setVisibility(View.VISIBLE);
  327. }
  328. } else {
  329. linearWeddingDate.setVisibility(View.GONE);
  330. if (!isEditable) {
  331. weddingCalendar.setVisibility(View.INVISIBLE);
  332. } else {
  333. weddingCalendar.setVisibility(View.VISIBLE);
  334. }
  335. }
  336. }
  337. });
  338. editButton.setOnClickListener(new View.OnClickListener() {
  339. @Override
  340. public void onClick(View v) {
  341. if (cameraButton.getVisibility() == View.GONE) {
  342. IMAGE_PATH = "";
  343. setUpperHintColor(getResources().getColor(R.color.bg_color_dark), editInputDob);
  344. setUpperHintColor(getResources().getColor(R.color.bg_color_dark), editInputEmail);
  345. setUpperHintColor(getResources().getColor(R.color.bg_color_dark), editInputWedding);
  346. mobile.setTextColor(getResources().getColor(R.color.material_grey300));
  347. designation.setTextColor(getResources().getColor(R.color.material_grey300));
  348. dealer.setTextColor(getResources().getColor(R.color.material_grey300));
  349. doj.setTextColor(getResources().getColor(R.color.material_grey300));
  350. zone.setTextColor(getResources().getColor(R.color.material_grey300));
  351. region.setTextColor(getResources().getColor(R.color.material_grey300));
  352. state.setTextColor(getResources().getColor(R.color.material_grey300));
  353. city.setTextColor(getResources().getColor(R.color.material_grey300));
  354. weddingDate.setHintTextColor(getResources().getColor(R.color.material_grey700));
  355. cameraButton.show();
  356. cameraButton.startAnimation(AnimationUtils.loadAnimation(MyProfileActivity.this, R.anim.zoomin));
  357. slideToTop(bottomLayout);
  358. email.setEnabled(true);
  359. checkBoxMarried.setEnabled(true);
  360. dobCalendar.setVisibility(View.VISIBLE);
  361. isEditable = true;
  362. } else {
  363. setUpperHintColor(getResources().getColor(R.color.btn_bg_color), editInputDob);
  364. setUpperHintColor(getResources().getColor(R.color.btn_bg_color), editInputEmail);
  365. setUpperHintColor(getResources().getColor(R.color.btn_bg_color), editInputWedding);
  366. mobile.setTextColor(getResources().getColor(R.color.btn_bg_color));
  367. designation.setTextColor(getResources().getColor(R.color.btn_bg_color));
  368. doj.setTextColor(getResources().getColor(R.color.btn_bg_color));
  369. zone.setTextColor(getResources().getColor(R.color.btn_bg_color));
  370. region.setTextColor(getResources().getColor(R.color.btn_bg_color));
  371. state.setTextColor(getResources().getColor(R.color.btn_bg_color));
  372. city.setTextColor(getResources().getColor(R.color.btn_bg_color));
  373. dealer.setTextColor(getResources().getColor(R.color.btn_bg_color));
  374. cameraButton.startAnimation(AnimationUtils.loadAnimation(MyProfileActivity.this, R.anim.zoomout));
  375. cameraButton.hide();
  376. slideToBottom(bottomLayout);
  377. email.setEnabled(false);
  378. checkBoxMarried.setEnabled(false);
  379. dobCalendar.setVisibility(View.INVISIBLE);
  380. weddingCalendar.setVisibility(View.INVISIBLE);
  381. IMAGE_PATH = "";
  382. isEditable = false;
  383. setMyProfile();
  384. }
  385. }
  386. });
  387. cameraButton.setOnClickListener(new View.OnClickListener() {
  388. @Override
  389. public void onClick(View v) {
  390. PickImageDialog.build(new PickSetup().setButtonOrientation(LinearLayout.HORIZONTAL)).setOnPickCancel(new IPickCancel() {
  391. @Override
  392. public void onCancelClick() {
  393. }
  394. }).show(MyProfileActivity.this);
  395. }
  396. });
  397. submitButton.setOnClickListener(new View.OnClickListener() {
  398. @Override
  399. public void onClick(View v) {
  400. updateProfile();
  401. }
  402. });
  403. helpIcon.setOnClickListener(new View.OnClickListener() {
  404. @Override
  405. public void onClick(View v) {
  406. showHelpScreen();
  407. }
  408. });
  409. }
  410. private void setUpperHintColor(int color, TextInputLayout txtInputLayout) {
  411. try {
  412. Field field = txtInputLayout.getClass().getDeclaredField("mFocusedTextColor");
  413. field.setAccessible(true);
  414. int[][] states = new int[][]{
  415. new int[]{}
  416. };
  417. int[] colors = new int[]{
  418. color
  419. };
  420. ColorStateList myList = new ColorStateList(states, colors);
  421. field.set(txtInputLayout, myList);
  422. Method method = txtInputLayout.getClass().getDeclaredMethod("updateLabelState", boolean.class);
  423. method.setAccessible(true);
  424. method.invoke(txtInputLayout, true);
  425. } catch (Exception e) {
  426. e.printStackTrace();
  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.GONE);
  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.placeholder))
  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. }