LoginActivity.java 2.8 KB
Newer Older
A
amitshekhariitbhu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 *  Copyright (C) 2017 MINDORKS NEXTGEN PRIVATE LIMITED
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      https://mindorks.com/license/apache-v2
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License
 */

package com.mindorks.framework.mvvm.ui.login;

19
import android.arch.lifecycle.ViewModelProviders;
L
Lam Tran 已提交
20 21 22 23
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
24
import com.mindorks.framework.mvvm.BR;
A
amitshekhariitbhu 已提交
25
import com.mindorks.framework.mvvm.R;
26
import com.mindorks.framework.mvvm.ViewModelProviderFactory;
A
amitshekhariitbhu 已提交
27 28
import com.mindorks.framework.mvvm.databinding.ActivityLoginBinding;
import com.mindorks.framework.mvvm.ui.base.BaseActivity;
29
import com.mindorks.framework.mvvm.ui.main.MainActivity;
A
amitshekhariitbhu 已提交
30 31 32 33 34 35
import javax.inject.Inject;

/**
 * Created by amitshekhar on 08/07/17.
 */

36 37
public class LoginActivity extends BaseActivity<ActivityLoginBinding, LoginViewModel> implements
    LoginNavigator {
A
amitshekhariitbhu 已提交
38

39 40
  @Inject
  ViewModelProviderFactory factory;
J
Jyoti Dubey 已提交
41
  private LoginViewModel mLoginViewModel;
42
  private ActivityLoginBinding mActivityLoginBinding;
L
Lam Tran 已提交
43

44 45 46
  public static Intent newIntent(Context context) {
    return new Intent(context, LoginActivity.class);
  }
L
Lam Tran 已提交
47

48 49 50 51
  @Override
  public int getBindingVariable() {
    return BR.viewModel;
  }
L
Lam Tran 已提交
52

53 54 55 56
  @Override
  public int getLayoutId() {
    return R.layout.activity_login;
  }
L
Lam Tran 已提交
57

58 59 60 61 62
  @Override
  public LoginViewModel getViewModel() {
    mLoginViewModel = ViewModelProviders.of(this, factory).get(LoginViewModel.class);
    return mLoginViewModel;
  }
L
Lam Tran 已提交
63

64 65 66 67
  @Override
  public void handleError(Throwable throwable) {
    // handle error
  }
L
Lam Tran 已提交
68

69 70 71 72 73 74 75 76 77
  @Override
  public void login() {
    String email = mActivityLoginBinding.etEmail.getText().toString();
    String password = mActivityLoginBinding.etPassword.getText().toString();
    if (mLoginViewModel.isEmailAndPasswordValid(email, password)) {
      hideKeyboard();
      mLoginViewModel.login(email, password);
    } else {
      Toast.makeText(this, getString(R.string.invalid_email_password), Toast.LENGTH_SHORT).show();
A
amitshekhariitbhu 已提交
78
    }
79
  }
L
Lam Tran 已提交
80

81 82 83 84 85 86
  @Override
  public void openMainActivity() {
    Intent intent = MainActivity.newIntent(LoginActivity.this);
    startActivity(intent);
    finish();
  }
L
Lam Tran 已提交
87

88 89 90 91 92 93
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mActivityLoginBinding = getViewDataBinding();
    mLoginViewModel.setNavigator(this);
  }
A
amitshekhariitbhu 已提交
94
}