Skip to content

Commit

Permalink
Use lambdas, remove extra type arguments (#1969)
Browse files Browse the repository at this point in the history
  • Loading branch information
samtstern authored Jun 30, 2021
1 parent 3401d5f commit bfdb796
Show file tree
Hide file tree
Showing 70 changed files with 980 additions and 1,760 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,48 +57,22 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
handleSignInResult(ErrorCodes.ANONYMOUS_UPGRADE_MERGE_CONFLICT, response);
}

mBinding.anonSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
signInAnonymously();
}
});

mBinding.beginFlow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startAuthUI();
}
});

mBinding.resolveMerge.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
resolveMerge();
}
});

mBinding.signOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
signOut();
}
});
mBinding.anonSignIn.setOnClickListener(view -> signInAnonymously());
mBinding.beginFlow.setOnClickListener(view -> startAuthUI());
mBinding.resolveMerge.setOnClickListener(view -> resolveMerge());
mBinding.signOut.setOnClickListener(view -> signOut());
}

public void signInAnonymously() {
FirebaseAuth.getInstance().signInAnonymously()
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
updateUI();

if (task.isSuccessful()) {
setStatus("Signed in anonymously as user "
+ getUserIdentifier(task.getResult().getUser()));
} else {
setStatus("Anonymous sign in failed.");
}
.addOnCompleteListener(this, task -> {
updateUI();

if (task.isSuccessful()) {
setStatus("Signed in anonymously as user "
+ getUserIdentifier(task.getResult().getUser()));
} else {
setStatus("Anonymous sign in failed.");
}
});
}
Expand All @@ -122,31 +96,25 @@ public void resolveMerge() {
// TODO: Show how to do good data moving

FirebaseAuth.getInstance().signInWithCredential(mPendingCredential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
mPendingCredential = null;
updateUI();

if (task.isSuccessful()) {
setStatus("Signed in as " + getUserIdentifier(task.getResult()
.getUser()));
} else {
Log.w(TAG, "Merge failed", task.getException());
setStatus("Failed to resolve merge conflict, see logs.");
}
.addOnCompleteListener(this, task -> {
mPendingCredential = null;
updateUI();

if (task.isSuccessful()) {
setStatus("Signed in as " + getUserIdentifier(task.getResult()
.getUser()));
} else {
Log.w(TAG, "Merge failed", task.getException());
setStatus("Failed to resolve merge conflict, see logs.");
}
});
}

public void signOut() {
AuthUI.getInstance().signOut(this)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
setStatus(null);
updateUI();
}
.addOnCompleteListener(task -> {
setStatus(null);
updateUI();
});
}

Expand Down
99 changes: 30 additions & 69 deletions app/src/main/java/com/firebase/uidemo/auth/AuthUiActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
setGoogleScopesEnabled(false);
} else {
setGoogleScopesEnabled(mBinding.googleProvider.isChecked());
mBinding.googleProvider.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
setGoogleScopesEnabled(checked);
}
});
mBinding.googleProvider.setOnCheckedChangeListener((compoundButton, checked) -> setGoogleScopesEnabled(checked));
}

if (ConfigurationUtils.isFacebookMisconfigured(this)) {
Expand All @@ -106,76 +101,45 @@ public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
setFacebookPermissionsEnabled(false);
} else {
setFacebookPermissionsEnabled(mBinding.facebookProvider.isChecked());
mBinding.facebookProvider.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
setFacebookPermissionsEnabled(checked);
}
});
}

mBinding.emailLinkProvider.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
flipPasswordProviderCheckbox(isChecked);
}
});
mBinding.facebookProvider.setOnCheckedChangeListener((compoundButton, checked) -> setFacebookPermissionsEnabled(checked));
}

mBinding.emailProvider.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
flipEmailLinkProviderCheckbox(isChecked);
}
});
mBinding.emailLinkProvider.setOnCheckedChangeListener((buttonView, isChecked) -> flipPasswordProviderCheckbox(isChecked));

mBinding.emailProvider.setOnCheckedChangeListener((buttonView, isChecked) -> flipEmailLinkProviderCheckbox(isChecked));

mBinding.emailLinkProvider.setChecked(false);
mBinding.emailProvider.setChecked(true);

// The custom layout in this app only supports Email and Google providers.
mBinding.customLayout.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
if (checked) {
mBinding.googleProvider.setChecked(true);
mBinding.emailProvider.setChecked(true);

mBinding.facebookProvider.setChecked(false);
mBinding.twitterProvider.setChecked(false);
mBinding.emailLinkProvider.setChecked(false);
mBinding.phoneProvider.setChecked(false);
mBinding.anonymousProvider.setChecked(false);
mBinding.microsoftProvider.setChecked(false);
mBinding.yahooProvider.setChecked(false);
mBinding.appleProvider.setChecked(false);
mBinding.githubProvider.setChecked(false);
}
mBinding.customLayout.setOnCheckedChangeListener((compoundButton, checked) -> {
if (checked) {
mBinding.googleProvider.setChecked(true);
mBinding.emailProvider.setChecked(true);

mBinding.facebookProvider.setChecked(false);
mBinding.twitterProvider.setChecked(false);
mBinding.emailLinkProvider.setChecked(false);
mBinding.phoneProvider.setChecked(false);
mBinding.anonymousProvider.setChecked(false);
mBinding.microsoftProvider.setChecked(false);
mBinding.yahooProvider.setChecked(false);
mBinding.appleProvider.setChecked(false);
mBinding.githubProvider.setChecked(false);
}
});

