Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package codelabs.siloam.activity;
- import android.app.DatePickerDialog;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.support.annotation.NonNull;
- import android.support.v7.app.AlertDialog;
- import android.support.v7.widget.Toolbar;
- import android.text.TextUtils;
- import android.util.Base64;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- import android.widget.DatePicker;
- import android.widget.EditText;
- import android.widget.TextView;
- import com.google.gson.JsonSyntaxException;
- import com.makeramen.roundedimageview.RoundedImageView;
- import com.squareup.picasso.Picasso;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.IOException;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.HashMap;
- import java.util.Locale;
- import java.util.Map;
- import butterknife.BindView;
- import butterknife.ButterKnife;
- import butterknife.OnClick;
- import codelabs.siloam.R;
- import codelabs.siloam.connection.ApiUtils;
- import codelabs.siloam.connection.AppConstant;
- import codelabs.siloam.connection.DataManager;
- import codelabs.siloam.connection.RetrofitInterface;
- import codelabs.siloam.imagepicker.FilePickUtils;
- import codelabs.siloam.imagepicker.LifeCycleCallBackManager;
- import codelabs.siloam.model.GetUpdateProfile;
- import codelabs.siloam.utils.RecentUtils;
- import retrofit2.Call;
- import retrofit2.Response;
- public class EditProfileActivity extends BaseActivity implements View.OnClickListener {
- private static final int CAMERA_PERMISSION = 11;
- private static final int STORAGE_PERMISSION_IMAGE = 22;
- @BindView(R.id.img_profile)
- RoundedImageView imgProfile;
- @BindView(R.id.tv_name)
- TextView tvName;
- @BindView(R.id.tv_nik)
- TextView tvNik;
- @BindView(R.id.ed_name)
- EditText edName;
- @BindView(R.id.select_hospital)
- TextView selectHospital;
- @BindView(R.id.ed_phone)
- EditText edPhone;
- @BindView(R.id.select_date)
- TextView selectDate;
- @BindView(R.id.tv_department)
- TextView tvDepartment;
- @BindView(R.id.btn_save)
- Button btnSave;
- @BindView(R.id.ed_email)
- EditText edEmail;
- private LifeCycleCallBackManager lifeCycleCallBackManager;
- private FilePickUtils filePickUtils;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_edit_profile);
- ButterKnife.bind(this);
- // setToolbar();
- // selectDate.setOnClickListener(this);
- btnSave.setOnClickListener(this);
- imgProfile.setOnClickListener(this);
- Picasso.get().load(DataManager.getInstance().getImage())
- .placeholder(R.drawable.placeholder)
- .placeholder(R.drawable.placeholder)
- .into(imgProfile);
- tvName.setText(DataManager.getInstance().getProfile().getFullname());
- tvNik.setText(DataManager.getInstance().getNik());
- edEmail.setText(DataManager.getInstance().getEmail());
- edName.setText(DataManager.getInstance().getProfile().getFullname());
- selectHospital.setText(DataManager.getInstance().getHospitalName());
- edPhone.setText(DataManager.getInstance().getProfile().getPhone());
- selectDate.setText(RecentUtils.formatDateToDateDMY(DataManager.getInstance().getDob()));
- tvDepartment.setText(DataManager.getInstance().getDepartmentName());
- filePickUtils = new FilePickUtils(this, onFileChoose);
- lifeCycleCallBackManager = filePickUtils.getCallBackManager();
- }
- private String selectedImage;
- private FilePickUtils.OnFileChoose onFileChoose = new FilePickUtils.OnFileChoose() {
- @Override
- public void onFileChoose(String fileUri, int requestCode) {
- // here you will get captured or selected image<br>
- File image = new File(fileUri);
- Picasso.get().load(image).resize(RecentUtils.ConvertDpToPx(EditProfileActivity.this,100),RecentUtils.ConvertDpToPx(EditProfileActivity.this,100)).into(imgProfile);
- Bitmap bitmap = BitmapFactory.decodeFile(image.getPath());
- ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
- bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
- byte[] byteArray = byteArrayOutputStream.toByteArray();
- selectedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
- }
- };
- // @Override
- // public boolean onOptionsItemSelected(MenuItem item) {
- // int id = item.getItemId();
- //
- // //noinspection SimplifiableIfStatement
- // if (id == android.R.id.home) {
- // onBackPressed();
- // return true;
- // }
- //
- // return super.onOptionsItemSelected(item);
- // }
- // private void setToolbar() {
- // Toolbar toolbar = findViewById(R.id.toolbar);
- // setSupportActionBar(toolbar);
- // getSupportActionBar().setTitle("Profile");
- // getSupportActionBar().setDisplayHomeAsUpEnabled(true);
- // }
- @Override
- public void onClick(View view) {
- if (view == btnSave) {
- doSave();
- } else if(view == imgProfile){
- openAddImage();
- }
- }
- public void openAddImage() {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setMessage("Please select image");
- builder.setPositiveButton("Camera", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- filePickUtils.requestImageCamera(CAMERA_PERMISSION, true, true,true);
- }
- });
- builder.setNegativeButton("Gallery", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- filePickUtils.requestImageGallery(STORAGE_PERMISSION_IMAGE, true, true,true);
- }
- });
- builder.show();
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if (lifeCycleCallBackManager != null) {
- lifeCycleCallBackManager.onActivityResult(requestCode, resultCode, data);
- }
- }//onActivityResult
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
- super.onRequestPermissionsResult(requestCode, permissions, grantResults);
- if (lifeCycleCallBackManager != null) {
- lifeCycleCallBackManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
- }
- }
- private void doSave() {
- String name = edName.getText().toString();
- // String dob = selectDate.getText().toString();
- String phone = edPhone.getText().toString();
- String email = edEmail.getText().toString();
- if (TextUtils.isEmpty(name)) {
- showToast("Please enter name");
- return;
- }
- if (TextUtils.isEmpty(phone)) {
- showToast("Please enter phone");
- return;
- }
- /* if (TextUtils.isEmpty(dob)) {
- showToast("Please enter date of birth");
- return;
- }*/
- if(!RecentUtils.isEmailValid(email)){
- showToast("Please enter valid email address");
- return;
- }
- Map<String, String> params = new HashMap<>();
- String[] names = name.split(" ");
- params.put("firstname", names[0]);
- if (names.length > 1) {
- params.put("lastname", names[1]);
- } else {
- params.put("lastname", "");
- }
- // params.put("address", "");
- params.put("gender", "male");
- params.put("dob", DataManager.getInstance().getDob());
- params.put("phone", phone);
- params.put("email", email);
- if(!TextUtils.isEmpty(selectedImage)){
- params.put("image", "data:image/jpeg;base64," + selectedImage);
- }
- showDialogProgress("Updating Profile Data");
- new NetworkCall().execute(params);
- /* RetrofitInterface apiService = ApiUtils.getAPIService();
- String auth = AppConstant.AuthValue + DataManager.getInstance().getToken();
- Call<GetUpdateProfile> call = apiService.doUpdateProfile(auth, params);
- call.enqueue(new Callback<GetUpdateProfile>() {
- @Override
- public void onResponse(@NonNull Call<GetUpdateProfile> call, @NonNull Response<GetUpdateProfile> data) {
- hideDialogProgress();
- if (data.isSuccessful()) {
- GetUpdateProfile response = data.body();
- if (response != null) {
- if (response.getSTATUS() == 200) {
- showToast(response.getMESSAGE());
- DataManager.getInstance().setLoginData(response.getDATA());
- } else {
- showToast(response.getMESSAGE());
- }
- } else {
- showToast(getString(R.string.no_response));
- }
- } else {
- RecentUtils.handleRetrofitError(data.code());
- }
- }
- @Override
- public void onFailure(@NonNull Call<GetUpdateProfile> call, @NonNull Throwable t) {
- if (!call.isCanceled()) {
- hideDialogProgress();
- showToast(getString(R.string.network_error));
- }
- }
- });*/
- }
- private class NetworkCall extends AsyncTask<Map<String, String>, Void, Response<GetUpdateProfile>> {
- @Override
- protected Response<GetUpdateProfile> doInBackground(Map<String, String>... params) {
- try {
- RetrofitInterface apiService = ApiUtils.getAPIService();
- String auth = AppConstant.AuthValue + DataManager.getInstance().getToken();
- Call<GetUpdateProfile> call = apiService.doUpdateProfile(auth, params[0]);
- Response<GetUpdateProfile> response = call.execute();
- return response;
- } catch (IOException | JsonSyntaxException e) {
- e.printStackTrace();
- }
- return null;
- }
- @Override
- protected void onPostExecute(Response<GetUpdateProfile> data) {
- hideDialogProgress();
- if(data!=null) {
- if (data.isSuccessful()) {
- GetUpdateProfile response = data.body();
- if (response != null) {
- if (response.getSTATUS() == 200) {
- showToast(response.getMESSAGE());
- DataManager.getInstance().setLoginData(response.getDATA());
- } else {
- showToast(response.getMESSAGE());
- }
- } else {
- showToast(getString(R.string.no_response));
- }
- } else {
- RecentUtils.handleRetrofitError(data.code());
- }
- }else{
- if (RecentUtils.checkInternet()) {
- showToast("Please check internet connection!");
- }
- }
- }
- }
- private void openDate() {
- final Calendar c = Calendar.getInstance();
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
- String dt = selectDate.getText().toString();
- if (!TextUtils.isEmpty(dt)) {
- try {
- c.setTime(simpleDateFormat.parse(dt));
- } catch (ParseException e) {
- e.printStackTrace();
- }
- }
- int year = c.get(Calendar.YEAR);
- int month = c.get(Calendar.MONTH);
- int day = c.get(Calendar.DAY_OF_MONTH);
- // Create a new instance of DatePickerDialog and return it
- DatePickerDialog datePicker = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
- @Override
- public void onDateSet(DatePicker datePicker, int year, int month, int day) {
- String dd;
- if (day < 10) {
- dd = "0" + day;
- } else {
- dd = "" + day;
- }
- String mm;
- month = month + 1;
- if (month < 10) {
- mm = "0" + month;
- } else {
- mm = "" + month;
- }
- String fullDate = year + "-" + mm + "-" + dd;
- selectDate.setText(fullDate);
- }
- }, year, month, day);
- datePicker.show();
- }
- @OnClick (R.id.iv_back)
- void closeactivity(){
- finish();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement