Show Logout button when signed in
This commit is contained in:
parent
d8f85844e6
commit
fd6b1308ce
@ -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) {
|
||||
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
31
app/src/main/java/core/notevault/sync/auth/TokenManager.java
Normal file
31
app/src/main/java/core/notevault/sync/auth/TokenManager.java
Normal file
@ -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();
|
||||
}
|
||||
}
|
@ -15,6 +15,7 @@ public class LoginCallBackImpl implements LoginCallback {
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
Toast.makeText(context, "Login successfull", Toast.LENGTH_SHORT).show();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -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();
|
||||
|
9
app/src/main/res/drawable/logout.xml
Normal file
9
app/src/main/res/drawable/logout.xml
Normal file
@ -0,0 +1,9 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="960"
|
||||
android:viewportHeight="960">
|
||||
<path
|
||||
android:pathData="M200,840q-33,0 -56.5,-23.5T120,760v-560q0,-33 23.5,-56.5T200,120h280v80L200,200v560h280v80L200,840ZM640,680 L585,622 687,520L360,520v-80h327L585,338l55,-58 200,200 -200,200Z"
|
||||
android:fillColor="#e8eaed"/>
|
||||
</vector>
|
@ -4,6 +4,6 @@
|
||||
<item
|
||||
android:id="@+id/action_login"
|
||||
android:title="Login"
|
||||
android:icon="@drawable/login"
|
||||
android:icon="@drawable/logout"
|
||||
app:showAsAction="always"/> <!-- Wichtig: immer in der Toolbar anzeigen -->
|
||||
</menu>
|
||||
|
Loading…
Reference in New Issue
Block a user