Advertisement
ricky_yulianto

Untitled

Jul 17th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.79 KB | None | 0 0
  1. package codelabs.ambarrukmo.fragment;
  2.  
  3. import android.app.DatePickerDialog;
  4. import android.app.Dialog;
  5. import android.app.TimePickerDialog;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.graphics.Color;
  9. import android.graphics.drawable.ColorDrawable;
  10. import android.net.Uri;
  11. import android.os.Bundle;
  12. import android.os.CountDownTimer;
  13. import android.support.annotation.NonNull;
  14. import android.support.annotation.Nullable;
  15. import android.support.v4.app.Fragment;
  16. import android.support.v4.app.FragmentManager;
  17. import android.support.v4.app.FragmentTransaction;
  18. import android.support.v7.app.AlertDialog;
  19. import android.text.TextUtils;
  20. import android.text.format.DateFormat;
  21. import android.view.Gravity;
  22. import android.view.LayoutInflater;
  23. import android.view.View;
  24. import android.view.ViewGroup;
  25. import android.view.Window;
  26. import android.widget.Button;
  27. import android.widget.DatePicker;
  28. import android.widget.EditText;
  29. import android.widget.LinearLayout;
  30. import android.widget.PopupMenu;
  31. import android.widget.ScrollView;
  32. import android.widget.TextView;
  33. import android.widget.TimePicker;
  34. import android.widget.Toast;
  35.  
  36.  
  37. import java.text.ParseException;
  38. import java.text.SimpleDateFormat;
  39. import java.util.Calendar;
  40. import java.util.Date;
  41. import java.util.HashMap;
  42. import java.util.Locale;
  43. import java.util.Map;
  44.  
  45. import javax.annotation.Nonnull;
  46.  
  47. import butterknife.BindView;
  48. import butterknife.ButterKnife;
  49. import butterknife.OnClick;
  50. import butterknife.Unbinder;
  51. import codelabs.ambarrukmo.R;
  52. import codelabs.ambarrukmo.activity.LoginActivity;
  53. import codelabs.ambarrukmo.activity.ProfileActivity;
  54. import codelabs.ambarrukmo.activity.RegisterActivity;
  55. import codelabs.ambarrukmo.connection.ApiUtils;
  56. import codelabs.ambarrukmo.connection.AppConstant;
  57. import codelabs.ambarrukmo.connection.DataManager;
  58. import codelabs.ambarrukmo.connection.RetrofitInterface;
  59. import codelabs.ambarrukmo.dialog.DialogProgress;
  60. import codelabs.ambarrukmo.model.DoPost;
  61. import codelabs.ambarrukmo.model.GetValletNumber;
  62. import codelabs.ambarrukmo.utils.MyLog;
  63. import codelabs.ambarrukmo.utils.RecentUtils;
  64. import retrofit2.Call;
  65. import retrofit2.Callback;
  66. import retrofit2.Response;
  67.  
  68.  
  69. public class AddParkingValleyFragment extends Fragment implements View.OnClickListener {
  70.  
  71. private static final String ARG_TEXT = "param1";
  72. public static final String IS_TASK="IS_TASK";
  73. private GetValletNumber.DATABean responseDataValet;
  74. protected DialogProgress dialogProgress;
  75.  
  76.  
  77. @BindView(R.id.tv_error)
  78. TextView tvError;
  79. @BindView(R.id.tv_error_desc)
  80. TextView tvErrorDesc;
  81. @BindView(R.id.container_error)
  82. LinearLayout containerError;
  83. @BindView(R.id.tv_loading)
  84. TextView tvLoading;
  85. @BindView(R.id.tv_loading_desc)
  86. TextView tvLoadingDesc;
  87. @BindView(R.id.container_loading)
  88. LinearLayout containerLoading;
  89. @BindView(R.id.no_valley)
  90. TextView tvNoValley;
  91. @BindView(R.id.edt_brand)
  92. EditText edtBrand;
  93. @BindView(R.id.edt_colour)
  94. EditText edtColour;
  95. @BindView(R.id.edt_police_no)
  96. EditText edtPoliceNo;
  97. @BindView(R.id.edt_date)
  98. EditText edtDate;
  99. @BindView(R.id.edt_time)
  100. EditText edtTime;
  101. @BindView(R.id.tv_note_valley)
  102. TextView tvNoteValley;
  103. @BindView(R.id.btn_booking_valley)
  104. Button btnBooking;
  105. @BindView(R.id.edt_type)
  106. EditText edtType;
  107. @BindView(R.id.tv_total_vallet)
  108. TextView tvTotalVallet;
  109.  
  110.  
  111. @Nullable
  112. private int pageText;
  113.  
  114.  
  115. Dialog myDialog;
  116. Unbinder unbinder;
  117. private Toast mToast;
  118. private Context mContext;
  119. private Call<GetValletNumber> callVallet;
  120.  
  121.  
  122.  
  123. public AddParkingValleyFragment() {
  124. // Required empty public constructor
  125. }
  126.  
  127. public static AddParkingValleyFragment newInstance(@NonNull final int pageText) {
  128. AddParkingValleyFragment fragment = new AddParkingValleyFragment();
  129. Bundle args = new Bundle();
  130. args.putInt(ARG_TEXT, pageText);
  131. fragment.setArguments(args);
  132.  
  133.  
  134. return fragment;
  135. }
  136.  
  137. @Override
  138. public void onCreate(Bundle savedInstanceState) {
  139. super.onCreate(savedInstanceState);
  140. if (getArguments() !=null) {
  141. pageText = getArguments().getInt(ARG_TEXT);
  142. }
  143. }
  144.  
  145. @Override
  146. public void onResume() {
  147. super.onResume();
  148. // Set title
  149. // ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(getResources().getString(R.string.setting_menu));
  150. }
  151. @Override
  152. public void onDestroyView() {
  153. super.onDestroyView();
  154. unbinder.unbind();
  155.  
  156. if (callVallet!=null) {
  157. callVallet.cancel();
  158. }
  159. }
  160.  
  161. // @Override
  162. // public void onStop() {
  163. // super.onStop();
  164. // unbinder.unbind();
  165. //
  166. // }
  167.  
  168.  
  169.  
  170.  
  171.  
  172. @Override
  173. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  174. Bundle savedInstanceState) {
  175. // Inflate the layout for this fragment
  176. View view = inflater.inflate(R.layout.fragment_add_parking_valley, container, false);
  177. unbinder = ButterKnife.bind(this, view);
  178. myDialog = new Dialog(getContext());
  179. initListeners();
  180. initVariables();
  181. callFunctions();
  182.  
  183. return view;
  184.  
  185. }
  186.  
  187.  
  188.  
  189. private void initVariables(){
  190. // tvNoValley.setOnClickListener(this);
  191. edtBrand.setOnClickListener(this);
  192. edtColour.setOnClickListener(this);
  193. edtPoliceNo.setOnClickListener(this);
  194. // edtDate.setOnClickListener(this);
  195. // edtTime.setOnClickListener(this);
  196. btnBooking.setOnClickListener(this);
  197. edtType.setOnClickListener(this);
  198. }
  199.  
  200. private void initListeners(){
  201.  
  202. }
  203.  
  204. private void callFunctions(){
  205. hideContainer();
  206. loadDataValet();
  207. }
  208.  
  209.  
  210. @OnClick(R.id.btn_booking_valley)
  211. public void onViewClicked() {
  212. // doSubmit();
  213. // ShowPopup();
  214.  
  215. }
  216.  
  217. public void ShowPopup(){
  218.  
  219. myDialog.setContentView(R.layout.dialog_parking_valley);
  220.  
  221.  
  222. Button btnOk;
  223. TextView tvNoValet;
  224. TextView tv_total_minutes;
  225. tv_total_minutes = (TextView) myDialog.findViewById(R.id.tv_total_minutes);
  226. tvNoValet = (TextView) myDialog.findViewById(R.id.no_valley);
  227. tvNoValet.setText("#" +" " +responseDataValet.getVallet_number());
  228. btnOk = (Button) myDialog.findViewById(R.id.btn_ok_valley);
  229. btnOk.setOnClickListener(new View.OnClickListener() {
  230. @Override
  231. public void onClick(View v) {
  232. myDialog.dismiss();
  233. Fragment fragment = new HistoryValleyFragment();
  234. FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
  235. FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  236. fragmentTransaction.replace(R.id.fragment_container, fragment);
  237. fragmentTransaction.addToBackStack(null);
  238. fragmentTransaction.commit();
  239.  
  240.  
  241. }
  242. });
  243.  
  244.  
  245.  
  246. myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  247. myDialog.show();
  248.  
  249.  
  250. }
  251.  
  252.  
  253. private void doSubmit() {
  254. String vallet_number = tvNoValley.getText().toString();
  255. String brand = edtBrand.getText().toString();
  256. String type = edtType.getText().toString();
  257. String color = edtColour.getText().toString();
  258. String policeNo = edtPoliceNo.getText().toString();
  259. // String date = edtDate.getText().toString();
  260. // String time = edtTime.getText().toString();
  261.  
  262.  
  263.  
  264. if (TextUtils.isEmpty(brand)){
  265. showToast("Please enter car brand");
  266. return;
  267. }
  268. if (TextUtils.isEmpty(type)){
  269. showToast("Please select type car");
  270. }
  271. if (TextUtils.isEmpty(color)){
  272. showToast("Please enter color car");
  273. }
  274. if (TextUtils.isEmpty(policeNo)){
  275. showToast("Please enter police number");
  276. }
  277. // if (TextUtils.isEmpty(date)){
  278. // showToast("Please select date");
  279. // }
  280. // if (TextUtils.isEmpty(time)) {
  281. // showToast("Please select time");
  282. // }
  283.  
  284.  
  285. showDialogProgress(getString(R.string.get_valet));
  286.  
  287. // FragmentTransaction ft = getFragmentManager().beginTransaction();
  288. // ft.detach(AddParkingValleyFragment.this).attach(AddParkingValleyFragment.this).commit();
  289.  
  290.  
  291. Map<String, String> params = new HashMap<>();
  292. params.put("vallet_number", vallet_number);
  293. params.put("no_pol", policeNo);
  294. params.put("car_brand", brand);
  295. params.put("car_type", type);
  296. params.put("car_color", color);
  297. // params.put("vallet_date", date);
  298. // params.put("vallet_time", time);
  299.  
  300.  
  301. RetrofitInterface apiService = ApiUtils.getAPIService();
  302. String auth = AppConstant.AuthValue + DataManager.getInstance().getToken();
  303. Call<DoPost> call = apiService.doCreateVallet(auth, params);
  304. call.enqueue(new Callback<DoPost>() {
  305. @Override
  306. public void onResponse(@NonNull Call<DoPost> call, @NonNull Response<DoPost> data) {
  307. hideDialogProgress();
  308. if (data.isSuccessful()) {
  309. DoPost response = data.body();
  310. if (response != null) {
  311. if (response.getSTATUS() == 200) {
  312. ShowPopup();
  313.  
  314. // FragmentTransaction ft = getFragmentManager().beginTransaction();
  315. // ft.detach(AddParkingValleyFragment.this).attach(AddParkingValleyFragment.this).commit();
  316.  
  317. } else {
  318. showToast(response.getMESSAGE());
  319. }
  320. } else {
  321. showToast(getString(R.string.no_response));
  322. }
  323. } else {
  324. RecentUtils.handleRetrofitError(data.code());
  325. }
  326. }
  327.  
  328. @Override
  329. public void onFailure(@NonNull Call<DoPost> call,@NonNull Throwable t) {
  330. if (!call.isCanceled()) {
  331. hideDialogProgress();
  332. showToast(getString(R.string.network_error));
  333. }
  334.  
  335. }
  336. });
  337.  
  338. }
  339.  
  340.  
  341. // private void openDate() {
  342. //
  343. // final Calendar c = Calendar.getInstance();
  344. // final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
  345. // final SimpleDateFormat toFormat = new SimpleDateFormat("dd MMMM yyyy", Locale.getDefault());
  346. // String dt = edtDate.getText().toString();
  347. // if (!TextUtils.isEmpty(dt)) {
  348. // try {
  349. // c.setTime(simpleDateFormat.parse(dt));
  350. // } catch (ParseException e) {
  351. // e.printStackTrace();
  352. // }
  353. // }
  354. //
  355. // int year = c.get(Calendar.YEAR);
  356. // int month = c.get(Calendar.MONTH);
  357. // int day = c.get(Calendar.DAY_OF_MONTH);
  358. //
  359. //
  360. // // Create a new instance of DatePickerDialog and return it
  361. // DatePickerDialog datePicker = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
  362. // @Override
  363. // public void onDateSet(DatePicker datePicker, int year, int month, int day) {
  364. // String dd;
  365. // if (day < 10) {
  366. // dd = "0" + day;
  367. // } else {
  368. // dd = "" + day;
  369. // }
  370. // String mm;
  371. // month = month + 1;
  372. // if (month < 10) {
  373. // mm = "0" + month;
  374. // } else {
  375. // mm = "" + month;
  376. // }
  377. //
  378. // String fullDate = year + "-" + mm + "-" + dd;
  379. // edtDate.setText(fullDate);
  380. //
  381. // }
  382. // }, day, month, year);
  383. // datePicker.getDatePicker().setMinDate(new Date().getTime());
  384. // datePicker.show();
  385. // }
  386.  
  387. // private void openTime() {
  388. // final Calendar c = Calendar.getInstance();
  389. //
  390. // SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
  391. // String dt = edtTime.getText().toString();
  392. // if (!TextUtils.isEmpty(dt)) {
  393. // try {
  394. // c.setTime(simpleDateFormat.parse(dt));
  395. // } catch (ParseException e) {
  396. // e.printStackTrace();
  397. // }
  398. // }
  399. //
  400. // int hour = c.get(Calendar.HOUR_OF_DAY);
  401. // int minute = c.get(Calendar.MINUTE);
  402. //
  403. // // Create a new instance of TimePickerDialog and return it
  404. // TimePickerDialog timePickerDialog = new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() {
  405. // @Override
  406. // public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
  407. //
  408. // String hour;
  409. // if(hourOfDay<10){
  410. // hour="0"+hourOfDay;
  411. // }else{
  412. // hour=hourOfDay+"";
  413. // }
  414. //
  415. //
  416. // String min;
  417. // if(minute<10){
  418. // min="0"+minute;
  419. // }else{
  420. // min=minute+"";
  421. // }
  422. //
  423. // String time = hour+":"+min;
  424. // edtTime.setText(time);
  425. //
  426. // }
  427. // }, hour, minute, DateFormat.is24HourFormat(getContext()));
  428. // timePickerDialog.show();
  429. // }
  430.  
  431.  
  432.  
  433.  
  434. public void showDialogProgress(String message) {
  435. if (message != null) {
  436. dialogProgress = new DialogProgress(getContext(), message, true);
  437. dialogProgress.setCancelable(false);
  438. dialogProgress.show();
  439. } else {
  440. dialogProgress = new DialogProgress(getContext(), "Loading ...", false);
  441. dialogProgress.setCancelable(false);
  442. dialogProgress.show();
  443. }
  444. }
  445.  
  446. public void hideDialogProgress() {
  447. if (dialogProgress != null) {
  448. if (dialogProgress.isShowing()) {
  449. dialogProgress.dismiss();
  450. }
  451. }
  452. }
  453.  
  454.  
  455. public void showToast(String val){
  456. if (mToast != null) mToast.cancel();
  457. mToast = Toast.makeText(getContext(), val, Toast.LENGTH_SHORT);
  458. mToast.show();
  459. }
  460.  
  461. private void loadDataValet() {
  462. // tvLoading.setText("Getting Valet Number");
  463. // tvLoadingDesc.setText("Please wait for getting about");
  464. // containerLoading.setVisibility(View.VISIBLE);
  465.  
  466.  
  467. showDialogProgress("Getting data");
  468.  
  469. RetrofitInterface apiService = ApiUtils.getAPIService();
  470. String auth = AppConstant.AuthValue + DataManager.getInstance().getToken();
  471. // Call<GetValletNumber> call = apiService.gettingValetNumber(auth);
  472. callVallet = apiService.gettingValetNumber(auth);
  473. callVallet.enqueue(new Callback<GetValletNumber>() {
  474. @Override
  475. public void onResponse(@Nonnull Call<GetValletNumber> call,@NonNull Response<GetValletNumber> data) {
  476. // containerLoading.setVisibility(View.GONE);
  477. hideDialogProgress();
  478. if (data.isSuccessful()) {
  479. GetValletNumber response = data.body();
  480. if (response != null) {
  481. if (response.getSTATUS() == 200) {
  482. // containerContent.setVisibility(View.VISIBLE);
  483. responseDataValet = response.getDATA();
  484.  
  485.  
  486. processValetNumber();
  487.  
  488. } else {
  489. // containerError.setVisibility(View.VISIBLE);
  490. // tvError.setText(response.getMESSAGE());
  491. }
  492. } else {
  493. // containerError.setVisibility(View.VISIBLE);
  494. // tvError.setText(getString(R.string.no_response));
  495. }
  496. } else {
  497. // containerError.setVisibility(View.VISIBLE);
  498. // RecentUtils.handleRetrofitError(data.code(), tvError);
  499. }
  500. }
  501.  
  502. @Override
  503. public void onFailure(@NonNull Call<GetValletNumber> call, @NonNull Throwable t) {
  504. if (!call.isCanceled()) {
  505. hideDialogProgress();
  506. // MyLog.logE(t.getMessage());
  507. // containerLoading.setVisibility(View.GONE);
  508. // containerError.setVisibility(View.VISIBLE);
  509. // tvError.setText(getString(R.string.network_error));
  510. }
  511.  
  512. }
  513. });
  514.  
  515. }
  516.  
  517.  
  518. private void processValetNumber() {
  519. tvNoValley.setText(responseDataValet.getVallet_number());
  520. tvTotalVallet.setText(responseDataValet.getTotal_booked()+ "/" + (responseDataValet.getTotal_slot()));
  521. // tvTotalVallet.setText(responseDataValet.get());
  522. }
  523.  
  524.  
  525.  
  526. public void hideContainer() {
  527. // containerContent.setVisibility(View.GONE);
  528. containerError.setVisibility(View.GONE);
  529. containerLoading.setVisibility(View.GONE);
  530. }
  531.  
  532.  
  533. @Override
  534. public void onClick(View view) {
  535. if (view == btnBooking) {
  536. doSubmit();
  537. // FragmentTransaction ft = getFragmentManager().beginTransaction();
  538. // ft.detach(AddParkingValleyFragment.this).attach(AddParkingValleyFragment.this).commit();
  539. // ShowPopup();
  540. // } else if (view == edtDate){
  541. // openDate();
  542. // } else if (view == edtTime){
  543. // openTime();
  544. }
  545. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement