esta guardar e nao esta a entar no perfil
This commit is contained in:
@@ -24,15 +24,24 @@ public class MedicationDialog extends DialogFragment {
|
||||
private TextView textTime;
|
||||
private Medication medicationToEdit;
|
||||
private OnMedicationSaveListener listener;
|
||||
private OnMedicationDeleteListener deleteListener;
|
||||
|
||||
public interface OnMedicationSaveListener {
|
||||
void onSave(Medication medication);
|
||||
}
|
||||
|
||||
public interface OnMedicationDeleteListener {
|
||||
void onDelete(Medication medication);
|
||||
}
|
||||
|
||||
public void setListener(OnMedicationSaveListener listener) {
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
public void setDeleteListener(OnMedicationDeleteListener listener) {
|
||||
this.deleteListener = listener;
|
||||
}
|
||||
|
||||
public void setMedicationToEdit(Medication medication) {
|
||||
this.medicationToEdit = medication;
|
||||
}
|
||||
@@ -108,10 +117,48 @@ public class MedicationDialog extends DialogFragment {
|
||||
if (listener != null)
|
||||
listener.onSave(newMed);
|
||||
}
|
||||
})
|
||||
.setNegativeButton("Cancelar", (dialog, id) -> dismiss());
|
||||
});
|
||||
|
||||
return builder.create();
|
||||
if (medicationToEdit != null) {
|
||||
builder.setNeutralButton("Eliminar", (dialog, id) -> {
|
||||
if (deleteListener != null) {
|
||||
deleteListener.onDelete(medicationToEdit);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
AlertDialog alertDialog = builder.create();
|
||||
|
||||
alertDialog.setOnShowListener(d -> {
|
||||
android.widget.Button btnPos = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
|
||||
if (btnPos != null) {
|
||||
// Apply our custom outline drawable to "Guardar"
|
||||
btnPos.setBackgroundResource(R.drawable.btn_outline_primary);
|
||||
btnPos.setTextColor(androidx.core.content.ContextCompat.getColor(requireContext(), R.color.primary_color));
|
||||
|
||||
int paddingPx = (int) (16 * getResources().getDisplayMetrics().density);
|
||||
btnPos.setPadding(paddingPx, 0, paddingPx, 0);
|
||||
}
|
||||
|
||||
android.widget.Button btnNeu = alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL);
|
||||
if (btnNeu != null) {
|
||||
btnNeu.setTextColor(androidx.core.content.ContextCompat.getColor(requireContext(), R.color.error_color));
|
||||
btnNeu.setBackgroundResource(R.drawable.btn_outline_error);
|
||||
|
||||
int paddingPx = (int) (16 * getResources().getDisplayMetrics().density);
|
||||
btnNeu.setPadding(paddingPx, 0, paddingPx, 0);
|
||||
|
||||
android.view.ViewGroup.LayoutParams lp = btnNeu.getLayoutParams();
|
||||
if (lp instanceof android.view.ViewGroup.MarginLayoutParams) {
|
||||
android.view.ViewGroup.MarginLayoutParams marginLp = (android.view.ViewGroup.MarginLayoutParams) lp;
|
||||
int marginPx = (int) (8 * getResources().getDisplayMetrics().density);
|
||||
marginLp.setMargins(marginLp.leftMargin, marginLp.topMargin, marginLp.rightMargin + marginPx, marginLp.bottomMargin);
|
||||
btnNeu.setLayoutParams(marginLp);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return alertDialog;
|
||||
}
|
||||
|
||||
private void showTimePicker() {
|
||||
|
||||
@@ -84,6 +84,20 @@ public class MedicationFragment extends Fragment {
|
||||
|
||||
dialog.dismiss();
|
||||
});
|
||||
|
||||
dialog.setDeleteListener(medicationToDelete -> {
|
||||
medicationViewModel.delete(medicationToDelete);
|
||||
|
||||
// Cancel alarm if configured
|
||||
try {
|
||||
com.example.cuida.utils.AlarmScheduler.cancelAlarm(
|
||||
requireContext(),
|
||||
medicationToDelete.name.hashCode());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
|
||||
dialog.show(getParentFragmentManager(), "MedicationDialog");
|
||||
}
|
||||
|
||||
|
||||
@@ -104,4 +104,15 @@ public class MedicationViewModel extends AndroidViewModel {
|
||||
.addOnSuccessListener(aVoid -> Log.d("MedicationViewModel", "Medication updated"))
|
||||
.addOnFailureListener(e -> Log.w("MedicationViewModel", "Error updating medication", e));
|
||||
}
|
||||
|
||||
public void delete(Medication medication) {
|
||||
if (auth.getCurrentUser() == null || medication.id == null)
|
||||
return;
|
||||
|
||||
db.collection("medicamentos")
|
||||
.document(medication.id)
|
||||
.delete()
|
||||
.addOnSuccessListener(aVoid -> Log.d("MedicationViewModel", "Medication deleted"))
|
||||
.addOnFailureListener(e -> Log.w("MedicationViewModel", "Error deleting medication", e));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,4 +45,20 @@ public class AlarmScheduler {
|
||||
alarmManager.set(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent);
|
||||
}
|
||||
}
|
||||
|
||||
public static void cancelAlarm(Context context, int requestCode) {
|
||||
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||
if (alarmManager == null)
|
||||
return;
|
||||
|
||||
Intent intent = new Intent(context, AlarmReceiver.class);
|
||||
PendingIntent pendingIntent = PendingIntent.getBroadcast(
|
||||
context,
|
||||
requestCode,
|
||||
intent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
|
||||
|
||||
alarmManager.cancel(pendingIntent);
|
||||
pendingIntent.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
11
app/src/main/res/drawable/btn_outline_error.xml
Normal file
11
app/src/main/res/drawable/btn_outline_error.xml
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:color="#40B00020">
|
||||
<item>
|
||||
<shape android:shape="rectangle">
|
||||
<solid android:color="@android:color/transparent" />
|
||||
<stroke android:width="2dp" android:color="@color/error_color" />
|
||||
<corners android:radius="8dp" />
|
||||
</shape>
|
||||
</item>
|
||||
</ripple>
|
||||
11
app/src/main/res/drawable/btn_outline_primary.xml
Normal file
11
app/src/main/res/drawable/btn_outline_primary.xml
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:color="#400066CC">
|
||||
<item>
|
||||
<shape android:shape="rectangle">
|
||||
<solid android:color="@android:color/transparent" />
|
||||
<stroke android:width="2dp" android:color="@color/primary_color" />
|
||||
<corners android:radius="8dp" />
|
||||
</shape>
|
||||
</item>
|
||||
</ripple>
|
||||
Reference in New Issue
Block a user