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