From fd6b1308ceb2c42cf6ac3650c07eadc318827ba9 Mon Sep 17 00:00:00 2001 From: sebastian Date: Sun, 3 Nov 2024 19:46:20 +0100 Subject: [PATCH] Show Logout button when signed in --- .../java/core/notevault/MainActivity.java | 63 ++++++++++++++++++- .../notevault/sync/auth/AuthRepository.java | 17 +++-- .../notevault/sync/auth/TokenManager.java | 31 +++++++++ .../notevault/ui/login/LoginCallBackImpl.java | 1 + .../ui/login/LoginDialogFragment.java | 17 ++++- app/src/main/res/drawable/logout.xml | 9 +++ app/src/main/res/menu/main.xml | 2 +- 7 files changed, 131 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/core/notevault/sync/auth/TokenManager.java create mode 100644 app/src/main/res/drawable/logout.xml diff --git a/app/src/main/java/core/notevault/MainActivity.java b/app/src/main/java/core/notevault/MainActivity.java index e87c87a..ef687eb 100644 --- a/app/src/main/java/core/notevault/MainActivity.java +++ b/app/src/main/java/core/notevault/MainActivity.java @@ -3,6 +3,7 @@ package core.notevault; import android.content.Intent; import android.net.Uri; import android.os.Bundle; +import android.text.TextUtils; import android.util.Log; import android.view.MenuItem; import android.view.View; @@ -21,6 +22,7 @@ import androidx.drawerlayout.widget.DrawerLayout; import androidx.appcompat.app.AppCompatActivity; import core.notevault.data.*; import core.notevault.databinding.ActivityMainBinding; +import core.notevault.sync.auth.AuthRepository; import core.notevault.sync.auth.LoginCallback; import core.notevault.ui.gallery.GalleryFragment; import core.notevault.ui.gallery.detail.ConcertSongSelector; @@ -37,7 +39,7 @@ import java.util.Date; import java.util.List; public class MainActivity extends AppCompatActivity implements MetaDataDialog.OnMetadataListener, - ConcertEditorDialog.OnConcertEditorListener, ConcertSongSelector.OnSongSelectedListener { + ConcertEditorDialog.OnConcertEditorListener, ConcertSongSelector.OnSongSelectedListener, LoginCallback { private AppBarConfiguration mAppBarConfiguration; private ActivityMainBinding binding; @@ -64,9 +66,59 @@ public class MainActivity extends AppCompatActivity implements MetaDataDialog.On NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration); NavigationUI.setupWithNavController(navigationView, navController); + setupLoginButton(); musicDB = MusicDatabase.getDatabase(this); } + @Override + public boolean onPrepareOptionsMenu(Menu menu) { + MenuItem loginItem = menu.findItem(R.id.action_login); + if (isLoggedIn()) { + loginItem.setIcon(R.drawable.logout); // Setze das Logout-Symbol + } else { + loginItem.setIcon(R.drawable.login); // Setze das Login-Symbol + } + return super.onPrepareOptionsMenu(menu); + } + + public void updateLoginButton() { + invalidateOptionsMenu(); // Menü neu zeichnen + } + + private void setupLoginButton() { + binding.appBarMain.toolbar.setOnMenuItemClickListener(item -> { + if (item.getItemId() == R.id.action_login) { + if (isLoggedIn()) { + performLogout(); // Logout-Logik aufrufen + } else { + showLoginDialog(); // Zeige das Login-Fenster an + } + return true; + } + return false; + }); + } + + private boolean isLoggedIn() { + AuthRepository authRepository = new AuthRepository(this); + return !TextUtils.isEmpty(authRepository.getToken()); + } + + private void showLoginDialog() { + LoginDialogFragment loginDialogFragment = new LoginDialogFragment(); + loginDialogFragment.show(getSupportFragmentManager(), "login"); + } + + private void performLogout() { + // Füge hier deine Logout-Logik hinzu + AuthRepository authRepository = new AuthRepository(this); + + authRepository.logout(); // Implementiere die Token-Löschung + updateLoginButton(); + } + + + @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. @@ -198,4 +250,13 @@ public class MainActivity extends AppCompatActivity implements MetaDataDialog.On } + @Override + public void onSuccess() { + updateLoginButton(); + } + + @Override + public void onError(String error) { + + } } \ No newline at end of file diff --git a/app/src/main/java/core/notevault/sync/auth/AuthRepository.java b/app/src/main/java/core/notevault/sync/auth/AuthRepository.java index a7036cb..0f9861e 100644 --- a/app/src/main/java/core/notevault/sync/auth/AuthRepository.java +++ b/app/src/main/java/core/notevault/sync/auth/AuthRepository.java @@ -9,11 +9,11 @@ import retrofit2.Response; public class AuthRepository { private final AuthService authService; - private final SharedPreferences preferences; + private final TokenManager tokenManager; public AuthRepository(Context context) { this.authService = ApiClient.getRetrofitInstance(context).create(AuthService.class); - this.preferences = context.getSharedPreferences("app_prefs", Context.MODE_PRIVATE); + this.tokenManager = new TokenManager(context); } public void performLogin(String username, String password, LoginCallback callback) { @@ -25,7 +25,6 @@ public class AuthRepository { if (response.isSuccessful() && response.body() != null) { String token = response.body().getToken(); saveToken(token); - // Erfolgsrückmeldung an den Callback senden callback.onSuccess(); } else { @@ -43,8 +42,14 @@ public class AuthRepository { } private void saveToken(String token) { - SharedPreferences.Editor editor = preferences.edit(); - editor.putString("jwt_token", token); - editor.apply(); + tokenManager.saveToken(token); + } + + public String getToken() { + return tokenManager.getToken(); + } + + public void logout() { + tokenManager.clearToken(); } } diff --git a/app/src/main/java/core/notevault/sync/auth/TokenManager.java b/app/src/main/java/core/notevault/sync/auth/TokenManager.java new file mode 100644 index 0000000..98951f2 --- /dev/null +++ b/app/src/main/java/core/notevault/sync/auth/TokenManager.java @@ -0,0 +1,31 @@ +package core.notevault.sync.auth; + +import android.content.Context; +import android.content.SharedPreferences; + +public class TokenManager { + private static final String PREF_NAME = "app_preferences"; + private static final String KEY_TOKEN = "jwt_token"; + + private SharedPreferences sharedPreferences; + + public TokenManager(Context context) { + sharedPreferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); + } + + public void saveToken(String token) { + SharedPreferences.Editor editor = sharedPreferences.edit(); + editor.putString(KEY_TOKEN, token); + editor.apply(); + } + + public String getToken() { + return sharedPreferences.getString(KEY_TOKEN, null); + } + + public void clearToken() { + SharedPreferences.Editor editor = sharedPreferences.edit(); + editor.remove(KEY_TOKEN); + editor.apply(); + } +} diff --git a/app/src/main/java/core/notevault/ui/login/LoginCallBackImpl.java b/app/src/main/java/core/notevault/ui/login/LoginCallBackImpl.java index 4c1c0e0..c777682 100644 --- a/app/src/main/java/core/notevault/ui/login/LoginCallBackImpl.java +++ b/app/src/main/java/core/notevault/ui/login/LoginCallBackImpl.java @@ -15,6 +15,7 @@ public class LoginCallBackImpl implements LoginCallback { @Override public void onSuccess() { Toast.makeText(context, "Login successfull", Toast.LENGTH_SHORT).show(); + } @Override diff --git a/app/src/main/java/core/notevault/ui/login/LoginDialogFragment.java b/app/src/main/java/core/notevault/ui/login/LoginDialogFragment.java index 07ed33d..0fcda36 100644 --- a/app/src/main/java/core/notevault/ui/login/LoginDialogFragment.java +++ b/app/src/main/java/core/notevault/ui/login/LoginDialogFragment.java @@ -2,6 +2,7 @@ package core.notevault.ui.login; import android.app.AlertDialog; import android.app.Dialog; +import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -11,8 +12,22 @@ import androidx.annotation.Nullable; import androidx.fragment.app.DialogFragment; import core.notevault.R; import core.notevault.sync.auth.AuthRepository; +import core.notevault.sync.auth.LoginCallback; +import retrofit2.Call; +import retrofit2.Callback; public class LoginDialogFragment extends DialogFragment { + private LoginCallback loginCallback; + + @Override + public void onAttach(Context context) { + super.onAttach(context); + if (context instanceof LoginCallback) { + loginCallback = (LoginCallback) context; + } else { + throw new RuntimeException(context.toString() + " must implement LoginDialogListener"); + } + } @NonNull @Override @@ -31,7 +46,7 @@ public class LoginDialogFragment extends DialogFragment { String username = usernameInput.getText().toString(); String password = passwordInput.getText().toString(); - authRepository.performLogin(username, password, new LoginCallBackImpl(this.getContext())); + authRepository.performLogin(username, password, loginCallback); } ) .setNegativeButton("Abbrechen", (dialog, which) -> {} ) .create(); diff --git a/app/src/main/res/drawable/logout.xml b/app/src/main/res/drawable/logout.xml new file mode 100644 index 0000000..d1d6253 --- /dev/null +++ b/app/src/main/res/drawable/logout.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/menu/main.xml b/app/src/main/res/menu/main.xml index 4c6e013..eb9f353 100644 --- a/app/src/main/res/menu/main.xml +++ b/app/src/main/res/menu/main.xml @@ -4,6 +4,6 @@