// useEmulator can't be reversed until the FirebaseApp is cleared, so we make this
// checkbox "sticky" until the app is restarted
mBinding.useAuthEmulator.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
mBinding.useAuthEmulator.setEnabled(false);
}
mBinding.useAuthEmulator.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked) {
mBinding.useAuthEmulator.setEnabled(false);
}
});

mBinding.signIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
signIn();
}
});
mBinding.signIn.setOnClickListener(view -> signIn());

mBinding.signInSilent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
silentSignIn();
}
});
mBinding.signInSilent.setOnClickListener(view -> silentSignIn());

if (ConfigurationUtils.isGoogleMisconfigured(this)
|| ConfigurationUtils.isFacebookMisconfigured(this)) {
Expand Down Expand Up @@ -274,14 +238,11 @@ private Intent getSignInIntent(@Nullable String link) {

public void silentSignIn() {
getAuthUI().silentSignIn(this, getSelectedProviders())
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
startSignedInActivity(null);
} else {
showSnackbar(R.string.sign_in_failed);
}
.addOnCompleteListener(this, task -> {
if (task.isSuccessful()) {
startSignedInActivity(null);
} else {
showSnackbar(R.string.sign_in_failed);
}
});
}
Expand Down
53 changes: 16 additions & 37 deletions app/src/main/java/com/firebase/uidemo/auth/SignedInActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,63 +82,42 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
populateProfile(response);
populateIdpToken(response);

mBinding.deleteAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
deleteAccountClicked();
}
});
mBinding.deleteAccount.setOnClickListener(view -> deleteAccountClicked());

mBinding.signOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
signOut();
}
});
mBinding.signOut.setOnClickListener(view -> signOut());
}

public void signOut() {
AuthUI.getInstance()
.signOut(this)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
startActivity(AuthUiActivity.createIntent(SignedInActivity.this));
finish();
} else {
Log.w(TAG, "signOut:failure", task.getException());
showSnackbar(R.string.sign_out_failed);
}
.addOnCompleteListener(task -> {
if (task.isSuccessful()) {
startActivity(AuthUiActivity.createIntent(SignedInActivity.this));
finish();
} else {
Log.w(TAG, "signOut:failure", task.getException());
showSnackbar(R.string.sign_out_failed);
}
});
}

public void deleteAccountClicked() {
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to delete this account?")
.setPositiveButton("Yes, nuke it!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
deleteAccount();
}
})
.setPositiveButton("Yes, nuke it!", (dialogInterface, i) -> deleteAccount())
.setNegativeButton("No", null)
.show();
}

private void deleteAccount() {
AuthUI.getInstance()
.delete(this)
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
startActivity(AuthUiActivity.createIntent(SignedInActivity.this));
finish();
} else {
showSnackbar(R.string.delete_account_failed);
}
.addOnCompleteListener(this, task -> {
if (task.isSuccessful()) {
startActivity(AuthUiActivity.createIntent(SignedInActivity.this));
finish();
} else {
showSnackbar(R.string.delete_account_failed);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,34 +65,16 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
mBinding.messagesList.setHasFixedSize(true);
mBinding.messagesList.setLayoutManager(manager);

mBinding.messagesList.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View view, int left, int top, int right, int bottom,
int oldLeft, int oldTop, int oldRight, int oldBottom) {
if (bottom < oldBottom) {
mBinding.messagesList.postDelayed(new Runnable() {
@Override
public void run() {
mBinding.messagesList.smoothScrollToPosition(0);
}
}, 100);
}
mBinding.messagesList.addOnLayoutChangeListener((view, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
if (bottom < oldBottom) {
mBinding.messagesList.postDelayed(() -> mBinding.messagesList.smoothScrollToPosition(
0), 100);
}
});

ImeHelper.setImeOnDoneListener(mBinding.messageEdit, new ImeHelper.DonePressedListener() {
@Override
public void onDonePressed() {
onSendClick();
}
});
ImeHelper.setImeOnDoneListener(mBinding.messageEdit, () -> onSendClick());

mBinding.sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onSendClick();
}
});
mBinding.sendButton.setOnClickListener(view -> onSendClick());
}

@Override
Expand Down Expand Up @@ -180,11 +162,7 @@ public void onDataChanged() {
}

private void onAddMessage(@NonNull Chat chat) {
sChatCollection.add(chat).addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e(TAG, "Failed to write message", e);
}
});
sChatCollection.add(chat).addOnFailureListener(this,
e -> Log.e(TAG, "Failed to write message", e));
}
}
Loading

0 comments on commit bfdb796

Please sign in to comment.