Skip to content

Commit

Permalink
improved Implement the 2 Priority Tasks for the Day
Browse files Browse the repository at this point in the history
fixed issue ContriHUB#4 . also improved UI
  • Loading branch information
RITIK-CHAUDHRY committed Oct 29, 2024
1 parent 5df9eff commit 6fcfb5b
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 91 deletions.
1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
import androidx.fragment.app.Fragment;

import com.mukudev.easy2do.R;
import com.google.android.material.card.MaterialCardView;

public class Fragment2PriorityTaskRule extends Fragment {
// List of motivational quotes
private static final String[] quotes = {
"Great job on completing your first task!",
"Awesome! You've completed both tasks!",
"Keep up the great work!",
"You're doing amazing!",
"Fantastic effort!"
"Great job on completing your first task!",
"Keep up the great work!",
"You're doing amazing!",
"You're making excellent progress!",
"Well done on prioritizing your tasks!"
};

// Function to get a random quote
Expand All @@ -36,31 +37,46 @@ private String getRandomQuote() {
return quotes[index];
}

private void handleTaskCompletion(CheckBox checkbox, EditText input, String taskKey, String message, SharedPreferences.Editor editor, CheckBox nextTaskCheckbox) {
private void handleTaskCompletion(CheckBox checkbox, EditText input, String taskKey, String message, SharedPreferences.Editor editor, View cardView) {
new AlertDialog.Builder(getContext())
.setTitle("Confirm Task Completion")
.setMessage("Are you sure you want to mark this task as completed? This action cannot be undone.")
.setPositiveButton(android.R.string.yes, (dialog, which) -> {
input.setEnabled(false);
checkbox.setEnabled(false);
editor.putBoolean(taskKey, true);
editor.apply();
Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
if (nextTaskCheckbox != null) {
nextTaskCheckbox.setEnabled(true);
}
String quote = getRandomQuote();
TextView motivationalMessage = getView().findViewById(R.id.motivational_message);
motivationalMessage.setText(quote);
motivationalMessage.setVisibility(View.VISIBLE);
})
.setNegativeButton(android.R.string.no, (dialog, which) -> {
checkbox.setChecked(false);
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
.setTitle("Confirm Task Completion")
.setMessage("Are you sure you want to mark this task as completed? This action cannot be undone.")
.setPositiveButton(android.R.string.yes, (dialog, which) -> {
input.setEnabled(false);
checkbox.setEnabled(false);
editor.putBoolean(taskKey, true);
editor.apply();
Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
cardView.setBackgroundColor(getResources().getColor(android.R.color.holo_green_light));

boolean bothTasksCompleted = getActivity().getSharedPreferences("PriorityTasks", Context.MODE_PRIVATE)
.getBoolean("task1_completed", false) && getActivity().getSharedPreferences("PriorityTasks", Context.MODE_PRIVATE)
.getBoolean("task2_completed", false);

TextView motivationalMessage = getView().findViewById(R.id.motivational_message);
if (bothTasksCompleted) {
motivationalMessage.setText("Awesome! You've completed both tasks!");
} else if (getRandomQuote().equals("Fantastic effort!")) {
String quote = getRandomQuote();
while (quote.equals("Fantastic effort!")) {
quote = getRandomQuote();
}
motivationalMessage.setText(quote);
} else {
motivationalMessage.setText(getRandomQuote());
}
motivationalMessage.setVisibility(View.VISIBLE);
})
.setNegativeButton(android.R.string.no, (dialog, which) -> {
checkbox.setChecked(false);
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}




@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Expand All @@ -71,6 +87,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
EditText task2Input = view.findViewById(R.id.task2_input);
CheckBox task2Checkbox = view.findViewById(R.id.task2_checkbox);
TextView motivationalMessage = view.findViewById(R.id.motivational_message);
View task1Card = view.findViewById(R.id.task1_card);
View task2Card = view.findViewById(R.id.task2_card);

SharedPreferences sharedPreferences = getActivity().getSharedPreferences("PriorityTasks", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
Expand All @@ -97,80 +115,63 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
task1Input.setEnabled(true);
task2Input.setEnabled(true);
task1Checkbox.setEnabled(true);
task2Checkbox.setEnabled(false); // Disable Task 2 checkbox on reset
task2Checkbox.setEnabled(true); // Enable both checkboxes
motivationalMessage.setVisibility(View.GONE);
task1Completed = false;
task2Completed = false;
} else {
// Load saved tasks
// Restore saved state
task1Input.setText(sharedPreferences.getString("task1", ""));
task2Input.setText(sharedPreferences.getString("task2", ""));
task1Checkbox.setChecked(task1Completed);
task2Checkbox.setChecked(task2Completed);
task1Input.setEnabled(!task1Completed);
task2Input.setEnabled(!task2Completed);
task1Checkbox.setEnabled(!task1Completed);
task2Checkbox.setEnabled(!task2Completed);
}

// Update UI based on task completion status
if (task1Completed) {
task1Input.setEnabled(false);
task1Checkbox.setEnabled(false);
task2Checkbox.setEnabled(true);
String quote1 = getRandomQuote();
motivationalMessage.setText(quote1);
motivationalMessage.setVisibility(View.VISIBLE);
} else {
task2Checkbox.setEnabled(false);
}

if (task2Completed) {
task2Input.setEnabled(false);
task2Checkbox.setEnabled(false);
String quote2 = getRandomQuote();
motivationalMessage.setText(quote2);
motivationalMessage.setVisibility(View.VISIBLE);
}

// Set up checkbox listeners
task1Checkbox.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked) {
handleTaskCompletion(task1Checkbox, task1Input, "task1_completed", "Great job on completing your first task!", editor, task2Checkbox);
}
});

task2Checkbox.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked) {
handleTaskCompletion(task2Checkbox, task2Input, "task2_completed", "Awesome! You've completed both tasks!", editor, null);
}
});

// Save tasks on input change
task1Input.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) {
public void onTextChanged(CharSequence s, int start, int before, int count) {}

@Override
public void afterTextChanged(Editable s) {
editor.putString("task1", s.toString());
editor.apply();
}

@Override
public void afterTextChanged(Editable s) {}
});

task2Input.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) {
public void onTextChanged(CharSequence s, int start, int before, int count) {}

@Override
public void afterTextChanged(Editable s) {
editor.putString("task2", s.toString());
editor.apply();
}
});

@Override
public void afterTextChanged(Editable s) {}
task1Checkbox.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked) {
handleTaskCompletion(task1Checkbox, task1Input, "task1_completed", "Task 1 completed!", editor, task1Card);
}
});

task2Checkbox.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked) {
handleTaskCompletion(task2Checkbox, task2Input, "task2_completed", "Task 2 completed!", editor, task2Card);
}
});

return view;
}

}
114 changes: 94 additions & 20 deletions app/src/main/res/layout/fragment_2priority_task_rule.xml
Original file line number Diff line number Diff line change
@@ -1,37 +1,111 @@

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
android:padding="16dp"
android:background="@android:color/white">

<EditText
android:id="@+id/task1_input"
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your first priority task"
android:layout_marginBottom="8dp" />
android:text="2 Priority Tasks"
android:textSize="24sp"
android:textStyle="bold"
android:gravity="center"
android:layout_marginBottom="16dp"
android:textColor="@android:color/black"/>

<CheckBox
android:id="@+id/task1_checkbox"
android:layout_width="wrap_content"
<com.google.android.material.card.MaterialCardView
android:id="@+id/task1_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Task 1 Completed"
android:layout_marginBottom="16dp" />
android:layout_marginBottom="16dp"
app:cardElevation="4dp"
app:cardCornerRadius="8dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">

<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@android:drawable/ic_menu_edit"
android:layout_marginEnd="8dp"
app:tint="@android:color/holo_blue_dark"/>

<EditText
android:id="@+id/task1_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Enter your first priority task"
android:background="@null"/>
</LinearLayout>

<EditText
android:id="@+id/task2_input"
<CheckBox
android:id="@+id/task1_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Task 1 Completed"
android:layout_marginTop="8dp"/>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>

<com.google.android.material.card.MaterialCardView
android:id="@+id/task2_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your second priority task"
android:layout_marginBottom="8dp" />
android:layout_marginBottom="16dp"
app:cardElevation="4dp"
app:cardCornerRadius="8dp">

<CheckBox
android:id="@+id/task2_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Task 2 Completed"
android:layout_marginBottom="16dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">

<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@android:drawable/ic_menu_edit"
android:layout_marginEnd="8dp"
app:tint="@android:color/holo_orange_dark"/>

<EditText
android:id="@+id/task2_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Enter your second priority task"
android:background="@null"/>
</LinearLayout>

<CheckBox
android:id="@+id/task2_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Task 2 Completed"
android:layout_marginTop="8dp"/>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>

<TextView
android:id="@+id/motivational_message"
Expand Down

0 comments on commit 6fcfb5b

Please sign in to comment.