Advertisement
Guest User

Untitled

a guest
Jul 8th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.78 KB | None | 0 0
  1. package com.example.tm_ma.boleias.Fragments;
  2.  
  3.  
  4.  
  5. import android.content.Context;
  6. import android.content.SharedPreferences;
  7. import android.graphics.drawable.Drawable;
  8. import android.os.Bundle;
  9. import android.app.Fragment;
  10. import android.support.v4.content.ContextCompat;
  11. import android.view.LayoutInflater;
  12. import android.view.View;
  13. import android.view.ViewGroup;
  14. import android.widget.Button;
  15. import android.widget.EditText;
  16. import android.widget.TextView;
  17.  
  18. import com.android.volley.Response;
  19. import com.android.volley.VolleyError;
  20. import com.example.tm_ma.boleias.Managers.UserManager;
  21. import com.example.tm_ma.boleias.Models.Username;
  22. import com.example.tm_ma.boleias.R;
  23. import com.example.tm_ma.boleias.WebServices.WebServices;
  24.  
  25. import org.json.JSONException;
  26. import org.json.JSONObject;
  27.  
  28.  
  29. /**
  30. * A simple {@link Fragment} subclass.
  31. */
  32. public class Login extends Fragment {
  33.  
  34. private EditText txtUsername;
  35. private EditText txtPassword;
  36. private Button btnLogin;
  37. private TextView lblForgot;
  38. private Context context;
  39.  
  40. private String username,password;
  41. private final String LOGIN = "login", USERNAME = "username", PASSWORD = "password";
  42. public Login() {
  43. // Required empty public constructor
  44. }
  45.  
  46. @Override
  47. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  48. Bundle savedInstanceState) {
  49. // Inflate the layout for this fragment
  50. View v = inflater.inflate(R.layout.fragment_login, container, false);
  51. init(v);
  52. return v;
  53. }
  54.  
  55. private void init(View v){
  56. final SharedPreferences sharedPref = v.getContext().getSharedPreferences(LOGIN, Context.MODE_PRIVATE);
  57. username = sharedPref.getString(USERNAME, "");
  58. password = sharedPref.getString(PASSWORD, "");
  59. context = v.getContext();
  60.  
  61. txtUsername = v.findViewById(R.id.txtUsername);
  62. txtPassword = v.findViewById(R.id.txtPassword);
  63. btnLogin = v.findViewById(R.id.btnLogin);
  64.  
  65. lblForgot = v.findViewById(R.id.lblForgot);
  66. lblForgot.setOnClickListener(new View.OnClickListener() {
  67. @Override
  68. public void onClick(View v) {
  69. setViewForgot();
  70. }
  71. });
  72.  
  73. txtUsername.setOnFocusChangeListener(new View.OnFocusChangeListener() {
  74. @Override
  75. public void onFocusChange(View v, boolean hasFocus) {
  76. if (hasFocus) {
  77. Drawable img = context.getResources().getDrawable( R.drawable.user_white);
  78. img.setBounds( 0, 0, 60, 60 );
  79. txtUsername.setTextColor(ContextCompat.getColor(context, R.color.colorWhite));
  80. txtUsername.setHintTextColor(ContextCompat.getColor(context, R.color.colorWhite));
  81. txtUsername.setBackground(ContextCompat.getDrawable(context, R.drawable.under_line_white));
  82. txtUsername.setCompoundDrawables(img, null, null, null);
  83. } else {
  84. Drawable img = context.getResources().getDrawable( R.drawable.user_gray);
  85. img.setBounds( 0, 0, 60, 60 );
  86. txtUsername.setTextColor(ContextCompat.getColor(context, R.color.colorLightGray));
  87. txtUsername.setBackground(ContextCompat.getDrawable(context, R.drawable.under_line_gray));
  88. txtUsername.setHintTextColor(ContextCompat.getColor(context, R.color.colorLightGray));
  89. txtUsername.setCompoundDrawables(img, null, null, null);
  90. }
  91. }
  92. });
  93. txtPassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
  94. @Override
  95. public void onFocusChange(View v, boolean hasFocus) {
  96. if (hasFocus) {
  97. Drawable img = context.getResources().getDrawable( R.drawable.password_white);
  98. img.setBounds( 0, 0, 60, 60 );
  99. txtPassword.setTextColor(ContextCompat.getColor(context, R.color.colorWhite));
  100. txtPassword.setHintTextColor(ContextCompat.getColor(context, R.color.colorWhite));
  101. txtPassword.setBackground(ContextCompat.getDrawable(context, R.drawable.under_line_white));
  102. txtPassword.setCompoundDrawables(img, null, null, null);
  103. } else {
  104. Drawable img = context.getResources().getDrawable( R.drawable.password_gray);
  105. img.setBounds( 0, 0, 60, 60 );
  106. txtPassword.setTextColor(ContextCompat.getColor(context, R.color.colorLightGray));
  107. txtPassword.setBackground(ContextCompat.getDrawable(context, R.drawable.under_line_gray));
  108. txtPassword.setHintTextColor(ContextCompat.getColor(context, R.color.colorLightGray));
  109. txtPassword.setCompoundDrawables(img, null, null, null);
  110. }
  111. }
  112. });
  113. }
  114.  
  115. private void setViewForgot(){
  116. ForgotPassword forgotPassword = new ForgotPassword();
  117. getFragmentManager().beginTransaction().replace(R.id.switchMainView, forgotPassword, forgotPassword.getTag()).commit();
  118. }
  119.  
  120.  
  121.  
  122. public void login(View v) {
  123. final Context vi = v.getContext();
  124. final Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>() {
  125. @Override
  126. public void onResponse(JSONObject response) {
  127. try {
  128. int id = response.getInt("id");
  129. String nome = response.getString("nome");
  130. String email = response.getString("email");
  131. String telemovel = response.getString("telemovel");
  132.  
  133. UserManager.SetLogin(new Username(id, nome, email, telemovel));
  134. final SharedPreferences sharedPref = vi.getSharedPreferences(LOGIN, Context.MODE_PRIVATE);
  135. SharedPreferences.Editor editor = sharedPref.edit();
  136. editor.putString(USERNAME, txtUsername.getText().toString());
  137. editor.putString(PASSWORD, txtPassword.getText().toString());
  138. editor.apply();
  139.  
  140. } catch (JSONException e) {
  141. e.printStackTrace();
  142. }
  143. }
  144. };
  145.  
  146. final Response.ErrorListener errorListener = new Response.ErrorListener() {
  147. @Override
  148. public void onErrorResponse(VolleyError error) {
  149.  
  150. }
  151. };
  152.  
  153. WebServices.getInstance(v.getContext()).Login(txtUsername.getText().toString(),
  154. txtPassword.getText().toString(), listener, errorListener);
  155.  
  156.  
  157. }
  158.  
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement