Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.tm_ma.boleias.Fragments;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.graphics.drawable.Drawable;
- import android.os.Bundle;
- import android.app.Fragment;
- import android.support.v4.content.ContextCompat;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import com.android.volley.Response;
- import com.android.volley.VolleyError;
- import com.example.tm_ma.boleias.Managers.UserManager;
- import com.example.tm_ma.boleias.Models.Username;
- import com.example.tm_ma.boleias.R;
- import com.example.tm_ma.boleias.WebServices.WebServices;
- import org.json.JSONException;
- import org.json.JSONObject;
- /**
- * A simple {@link Fragment} subclass.
- */
- public class Login extends Fragment {
- private EditText txtUsername;
- private EditText txtPassword;
- private Button btnLogin;
- private TextView lblForgot;
- private Context context;
- private String username,password;
- private final String LOGIN = "login", USERNAME = "username", PASSWORD = "password";
- public Login() {
- // Required empty public constructor
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // Inflate the layout for this fragment
- View v = inflater.inflate(R.layout.fragment_login, container, false);
- init(v);
- return v;
- }
- private void init(View v){
- final SharedPreferences sharedPref = v.getContext().getSharedPreferences(LOGIN, Context.MODE_PRIVATE);
- username = sharedPref.getString(USERNAME, "");
- password = sharedPref.getString(PASSWORD, "");
- context = v.getContext();
- txtUsername = v.findViewById(R.id.txtUsername);
- txtPassword = v.findViewById(R.id.txtPassword);
- btnLogin = v.findViewById(R.id.btnLogin);
- lblForgot = v.findViewById(R.id.lblForgot);
- lblForgot.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- setViewForgot();
- }
- });
- txtUsername.setOnFocusChangeListener(new View.OnFocusChangeListener() {
- @Override
- public void onFocusChange(View v, boolean hasFocus) {
- if (hasFocus) {
- Drawable img = context.getResources().getDrawable( R.drawable.user_white);
- img.setBounds( 0, 0, 60, 60 );
- txtUsername.setTextColor(ContextCompat.getColor(context, R.color.colorWhite));
- txtUsername.setHintTextColor(ContextCompat.getColor(context, R.color.colorWhite));
- txtUsername.setBackground(ContextCompat.getDrawable(context, R.drawable.under_line_white));
- txtUsername.setCompoundDrawables(img, null, null, null);
- } else {
- Drawable img = context.getResources().getDrawable( R.drawable.user_gray);
- img.setBounds( 0, 0, 60, 60 );
- txtUsername.setTextColor(ContextCompat.getColor(context, R.color.colorLightGray));
- txtUsername.setBackground(ContextCompat.getDrawable(context, R.drawable.under_line_gray));
- txtUsername.setHintTextColor(ContextCompat.getColor(context, R.color.colorLightGray));
- txtUsername.setCompoundDrawables(img, null, null, null);
- }
- }
- });
- txtPassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
- @Override
- public void onFocusChange(View v, boolean hasFocus) {
- if (hasFocus) {
- Drawable img = context.getResources().getDrawable( R.drawable.password_white);
- img.setBounds( 0, 0, 60, 60 );
- txtPassword.setTextColor(ContextCompat.getColor(context, R.color.colorWhite));
- txtPassword.setHintTextColor(ContextCompat.getColor(context, R.color.colorWhite));
- txtPassword.setBackground(ContextCompat.getDrawable(context, R.drawable.under_line_white));
- txtPassword.setCompoundDrawables(img, null, null, null);
- } else {
- Drawable img = context.getResources().getDrawable( R.drawable.password_gray);
- img.setBounds( 0, 0, 60, 60 );
- txtPassword.setTextColor(ContextCompat.getColor(context, R.color.colorLightGray));
- txtPassword.setBackground(ContextCompat.getDrawable(context, R.drawable.under_line_gray));
- txtPassword.setHintTextColor(ContextCompat.getColor(context, R.color.colorLightGray));
- txtPassword.setCompoundDrawables(img, null, null, null);
- }
- }
- });
- }
- private void setViewForgot(){
- ForgotPassword forgotPassword = new ForgotPassword();
- getFragmentManager().beginTransaction().replace(R.id.switchMainView, forgotPassword, forgotPassword.getTag()).commit();
- }
- public void login(View v) {
- final Context vi = v.getContext();
- final Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>() {
- @Override
- public void onResponse(JSONObject response) {
- try {
- int id = response.getInt("id");
- String nome = response.getString("nome");
- String email = response.getString("email");
- String telemovel = response.getString("telemovel");
- UserManager.SetLogin(new Username(id, nome, email, telemovel));
- final SharedPreferences sharedPref = vi.getSharedPreferences(LOGIN, Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = sharedPref.edit();
- editor.putString(USERNAME, txtUsername.getText().toString());
- editor.putString(PASSWORD, txtPassword.getText().toString());
- editor.apply();
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- };
- final Response.ErrorListener errorListener = new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- }
- };
- WebServices.getInstance(v.getContext()).Login(txtUsername.getText().toString(),
- txtPassword.getText().toString(), listener, errorListener);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement