Skip to content

Commit

Permalink
added logout button to redirect back to login
Browse files Browse the repository at this point in the history
  • Loading branch information
rrhzhang committed Apr 20, 2024
1 parent deb18c6 commit cea13ad
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,54 +1,62 @@
package com.example.spotifywrapped2340;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class SettingsActivity extends AppCompatActivity {

EditText newPasswordEditText, confirmNewPasswordEditText;
Button updateButton, backButton, deleteAccountButton;
private EditText newPasswordEditText, confirmNewPasswordEditText;
private Button updateButton, backButton, deleteAccountButton, logoutButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);

initializeViews();
setupButtonListeners();
}

private void initializeViews() {
newPasswordEditText = findViewById(R.id.update_password);
confirmNewPasswordEditText = findViewById(R.id.confirm_new_password);
updateButton = findViewById(R.id.update_button);
backButton = findViewById(R.id.back_button);
deleteAccountButton = findViewById(R.id.delete_account_button);
logoutButton = findViewById(R.id.logout_button);
}

private void setupButtonListeners() {
backButton.setOnClickListener(v -> finish());

updateButton.setOnClickListener(v -> {
String newPassword = newPasswordEditText.getText().toString().trim();
String confirmNewPassword = confirmNewPasswordEditText.getText().toString().trim();

if (!newPassword.equals(confirmNewPassword)) {
Toast.makeText(SettingsActivity.this, "Passwords do not match", Toast.LENGTH_SHORT).show();
} else if (newPassword.length() < 1) {
Toast.makeText(SettingsActivity.this, "Enter a password!", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this, "Passwords do not match", Toast.LENGTH_SHORT).show();
} else if (newPassword.isEmpty()) {
Toast.makeText(this, "Enter a password!", Toast.LENGTH_SHORT).show();
} else {
updatePassword(newPassword);
}
});

deleteAccountButton.setOnClickListener(v -> showDeleteConfirmationDialog());

logoutButton.setOnClickListener(v -> logoutUser());
}

private void updatePassword(String newPassword) {
Expand All @@ -72,29 +80,43 @@ private void showDeleteConfirmationDialog() {
.setTitle("Confirm Deletion")
.setMessage("Are you sure you want to delete your account?")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
deleteAccount();
}})
.setNegativeButton(android.R.string.no, null).show();
.setPositiveButton(android.R.string.yes, (dialog, which) -> deleteAccount())
.setNegativeButton(android.R.string.no, null)
.show();
}

private void deleteAccount() {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
user.delete().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(SettingsActivity.this, "Account deleted successfully", Toast.LENGTH_SHORT).show();
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(SettingsActivity.this, LoginActivity.class));
finish();
} else {
Toast.makeText(SettingsActivity.this, "Failed to delete account", Toast.LENGTH_LONG).show();
}
user.delete().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
Toast.makeText(SettingsActivity.this, "Account deleted successfully", Toast.LENGTH_SHORT).show();
logoutUser();
} else {
Toast.makeText(SettingsActivity.this, "Failed to delete account", Toast.LENGTH_LONG).show();
}
});
}
}

private void logoutUser() {
clearRememberMePreferences();
FirebaseAuth.getInstance().signOut();
navigateToLoginActivity("Logged out successfully");
}

private void clearRememberMePreferences() {
SharedPreferences preferences = getSharedPreferences("checkbox", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("remember", "false");
editor.apply();
}

private void navigateToLoginActivity(String message) {
Toast.makeText(SettingsActivity.this, message, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(SettingsActivity.this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
}
10 changes: 10 additions & 0 deletions app/src/main/res/layout/activity_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@
android:textColor="@color/spotifyGreen"
android:backgroundTint="@color/white"
app:cornerRadius="10dp"/>
<Button
android:id="@+id/logout_button"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="@string/logout"
android:textColor="@color/black"
android:backgroundTint="@color/white"
app:cornerRadius="10dp" />
<Button
android:id="@+id/delete_account_button"
android:layout_width="300dp"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
<string name="update_information">Update Information</string>
<string name="go_back">Go Back</string>
<string name="waiting_for_llm_api_result">Waiting for LLM API result...</string>
<string name="logout">Logout</string>
</resources>

0 comments on commit cea13ad

Please sign in to comment.