Optimizacion para mantener session
This commit is contained in:
parent
ecfad9f62f
commit
5edbf539bd
|
|
@ -66,7 +66,9 @@ public class LoginFragment extends Fragment {
|
|||
@Override
|
||||
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
|
||||
if (response.isSuccessful() && response.body() != null && response.body().isSuccess()) {
|
||||
saveUserSession(response.body().getUser().getId(), response.body().getUser().getUsername());
|
||||
int userId = response.body().getUser().getId();
|
||||
String username = response.body().getUser().getUsername();
|
||||
saveUserSession(userId, username, false);
|
||||
showToast("Inicio de sesión exitoso");
|
||||
goToMainActivity();
|
||||
} else {
|
||||
|
|
@ -80,14 +82,18 @@ public class LoginFragment extends Fragment {
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void loginAsGuest() {
|
||||
saveUserSession(-1, "Invitado");
|
||||
saveUserSession(-1, "Invitado", true);
|
||||
showToast("Sesión como invitado");
|
||||
goToMainActivity();
|
||||
}
|
||||
|
||||
private void saveUserSession(int userId, String username) {
|
||||
private void saveUserSession(int userId, String username, boolean isGuest) {
|
||||
SharedPreferences prefs = requireActivity().getSharedPreferences("user_session", Context.MODE_PRIVATE);
|
||||
prefs.edit()
|
||||
.putBoolean("is_logged_in", true)
|
||||
.putBoolean("guest", isGuest)
|
||||
.putInt("user_id", userId)
|
||||
.putString("username", username)
|
||||
.apply();
|
||||
|
|
|
|||
|
|
@ -3,11 +3,9 @@ package com.santiparra.yomitrack.ui.fragments.login;
|
|||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
|
||||
import android.os.Handler;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import com.santiparra.yomitrack.ui.MainActivity;
|
||||
import com.santiparra.yomitrack.ui.fragments.login.LoginActivity;
|
||||
|
||||
/**
|
||||
* Actividad inicial que decide si ir a LoginActivity o directamente a MainActivity.
|
||||
|
|
@ -18,17 +16,20 @@ public class SplashActivity extends AppCompatActivity {
|
|||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
SharedPreferences prefs = getSharedPreferences("app_prefs", MODE_PRIVATE);
|
||||
int userId = prefs.getInt("current_user_id", -999);
|
||||
// Espera 1.5 segundos antes de navegar
|
||||
new Handler().postDelayed(() -> {
|
||||
SharedPreferences prefs = getSharedPreferences("user_session", MODE_PRIVATE);
|
||||
boolean isLoggedIn = prefs.getBoolean("is_logged_in", false);
|
||||
|
||||
if (userId != -999) {
|
||||
// Ya hay una sesión guardada: entrar a la app
|
||||
startActivity(new Intent(this, MainActivity.class));
|
||||
if (isLoggedIn) {
|
||||
// Usuario ya ha iniciado sesión previamente
|
||||
startActivity(new Intent(SplashActivity.this, MainActivity.class));
|
||||
} else {
|
||||
// No hay sesión: ir a login
|
||||
startActivity(new Intent(this, LoginActivity.class));
|
||||
// Ir a login si no ha iniciado sesión
|
||||
startActivity(new Intent(SplashActivity.this, LoginActivity.class));
|
||||
}
|
||||
|
||||
finish(); // Cierra el Splash
|
||||
finish();
|
||||
}, 1500); // 1.5 segundos (puedes ajustar el tiempo)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue