Funcionamiento compelto de las clases profile y de anime y manga donde se añade y se borran sin ningun problema!
This commit is contained in:
parent
2821526fee
commit
5b258bc19a
|
|
@ -1,5 +1,6 @@
|
||||||
package com.santiparra.yomitrack.api;
|
package com.santiparra.yomitrack.api;
|
||||||
|
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
import com.santiparra.yomitrack.db.entities.AnimeEntity;
|
import com.santiparra.yomitrack.db.entities.AnimeEntity;
|
||||||
import com.santiparra.yomitrack.db.entities.MangaEntity;
|
import com.santiparra.yomitrack.db.entities.MangaEntity;
|
||||||
import com.santiparra.yomitrack.db.entities.UserEntity;
|
import com.santiparra.yomitrack.db.entities.UserEntity;
|
||||||
|
|
@ -36,7 +37,7 @@ public interface ApiService {
|
||||||
|
|
||||||
// ---------------- Anime ----------------
|
// ---------------- Anime ----------------
|
||||||
@POST("anime/add")
|
@POST("anime/add")
|
||||||
Call<String> insertAnime(@Body AnimeEntity anime);
|
Call<ApiResponse> insertAnime(@Body AnimeEntity anime);
|
||||||
|
|
||||||
@GET("anime/list/{userId}")
|
@GET("anime/list/{userId}")
|
||||||
Call<List<AnimeEntity>> getAnimeByUser(@Path("userId") int userId);
|
Call<List<AnimeEntity>> getAnimeByUser(@Path("userId") int userId);
|
||||||
|
|
@ -57,7 +58,7 @@ public interface ApiService {
|
||||||
|
|
||||||
// ---------------- Manga ----------------
|
// ---------------- Manga ----------------
|
||||||
@POST("manga/add")
|
@POST("manga/add")
|
||||||
Call<String> insertManga(@Body MangaEntity manga);
|
Call<ApiResponse> insertManga(@Body MangaEntity manga);
|
||||||
|
|
||||||
@GET("manga/list/{userId}")
|
@GET("manga/list/{userId}")
|
||||||
Call<List<MangaEntity>> getMangaByUser(@Path("userId") int userId);
|
Call<List<MangaEntity>> getMangaByUser(@Path("userId") int userId);
|
||||||
|
|
@ -82,8 +83,8 @@ public interface ApiService {
|
||||||
|
|
||||||
@GET("api/activity/list/{userId}")
|
@GET("api/activity/list/{userId}")
|
||||||
Call<List<ActivityLog>> getActivityLog(@Path("userId") int userId);
|
Call<List<ActivityLog>> getActivityLog(@Path("userId") int userId);
|
||||||
@POST("activity/add")
|
@POST("api/activity/add")
|
||||||
Call<JSONObject> postActivity(@Body Map<String, Object> body);
|
Call<JsonObject> postActivity(@Body Map<String, Object> body);
|
||||||
|
|
||||||
|
|
||||||
// ---------------- AniList API ----------------
|
// ---------------- AniList API ----------------
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,9 @@
|
||||||
// AddAnimeFragment.java
|
|
||||||
|
|
||||||
package com.santiparra.yomitrack.ui.fragments.addanime;
|
package com.santiparra.yomitrack.ui.fragments.addanime;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.KeyEvent;
|
import android.view.KeyEvent;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
|
@ -22,11 +21,13 @@ import androidx.fragment.app.Fragment;
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||||
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
|
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
import com.santiparra.yomitrack.R;
|
import com.santiparra.yomitrack.R;
|
||||||
import com.santiparra.yomitrack.api.ApiClient;
|
import com.santiparra.yomitrack.api.ApiClient;
|
||||||
import com.santiparra.yomitrack.api.ApiService;
|
import com.santiparra.yomitrack.api.ApiService;
|
||||||
import com.santiparra.yomitrack.db.entities.AnimeEntity;
|
import com.santiparra.yomitrack.db.entities.AnimeEntity;
|
||||||
import com.santiparra.yomitrack.model.AniListMedia;
|
import com.santiparra.yomitrack.model.AniListMedia;
|
||||||
|
import com.santiparra.yomitrack.model.ApiResponse;
|
||||||
import com.santiparra.yomitrack.model.adapters.anime_adapter.AnimeSearchAdapter;
|
import com.santiparra.yomitrack.model.adapters.anime_adapter.AnimeSearchAdapter;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
@ -46,6 +47,7 @@ public class AddAnimeFragment extends Fragment {
|
||||||
private AnimeSearchAdapter searchAdapter;
|
private AnimeSearchAdapter searchAdapter;
|
||||||
private ApiService api;
|
private ApiService api;
|
||||||
private int userId;
|
private int userId;
|
||||||
|
private String selectedImageUrl = "";
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -137,11 +139,13 @@ public class AddAnimeFragment extends Fragment {
|
||||||
anime.setScore(score);
|
anime.setScore(score);
|
||||||
anime.setProgress(progress);
|
anime.setProgress(progress);
|
||||||
|
|
||||||
api.insertAnime(anime).enqueue(new Callback<String>() {
|
selectedImageUrl = selected.getImageUrl();
|
||||||
|
|
||||||
|
api.insertAnime(anime).enqueue(new Callback<ApiResponse>() {
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(Call<String> call, Response<String> response) {
|
public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
|
||||||
if (response.isSuccessful()) {
|
if (response.isSuccessful() && response.body() != null) {
|
||||||
Toast.makeText(getContext(), "Anime añadido", Toast.LENGTH_SHORT).show();
|
Toast.makeText(getContext(), response.body().getMessage(), Toast.LENGTH_SHORT).show();
|
||||||
registrarActividad(anime.getTitle());
|
registrarActividad(anime.getTitle());
|
||||||
requireActivity().getSupportFragmentManager().popBackStack();
|
requireActivity().getSupportFragmentManager().popBackStack();
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -150,7 +154,7 @@ public class AddAnimeFragment extends Fragment {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Call<String> call, Throwable t) {
|
public void onFailure(Call<ApiResponse> call, Throwable t) {
|
||||||
Toast.makeText(getContext(), "Fallo en la conexión", Toast.LENGTH_SHORT).show();
|
Toast.makeText(getContext(), "Fallo en la conexión", Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -159,16 +163,24 @@ public class AddAnimeFragment extends Fragment {
|
||||||
private void registrarActividad(String titulo) {
|
private void registrarActividad(String titulo) {
|
||||||
Map<String, Object> actividad = new HashMap<>();
|
Map<String, Object> actividad = new HashMap<>();
|
||||||
actividad.put("userId", userId);
|
actividad.put("userId", userId);
|
||||||
actividad.put("action", "Añadió");
|
actividad.put("action", "añadió un anime");
|
||||||
actividad.put("mediaTitle", titulo);
|
actividad.put("mediaTitle", titulo);
|
||||||
|
actividad.put("imageUrl", selectedImageUrl);
|
||||||
|
|
||||||
api.postActivity(actividad).enqueue(new Callback<>() {
|
api.postActivity(actividad).enqueue(new Callback<JsonObject>() {
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(Call call, Response response) {
|
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
|
||||||
|
Log.d("ACTIVITY_POST", "Código de respuesta: " + response.code());
|
||||||
|
if (!response.isSuccessful()) {
|
||||||
|
Log.e("ACTIVITY_POST", "Error en response: " + response.errorBody());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Call call, Throwable t) {
|
public void onFailure(Call<JsonObject> call, Throwable t) {
|
||||||
|
Log.e("ACTIVITY_POST", "Error al registrar actividad: " + t.getMessage(), t);
|
||||||
|
if (!isAdded()) return;
|
||||||
|
Toast.makeText(getContext(), "Error al registrar actividad", Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,11 +20,13 @@ import androidx.fragment.app.Fragment;
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||||
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
|
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
import com.santiparra.yomitrack.R;
|
import com.santiparra.yomitrack.R;
|
||||||
import com.santiparra.yomitrack.api.ApiClient;
|
import com.santiparra.yomitrack.api.ApiClient;
|
||||||
import com.santiparra.yomitrack.api.ApiService;
|
import com.santiparra.yomitrack.api.ApiService;
|
||||||
import com.santiparra.yomitrack.db.entities.MangaEntity;
|
import com.santiparra.yomitrack.db.entities.MangaEntity;
|
||||||
import com.santiparra.yomitrack.model.AniListMedia;
|
import com.santiparra.yomitrack.model.AniListMedia;
|
||||||
|
import com.santiparra.yomitrack.model.ApiResponse;
|
||||||
import com.santiparra.yomitrack.model.adapters.manga_adapter.MangaSearchAdapter;
|
import com.santiparra.yomitrack.model.adapters.manga_adapter.MangaSearchAdapter;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
@ -44,6 +46,7 @@ public class AddMangaFragment extends Fragment {
|
||||||
private MangaSearchAdapter searchAdapter;
|
private MangaSearchAdapter searchAdapter;
|
||||||
private ApiService api;
|
private ApiService api;
|
||||||
private int userId;
|
private int userId;
|
||||||
|
private String selectedImageUrl = "";
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -135,11 +138,13 @@ public class AddMangaFragment extends Fragment {
|
||||||
manga.setScore(score);
|
manga.setScore(score);
|
||||||
manga.setProgress(progress);
|
manga.setProgress(progress);
|
||||||
|
|
||||||
api.insertManga(manga).enqueue(new Callback<String>() {
|
selectedImageUrl = selected.getImageUrl();
|
||||||
|
|
||||||
|
api.insertManga(manga).enqueue(new Callback<ApiResponse>() {
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(Call<String> call, Response<String> response) {
|
public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
|
||||||
if (response.isSuccessful()) {
|
if (response.isSuccessful() && response.body() != null) {
|
||||||
Toast.makeText(getContext(), "Manga añadido correctamente", Toast.LENGTH_SHORT).show();
|
Toast.makeText(getContext(), response.body().getMessage(), Toast.LENGTH_SHORT).show();
|
||||||
registrarActividad(manga.getTitle());
|
registrarActividad(manga.getTitle());
|
||||||
requireActivity().getSupportFragmentManager().popBackStack();
|
requireActivity().getSupportFragmentManager().popBackStack();
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -148,7 +153,7 @@ public class AddMangaFragment extends Fragment {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Call<String> call, Throwable t) {
|
public void onFailure(Call<ApiResponse> call, Throwable t) {
|
||||||
Toast.makeText(getContext(), "Fallo en la conexión", Toast.LENGTH_SHORT).show();
|
Toast.makeText(getContext(), "Fallo en la conexión", Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -157,16 +162,21 @@ public class AddMangaFragment extends Fragment {
|
||||||
private void registrarActividad(String titulo) {
|
private void registrarActividad(String titulo) {
|
||||||
Map<String, Object> actividad = new HashMap<>();
|
Map<String, Object> actividad = new HashMap<>();
|
||||||
actividad.put("userId", userId);
|
actividad.put("userId", userId);
|
||||||
actividad.put("action", "Añadió");
|
actividad.put("action", "añadió un manga");
|
||||||
actividad.put("mediaTitle", titulo);
|
actividad.put("mediaTitle", titulo);
|
||||||
|
actividad.put("imageUrl", selectedImageUrl);
|
||||||
|
|
||||||
api.postActivity(actividad).enqueue(new Callback<>() {
|
api.postActivity(actividad).enqueue(new Callback<JsonObject>() {
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(Call call, Response response) {
|
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
|
||||||
|
if (!response.isSuccessful()) {
|
||||||
|
// Puedes logear el error si lo deseas
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Call call, Throwable t) {
|
public void onFailure(Call<JsonObject> call, Throwable t) {
|
||||||
|
Toast.makeText(getContext(), "Error al registrar actividad", Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,7 @@ public class FragmentAnime extends Fragment {
|
||||||
private RecyclerView recyclerView;
|
private RecyclerView recyclerView;
|
||||||
private AnimeAdapter adapter;
|
private AnimeAdapter adapter;
|
||||||
private ApiService api;
|
private ApiService api;
|
||||||
private int userId = 1;
|
private int userId;
|
||||||
|
|
||||||
private ImageButton btnViewCompact, btnViewNormal, btnViewLarge;
|
private ImageButton btnViewCompact, btnViewNormal, btnViewLarge;
|
||||||
private int currentViewType = AnimeAdapter.VIEW_NORMAL;
|
private int currentViewType = AnimeAdapter.VIEW_NORMAL;
|
||||||
|
|
@ -69,22 +69,13 @@ public class FragmentAnime extends Fragment {
|
||||||
@Nullable Bundle savedInstanceState) {
|
@Nullable Bundle savedInstanceState) {
|
||||||
super.onViewCreated(view, savedInstanceState);
|
super.onViewCreated(view, savedInstanceState);
|
||||||
|
|
||||||
recyclerView = view.findViewById(R.id.recyclerViewAnime);
|
initViews(view);
|
||||||
btnViewCompact = view.findViewById(R.id.btnViewCompact);
|
setupRecyclerView();
|
||||||
btnViewNormal = view.findViewById(R.id.btnViewNormal);
|
setupViewButtons();
|
||||||
btnViewLarge = view.findViewById(R.id.btnViewLarge);
|
setupSearchListener();
|
||||||
FloatingActionButton fabAdd = view.findViewById(R.id.fabAddAnime);
|
setupFab(view);
|
||||||
editSearch = view.findViewById(R.id.editSearch);
|
setupInsets(view);
|
||||||
|
|
||||||
editSearch.addTextChangedListener(new TextWatcher() {
|
|
||||||
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
|
|
||||||
@Override public void onTextChanged(CharSequence s, int start, int before, int count) {
|
|
||||||
filterAnimeList(s.toString());
|
|
||||||
}
|
|
||||||
@Override public void afterTextChanged(Editable s) {}
|
|
||||||
});
|
|
||||||
|
|
||||||
api = ApiClient.getClient().create(ApiService.class);
|
|
||||||
SharedPreferences prefs = requireContext().getSharedPreferences("user_session", Context.MODE_PRIVATE);
|
SharedPreferences prefs = requireContext().getSharedPreferences("user_session", Context.MODE_PRIVATE);
|
||||||
userId = prefs.getInt("user_id", -1);
|
userId = prefs.getInt("user_id", -1);
|
||||||
|
|
||||||
|
|
@ -93,25 +84,26 @@ public class FragmentAnime extends Fragment {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
loadMoreAnimes(currentPage);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initViews(View view) {
|
||||||
|
recyclerView = view.findViewById(R.id.recyclerViewAnime);
|
||||||
|
btnViewCompact = view.findViewById(R.id.btnViewCompact);
|
||||||
|
btnViewNormal = view.findViewById(R.id.btnViewNormal);
|
||||||
|
btnViewLarge = view.findViewById(R.id.btnViewLarge);
|
||||||
|
editSearch = view.findViewById(R.id.editSearch);
|
||||||
|
api = ApiClient.getClient().create(ApiService.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupRecyclerView() {
|
||||||
adapter = new AnimeAdapter(animeList, currentViewType, this::showEditDialog, this::deleteAnime);
|
adapter = new AnimeAdapter(animeList, currentViewType, this::showEditDialog, this::deleteAnime);
|
||||||
recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
|
recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
|
||||||
recyclerView.setAdapter(adapter);
|
recyclerView.setAdapter(adapter);
|
||||||
|
|
||||||
fabAdd.setOnClickListener(v -> requireActivity().getSupportFragmentManager()
|
|
||||||
.beginTransaction()
|
|
||||||
.replace(R.id.frame_layout, new AddAnimeFragment())
|
|
||||||
.addToBackStack(null)
|
|
||||||
.commit());
|
|
||||||
|
|
||||||
btnViewCompact.setOnClickListener(v -> setViewType(AnimeAdapter.VIEW_COMPACT));
|
|
||||||
btnViewNormal.setOnClickListener(v -> setViewType(AnimeAdapter.VIEW_NORMAL));
|
|
||||||
btnViewLarge.setOnClickListener(v -> setViewType(AnimeAdapter.VIEW_LARGE));
|
|
||||||
|
|
||||||
setViewType(currentViewType);
|
|
||||||
loadMoreAnimes(currentPage);
|
|
||||||
|
|
||||||
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
||||||
@Override public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
|
@Override
|
||||||
|
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
|
||||||
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
|
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
|
||||||
if (layoutManager == null) return;
|
if (layoutManager == null) return;
|
||||||
|
|
||||||
|
|
@ -125,7 +117,34 @@ public class FragmentAnime extends Fragment {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupViewButtons() {
|
||||||
|
btnViewCompact.setOnClickListener(v -> setViewType(AnimeAdapter.VIEW_COMPACT));
|
||||||
|
btnViewNormal.setOnClickListener(v -> setViewType(AnimeAdapter.VIEW_NORMAL));
|
||||||
|
btnViewLarge.setOnClickListener(v -> setViewType(AnimeAdapter.VIEW_LARGE));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupSearchListener() {
|
||||||
|
editSearch.addTextChangedListener(new TextWatcher() {
|
||||||
|
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
|
||||||
|
@Override public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||||
|
filterAnimeList(s.toString());
|
||||||
|
}
|
||||||
|
@Override public void afterTextChanged(Editable s) {}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupFab(View view) {
|
||||||
|
FloatingActionButton fabAdd = view.findViewById(R.id.fabAddAnime);
|
||||||
|
fabAdd.setOnClickListener(v -> requireActivity().getSupportFragmentManager()
|
||||||
|
.beginTransaction()
|
||||||
|
.replace(R.id.frame_layout, new AddAnimeFragment())
|
||||||
|
.addToBackStack(null)
|
||||||
|
.commit());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupInsets(View view) {
|
||||||
ViewCompat.setOnApplyWindowInsetsListener(view, (v, insets) -> {
|
ViewCompat.setOnApplyWindowInsetsListener(view, (v, insets) -> {
|
||||||
int bottomInset = insets.getInsets(WindowInsetsCompat.Type.systemBars()).bottom;
|
int bottomInset = insets.getInsets(WindowInsetsCompat.Type.systemBars()).bottom;
|
||||||
recyclerView.setPadding(
|
recyclerView.setPadding(
|
||||||
|
|
@ -150,13 +169,9 @@ public class FragmentAnime extends Fragment {
|
||||||
|
|
||||||
private void setViewType(int viewType) {
|
private void setViewType(int viewType) {
|
||||||
currentViewType = viewType;
|
currentViewType = viewType;
|
||||||
|
recyclerView.setLayoutManager(viewType == AnimeAdapter.VIEW_LARGE ?
|
||||||
if (viewType == AnimeAdapter.VIEW_LARGE) {
|
new GridLayoutManager(requireContext(), 2) :
|
||||||
recyclerView.setLayoutManager(new GridLayoutManager(requireContext(), 2));
|
new LinearLayoutManager(requireContext()));
|
||||||
} else {
|
|
||||||
recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
|
|
||||||
}
|
|
||||||
|
|
||||||
adapter = new AnimeAdapter(animeList, viewType, this::showEditDialog, this::deleteAnime);
|
adapter = new AnimeAdapter(animeList, viewType, this::showEditDialog, this::deleteAnime);
|
||||||
recyclerView.setAdapter(adapter);
|
recyclerView.setAdapter(adapter);
|
||||||
}
|
}
|
||||||
|
|
@ -175,9 +190,8 @@ public class FragmentAnime extends Fragment {
|
||||||
public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
|
public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
|
||||||
if (!isAdded()) return;
|
if (!isAdded()) return;
|
||||||
|
|
||||||
if (response.isSuccessful()) {
|
if (response.isSuccessful() && response.body() != null) {
|
||||||
String msg = response.body() != null ? response.body().getMessage() : "Anime eliminado";
|
Toast.makeText(requireContext(), response.body().getMessage(), Toast.LENGTH_SHORT).show();
|
||||||
Toast.makeText(requireContext(), msg, Toast.LENGTH_SHORT).show();
|
|
||||||
currentPage = 1;
|
currentPage = 1;
|
||||||
animeList.clear();
|
animeList.clear();
|
||||||
loadMoreAnimes(currentPage);
|
loadMoreAnimes(currentPage);
|
||||||
|
|
@ -208,7 +222,6 @@ public class FragmentAnime extends Fragment {
|
||||||
List<AnimeEntity> nuevos = response.body().getData();
|
List<AnimeEntity> nuevos = response.body().getData();
|
||||||
animeList.addAll(nuevos);
|
animeList.addAll(nuevos);
|
||||||
adapter.notifyItemRangeInserted(animeList.size() - nuevos.size(), nuevos.size());
|
adapter.notifyItemRangeInserted(animeList.size() - nuevos.size(), nuevos.size());
|
||||||
|
|
||||||
isLoading = response.body().isHasNextPage();
|
isLoading = response.body().isHasNextPage();
|
||||||
} else {
|
} else {
|
||||||
isLoading = false;
|
isLoading = false;
|
||||||
|
|
|
||||||
|
|
@ -13,12 +13,16 @@ import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
|
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
import com.santiparra.yomitrack.R;
|
import com.santiparra.yomitrack.R;
|
||||||
import com.santiparra.yomitrack.api.ApiClient;
|
import com.santiparra.yomitrack.api.ApiClient;
|
||||||
import com.santiparra.yomitrack.api.ApiService;
|
import com.santiparra.yomitrack.api.ApiService;
|
||||||
import com.santiparra.yomitrack.db.entities.AnimeEntity;
|
import com.santiparra.yomitrack.db.entities.AnimeEntity;
|
||||||
import com.santiparra.yomitrack.model.ApiResponse;
|
import com.santiparra.yomitrack.model.ApiResponse;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import retrofit2.Call;
|
import retrofit2.Call;
|
||||||
import retrofit2.Callback;
|
import retrofit2.Callback;
|
||||||
import retrofit2.Response;
|
import retrofit2.Response;
|
||||||
|
|
@ -142,6 +146,7 @@ public class EditAnimeFragment extends Fragment {
|
||||||
if (response.isSuccessful()) {
|
if (response.isSuccessful()) {
|
||||||
String message = response.body() != null ? response.body().getMessage() : "Anime actualizado";
|
String message = response.body() != null ? response.body().getMessage() : "Anime actualizado";
|
||||||
Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show();
|
Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show();
|
||||||
|
registrarActividad(anime.getTitle());
|
||||||
requireContext().getSharedPreferences("user_session", Context.MODE_PRIVATE)
|
requireContext().getSharedPreferences("user_session", Context.MODE_PRIVATE)
|
||||||
.edit().putBoolean("refresh_profile", true).apply();
|
.edit().putBoolean("refresh_profile", true).apply();
|
||||||
requireActivity().getSupportFragmentManager().popBackStack();
|
requireActivity().getSupportFragmentManager().popBackStack();
|
||||||
|
|
@ -184,4 +189,30 @@ public class EditAnimeFragment extends Fragment {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void registrarActividad(String titulo) {
|
||||||
|
int userId = requireContext()
|
||||||
|
.getSharedPreferences("user_session", Context.MODE_PRIVATE)
|
||||||
|
.getInt("user_id", -1);
|
||||||
|
|
||||||
|
if (userId == -1) return;
|
||||||
|
|
||||||
|
Map<String, Object> actividad = new HashMap<>();
|
||||||
|
actividad.put("userId", userId);
|
||||||
|
actividad.put("action", "editó un anime");
|
||||||
|
actividad.put("mediaTitle", titulo);
|
||||||
|
actividad.put("imageUrl", anime.getImageUrl());
|
||||||
|
|
||||||
|
api.postActivity(actividad).enqueue(new Callback<JsonObject>() {
|
||||||
|
@Override
|
||||||
|
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
|
||||||
|
Log.d("ACTIVITY_EDIT", "Actividad registrada: " + response.code());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFailure(Call<JsonObject> call, Throwable t) {
|
||||||
|
Log.e("ACTIVITY_EDIT", "Error al registrar actividad: " + t.getMessage(), t);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,12 +13,16 @@ import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
|
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
import com.santiparra.yomitrack.R;
|
import com.santiparra.yomitrack.R;
|
||||||
import com.santiparra.yomitrack.api.ApiClient;
|
import com.santiparra.yomitrack.api.ApiClient;
|
||||||
import com.santiparra.yomitrack.api.ApiService;
|
import com.santiparra.yomitrack.api.ApiService;
|
||||||
import com.santiparra.yomitrack.db.entities.MangaEntity;
|
import com.santiparra.yomitrack.db.entities.MangaEntity;
|
||||||
import com.santiparra.yomitrack.model.ApiResponse;
|
import com.santiparra.yomitrack.model.ApiResponse;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import retrofit2.Call;
|
import retrofit2.Call;
|
||||||
import retrofit2.Callback;
|
import retrofit2.Callback;
|
||||||
import retrofit2.Response;
|
import retrofit2.Response;
|
||||||
|
|
@ -36,8 +40,7 @@ public class EditMangaFragment extends Fragment {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
|
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||||
Bundle savedInstanceState) {
|
|
||||||
return inflater.inflate(R.layout.fragment_edit_manga, container, false);
|
return inflater.inflate(R.layout.fragment_edit_manga, container, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -134,13 +137,11 @@ public class EditMangaFragment extends Fragment {
|
||||||
api.updateManga(manga.getId(), manga).enqueue(new Callback<ApiResponse>() {
|
api.updateManga(manga.getId(), manga).enqueue(new Callback<ApiResponse>() {
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
|
public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
|
||||||
Log.d("API_RESPONSE", "onResponse ejecutado: " + response.body());
|
|
||||||
|
|
||||||
if (!isAdded()) return;
|
if (!isAdded()) return;
|
||||||
|
|
||||||
if (response.isSuccessful()) {
|
if (response.isSuccessful() && response.body() != null) {
|
||||||
String message = response.body() != null ? response.body().getMessage() : "Manga actualizado";
|
Toast.makeText(requireContext(), response.body().getMessage(), Toast.LENGTH_SHORT).show();
|
||||||
Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show();
|
registrarActividad(manga.getTitle());
|
||||||
requireContext().getSharedPreferences("user_session", Context.MODE_PRIVATE)
|
requireContext().getSharedPreferences("user_session", Context.MODE_PRIVATE)
|
||||||
.edit().putBoolean("refresh_profile", true).apply();
|
.edit().putBoolean("refresh_profile", true).apply();
|
||||||
requireActivity().getSupportFragmentManager().popBackStack();
|
requireActivity().getSupportFragmentManager().popBackStack();
|
||||||
|
|
@ -151,7 +152,6 @@ public class EditMangaFragment extends Fragment {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Call<ApiResponse> call, Throwable t) {
|
public void onFailure(Call<ApiResponse> call, Throwable t) {
|
||||||
Log.e("API_RESPONSE", "onFailure ejecutado: " + t.getMessage(), t);
|
|
||||||
if (!isAdded()) return;
|
if (!isAdded()) return;
|
||||||
Toast.makeText(requireContext(), "Fallo en la conexión", Toast.LENGTH_SHORT).show();
|
Toast.makeText(requireContext(), "Fallo en la conexión", Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
|
|
@ -164,9 +164,8 @@ public class EditMangaFragment extends Fragment {
|
||||||
public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
|
public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
|
||||||
if (!isAdded()) return;
|
if (!isAdded()) return;
|
||||||
|
|
||||||
if (response.isSuccessful()) {
|
if (response.isSuccessful() && response.body() != null) {
|
||||||
String message = response.body() != null ? response.body().getMessage() : "Manga eliminado";
|
Toast.makeText(requireContext(), response.body().getMessage(), Toast.LENGTH_SHORT).show();
|
||||||
Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show();
|
|
||||||
requireContext().getSharedPreferences("user_session", Context.MODE_PRIVATE)
|
requireContext().getSharedPreferences("user_session", Context.MODE_PRIVATE)
|
||||||
.edit().putBoolean("refresh_profile", true).apply();
|
.edit().putBoolean("refresh_profile", true).apply();
|
||||||
requireActivity().getSupportFragmentManager().popBackStack();
|
requireActivity().getSupportFragmentManager().popBackStack();
|
||||||
|
|
@ -177,10 +176,35 @@ public class EditMangaFragment extends Fragment {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Call<ApiResponse> call, Throwable t) {
|
public void onFailure(Call<ApiResponse> call, Throwable t) {
|
||||||
Log.e("API_RESPONSE", "onFailure eliminar: " + t.getMessage(), t);
|
|
||||||
if (!isAdded()) return;
|
if (!isAdded()) return;
|
||||||
Toast.makeText(requireContext(), "Fallo en la conexión", Toast.LENGTH_SHORT).show();
|
Toast.makeText(requireContext(), "Fallo en la conexión", Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void registrarActividad(String titulo) {
|
||||||
|
int userId = requireContext()
|
||||||
|
.getSharedPreferences("user_session", Context.MODE_PRIVATE)
|
||||||
|
.getInt("user_id", -1);
|
||||||
|
|
||||||
|
if (userId == -1) return;
|
||||||
|
|
||||||
|
Map<String, Object> actividad = new HashMap<>();
|
||||||
|
actividad.put("userId", userId);
|
||||||
|
actividad.put("action", "editó un manga");
|
||||||
|
actividad.put("mediaTitle", titulo);
|
||||||
|
actividad.put("imageUrl", manga.getImageUrl());
|
||||||
|
|
||||||
|
api.postActivity(actividad).enqueue(new Callback<JsonObject>() {
|
||||||
|
@Override
|
||||||
|
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
|
||||||
|
Log.d("ACTIVITY_EDIT_MANGA", "Actividad registrada: " + response.code());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFailure(Call<JsonObject> call, Throwable t) {
|
||||||
|
Log.e("ACTIVITY_EDIT_MANGA", "Error al registrar actividad: " + t.getMessage(), t);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,7 @@ public class FragmentManga extends Fragment {
|
||||||
private RecyclerView recyclerView;
|
private RecyclerView recyclerView;
|
||||||
private MangaAdapter adapter;
|
private MangaAdapter adapter;
|
||||||
private ApiService api;
|
private ApiService api;
|
||||||
private int userId = 1;
|
private int userId;
|
||||||
|
|
||||||
private ImageButton btnViewCompact, btnViewNormal, btnViewLarge;
|
private ImageButton btnViewCompact, btnViewNormal, btnViewLarge;
|
||||||
private int currentViewType = MangaAdapter.VIEW_NORMAL;
|
private int currentViewType = MangaAdapter.VIEW_NORMAL;
|
||||||
|
|
@ -58,31 +58,17 @@ public class FragmentManga extends Fragment {
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
public View onCreateView(@NonNull LayoutInflater inflater,
|
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||||
@Nullable ViewGroup container,
|
|
||||||
@Nullable Bundle savedInstanceState) {
|
|
||||||
return inflater.inflate(R.layout.fragment_mlist, container, false);
|
return inflater.inflate(R.layout.fragment_mlist, container, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onViewCreated(@NonNull View view,
|
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||||
@Nullable Bundle savedInstanceState) {
|
|
||||||
super.onViewCreated(view, savedInstanceState);
|
super.onViewCreated(view, savedInstanceState);
|
||||||
|
|
||||||
recyclerView = view.findViewById(R.id.recyclerViewManga);
|
initViews(view);
|
||||||
btnViewCompact = view.findViewById(R.id.btnViewCompact);
|
setupListeners();
|
||||||
btnViewNormal = view.findViewById(R.id.btnViewNormal);
|
setupRecyclerView();
|
||||||
btnViewLarge = view.findViewById(R.id.btnViewLarge);
|
|
||||||
FloatingActionButton fabAdd = view.findViewById(R.id.fabAddManga);
|
|
||||||
editSearch = view.findViewById(R.id.editSearch);
|
|
||||||
|
|
||||||
editSearch.addTextChangedListener(new TextWatcher() {
|
|
||||||
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
|
|
||||||
@Override public void afterTextChanged(Editable s) {}
|
|
||||||
@Override public void onTextChanged(CharSequence s, int start, int before, int count) {
|
|
||||||
filterMangaList(s.toString());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
api = ApiClient.getClient().create(ApiService.class);
|
api = ApiClient.getClient().create(ApiService.class);
|
||||||
SharedPreferences prefs = requireContext().getSharedPreferences("user_session", Context.MODE_PRIVATE);
|
SharedPreferences prefs = requireContext().getSharedPreferences("user_session", Context.MODE_PRIVATE);
|
||||||
|
|
@ -93,18 +79,54 @@ public class FragmentManga extends Fragment {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setViewType(currentViewType);
|
||||||
|
loadMoreMangas(currentPage);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initViews(View view) {
|
||||||
|
recyclerView = view.findViewById(R.id.recyclerViewManga);
|
||||||
|
btnViewCompact = view.findViewById(R.id.btnViewCompact);
|
||||||
|
btnViewNormal = view.findViewById(R.id.btnViewNormal);
|
||||||
|
btnViewLarge = view.findViewById(R.id.btnViewLarge);
|
||||||
|
FloatingActionButton fabAdd = view.findViewById(R.id.fabAddManga);
|
||||||
|
editSearch = view.findViewById(R.id.editSearch);
|
||||||
|
|
||||||
fabAdd.setOnClickListener(v -> requireActivity().getSupportFragmentManager()
|
fabAdd.setOnClickListener(v -> requireActivity().getSupportFragmentManager()
|
||||||
.beginTransaction()
|
.beginTransaction()
|
||||||
.replace(R.id.frame_layout, new AddMangaFragment())
|
.replace(R.id.frame_layout, new AddMangaFragment())
|
||||||
.addToBackStack(null)
|
.addToBackStack(null)
|
||||||
.commit());
|
.commit());
|
||||||
|
|
||||||
|
ViewCompat.setOnApplyWindowInsetsListener(view, (v, insets) -> {
|
||||||
|
int bottomInset = insets.getInsets(WindowInsetsCompat.Type.systemBars()).bottom;
|
||||||
|
recyclerView.setPadding(
|
||||||
|
recyclerView.getPaddingLeft(),
|
||||||
|
recyclerView.getPaddingTop(),
|
||||||
|
recyclerView.getPaddingRight(),
|
||||||
|
bottomInset + 95
|
||||||
|
);
|
||||||
|
return insets;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupListeners() {
|
||||||
|
editSearch.addTextChangedListener(new TextWatcher() {
|
||||||
|
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
|
||||||
|
@Override public void afterTextChanged(Editable s) {}
|
||||||
|
@Override public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||||
|
filterMangaList(s.toString());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
btnViewCompact.setOnClickListener(v -> setViewType(MangaAdapter.VIEW_COMPACT));
|
btnViewCompact.setOnClickListener(v -> setViewType(MangaAdapter.VIEW_COMPACT));
|
||||||
btnViewNormal.setOnClickListener(v -> setViewType(MangaAdapter.VIEW_NORMAL));
|
btnViewNormal.setOnClickListener(v -> setViewType(MangaAdapter.VIEW_NORMAL));
|
||||||
btnViewLarge.setOnClickListener(v -> setViewType(MangaAdapter.VIEW_LARGE));
|
btnViewLarge.setOnClickListener(v -> setViewType(MangaAdapter.VIEW_LARGE));
|
||||||
|
}
|
||||||
|
|
||||||
setViewType(currentViewType);
|
private void setupRecyclerView() {
|
||||||
loadMoreMangas(currentPage);
|
adapter = new MangaAdapter(mangaList, currentViewType, this::showEditDialog, this::deleteManga);
|
||||||
|
recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
|
||||||
|
recyclerView.setAdapter(adapter);
|
||||||
|
|
||||||
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -122,27 +144,6 @@ public class FragmentManga extends Fragment {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
ViewCompat.setOnApplyWindowInsetsListener(view, (v, insets) -> {
|
|
||||||
int bottomInset = insets.getInsets(WindowInsetsCompat.Type.systemBars()).bottom;
|
|
||||||
recyclerView.setPadding(
|
|
||||||
recyclerView.getPaddingLeft(),
|
|
||||||
recyclerView.getPaddingTop(),
|
|
||||||
recyclerView.getPaddingRight(),
|
|
||||||
bottomInset + 95
|
|
||||||
);
|
|
||||||
return insets;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void filterMangaList(String query) {
|
|
||||||
List<MangaEntity> filtered = new ArrayList<>();
|
|
||||||
for (MangaEntity manga : mangaList) {
|
|
||||||
if (manga.getTitle().toLowerCase().contains(query.toLowerCase())) {
|
|
||||||
filtered.add(manga);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
adapter.updateList(filtered);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setViewType(int viewType) {
|
private void setViewType(int viewType) {
|
||||||
|
|
@ -154,12 +155,20 @@ public class FragmentManga extends Fragment {
|
||||||
recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
|
recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
|
||||||
}
|
}
|
||||||
|
|
||||||
adapter = new MangaAdapter(mangaList, viewType,
|
adapter = new MangaAdapter(mangaList, viewType, this::showEditDialog, this::deleteManga);
|
||||||
this::showEditDialog,
|
|
||||||
this::deleteManga);
|
|
||||||
recyclerView.setAdapter(adapter);
|
recyclerView.setAdapter(adapter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void filterMangaList(String query) {
|
||||||
|
List<MangaEntity> filtered = new ArrayList<>();
|
||||||
|
for (MangaEntity manga : mangaList) {
|
||||||
|
if (manga.getTitle().toLowerCase().contains(query.toLowerCase())) {
|
||||||
|
filtered.add(manga);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
adapter.updateList(filtered);
|
||||||
|
}
|
||||||
|
|
||||||
private void showEditDialog(MangaEntity manga) {
|
private void showEditDialog(MangaEntity manga) {
|
||||||
requireActivity().getSupportFragmentManager()
|
requireActivity().getSupportFragmentManager()
|
||||||
.beginTransaction()
|
.beginTransaction()
|
||||||
|
|
@ -175,8 +184,7 @@ public class FragmentManga extends Fragment {
|
||||||
if (!isAdded()) return;
|
if (!isAdded()) return;
|
||||||
|
|
||||||
if (response.isSuccessful()) {
|
if (response.isSuccessful()) {
|
||||||
String msg = response.body() != null ? response.body().getMessage() : "Manga eliminado";
|
Toast.makeText(requireContext(), response.body() != null ? response.body().getMessage() : "Manga eliminado", Toast.LENGTH_SHORT).show();
|
||||||
Toast.makeText(requireContext(), msg, Toast.LENGTH_SHORT).show();
|
|
||||||
currentPage = 1;
|
currentPage = 1;
|
||||||
mangaList.clear();
|
mangaList.clear();
|
||||||
loadMoreMangas(currentPage);
|
loadMoreMangas(currentPage);
|
||||||
|
|
|
||||||
|
|
@ -10,17 +10,20 @@ import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.*;
|
import android.widget.*;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
|
|
||||||
|
import com.bumptech.glide.Glide;
|
||||||
import com.santiparra.yomitrack.R;
|
import com.santiparra.yomitrack.R;
|
||||||
import com.santiparra.yomitrack.api.ApiClient;
|
import com.santiparra.yomitrack.api.ApiClient;
|
||||||
import com.santiparra.yomitrack.api.ApiService;
|
import com.santiparra.yomitrack.api.ApiService;
|
||||||
import com.santiparra.yomitrack.model.UserStatsResponse;
|
import com.santiparra.yomitrack.model.UserStatsResponse;
|
||||||
import com.santiparra.yomitrack.utils.ActivityLog;
|
import com.santiparra.yomitrack.utils.ActivityLog;
|
||||||
|
|
||||||
import org.json.JSONObject;
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
@ -42,11 +45,28 @@ public class FragmentProfile extends Fragment {
|
||||||
private String username;
|
private String username;
|
||||||
private View view;
|
private View view;
|
||||||
|
|
||||||
|
private SharedPreferences bioPrefs;
|
||||||
|
private static final String BIO_PREFS = "bio_prefs";
|
||||||
|
private static final String BIO_KEY = "bio_text";
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||||
view = inflater.inflate(R.layout.fragment_profile, container, false);
|
view = inflater.inflate(R.layout.fragment_profile, container, false);
|
||||||
|
initViews();
|
||||||
|
setupUserInfo();
|
||||||
|
setupListeners();
|
||||||
|
return view;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onResume() {
|
||||||
|
super.onResume();
|
||||||
|
loadStats();
|
||||||
|
loadActivity();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initViews() {
|
||||||
avatarImage = view.findViewById(R.id.avatarImage);
|
avatarImage = view.findViewById(R.id.avatarImage);
|
||||||
coverImage = view.findViewById(R.id.coverImage);
|
coverImage = view.findViewById(R.id.coverImage);
|
||||||
usernameText = view.findViewById(R.id.usernameText);
|
usernameText = view.findViewById(R.id.usernameText);
|
||||||
|
|
@ -57,28 +77,26 @@ public class FragmentProfile extends Fragment {
|
||||||
animeStatsContainer = view.findViewById(R.id.animeStatsContainer);
|
animeStatsContainer = view.findViewById(R.id.animeStatsContainer);
|
||||||
mangaStatsContainer = view.findViewById(R.id.mangaStatsContainer);
|
mangaStatsContainer = view.findViewById(R.id.mangaStatsContainer);
|
||||||
activityContainer = view.findViewById(R.id.activityContainer);
|
activityContainer = view.findViewById(R.id.activityContainer);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupUserInfo() {
|
||||||
SharedPreferences prefs = requireContext().getSharedPreferences("user_session", Context.MODE_PRIVATE);
|
SharedPreferences prefs = requireContext().getSharedPreferences("user_session", Context.MODE_PRIVATE);
|
||||||
userId = prefs.getInt("user_id", -1);
|
userId = prefs.getInt("user_id", -1);
|
||||||
username = prefs.getString("username", "Usuario");
|
username = prefs.getString("username", "Usuario");
|
||||||
|
|
||||||
api = ApiClient.getClient().create(ApiService.class);
|
|
||||||
usernameText.setText(username);
|
usernameText.setText(username);
|
||||||
|
|
||||||
|
bioPrefs = requireContext().getSharedPreferences(BIO_PREFS, Context.MODE_PRIVATE);
|
||||||
|
editBiography.setText(bioPrefs.getString(BIO_KEY, ""));
|
||||||
|
|
||||||
|
api = ApiClient.getClient().create(ApiService.class);
|
||||||
|
|
||||||
loadStats();
|
loadStats();
|
||||||
loadActivity();
|
loadActivity();
|
||||||
|
|
||||||
buttonPostStatus.setOnClickListener(v -> postStatus());
|
|
||||||
buttonSaveBio.setOnClickListener(v -> saveBiography());
|
|
||||||
|
|
||||||
return view;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private void setupListeners() {
|
||||||
public void onResume() {
|
buttonPostStatus.setOnClickListener(v -> postStatus());
|
||||||
super.onResume();
|
buttonSaveBio.setOnClickListener(v -> saveBiography());
|
||||||
loadStats();
|
|
||||||
loadActivity();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadStats() {
|
private void loadStats() {
|
||||||
|
|
@ -104,48 +122,41 @@ public class FragmentProfile extends Fragment {
|
||||||
container.removeAllViews();
|
container.removeAllViews();
|
||||||
|
|
||||||
if (stats == null || stats.isEmpty()) {
|
if (stats == null || stats.isEmpty()) {
|
||||||
TextView noData = new TextView(getContext());
|
addTextToContainer(container, "No hay estadísticas disponibles");
|
||||||
noData.setText("No hay estadísticas disponibles");
|
|
||||||
noData.setPadding(16, 8, 16, 8);
|
|
||||||
container.addView(noData);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int total = 0;
|
int total = stats.values().stream().mapToInt(Integer::intValue).sum();
|
||||||
for (int count : stats.values()) total += count;
|
|
||||||
|
|
||||||
LayoutInflater inflater = LayoutInflater.from(getContext());
|
LayoutInflater inflater = LayoutInflater.from(getContext());
|
||||||
|
|
||||||
for (Map.Entry<String, Integer> entry : stats.entrySet()) {
|
for (Map.Entry<String, Integer> entry : stats.entrySet()) {
|
||||||
View statView = inflater.inflate(R.layout.item_stat_bar, container, false);
|
View statView = inflater.inflate(R.layout.item_stat_bar, container, false);
|
||||||
TextView label = statView.findViewById(R.id.statLabelFull);
|
TextView label = statView.findViewById(R.id.statLabelFull);
|
||||||
ProgressBar bar = statView.findViewById(R.id.statProgressBar);
|
ProgressBar bar = statView.findViewById(R.id.statProgressBar);
|
||||||
|
|
||||||
label.setText(String.format(Locale.getDefault(), "%s • %d", entry.getKey(), entry.getValue()));
|
label.setText(String.format(Locale.getDefault(), "%s • %d", entry.getKey(), entry.getValue()));
|
||||||
int progress = total > 0 ? (entry.getValue() * 100 / total) : 0;
|
bar.setProgress(total > 0 ? (entry.getValue() * 100 / total) : 0);
|
||||||
bar.setProgress(progress);
|
bar.setProgressTintList(ColorStateList.valueOf(getColorForStatus(entry.getKey())));
|
||||||
|
|
||||||
// Usar método compatible para aplicar color de estado
|
|
||||||
int color = getColorForStatus(entry.getKey());
|
|
||||||
bar.setProgressTintList(ColorStateList.valueOf(color));
|
|
||||||
|
|
||||||
container.addView(statView);
|
container.addView(statView);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void addTextToContainer(LinearLayout container, String message) {
|
||||||
|
TextView noData = new TextView(getContext());
|
||||||
|
noData.setText(message);
|
||||||
|
noData.setPadding(16, 8, 16, 8);
|
||||||
|
container.addView(noData);
|
||||||
|
}
|
||||||
|
|
||||||
private int getColorForStatus(String status) {
|
private int getColorForStatus(String status) {
|
||||||
switch (status.toLowerCase(Locale.ROOT)) {
|
switch (status.toLowerCase(Locale.ROOT)) {
|
||||||
case "watching":
|
case "watching": return requireContext().getColor(R.color.status_watching);
|
||||||
return requireContext().getColor(R.color.status_watching);
|
case "completed": return requireContext().getColor(R.color.status_completed);
|
||||||
case "completed":
|
case "paused": return requireContext().getColor(R.color.status_paused);
|
||||||
return requireContext().getColor(R.color.status_completed);
|
case "dropped": return requireContext().getColor(R.color.status_dropped);
|
||||||
case "paused":
|
case "planning": return requireContext().getColor(R.color.status_planning);
|
||||||
return requireContext().getColor(R.color.status_paused);
|
default: return requireContext().getColor(R.color.gray);
|
||||||
case "dropped":
|
|
||||||
return requireContext().getColor(R.color.status_dropped);
|
|
||||||
case "planning":
|
|
||||||
return requireContext().getColor(R.color.status_planning);
|
|
||||||
default:
|
|
||||||
return requireContext().getColor(R.color.gray);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -163,6 +174,10 @@ public class FragmentProfile extends Fragment {
|
||||||
((TextView) card.findViewById(R.id.activityAction)).setText(log.getAction());
|
((TextView) card.findViewById(R.id.activityAction)).setText(log.getAction());
|
||||||
((TextView) card.findViewById(R.id.activityTitle)).setText(log.getMediaTitle());
|
((TextView) card.findViewById(R.id.activityTitle)).setText(log.getMediaTitle());
|
||||||
((TextView) card.findViewById(R.id.activityTime)).setText(log.getTimestamp());
|
((TextView) card.findViewById(R.id.activityTime)).setText(log.getTimestamp());
|
||||||
|
|
||||||
|
if (!TextUtils.isEmpty(log.getImageUrl())) {
|
||||||
|
Glide.with(requireContext()).load(log.getImageUrl()).into((ImageView) card.findViewById(R.id.activityCover));
|
||||||
|
}
|
||||||
activityContainer.addView(card);
|
activityContainer.addView(card);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -187,9 +202,9 @@ public class FragmentProfile extends Fragment {
|
||||||
post.put("action", "publicó");
|
post.put("action", "publicó");
|
||||||
post.put("mediaTitle", status);
|
post.put("mediaTitle", status);
|
||||||
|
|
||||||
api.postActivity(post).enqueue(new Callback<JSONObject>() {
|
api.postActivity(post).enqueue(new Callback<JsonObject>() {
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(Call<JSONObject> call, Response<JSONObject> response) {
|
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
|
||||||
if (response.isSuccessful()) {
|
if (response.isSuccessful()) {
|
||||||
editStatus.setText("");
|
editStatus.setText("");
|
||||||
loadActivity();
|
loadActivity();
|
||||||
|
|
@ -198,14 +213,14 @@ public class FragmentProfile extends Fragment {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Call<JSONObject> call, Throwable t) {
|
public void onFailure(Call<JsonObject> call, Throwable t) {
|
||||||
Toast.makeText(getContext(), "Error al publicar", Toast.LENGTH_SHORT).show();
|
Toast.makeText(getContext(), "Error al publicar", Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void saveBiography() {
|
private void saveBiography() {
|
||||||
String bio = editBiography.getText().toString().trim();
|
bioPrefs.edit().putString(BIO_KEY, editBiography.getText().toString().trim()).apply();
|
||||||
Toast.makeText(getContext(), "Biografía guardada", Toast.LENGTH_SHORT).show();
|
Toast.makeText(getContext(), "Biografía guardada", Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,13 @@ public class ActivityLog {
|
||||||
@SerializedName("timestamp")
|
@SerializedName("timestamp")
|
||||||
private String timestamp;
|
private String timestamp;
|
||||||
|
|
||||||
|
@SerializedName("imagenUrl")
|
||||||
|
private String imageUrl;
|
||||||
|
|
||||||
|
public String getImageUrl() {
|
||||||
|
return imageUrl;
|
||||||
|
}
|
||||||
|
|
||||||
public String getAction() { return action; }
|
public String getAction() { return action; }
|
||||||
|
|
||||||
public String getMediaTitle() { return mediaTitle; }
|
public String getMediaTitle() { return mediaTitle; }
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue