|
1 | 1 | package com.example.saar.ChangeCredentials;
|
2 | 2 |
|
| 3 | +import android.app.ProgressDialog; |
| 4 | +import android.content.Intent; |
3 | 5 | import android.os.Bundle;
|
4 | 6 | import android.support.annotation.NonNull;
|
5 | 7 | import android.support.annotation.Nullable;
|
6 | 8 | import android.support.v4.app.Fragment;
|
7 | 9 | import android.view.LayoutInflater;
|
8 | 10 | import android.view.View;
|
9 | 11 | import android.view.ViewGroup;
|
| 12 | +import android.widget.Button; |
| 13 | +import android.widget.EditText; |
| 14 | +import android.widget.Toast; |
10 | 15 |
|
| 16 | +import com.android.volley.AuthFailureError; |
| 17 | +import com.android.volley.DefaultRetryPolicy; |
| 18 | +import com.android.volley.Request; |
| 19 | +import com.android.volley.RequestQueue; |
| 20 | +import com.android.volley.Response; |
| 21 | +import com.android.volley.VolleyError; |
| 22 | +import com.android.volley.toolbox.StringRequest; |
| 23 | +import com.android.volley.toolbox.Volley; |
| 24 | +import com.example.saar.Constant; |
| 25 | +import com.example.saar.OtpActivity; |
11 | 26 | import com.example.saar.R;
|
| 27 | +import com.example.saar.Utils.Utils; |
| 28 | + |
| 29 | +import org.json.JSONArray; |
| 30 | +import org.json.JSONException; |
| 31 | +import org.json.JSONObject; |
| 32 | + |
| 33 | +import java.util.HashMap; |
| 34 | +import java.util.Map; |
| 35 | + |
| 36 | +import timber.log.Timber; |
12 | 37 |
|
13 | 38 | public class ForgotPasswordFragment extends Fragment {
|
14 | 39 |
|
| 40 | + EditText email, rollno; |
| 41 | + Button forgotPassword; |
| 42 | + ProgressDialog progressDialog; |
| 43 | + |
15 | 44 | @Override
|
16 | 45 | public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
17 | 46 | Bundle savedInstanceState) {
|
18 | 47 | // Inflate the layout for this fragment
|
19 |
| - return inflater.inflate(R.layout.fragment_forgot_password, container, false); |
| 48 | + View rootView = inflater.inflate(R.layout.fragment_forgot_password, container, false); |
| 49 | + email=rootView.findViewById(R.id.email_text_forgot_password); |
| 50 | + rollno=rootView.findViewById(R.id.roll_text_forgot_password); |
| 51 | + forgotPassword=rootView.findViewById(R.id.forgot_password_button); |
| 52 | + return rootView; |
20 | 53 | }
|
21 | 54 |
|
22 | 55 | @Override
|
23 | 56 | public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
24 | 57 | super.onViewCreated(view, savedInstanceState);
|
25 | 58 | getActivity().setTitle(R.string.forgot_password);
|
| 59 | + |
| 60 | + forgotPassword.setOnClickListener(new View.OnClickListener() { |
| 61 | + @Override |
| 62 | + public void onClick(View v) { |
| 63 | + requestDatas(); |
| 64 | + } |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + private void requestDatas() { |
| 69 | + Utils.closeKeyboard(getView(), getContext()); |
| 70 | + progressDialog = new ProgressDialog(getContext()); |
| 71 | + progressDialog.setMessage("Requesting...."); |
| 72 | + progressDialog.show(); |
| 73 | + StringRequest stringRequest = new StringRequest(Request.Method.POST, Constant.FORGOT_PASSWORD_URL, new Response.Listener<String>() { |
| 74 | + |
| 75 | + @Override |
| 76 | + public void onResponse(String response) { |
| 77 | + Timber.d(response); |
| 78 | + progressDialog.dismiss(); |
| 79 | + try { |
| 80 | + JSONObject jsonObject = new JSONObject(response); |
| 81 | + int status = Integer.parseInt(jsonObject.getString("status")); |
| 82 | + |
| 83 | + if (status == 204) { |
| 84 | + Timber.d(getString(R.string.success_forgot_password_request)); |
| 85 | + Toast.makeText(getContext(), getString(R.string.success_forgot_password_request), Toast.LENGTH_LONG).show(); |
| 86 | + |
| 87 | + Intent intent = new Intent(getContext(), OtpActivity.class); |
| 88 | + intent.putExtra("rollno",rollno.getText().toString()); |
| 89 | + intent.putExtra("forgot_password","forgot_password"); |
| 90 | + startActivity(intent); |
| 91 | + } else { |
| 92 | + Timber.d(getString(R.string.error_Forgot_password)); |
| 93 | + JSONArray jsonArray = jsonObject.getJSONArray("messages"); |
| 94 | + Toast.makeText(getContext(), jsonArray.toString(), Toast.LENGTH_LONG).show(); |
| 95 | + Timber.d(jsonArray.toString()); |
| 96 | + } |
| 97 | + |
| 98 | + } catch (JSONException e) { |
| 99 | + e.printStackTrace(); |
| 100 | + } |
| 101 | + } |
| 102 | + }, new Response.ErrorListener() { |
| 103 | + @Override |
| 104 | + public void onErrorResponse(VolleyError error) { |
| 105 | + //otp_progress.setVisibility(View.GONE); |
| 106 | + progressDialog.dismiss(); |
| 107 | + Toast.makeText(getContext(), error.getLocalizedMessage(), Toast.LENGTH_LONG).show(); |
| 108 | + Timber.d(error.toString()); |
| 109 | + } |
| 110 | + |
| 111 | + }) { |
| 112 | + protected Map<String, String> getParams() throws AuthFailureError { |
| 113 | + Map<String, String> params = new HashMap<String, String>(); |
| 114 | + params.put("email", email.getText().toString()); |
| 115 | + params.put("rollno", rollno.getText().toString()); |
| 116 | + return params; |
| 117 | + } |
| 118 | + }; |
| 119 | + stringRequest.setRetryPolicy(new DefaultRetryPolicy( |
| 120 | + 10000, |
| 121 | + DefaultRetryPolicy.DEFAULT_MAX_RETRIES, |
| 122 | + DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); |
| 123 | + |
| 124 | + RequestQueue requestQueue = Volley.newRequestQueue(getContext()); |
| 125 | + Request<String> data = requestQueue.add(stringRequest); |
26 | 126 | }
|
27 | 127 | }
|
0 commit comments