Commit 7b9c93e2 authored by Willard's avatar Willard

Remove AsyncTask from login, add proper messages

parent e4ac1aeb
......@@ -27,8 +27,6 @@ import retrofit2.Response;
public class LoginActivity extends AppCompatActivity {
private UserLoginTask mAuthTask = null;
private AutoCompleteTextView mUsernameView;
private EditText mPasswordView;
private View mProgressView;
......@@ -92,10 +90,6 @@ public class LoginActivity extends AppCompatActivity {
}
private void attemptLogin() {
if (mAuthTask != null) {
return;
}
mUsernameView.setError(null);
mPasswordView.setError(null);
......@@ -121,14 +115,34 @@ public class LoginActivity extends AppCompatActivity {
focusView.requestFocus();
} else {
showProgress(true);
mAuthTask = new UserLoginTask(username, password);
mAuthTask.execute((Void) null);
Intent intent = new Intent(this, NavDrawerActivity.class);
AppUtils.username = username;
AppUtils.password = password;
Call<Token> call = AppUtils.service.getToken();
call.enqueue(new Callback<Token>() {
@Override
public void onResponse(Call<Token> call, Response<Token> response) {
showProgress(false);
if(response.code() == 401) {
mUsernameView.setError("");
mPasswordView.setError("");
Toast.makeText(LoginActivity.this, "Invalid credentials!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(LoginActivity.this, "Logged in as " + AppUtils.username + " successfully.", Toast.LENGTH_LONG).show();
Intent intent = new Intent(LoginActivity.this, NavDrawerActivity.class);
startActivity(intent);
}
}
@Override
public void onFailure(Call<Token> call, Throwable t) {
showProgress(false);
Toast.makeText(LoginActivity.this, "Server failure, please try again. Message: " + t.getMessage(), Toast.LENGTH_LONG);
}
});
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
private void showProgress(final boolean show) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
......@@ -156,50 +170,4 @@ public class LoginActivity extends AppCompatActivity {
mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
}
}
public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
private final String mUsername;
private final String mPassword;
UserLoginTask(String username, String password) {
mUsername = username;
mPassword = password;
}
@Override
protected Boolean doInBackground(Void... params) {
AppUtils.username = mUsername;
AppUtils.password = mPassword;
Call<Token> call = AppUtils.service.getToken();
call.enqueue(new Callback<Token>() {
@Override
public void onResponse(Call<Token> call, Response<Token> response) {
Toast.makeText(LoginActivity.this, response.body().getToken(), Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Call<Token> call, Throwable t) {
}
});
return true;
}
@Override
protected void onPostExecute(final Boolean success) {
mAuthTask = null;
showProgress(false);
if (success) {
finish();
}
}
@Override
protected void onCancelled() {
mAuthTask = null;
showProgress(false);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment