Advertisement
ricky_yulianto

Untitled

Feb 15th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.29 KB | None | 0 0
  1. package codelabs.siloam.activity;
  2.  
  3. import android.app.DatePickerDialog;
  4. import android.content.DialogInterface;
  5. import android.content.Intent;
  6. import android.graphics.Bitmap;
  7. import android.graphics.BitmapFactory;
  8. import android.os.AsyncTask;
  9. import android.os.Bundle;
  10. import android.support.annotation.NonNull;
  11. import android.support.v7.app.AlertDialog;
  12. import android.support.v7.widget.Toolbar;
  13. import android.text.TextUtils;
  14. import android.util.Base64;
  15. import android.view.MenuItem;
  16. import android.view.View;
  17. import android.widget.Button;
  18. import android.widget.DatePicker;
  19. import android.widget.EditText;
  20. import android.widget.TextView;
  21.  
  22. import com.google.gson.JsonSyntaxException;
  23. import com.makeramen.roundedimageview.RoundedImageView;
  24. import com.squareup.picasso.Picasso;
  25.  
  26. import java.io.ByteArrayOutputStream;
  27. import java.io.File;
  28. import java.io.IOException;
  29. import java.text.ParseException;
  30. import java.text.SimpleDateFormat;
  31. import java.util.Calendar;
  32. import java.util.HashMap;
  33. import java.util.Locale;
  34. import java.util.Map;
  35.  
  36. import butterknife.BindView;
  37. import butterknife.ButterKnife;
  38. import butterknife.OnClick;
  39. import codelabs.siloam.R;
  40. import codelabs.siloam.connection.ApiUtils;
  41. import codelabs.siloam.connection.AppConstant;
  42. import codelabs.siloam.connection.DataManager;
  43. import codelabs.siloam.connection.RetrofitInterface;
  44. import codelabs.siloam.imagepicker.FilePickUtils;
  45. import codelabs.siloam.imagepicker.LifeCycleCallBackManager;
  46. import codelabs.siloam.model.GetUpdateProfile;
  47. import codelabs.siloam.utils.RecentUtils;
  48. import retrofit2.Call;
  49. import retrofit2.Response;
  50.  
  51. public class EditProfileActivity extends BaseActivity implements View.OnClickListener {
  52.  
  53. private static final int CAMERA_PERMISSION = 11;
  54. private static final int STORAGE_PERMISSION_IMAGE = 22;
  55.  
  56. @BindView(R.id.img_profile)
  57. RoundedImageView imgProfile;
  58. @BindView(R.id.tv_name)
  59. TextView tvName;
  60. @BindView(R.id.tv_nik)
  61. TextView tvNik;
  62. @BindView(R.id.ed_name)
  63. EditText edName;
  64. @BindView(R.id.select_hospital)
  65. TextView selectHospital;
  66. @BindView(R.id.ed_phone)
  67. EditText edPhone;
  68. @BindView(R.id.select_date)
  69. TextView selectDate;
  70. @BindView(R.id.tv_department)
  71. TextView tvDepartment;
  72. @BindView(R.id.btn_save)
  73. Button btnSave;
  74. @BindView(R.id.ed_email)
  75. EditText edEmail;
  76.  
  77. private LifeCycleCallBackManager lifeCycleCallBackManager;
  78. private FilePickUtils filePickUtils;
  79.  
  80. @Override
  81. protected void onCreate(Bundle savedInstanceState) {
  82. super.onCreate(savedInstanceState);
  83. setContentView(R.layout.activity_edit_profile);
  84. ButterKnife.bind(this);
  85.  
  86. // setToolbar();
  87.  
  88. // selectDate.setOnClickListener(this);
  89. btnSave.setOnClickListener(this);
  90. imgProfile.setOnClickListener(this);
  91.  
  92. Picasso.get().load(DataManager.getInstance().getImage())
  93. .placeholder(R.drawable.placeholder)
  94. .placeholder(R.drawable.placeholder)
  95. .into(imgProfile);
  96.  
  97. tvName.setText(DataManager.getInstance().getProfile().getFullname());
  98. tvNik.setText(DataManager.getInstance().getNik());
  99.  
  100. edEmail.setText(DataManager.getInstance().getEmail());
  101. edName.setText(DataManager.getInstance().getProfile().getFullname());
  102. selectHospital.setText(DataManager.getInstance().getHospitalName());
  103. edPhone.setText(DataManager.getInstance().getProfile().getPhone());
  104. selectDate.setText(RecentUtils.formatDateToDateDMY(DataManager.getInstance().getDob()));
  105. tvDepartment.setText(DataManager.getInstance().getDepartmentName());
  106.  
  107.  
  108. filePickUtils = new FilePickUtils(this, onFileChoose);
  109. lifeCycleCallBackManager = filePickUtils.getCallBackManager();
  110.  
  111. }
  112.  
  113.  
  114. private String selectedImage;
  115. private FilePickUtils.OnFileChoose onFileChoose = new FilePickUtils.OnFileChoose() {
  116. @Override
  117. public void onFileChoose(String fileUri, int requestCode) {
  118. // here you will get captured or selected image<br>
  119. File image = new File(fileUri);
  120.  
  121. Picasso.get().load(image).resize(RecentUtils.ConvertDpToPx(EditProfileActivity.this,100),RecentUtils.ConvertDpToPx(EditProfileActivity.this,100)).into(imgProfile);
  122.  
  123. Bitmap bitmap = BitmapFactory.decodeFile(image.getPath());
  124. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  125. bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
  126. byte[] byteArray = byteArrayOutputStream.toByteArray();
  127. selectedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
  128.  
  129. }
  130. };
  131.  
  132. // @Override
  133. // public boolean onOptionsItemSelected(MenuItem item) {
  134. // int id = item.getItemId();
  135. //
  136. // //noinspection SimplifiableIfStatement
  137. // if (id == android.R.id.home) {
  138. // onBackPressed();
  139. // return true;
  140. // }
  141. //
  142. // return super.onOptionsItemSelected(item);
  143. // }
  144.  
  145.  
  146. // private void setToolbar() {
  147. // Toolbar toolbar = findViewById(R.id.toolbar);
  148. // setSupportActionBar(toolbar);
  149. // getSupportActionBar().setTitle("Profile");
  150. // getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  151. // }
  152.  
  153. @Override
  154. public void onClick(View view) {
  155. if (view == btnSave) {
  156. doSave();
  157. } else if(view == imgProfile){
  158. openAddImage();
  159. }
  160. }
  161.  
  162.  
  163. public void openAddImage() {
  164. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  165. builder.setMessage("Please select image");
  166. builder.setPositiveButton("Camera", new DialogInterface.OnClickListener() {
  167. @Override
  168. public void onClick(DialogInterface dialogInterface, int i) {
  169. filePickUtils.requestImageCamera(CAMERA_PERMISSION, true, true,true);
  170. }
  171. });
  172. builder.setNegativeButton("Gallery", new DialogInterface.OnClickListener() {
  173. @Override
  174. public void onClick(DialogInterface dialogInterface, int i) {
  175. filePickUtils.requestImageGallery(STORAGE_PERMISSION_IMAGE, true, true,true);
  176. }
  177. });
  178. builder.show();
  179.  
  180. }
  181.  
  182.  
  183. @Override
  184. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  185. super.onActivityResult(requestCode, resultCode, data);
  186. if (lifeCycleCallBackManager != null) {
  187. lifeCycleCallBackManager.onActivityResult(requestCode, resultCode, data);
  188. }
  189.  
  190. }//onActivityResult
  191.  
  192.  
  193. @Override
  194. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  195. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  196. if (lifeCycleCallBackManager != null) {
  197. lifeCycleCallBackManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
  198. }
  199. }
  200.  
  201. private void doSave() {
  202. String name = edName.getText().toString();
  203. // String dob = selectDate.getText().toString();
  204. String phone = edPhone.getText().toString();
  205. String email = edEmail.getText().toString();
  206.  
  207. if (TextUtils.isEmpty(name)) {
  208. showToast("Please enter name");
  209. return;
  210. }
  211. if (TextUtils.isEmpty(phone)) {
  212. showToast("Please enter phone");
  213. return;
  214. }
  215. /* if (TextUtils.isEmpty(dob)) {
  216. showToast("Please enter date of birth");
  217. return;
  218. }*/
  219.  
  220. if(!RecentUtils.isEmailValid(email)){
  221. showToast("Please enter valid email address");
  222. return;
  223. }
  224.  
  225.  
  226.  
  227. Map<String, String> params = new HashMap<>();
  228. String[] names = name.split(" ");
  229.  
  230. params.put("firstname", names[0]);
  231. if (names.length > 1) {
  232. params.put("lastname", names[1]);
  233. } else {
  234. params.put("lastname", "");
  235. }
  236. // params.put("address", "");
  237. params.put("gender", "male");
  238. params.put("dob", DataManager.getInstance().getDob());
  239. params.put("phone", phone);
  240. params.put("email", email);
  241.  
  242. if(!TextUtils.isEmpty(selectedImage)){
  243. params.put("image", "data:image/jpeg;base64," + selectedImage);
  244. }
  245.  
  246. showDialogProgress("Updating Profile Data");
  247.  
  248. new NetworkCall().execute(params);
  249. /* RetrofitInterface apiService = ApiUtils.getAPIService();
  250. String auth = AppConstant.AuthValue + DataManager.getInstance().getToken();
  251. Call<GetUpdateProfile> call = apiService.doUpdateProfile(auth, params);
  252. call.enqueue(new Callback<GetUpdateProfile>() {
  253. @Override
  254. public void onResponse(@NonNull Call<GetUpdateProfile> call, @NonNull Response<GetUpdateProfile> data) {
  255. hideDialogProgress();
  256. if (data.isSuccessful()) {
  257. GetUpdateProfile response = data.body();
  258. if (response != null) {
  259. if (response.getSTATUS() == 200) {
  260. showToast(response.getMESSAGE());
  261. DataManager.getInstance().setLoginData(response.getDATA());
  262. } else {
  263. showToast(response.getMESSAGE());
  264. }
  265. } else {
  266. showToast(getString(R.string.no_response));
  267. }
  268. } else {
  269. RecentUtils.handleRetrofitError(data.code());
  270. }
  271. }
  272.  
  273. @Override
  274. public void onFailure(@NonNull Call<GetUpdateProfile> call, @NonNull Throwable t) {
  275. if (!call.isCanceled()) {
  276. hideDialogProgress();
  277. showToast(getString(R.string.network_error));
  278. }
  279. }
  280. });*/
  281. }
  282.  
  283.  
  284. private class NetworkCall extends AsyncTask<Map<String, String>, Void, Response<GetUpdateProfile>> {
  285. @Override
  286. protected Response<GetUpdateProfile> doInBackground(Map<String, String>... params) {
  287. try {
  288.  
  289. RetrofitInterface apiService = ApiUtils.getAPIService();
  290. String auth = AppConstant.AuthValue + DataManager.getInstance().getToken();
  291. Call<GetUpdateProfile> call = apiService.doUpdateProfile(auth, params[0]);
  292. Response<GetUpdateProfile> response = call.execute();
  293. return response;
  294. } catch (IOException | JsonSyntaxException e) {
  295. e.printStackTrace();
  296. }
  297. return null;
  298. }
  299.  
  300. @Override
  301. protected void onPostExecute(Response<GetUpdateProfile> data) {
  302. hideDialogProgress();
  303. if(data!=null) {
  304. if (data.isSuccessful()) {
  305. GetUpdateProfile response = data.body();
  306. if (response != null) {
  307. if (response.getSTATUS() == 200) {
  308. showToast(response.getMESSAGE());
  309. DataManager.getInstance().setLoginData(response.getDATA());
  310. } else {
  311. showToast(response.getMESSAGE());
  312. }
  313. } else {
  314. showToast(getString(R.string.no_response));
  315. }
  316. } else {
  317. RecentUtils.handleRetrofitError(data.code());
  318. }
  319. }else{
  320.  
  321. if (RecentUtils.checkInternet()) {
  322. showToast("Please check internet connection!");
  323. }
  324. }
  325. }
  326. }
  327.  
  328. private void openDate() {
  329.  
  330. final Calendar c = Calendar.getInstance();
  331. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
  332. String dt = selectDate.getText().toString();
  333. if (!TextUtils.isEmpty(dt)) {
  334. try {
  335. c.setTime(simpleDateFormat.parse(dt));
  336. } catch (ParseException e) {
  337. e.printStackTrace();
  338. }
  339. }
  340.  
  341. int year = c.get(Calendar.YEAR);
  342. int month = c.get(Calendar.MONTH);
  343. int day = c.get(Calendar.DAY_OF_MONTH);
  344.  
  345. // Create a new instance of DatePickerDialog and return it
  346. DatePickerDialog datePicker = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
  347. @Override
  348. public void onDateSet(DatePicker datePicker, int year, int month, int day) {
  349. String dd;
  350. if (day < 10) {
  351. dd = "0" + day;
  352. } else {
  353. dd = "" + day;
  354. }
  355. String mm;
  356. month = month + 1;
  357. if (month < 10) {
  358. mm = "0" + month;
  359. } else {
  360. mm = "" + month;
  361. }
  362.  
  363. String fullDate = year + "-" + mm + "-" + dd;
  364. selectDate.setText(fullDate);
  365.  
  366. }
  367. }, year, month, day);
  368.  
  369. datePicker.show();
  370. }
  371. @OnClick (R.id.iv_back)
  372. void closeactivity(){
  373. finish();
  374. }
  375.  
  376. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement