Skip to content

Instantly share code, notes, and snippets.

@marcusedu
Forked from anonymous/LoginActivity.java
Last active April 18, 2017 16:56
Show Gist options
  • Save marcusedu/ae7536f958538a7ac50a4798b05d21c6 to your computer and use it in GitHub Desktop.
Save marcusedu/ae7536f958538a7ac50a4798b05d21c6 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="contract.manager.login.LoginActivity">
<FrameLayout
android:id="@+id/login_conteiner"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="contract.manager.login.LoginFragmentView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="32dp">
<ImageView
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_gravity="center"
android:layout_marginBottom="32dp"
android:src="@mipmap/contrato" />
<android.support.design.widget.TextInputLayout
android:id="@+id/login_til_email"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/login_til_senha"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Senha (6 a 8 digitos)"
android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout>
<TextView
android:id="@+id/login_tv_esqueceu_senha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:clickable="true"
android:padding="8dp"
android:text="esqueceu a senha?"
android:textColor="@color/alerta"
android:visibility="invisible" />
<android.support.v7.widget.AppCompatButton
android:id="@+id/login_bt_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login" />
</LinearLayout>
<ProgressBar
android:id="@+id/login_pb_carregando"
style="?android:attr/progressBarStyle"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="right"
android:visibility="gone"
android:indeterminate="true"
android:layout_margin="8dp" />
</FrameLayout>
package contract.manager.login;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import contract.manager.R;
public class LoginActivity extends AppCompatActivity {
private Contracts.Presenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
LoginFragmentView view = LoginFragmentView.newInstance();
presenter = new LoginPresenter(view);
getSupportFragmentManager().beginTransaction().add(R.id.login_conteiner, view, "login").commit();
}
@Override
protected void onStart() {
super.onStart();
presenter.start();
}
@Override
protected void onStop() {
super.onStop();
presenter.stop();
}
}
package contract.manager.login;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.auth.FirebaseAuth;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import contract.manager.R;
public class LoginFragmentView extends Fragment implements Contracts.View {
private static final String TAG = "LoginFragmentView";
@BindView(R.id.login_til_email)
TextInputLayout email;
@BindView(R.id.login_til_senha)
TextInputLayout senha;
@BindView(R.id.login_tv_esqueceu_senha)
TextView esqueceu;
@BindView(R.id.login_pb_carregando)
ProgressBar carregando;
@BindView(R.id.login_bt_login)
Button login;
private Contracts.Presenter presenter;
public LoginFragmentView() {
}
public static LoginFragmentView newInstance() {
LoginFragmentView fragment = new LoginFragmentView();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_login_fragment_view, container, false);
ButterKnife.bind(this, v);
return v;
}
@Override
public void setPresenter(Contracts.Presenter presenter) {
this.presenter = presenter;
}
@OnClick(R.id.login_bt_login)
void login() {
limparErros();
presenter.autenticarUsuario(
email.getEditText().getText().toString(),
senha.getEditText().getText().toString());
}
@Override
public void mostrarEmailErro(boolean mostrar) {
if (mostrar) {
email.setError("Email inválido");
} else {
email.setErrorEnabled(false);
}
}
@Override
public void mostrarSenhaErro(boolean mostrar) {
if (mostrar) {
senha.setError("Senha inválida");
} else {
senha.setErrorEnabled(false);
}
}
@Override
public void mostrarEmailNaoCadastradoErro(boolean mostrar) {
if (mostrar) {
email.setError("Email não está cadastrado");
} else {
email.setErrorEnabled(false);
}
}
@Override
public void mostrarHorarioErro(boolean mostrar) {
SnackErro("Fora do horário de trabalho.");
}
@Override
public void mostrarInativoErro(boolean mostrar) {
SnackErro("Usuario inativo.");
}
private void SnackErro(String msg) {
if (getView() == null) return;
Snackbar.make(getView(), msg, Snackbar.LENGTH_INDEFINITE).setAction("Fechar", new View.OnClickListener() {
@Override
public void onClick(View v) {
getActivity().finish();
}
}).show();
}
@Override
public void mostrarCarregando(boolean mostrar) {
carregando.setVisibility(mostrar ? View.VISIBLE : View.GONE);
login.setEnabled(!mostrar);
email.setEnabled(!mostrar);
senha.setEnabled(!mostrar);
}
@Override
public void mostrarEsqueceuSenha(boolean mostrar) {
senha.setError("Confira sua senha e tente novamente.");
esqueceu.setVisibility(mostrar ? View.VISIBLE : View.INVISIBLE);
}
@Override
public void mostrarTelaPrincipal() {
// startActivity(new Intent(getContext(),PrincipalActivity.class));
Toast.makeText(getContext(), "Logado com: " + FirebaseAuth.getInstance().getCurrentUser().getEmail(), Toast.LENGTH_SHORT).show();
}
private void limparErros() {
TextInputLayout inputLayout[] = {email, senha};
for (TextInputLayout inp : inputLayout) {
inp.setErrorEnabled(false);
}
esqueceu.setVisibility(View.INVISIBLE);
login.setEnabled(true);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="contract.manager">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".splash.SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".login.LoginActivity">
<intent-filter>
<action android:name="android.intent.action.QUICK_VIEW" />
</intent-filter>
</activity>
<activity
android:name=".principal.MainActivity"
android:label="@string/title_activity_main"
android:theme="@style/AppTheme.NoActionBar"></activity>
</application>
</manifest>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment