diff --git a/.idea/deploymentTargetSelector.xml b/.idea/deploymentTargetSelector.xml index 1ad60c3..505df14 100644 --- a/.idea/deploymentTargetSelector.xml +++ b/.idea/deploymentTargetSelector.xml @@ -2,8 +2,16 @@ - + diff --git a/.idea/misc.xml b/.idea/misc.xml index 8978d23..74dd639 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,7 @@ + - + diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml index 931b96c..16660f1 100644 --- a/.idea/runConfigurations.xml +++ b/.idea/runConfigurations.xml @@ -5,8 +5,12 @@ diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..c5f3f6b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "java.configuration.updateBuildConfiguration": "interactive" +} \ No newline at end of file diff --git a/app/src/main/java/com/mukudev/easy2do/Fragments/Fragment2DayRule.java b/app/src/main/java/com/mukudev/easy2do/Fragments/Fragment2DayRule.java index ca3c254..8b10893 100644 --- a/app/src/main/java/com/mukudev/easy2do/Fragments/Fragment2DayRule.java +++ b/app/src/main/java/com/mukudev/easy2do/Fragments/Fragment2DayRule.java @@ -1,40 +1,191 @@ + package com.mukudev.easy2do.Fragments; import android.os.Bundle; +import android.content.Context; +import android.content.SharedPreferences; +import android.widget.Button; +import java.util.Set; +import java.util.HashSet; +import java.util.Calendar; +import androidx.appcompat.app.AlertDialog; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.CalendarView; import android.widget.Toast; +import android.widget.TextView; import com.mukudev.easy2do.R; public class Fragment2DayRule extends Fragment { + private Button markDoneButton; + private Set markedDays; + private SharedPreferences sharedPreferences; + private CalendarView calendarView; + private TextView streakTextView; + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.fragment_2day_rule, container, false); - // Initialize the CalendarView - CalendarView calendarView = view.findViewById(R.id.calendarView); + calendarView = view.findViewById(R.id.calendarView); + markDoneButton = view.findViewById(R.id.markDoneButton); + streakTextView = view.findViewById(R.id.streakTextView); - // Set OnDateChangeListener to capture date changes - calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() { - @Override - public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) { + sharedPreferences = requireContext().getSharedPreferences("HabitTracker", Context.MODE_PRIVATE); + markedDays = new HashSet<>(sharedPreferences.getStringSet("markedDays", new HashSet<>())); - String selectedDate = dayOfMonth + "/" + (month + 1) + "/" + year; + setupCalendarView(); + setupMarkDoneButton(); + updateStreak(); + + return view; + } - // Add functionality to save the mark date and it should be asserted because it cannot change after its marked - Toast.makeText(getContext(), "Selected Date: " + selectedDate, Toast.LENGTH_SHORT).show(); + private void setupCalendarView() { + Calendar today = Calendar.getInstance(); + calendarView.setMaxDate(today.getTimeInMillis()); + calendarView.setOnDateChangeListener((view, year, month, dayOfMonth) -> { + Calendar selectedDate = Calendar.getInstance(); + selectedDate.set(year, month, dayOfMonth); + Calendar currentDate = Calendar.getInstance(); + + if (selectedDate.before(currentDate)) { + showPastDatePopup(); + } else if (selectedDate.after(currentDate)) { + Toast.makeText(getContext(), "You cannot select future dates", Toast.LENGTH_SHORT).show(); + } else { + updateMarkDoneButtonState(selectedDate); + if (!isCurrentDateMarked()) { + showMarkAsDoneConfirmation(); + } else { + Toast.makeText(getContext(), "This day is already marked as done!", Toast.LENGTH_SHORT).show(); + } } }); - return view; + calendarView.setDate(today.getTimeInMillis(), false, true); + highlightDates(); + } + + private void setupMarkDoneButton() { + markDoneButton.setOnClickListener(v -> showMarkAsDoneConfirmation()); + updateMarkDoneButtonState(Calendar.getInstance()); + } + + private void showPastDatePopup() { + new AlertDialog.Builder(requireContext()) + .setTitle("Past Date") + .setMessage("You cannot select previous dates.") + .setPositiveButton("OK", null) + .show(); + } + + private void showMarkAsDoneConfirmation() { + new AlertDialog.Builder(requireContext()) + .setTitle("Mark as Done") + .setMessage("Are you sure you want to mark this day as done? This action cannot be undone.") + .setPositiveButton("Yes", (dialog, which) -> markTodayAsDone()) + .setNegativeButton("No", null) + .show(); + } + + private void markTodayAsDone() { + Calendar calendar = Calendar.getInstance(); + String currentDate = formatDate(calendar); + + if (!markedDays.contains(currentDate)) { + markedDays.add(currentDate); + SharedPreferences.Editor editor = sharedPreferences.edit(); + editor.putStringSet("markedDays", markedDays); + editor.apply(); + + Toast.makeText(getContext(), "Marked as done!", Toast.LENGTH_SHORT).show(); + } + updateMarkDoneButtonState(calendar); + updateStreak(); + highlightDates(); + } + + private void updateMarkDoneButtonState(Calendar selectedDate) { + boolean isToday = isToday(selectedDate); + String currentDate = formatDate(selectedDate); + markDoneButton.setEnabled(isToday && !markedDays.contains(currentDate)); + } + private String formatDate(Calendar calendar) { + return calendar.get(Calendar.DAY_OF_MONTH) + "/" + + (calendar.get(Calendar.MONTH) + 1) + "/" + + calendar.get(Calendar.YEAR); } + private boolean isToday(Calendar date) { + Calendar today = Calendar.getInstance(); + return date.get(Calendar.YEAR) == today.get(Calendar.YEAR) && + date.get(Calendar.MONTH) == today.get(Calendar.MONTH) && + date.get(Calendar.DAY_OF_MONTH) == today.get(Calendar.DAY_OF_MONTH); + } + + private boolean isCurrentDateMarked() { + Calendar calendar = Calendar.getInstance(); + String currentDate = formatDate(calendar); + return markedDays.contains(currentDate); + } + + private void updateStreak() { + int streak = calculateStreak(); + streakTextView.setText("Current Streak: " + streak + " days"); + } + + private int calculateStreak() { + Calendar today = Calendar.getInstance(); + int streak = 0; + + while (markedDays.contains(formatDate(today))) { + streak++; + today.add(Calendar.DAY_OF_MONTH, -1); + } + + return streak; + } + + private View getDayViewByDate(long dateInMillis) { + // Implement the logic to get the day view by date + // This is a placeholder implementation and may need to be adjusted based on your CalendarView implementation + return calendarView.findViewWithTag(dateInMillis); + } + + private void highlightDates() { + for (String date : markedDays) { + // Logic to highlight the marked dates + String[] parts = date.split("/"); + int day = Integer.parseInt(parts[0]); + int month = Integer.parseInt(parts[1]) - 1; // Calendar months are 0-based + int year = Integer.parseInt(parts[2]); + + Calendar calendar = Calendar.getInstance(); + calendar.set(year, month, day); + + long dateInMillis = calendar.getTimeInMillis(); + calendarView.setDate(dateInMillis, false, true); + + // Assuming you have a method to get the day view by date + View dayView = getDayViewByDate(dateInMillis); + if (dayView != null) { + dayView.setBackgroundResource(R.drawable.dark_red_circle); + } + } + } + + @Override + public void onResume() { + super.onResume(); + setupCalendarView(); + updateMarkDoneButtonState(Calendar.getInstance()); + updateStreak(); + } } diff --git a/app/src/main/res/drawable/dark_red_circle.xml b/app/src/main/res/drawable/dark_red_circle.xml new file mode 100644 index 0000000..2fd16a2 --- /dev/null +++ b/app/src/main/res/drawable/dark_red_circle.xml @@ -0,0 +1,8 @@ + + + + + + diff --git a/app/src/main/res/layout/fragment_2day_rule.xml b/app/src/main/res/layout/fragment_2day_rule.xml index 6068d0d..ecd9d39 100644 --- a/app/src/main/res/layout/fragment_2day_rule.xml +++ b/app/src/main/res/layout/fragment_2day_rule.xml @@ -1,5 +1,6 @@ + tools:layout_editor_absoluteX="0dp" /> + - - - - + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintEnd_toEndOf="parent"/> diff --git a/app/src/main/res/layout/fragment_2priority_task_rule.xml b/app/src/main/res/layout/fragment_2priority_task_rule.xml index a1b615c..b2b4782 100644 --- a/app/src/main/res/layout/fragment_2priority_task_rule.xml +++ b/app/src/main/res/layout/fragment_2priority_task_rule.xml @@ -12,4 +12,4 @@ android:textAlignment="center" android:textSize="20sp" android:text="2-Priority Tasks for Today!\n(--To be Implemented--)" /> - + \ No newline at end of file diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 35c826f..52779df 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -25,5 +25,19 @@ @color/white @drawable/selector + + - \ No newline at end of file + + + + diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index f45e369..4ce0601 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,5 +1,5 @@ [versions] -agp = "8.7.0" +agp = "8.7.1" junit = "4.13.2" junitVersion = "1.2.1" espressoCore = "3.6.1"