Last active
January 13, 2017 17:53
-
-
Save falvojr/b220e941aed04d3f324ba9ccaff04625 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AuthActivity extends BaseActivity implements AuthContract.View { | |
@BindView(R.id.ivGitHubStatus) ImageView mImgStatusImage; | |
@BindView(R.id.tvGitHubStatus) TextView mLblStatusText; | |
@BindView(R.id.tilUsername) TextInputLayout mWrapperTxtUsername; | |
@BindView(R.id.tilPassword) TextInputLayout mWrapperTxtPassword; | |
@BindView(R.id.btOAuth) Button mBtnOAuth; | |
@Inject @Named("secret") SharedPreferences mSharedPrefs; | |
@Inject AppHelper mAppHelper; //TODO Dagger! | |
@Inject AuthContract.Presenter mPresenter; //TODO Dagger! | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_auth); | |
ButterKnife.bind(this); | |
super.getDaggerDiComponent().inject(this); | |
mPresenter.setView(this); | |
this.bindUsingRx(); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
// Restore default GitHub fields status | |
onLoadStatusType(Status.Type.NONE); | |
// Get last status from GitHub Status API | |
mPresenter.loadStatus(); | |
// Process (if necessary) OAUth redirect | |
this.processOAuthRedirectUri(); | |
} | |
@Override | |
public void onLoadStatusType(Status.Type statusType) { | |
int color = ContextCompat.getColor(AuthActivity.this, statusType.getColorRes()); | |
DrawableCompat.setTint(mImgStatusImage.getDrawable(), color); | |
mLblStatusText.setTextColor(color); | |
mLblStatusText.setText(getString(statusType.getMessageRes())); | |
} | |
@Override | |
public void onAuthSuccess(String credential, User entity) { | |
final String authCredentialKey = getString(R.string.sp_auth_credential_key); | |
mSharedPrefs.edit().putString(authCredentialKey, credential).apply(); | |
//TODO Redirect to home activity | |
Snackbar.make(mImgStatusImage, credential, Snackbar.LENGTH_LONG).show(); | |
} | |
@Override | |
public void showError(String message) { | |
Snackbar.make(mImgStatusImage, message, Snackbar.LENGTH_LONG).show(); | |
} | |
@OnClick(R.id.btBasicAuth) | |
public void onBasicAuthClick(final View view) { | |
if (mAppHelper.validateRequiredFields(mWrapperTxtUsername, mWrapperTxtPassword)) { | |
final String username = mWrapperTxtUsername.getEditText().getText().toString(); | |
final String password = mWrapperTxtPassword.getEditText().getText().toString(); | |
final String authCredential = Credentials.basic(username, password); | |
mPresenter.callGetUser(authCredential); | |
} | |
} | |
private void bindUsingRx() { | |
RxView.clicks(mBtnOAuth).subscribe(aVoid -> { | |
final String baseUrl = GitHubOAuthService.BASE_URL + "authorize"; | |
final String clientId = getString(R.string.oauth_client_id); | |
final String redirectUri = getOAuthRedirectUri(); | |
final Uri uri = Uri.parse(baseUrl + "?client_id=" + clientId + "&redirect_uri=" + redirectUri); | |
Intent intent = new Intent(Intent.ACTION_VIEW, uri); | |
startActivity(intent); | |
}); | |
if (mWrapperTxtUsername.getEditText() != null && mWrapperTxtPassword.getEditText() != null) { | |
RxTextView.textChanges(mWrapperTxtUsername.getEditText()) | |
.skip(1) | |
.subscribe(text -> { | |
mAppHelper.validateRequiredFields(mWrapperTxtUsername); | |
}); | |
RxTextView.textChanges(mWrapperTxtPassword.getEditText()) | |
.skip(1) | |
.subscribe(text -> { | |
mAppHelper.validateRequiredFields(mWrapperTxtPassword); | |
}); | |
} | |
} | |
private void processOAuthRedirectUri() { | |
// the intent filter defined in AndroidManifest will handle the return from ACTION_VIEW intent | |
final Uri uri = getIntent().getData(); | |
if (uri != null && uri.toString().startsWith(this.getOAuthRedirectUri())) { | |
// use the parameter your API exposes for the code (mostly it's "code") | |
String code = uri.getQueryParameter("code"); | |
if (code != null) { | |
// get access token | |
final String clientId = getString(R.string.oauth_client_id); | |
final String clientSecret = getString(R.string.oauth_client_secret); | |
mPresenter.callAccessToken(clientId, clientSecret, code); | |
} else if (uri.getQueryParameter("error") != null) { | |
showError(uri.getQueryParameter("error")); | |
} | |
// Clear Intent Data preventing multiple calls | |
getIntent().setData(null); | |
} | |
} | |
@NonNull | |
private String getOAuthRedirectUri() { | |
return getString(R.string.oauth_schema) + "://" + getString(R.string.oauth_host); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getMenuInflater().inflate(R.menu.main_menu, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
switch (item.getItemId()) { | |
case R.id.icCallHelpdesk: | |
final String phone = "+55 16 99999-9999"; | |
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phone, null)); | |
startActivity(intent); | |
return true; | |
default: | |
return super.onOptionsItemSelected(item); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment