This repository was archived by the owner on Nov 29, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathLoginActivity.java
More file actions
130 lines (105 loc) · 4.09 KB
/
LoginActivity.java
File metadata and controls
130 lines (105 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package ro.code4.monitorizarevot;
import android.app.ActivityOptions;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Pair;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.hbb20.CountryCodePicker;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnEditorAction;
import ro.code4.monitorizarevot.viewmodel.LoginViewModel;
import static ro.code4.monitorizarevot.constants.Constants.ORGANISATION_WEB_URL;
public class LoginActivity extends BaseActivity<LoginViewModel> {
@BindView(R.id.country_code)
CountryCodePicker countryCodePicker;
@BindView(R.id.phone)
EditText username;
@BindView(R.id.branch)
EditText password;
@BindView(R.id.app_version)
TextView appVersion;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
appVersion.setText(getString(R.string.app_version, BuildConfig.VERSION_NAME));
viewModel.message().observe(this, new Observer<String>() {
@Override
public void onChanged(@Nullable String message) {
showErrorDialog(message);
}
});
viewModel.loginStatus().observe(this, new Observer<Boolean>() {
@Override
public void onChanged(Boolean status) {
if (status) {
performNavigation();
}
}
});
}
@Override
protected void setupViewModel() {
viewModel = ViewModelProviders.of(this, factory).get(LoginViewModel.class);
}
private void openOrganisationWebpage() {
Intent openBrowser = new Intent(Intent.ACTION_VIEW, Uri.parse(ORGANISATION_WEB_URL));
startActivity(openBrowser);
}
@OnClick(R.id.login_button)
void onLoginButtonClick() {
login();
}
@OnClick(R.id.login_organisation_link)
void onOrganisationLinkClick() {
openOrganisationWebpage();
}
@OnEditorAction(R.id.branch)
boolean onDoneEditPassword(TextView textView, int i, KeyEvent keyEvent) {
if (i == EditorInfo.IME_ACTION_DONE) {
login();
}
return false;
}
private void login() {
String countryCode = countryCodePicker.getSelectedCountryCodeWithPlus();
String phoneNumber = username.getText().toString();
String pin = password.getText().toString();
String udid = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
viewModel.login(countryCode + phoneNumber, pin, udid);
}
private void showErrorDialog(String message) {
if (!TextUtils.isEmpty(message)) {
Toast.makeText(App.getContext(), message, Toast.LENGTH_LONG).show();
}
}
private void performNavigation() {
Intent intent = new Intent(LoginActivity.this, ToolbarActivity.class);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
ActivityOptions options;
Pair<View, String> sharedBackground = new Pair<>(findViewById(R.id.purple_background),
getString(R.string.shared_element_login_background));
Pair<View, String> sharedLogo = new Pair<>(findViewById(R.id.logo), getString(R.string.shared_element_logo));
options = ActivityOptions
.makeSceneTransitionAnimation(LoginActivity.this, sharedBackground, sharedLogo);
startActivity(intent, options.toBundle());
} else {
startActivity(intent);
}
finish(); //TODO finish after transition is complete
}
}