Commit 430da406 authored by Chamod Ishankha's avatar Chamod Ishankha

complete user profile without image upload

parent 227c13db
package com.kaluwa.enterprises.babycare.activities; package com.kaluwa.enterprises.babycare.activities;
import static com.kaluwa.enterprises.babycare.utils.Utils.DATE_FORMAT;
import static com.kaluwa.enterprises.babycare.config.TokenSaver.clearToken; import static com.kaluwa.enterprises.babycare.config.TokenSaver.clearToken;
import static com.kaluwa.enterprises.babycare.config.TokenSaver.getToken; import static com.kaluwa.enterprises.babycare.config.TokenSaver.getToken;
import static com.kaluwa.enterprises.babycare.utils.Utils.animationChanger; import static com.kaluwa.enterprises.babycare.utils.Utils.animationChanger;
import static com.kaluwa.enterprises.babycare.utils.Utils.disableEditText; import static com.kaluwa.enterprises.babycare.utils.Utils.disableEditText;
import static com.kaluwa.enterprises.babycare.utils.Utils.loader; import static com.kaluwa.enterprises.babycare.utils.Utils.loader;
import static com.kaluwa.enterprises.babycare.utils.Utils.mobileNumberValidation; import static com.kaluwa.enterprises.babycare.utils.Utils.mobileNumberValidation;
import static com.kaluwa.enterprises.babycare.utils.Utils.setUpDOBPicker;
import android.annotation.SuppressLint;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.text.TextUtils; import android.text.TextUtils;
...@@ -35,7 +38,11 @@ import com.kaluwa.enterprises.babycare.dto.AuthenticationDto; ...@@ -35,7 +38,11 @@ import com.kaluwa.enterprises.babycare.dto.AuthenticationDto;
import com.kaluwa.enterprises.babycare.dto.UserDto; import com.kaluwa.enterprises.babycare.dto.UserDto;
import com.kaluwa.enterprises.babycare.error.ErrorDto; import com.kaluwa.enterprises.babycare.error.ErrorDto;
import com.kaluwa.enterprises.babycare.service.UserApiService; import com.kaluwa.enterprises.babycare.service.UserApiService;
import com.kaluwa.enterprises.babycare.utils.Utils;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
...@@ -46,7 +53,7 @@ import retrofit2.Response; ...@@ -46,7 +53,7 @@ import retrofit2.Response;
public class UserProfileActivity extends AppCompatActivity { public class UserProfileActivity extends AppCompatActivity {
private final static String TAG = "UserProfileActivity"; private final static String TAG = "UserProfileActivity";
private EditText etFirstName, etLastName, etType, etEmail, etMobile; private EditText etFirstName, etLastName, etType, etEmail, etMobile, etDob;
private Button btnEdit, btnCancel; private Button btnEdit, btnCancel;
private SpinKitView progressBar; private SpinKitView progressBar;
private View overlay; private View overlay;
...@@ -78,11 +85,13 @@ public class UserProfileActivity extends AppCompatActivity { ...@@ -78,11 +85,13 @@ public class UserProfileActivity extends AppCompatActivity {
etType = findViewById(R.id.up_et_type); etType = findViewById(R.id.up_et_type);
etEmail = findViewById(R.id.up_et_email); etEmail = findViewById(R.id.up_et_email);
etMobile = findViewById(R.id.up_et_mobile); etMobile = findViewById(R.id.up_et_mobile);
etDob = findViewById(R.id.up_et_dob);
// buttons // buttons
btnEdit = findViewById(R.id.l_btn_edit); btnEdit = findViewById(R.id.l_btn_edit);
btnCancel = findViewById(R.id.l_btn_cancel); btnCancel = findViewById(R.id.l_btn_cancel);
// disable all edit texts // disable all edit texts
// call custom date-picker
disableET(); disableET();
AtomicBoolean enableEdit = new AtomicBoolean(false); AtomicBoolean enableEdit = new AtomicBoolean(false);
btnCancel.setOnClickListener(v -> { btnCancel.setOnClickListener(v -> {
...@@ -91,6 +100,7 @@ public class UserProfileActivity extends AppCompatActivity { ...@@ -91,6 +100,7 @@ public class UserProfileActivity extends AppCompatActivity {
btnCancel.setVisibility(View.GONE); btnCancel.setVisibility(View.GONE);
btnEdit.setText(R.string.edit_btn_value); btnEdit.setText(R.string.edit_btn_value);
btnEdit.setBackgroundResource(R.drawable.positive_btn_background); btnEdit.setBackgroundResource(R.drawable.positive_btn_background);
etDob.setOnClickListener(null);
}); });
btnEdit.setOnClickListener(v -> { btnEdit.setOnClickListener(v -> {
if (!enableEdit.get()) { if (!enableEdit.get()) {
...@@ -99,6 +109,7 @@ public class UserProfileActivity extends AppCompatActivity { ...@@ -99,6 +109,7 @@ public class UserProfileActivity extends AppCompatActivity {
btnCancel.setVisibility(View.VISIBLE); btnCancel.setVisibility(View.VISIBLE);
btnEdit.setText(R.string.update_btn_value); btnEdit.setText(R.string.update_btn_value);
btnEdit.setBackgroundResource(R.drawable.green_btn_background); btnEdit.setBackgroundResource(R.drawable.green_btn_background);
setUpDOBPicker(etDob, this);
} else { } else {
// validate text // validate text
UserDto user = validate(); UserDto user = validate();
...@@ -122,6 +133,7 @@ public class UserProfileActivity extends AppCompatActivity { ...@@ -122,6 +133,7 @@ public class UserProfileActivity extends AppCompatActivity {
btnCancel.setVisibility(View.GONE); btnCancel.setVisibility(View.GONE);
btnEdit.setText(R.string.edit_btn_value); btnEdit.setText(R.string.edit_btn_value);
btnEdit.setBackgroundResource(R.drawable.positive_btn_background); btnEdit.setBackgroundResource(R.drawable.positive_btn_background);
etDob.setOnClickListener(null);
Toast.makeText(UserProfileActivity.this, "User details successfully updated.", Toast.LENGTH_SHORT).show(); Toast.makeText(UserProfileActivity.this, "User details successfully updated.", Toast.LENGTH_SHORT).show();
loader(overlay, progressBar, false); loader(overlay, progressBar, false);
} else { } else {
...@@ -140,7 +152,7 @@ public class UserProfileActivity extends AppCompatActivity { ...@@ -140,7 +152,7 @@ public class UserProfileActivity extends AppCompatActivity {
Toast.makeText(UserProfileActivity.this, "An unexpected error occurred", Toast.LENGTH_LONG).show(); Toast.makeText(UserProfileActivity.this, "An unexpected error occurred", Toast.LENGTH_LONG).show();
} }
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, e.getMessage()); Log.e(TAG, "else-error: "+e.getMessage());
Toast.makeText(UserProfileActivity.this, e.getMessage(), Toast.LENGTH_LONG).show(); Toast.makeText(UserProfileActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
} }
loader(overlay, progressBar, false); loader(overlay, progressBar, false);
...@@ -155,6 +167,7 @@ public class UserProfileActivity extends AppCompatActivity { ...@@ -155,6 +167,7 @@ public class UserProfileActivity extends AppCompatActivity {
}); });
} }
@SuppressLint("NewApi")
private UserDto validate() { private UserDto validate() {
UserDto userDto = new UserDto(); UserDto userDto = new UserDto();
String firstName, lastName, mobileNumber; String firstName, lastName, mobileNumber;
...@@ -180,6 +193,10 @@ public class UserProfileActivity extends AppCompatActivity { ...@@ -180,6 +193,10 @@ public class UserProfileActivity extends AppCompatActivity {
userDto.setFirstName(firstName); userDto.setFirstName(firstName);
userDto.setLastName(lastName); userDto.setLastName(lastName);
userDto.setPhone(mobileNumber); userDto.setPhone(mobileNumber);
if (!TextUtils.isEmpty(etDob.getText().toString())) {
LocalDate dob = LocalDate.parse(etDob.getText().toString(), DateTimeFormatter.ofPattern(DATE_FORMAT));
userDto.setDob(dob.toString());
}
} }
return userDto; return userDto;
...@@ -189,6 +206,7 @@ public class UserProfileActivity extends AppCompatActivity { ...@@ -189,6 +206,7 @@ public class UserProfileActivity extends AppCompatActivity {
loader(overlay, progressBar, true); loader(overlay, progressBar, true);
Call<UserDto> call = userApiService.getUserById(authDto.getUserId()); Call<UserDto> call = userApiService.getUserById(authDto.getUserId());
call.enqueue(new Callback<UserDto>() { call.enqueue(new Callback<UserDto>() {
@SuppressLint("NewApi")
@Override @Override
public void onResponse(Call<UserDto> call, Response<UserDto> response) { public void onResponse(Call<UserDto> call, Response<UserDto> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
...@@ -198,6 +216,12 @@ public class UserProfileActivity extends AppCompatActivity { ...@@ -198,6 +216,12 @@ public class UserProfileActivity extends AppCompatActivity {
etType.setText(!Objects.equals(user.getRole(), "USER") ? user.getRole():"PARENT"); etType.setText(!Objects.equals(user.getRole(), "USER") ? user.getRole():"PARENT");
etEmail.setText(user.getEmail()); etEmail.setText(user.getEmail());
etMobile.setText(user.getPhone()); etMobile.setText(user.getPhone());
if (user.getDob() != null) {
LocalDate localDate = Utils.getLocalDate(user.getDob());
if (localDate != null) {
etDob.setText(localDate.format(DateTimeFormatter.ofPattern(DATE_FORMAT)));
}
}
loader(overlay, progressBar, false); loader(overlay, progressBar, false);
} else { } else {
try { try {
...@@ -224,7 +248,8 @@ public class UserProfileActivity extends AppCompatActivity { ...@@ -224,7 +248,8 @@ public class UserProfileActivity extends AppCompatActivity {
@Override @Override
public void onFailure(Call<UserDto> call, Throwable t) { public void onFailure(Call<UserDto> call, Throwable t) {
Toast.makeText(UserProfileActivity.this, "Error to Failure", Toast.LENGTH_LONG).show(); Log.e(TAG, t.getMessage());
Toast.makeText(UserProfileActivity.this, "Load data Error to Failure", Toast.LENGTH_LONG).show();
loader(overlay, progressBar, false); loader(overlay, progressBar, false);
} }
}); });
...@@ -236,6 +261,7 @@ public class UserProfileActivity extends AppCompatActivity { ...@@ -236,6 +261,7 @@ public class UserProfileActivity extends AppCompatActivity {
disableEditText(etType, false, this); disableEditText(etType, false, this);
disableEditText(etEmail, false, this); disableEditText(etEmail, false, this);
disableEditText(etMobile, false, this); disableEditText(etMobile, false, this);
disableEditText(etDob, false, this);
} }
private void enableET() { private void enableET() {
......
package com.kaluwa.enterprises.babycare.dto; package com.kaluwa.enterprises.babycare.dto;
import java.time.LocalDate;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
...@@ -15,4 +17,5 @@ public class UserDto { ...@@ -15,4 +17,5 @@ public class UserDto {
private String phone; private String phone;
private String role; private String role;
private String status; private String status;
private Object dob;
} }
\ No newline at end of file
package com.kaluwa.enterprises.babycare.utils; package com.kaluwa.enterprises.babycare.utils;
import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Context; import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.EditText; import android.widget.EditText;
import com.github.ybq.android.spinkit.SpinKitView; import com.github.ybq.android.spinkit.SpinKitView;
import com.kaluwa.enterprises.babycare.R; import com.kaluwa.enterprises.babycare.R;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class Utils { public class Utils {
private static final String TAG = "Utils";
public static final String DATE_FORMAT = "dd/M/yyyy";
public static void loader(View overlay, SpinKitView spinKitView, boolean status) { public static void loader(View overlay, SpinKitView spinKitView, boolean status) {
if (status) { if (status) {
overlay.setVisibility(View.VISIBLE); overlay.setVisibility(View.VISIBLE);
...@@ -52,4 +66,94 @@ public class Utils { ...@@ -52,4 +66,94 @@ public class Utils {
} }
} }
public static void setUpDOBPicker(EditText editText, Context context) {
editText.setOnClickListener(v -> {
final Calendar calendar = Calendar.getInstance();
// Check if EditText is not empty and parse the date
if (!TextUtils.isEmpty(editText.getText().toString())) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT, Locale.getDefault());
calendar.setTime(Objects.requireNonNull(sdf.parse(editText.getText().toString())));
} catch (ParseException e) {
e.printStackTrace();
}
}
int day = calendar.get(Calendar.DAY_OF_MONTH);
int month = calendar.get(Calendar.MONTH);
int year = calendar.get(Calendar.YEAR);
// Calculate the date 18 years ago from today
Calendar maxDate = Calendar.getInstance();
maxDate.add(Calendar.YEAR, -18);
// Date Picker Dialog
DatePickerDialog datePicker = new DatePickerDialog(context, (view, selectedYear, selectedMonth, selectedDayOfMonth) -> {
editText.setText(selectedDayOfMonth + "/" + (selectedMonth + 1) + "/" + selectedYear);
}, year, month, day);
// Set the maximum date (18 years ago)
datePicker.getDatePicker().setMaxDate(maxDate.getTimeInMillis());
// Show the date picker
datePicker.show();
});
}
public static void setUpDatePicker(EditText editText, Context context) {
editText.setOnClickListener(v -> {
final Calendar calendar = Calendar.getInstance();
// Check if EditText is not empty and parse the date
if (!TextUtils.isEmpty(editText.getText().toString())) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT, Locale.getDefault());
calendar.setTime(Objects.requireNonNull(sdf.parse(editText.getText().toString())));
} catch (ParseException e) {
e.printStackTrace();
}
}
int day = calendar.get(Calendar.DAY_OF_MONTH);
int month = calendar.get(Calendar.MONTH);
int year = calendar.get(Calendar.YEAR);
// Date Picker Dialog
DatePickerDialog datePicker = new DatePickerDialog(context, (view, selectedYear, selectedMonth, selectedDayOfMonth) -> {
editText.setText(selectedDayOfMonth + "/" + (selectedMonth + 1) + "/" + selectedYear);
}, year, month, day);
// Show the date picker
datePicker.show();
});
}
@SuppressLint("NewApi")
public static LocalDate getLocalDate(Object dob) {
LocalDate localDate = null;
if (dob instanceof LocalDate) {
localDate = (LocalDate) dob;
} else if (dob instanceof List) {
// dob is a List, convert it to LocalDate
List dobList = (List) dob;
if (dobList.size() == 3) {
try {
int year = ((Double) dobList.get(0)).intValue();
int month = ((Double) dobList.get(1)).intValue();
int day = ((Double) dobList.get(2)).intValue();
localDate = LocalDate.of(year, month, day);
} catch (Exception e) {
e.printStackTrace();
}
} else {
Log.e(TAG, "Unsupported date format");
}
} else {
// Handle other unexpected types if necessary
Log.e(TAG, "Unsupported date format");
}
return localDate;
}
} }
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android" <alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500" android:duration="300"
android:fromAlpha="0.0" android:fromAlpha="0.0"
android:toAlpha="1.0"/> android:toAlpha="1.0"/>
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android" <alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500" android:duration="300"
android:fromAlpha="1.0" android:fromAlpha="1.0"
android:toAlpha="0.0"/> android:toAlpha="0.0"/>
...@@ -140,11 +140,13 @@ ...@@ -140,11 +140,13 @@
android:id="@+id/up_et_dob" android:id="@+id/up_et_dob"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="45dp" android:layout_height="45dp"
android:inputType="text" android:inputType="date"
android:background="@drawable/et_background" android:background="@drawable/et_background"
android:padding="8dp" android:padding="8dp"
android:fontFamily="@font/acme_regular" android:fontFamily="@font/acme_regular"
android:textColor="@color/purple"/> android:textColor="@color/purple"
android:hint="Select your Date of birth (dd/mm/yyyy)*"
android:focusable="false"/>
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment