From c7053368a22087ffaba474f800dc3b3cd6cb8056 Mon Sep 17 00:00:00 2001 From: santi Date: Fri, 23 May 2025 00:11:11 +0200 Subject: [PATCH] Terminacion de la clase FragmentProfile que ya tiene toda la funcionalidad que a de tener y optimizacion de otras para que funcione y creacion de javas y xml! --- .../yomitrack/model/CommentDialog.java | 58 ++++++++++++++++ .../yomitrack/model/CommentModel.java | 31 +++++++++ .../santiparra/yomitrack/utils/DateUtils.java | 62 +++++++++++++++++ app/src/main/res/drawable/ic_heart.xml | 13 ++++ app/src/main/res/drawable/ic_heart_filled.xml | 9 +++ .../main/res/drawable/ic_heart_outline.xml | 9 +++ app/src/main/res/layout/dialog_comment.xml | 20 ++++++ app/src/main/res/layout/item_comment.xml | 69 +++++++++++++++++++ 8 files changed, 271 insertions(+) create mode 100644 app/src/main/java/com/santiparra/yomitrack/model/CommentDialog.java create mode 100644 app/src/main/java/com/santiparra/yomitrack/model/CommentModel.java create mode 100644 app/src/main/java/com/santiparra/yomitrack/utils/DateUtils.java create mode 100644 app/src/main/res/drawable/ic_heart.xml create mode 100644 app/src/main/res/drawable/ic_heart_filled.xml create mode 100644 app/src/main/res/drawable/ic_heart_outline.xml create mode 100644 app/src/main/res/layout/dialog_comment.xml create mode 100644 app/src/main/res/layout/item_comment.xml diff --git a/app/src/main/java/com/santiparra/yomitrack/model/CommentDialog.java b/app/src/main/java/com/santiparra/yomitrack/model/CommentDialog.java new file mode 100644 index 0000000..52a2f9b --- /dev/null +++ b/app/src/main/java/com/santiparra/yomitrack/model/CommentDialog.java @@ -0,0 +1,58 @@ +package com.santiparra.yomitrack.model; + +import android.app.Dialog; +import android.content.Context; +import android.widget.Button; +import android.widget.EditText; +import android.widget.Toast; + +import androidx.annotation.NonNull; + +import com.santiparra.yomitrack.R; +import com.santiparra.yomitrack.api.ApiClient; +import com.santiparra.yomitrack.api.ApiService; +import com.google.gson.JsonObject; + +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; + +public class CommentDialog extends Dialog { + + public CommentDialog(@NonNull Context context, int userId, int activityId, Runnable onCommentPosted) { + super(context); + setContentView(R.layout.dialog_comment); + + EditText commentInput = findViewById(R.id.commentInput); + Button sendButton = findViewById(R.id.sendComment); + + sendButton.setOnClickListener(v -> { + String commentText = commentInput.getText().toString().trim(); + if (commentText.isEmpty()) { + Toast.makeText(context, "Escribe un comentario", Toast.LENGTH_SHORT).show(); + return; + } + + JsonObject body = new JsonObject(); + body.addProperty("userId", userId); + body.addProperty("activityId", activityId); + body.addProperty("text", commentText); + + ApiClient.getClient().create(ApiService.class).postComment(body).enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if (response.isSuccessful()) { + Toast.makeText(context, "Comentario enviado", Toast.LENGTH_SHORT).show(); + dismiss(); + onCommentPosted.run(); // recarga comentarios en FragmentProfile + } + } + + @Override + public void onFailure(Call call, Throwable t) { + Toast.makeText(context, "Error al comentar", Toast.LENGTH_SHORT).show(); + } + }); + }); + } +} diff --git a/app/src/main/java/com/santiparra/yomitrack/model/CommentModel.java b/app/src/main/java/com/santiparra/yomitrack/model/CommentModel.java new file mode 100644 index 0000000..a0f3ecd --- /dev/null +++ b/app/src/main/java/com/santiparra/yomitrack/model/CommentModel.java @@ -0,0 +1,31 @@ +package com.santiparra.yomitrack.model; + +public class CommentModel { + public String text; + public boolean liked; + private String username; + private String avatarUrl; + private String created_at; + + + public CommentModel(String text) { + this.text = text; + this.liked = false; + } + + public String getText() { + return text; + } + + public String getUsername() { + return username; + } + + public String getAvatarUrl() { + return avatarUrl; + } + + public String getCreatedAt() { + return created_at; + } +} diff --git a/app/src/main/java/com/santiparra/yomitrack/utils/DateUtils.java b/app/src/main/java/com/santiparra/yomitrack/utils/DateUtils.java new file mode 100644 index 0000000..ceb16bc --- /dev/null +++ b/app/src/main/java/com/santiparra/yomitrack/utils/DateUtils.java @@ -0,0 +1,62 @@ +package com.santiparra.yomitrack.utils; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.*; + +public class DateUtils { + + public static String getRelativeTime(String isoDate) { + try { + // Convertir la fecha ISO + SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault()); + inputFormat.setTimeZone(TimeZone.getTimeZone("UTC")); + Date date = inputFormat.parse(isoDate); + + // Obtener ahora y diferencia + Date now = new Date(); + long diffMillis = now.getTime() - date.getTime(); + long diffSeconds = diffMillis / 1000; + long diffMinutes = diffSeconds / 60; + long diffHours = diffMinutes / 60; + long diffDays = diffHours / 24; + + if (diffSeconds < 60) { + return "Hace unos segundos"; + } else if (diffMinutes < 60) { + return "Hace " + diffMinutes + " min"; + } else if (diffHours < 24 && isSameDay(now, date)) { + return "Hace " + diffHours + " horas"; + } else if (isYesterday(now, date)) { + return "Ayer"; + } else if (diffDays < 7) { + return "Hace " + diffDays + " días"; + } else { + SimpleDateFormat dateFormat = new SimpleDateFormat("d 'de' MMMM 'de' yyyy", new Locale("es")); + return dateFormat.format(date); + } + } catch (ParseException e) { + e.printStackTrace(); + return isoDate; + } + } + + private static boolean isSameDay(Date now, Date date) { + Calendar cal1 = Calendar.getInstance(); + Calendar cal2 = Calendar.getInstance(); + cal1.setTime(now); + cal2.setTime(date); + return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && + cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR); + } + + private static boolean isYesterday(Date now, Date date) { + Calendar cal1 = Calendar.getInstance(); + Calendar cal2 = Calendar.getInstance(); + cal1.setTime(now); + cal2.setTime(date); + cal1.add(Calendar.DAY_OF_YEAR, -1); + return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && + cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR); + } +} diff --git a/app/src/main/res/drawable/ic_heart.xml b/app/src/main/res/drawable/ic_heart.xml new file mode 100644 index 0000000..b769871 --- /dev/null +++ b/app/src/main/res/drawable/ic_heart.xml @@ -0,0 +1,13 @@ + + + diff --git a/app/src/main/res/drawable/ic_heart_filled.xml b/app/src/main/res/drawable/ic_heart_filled.xml new file mode 100644 index 0000000..14fe125 --- /dev/null +++ b/app/src/main/res/drawable/ic_heart_filled.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_heart_outline.xml b/app/src/main/res/drawable/ic_heart_outline.xml new file mode 100644 index 0000000..acdb85a --- /dev/null +++ b/app/src/main/res/drawable/ic_heart_outline.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/dialog_comment.xml b/app/src/main/res/layout/dialog_comment.xml new file mode 100644 index 0000000..5149169 --- /dev/null +++ b/app/src/main/res/layout/dialog_comment.xml @@ -0,0 +1,20 @@ + + + + +