From d9d2badf25d6a899c581a847fdb57ae324ff9a50 Mon Sep 17 00:00:00 2001 From: fernandezian13 Date: Tue, 30 Jul 2024 17:01:33 -0500 Subject: [PATCH 1/5] Update Goal.java Added updateGoalInCSV method updates the goal in the CSV file if it matches the existing goal's attributes. markGoalCompleted method now calls the new updateGoalInCSV method to update the goal's completion status in the CSV file, ensuring the state/checkbox persists. --- .../usta/cs3443/habitquest/model/Goal.java | 91 ++++++++++--------- 1 file changed, 47 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/edu/usta/cs3443/habitquest/model/Goal.java b/app/src/main/java/edu/usta/cs3443/habitquest/model/Goal.java index c88624b..914dfab 100644 --- a/app/src/main/java/edu/usta/cs3443/habitquest/model/Goal.java +++ b/app/src/main/java/edu/usta/cs3443/habitquest/model/Goal.java @@ -19,37 +19,6 @@ import java.util.List; import java.util.Scanner; -/** - * Goal: The Goal class represents a goal or habit that a user wants to track. It includes attributes to describe the goal and methods to manage it. - * - * Attributes: - * goalName - name of goal. - * goalType - type of goal (personal, education, etc). - * goalDescrip - description of the goal. - * goalStart - the date the goal starts. - * goalEnd - the date the goal ends. - * goalCompleted - boolean indicating if the goal was completed. - * - * Methods: - * createGoal() - creates a new goal. - * updateGoal() - updates the goal. - * deleteGoal() - delete the goal. - * getGoal() - retrieves the goal. - * getGoalName() - retrieve goal name. - * getGoalType() - retrieve goal type. - * getGoalDescription() - retrieve goal description. - * getGoalStart() - retrieve start date. // Changed method name from getStart to getGoalStart - * getGoalEnd() - retrieve end date. // Changed method name from getEnd to getGoalEnd - * isGoalCompleted() - true/false completion data. // Changed method name from isCompleted to isGoalCompleted - * setGoalName() - set goal name. - * setGoalType() - set goal type. - * setGoalDescription() - set goal description. - * setGoalStart() - set start date. // Changed method name from setStart to setGoalStart - * setGoalEnd() - set end date. // Changed method name from setEnd to setGoalEnd - * setGoalCompleted() - set completion status. // Changed method name from setCompleted to setGoalCompleted - * isCompleted() - true/false completion data. - */ - public class Goal { private String goalName; private String goalType; @@ -83,12 +52,10 @@ public Goal(String goalName, String goalType, String goalDescription, String goa public void setGoalEnd(String goalEnd) { this.goalEnd = goalEnd; } public void setGoalCompleted(Boolean goalCompleted) { this.goalCompleted = goalCompleted; } - public String toCSVString() { return goalName + "," + goalType + "," + goalDescription + "," + goalStart + "," + goalEnd + "," + goalCompleted; } - // Check if the goal is expired public boolean isExpired(Date today) { SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy"); @@ -189,6 +156,7 @@ public static String readFile(String filename, Context context) throws IOExcepti return content.toString(); } + public static void deleteGoalFromCSV(Goal goalToDelete, Context context) throws IOException { String filename = "goals.csv"; File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), filename); @@ -228,7 +196,50 @@ public static void deleteGoalFromCSV(Goal goalToDelete, Context context) throws } } } - //change goal to completed, this would rewrite the goals.csv file with chage in goalCompleted + + public static void updateGoalInCSV(Goal updatedGoal, Context context) throws IOException { + // New method to update a goal in the CSV file + String filename = "goals.csv"; + File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), filename); + + if (!file.exists()) { + return; + } + + List lines = new ArrayList<>(); + try (Scanner scanner = new Scanner(file)) { + while (scanner.hasNextLine()) { + lines.add(scanner.nextLine()); + } + } + + try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) { + for (String line : lines) { + String[] columns = line.split(","); + if (columns.length == 6) { + String goalName = columns[0].trim(); + String goalType = columns[1].trim(); + String goalDescription = columns[2].trim(); + String goalStart = columns[3].trim(); + String goalEnd = columns[4].trim(); + Boolean goalCompleted = Boolean.parseBoolean(columns[5].trim()); + + if (updatedGoal.getGoalName().equals(goalName) && + updatedGoal.getGoalType().equals(goalType) && + updatedGoal.getGoalDescription().equals(goalDescription) && + updatedGoal.getGoalStart().equals(goalStart) && + updatedGoal.getGoalEnd().equals(goalEnd)) { + writer.write(updatedGoal.toCSVString()); + } else { + writer.write(line); + } + writer.newLine(); + } + } + } + } + + // Change goal to completed, this would rewrite the goals.csv file with change in goalCompleted public void markGoalCompleted(Goal goalToComplete, Context context) throws IOException { List goals = loadGoalsFromCSV(context); @@ -236,20 +247,16 @@ public void markGoalCompleted(Goal goalToComplete, Context context) throws IOExc for (Goal goal : goals) { if (goal.equals(goalToComplete)) { goal.setGoalCompleted(true); + updateGoalInCSV(goal, context); // Update goal in CSV file break; } } - - // Rewrite the CSV file - writeGoalsToCSV(goals, context); } private void writeGoalsToCSV(List goals, Context context) throws IOException { String filename = "goals.csv"; File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), filename); - - try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) { // Write CSV header writer.write("goalName,goalType,goalDescription,goalStart,goalEnd,goalCompleted\n"); @@ -265,8 +272,4 @@ private void writeGoalsToCSV(List goals, Context context) throws IOExcepti } } } - - - - -} \ No newline at end of file +} From f86b1d3c8ccea2bd6637d0dce4f0976503199b41 Mon Sep 17 00:00:00 2001 From: fernandezian13 Date: Tue, 30 Jul 2024 17:05:45 -0500 Subject: [PATCH 2/5] Update Goal.java Re-added the commented description block. --- .../usta/cs3443/habitquest/model/Goal.java | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/edu/usta/cs3443/habitquest/model/Goal.java b/app/src/main/java/edu/usta/cs3443/habitquest/model/Goal.java index 914dfab..6d4f2e3 100644 --- a/app/src/main/java/edu/usta/cs3443/habitquest/model/Goal.java +++ b/app/src/main/java/edu/usta/cs3443/habitquest/model/Goal.java @@ -19,6 +19,37 @@ import java.util.List; import java.util.Scanner; +/** + * Goal: The Goal class represents a goal or habit that a user wants to track. It includes attributes to describe the goal and methods to manage it. + * + * Attributes: + * goalName - name of goal. + * goalType - type of goal (personal, education, etc). + * goalDescrip - description of the goal. + * goalStart - the date the goal starts. + * goalEnd - the date the goal ends. + * goalCompleted - boolean indicating if the goal was completed. + * + * Methods: + * createGoal() - creates a new goal. + * updateGoal() - updates the goal. + * deleteGoal() - delete the goal. + * getGoal() - retrieves the goal. + * getGoalName() - retrieve goal name. + * getGoalType() - retrieve goal type. + * getGoalDescription() - retrieve goal description. + * getGoalStart() - retrieve start date. // Changed method name from getStart to getGoalStart + * getGoalEnd() - retrieve end date. // Changed method name from getEnd to getGoalEnd + * isGoalCompleted() - true/false completion data. // Changed method name from isCompleted to isGoalCompleted + * setGoalName() - set goal name. + * setGoalType() - set goal type. + * setGoalDescription() - set goal description. + * setGoalStart() - set start date. // Changed method name from setStart to setGoalStart + * setGoalEnd() - set end date. // Changed method name from setEnd to setGoalEnd + * setGoalCompleted() - set completion status. // Changed method name from setCompleted to setGoalCompleted + * isCompleted() - true/false completion data. + */ + public class Goal { private String goalName; private String goalType; @@ -241,16 +272,19 @@ public static void updateGoalInCSV(Goal updatedGoal, Context context) throws IOE // Change goal to completed, this would rewrite the goals.csv file with change in goalCompleted public void markGoalCompleted(Goal goalToComplete, Context context) throws IOException { + // Method to mark a goal as completed and update the CSV file List goals = loadGoalsFromCSV(context); // Find the goal to update for (Goal goal : goals) { if (goal.equals(goalToComplete)) { goal.setGoalCompleted(true); - updateGoalInCSV(goal, context); // Update goal in CSV file break; } } + + // Rewrite the CSV file + writeGoalsToCSV(goals, context); } private void writeGoalsToCSV(List goals, Context context) throws IOException { From 5ac682523c435812c03cd823d442ced15752bd21 Mon Sep 17 00:00:00 2001 From: fernandezian13 Date: Tue, 30 Jul 2024 17:12:17 -0500 Subject: [PATCH 3/5] Update GoalAdapter.java Addition of context constructor to call the updateGoalInCSV method to save the completion state of a goal when it is checked. --- .../cs3443/habitquest/model/GoalAdapter.java | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/edu/usta/cs3443/habitquest/model/GoalAdapter.java b/app/src/main/java/edu/usta/cs3443/habitquest/model/GoalAdapter.java index 494ff51..398cf31 100644 --- a/app/src/main/java/edu/usta/cs3443/habitquest/model/GoalAdapter.java +++ b/app/src/main/java/edu/usta/cs3443/habitquest/model/GoalAdapter.java @@ -18,9 +18,11 @@ public class GoalAdapter extends RecyclerView.Adapter { private List goals; + private Context context; - public GoalAdapter(List goals) { + public GoalAdapter(List goals, Context context) { this.goals = goals; + this.context = context; // Added context to the constructor to be able to save the state to CSV. } @NonNull @@ -47,14 +49,12 @@ public void onBindViewHolder(@NonNull GoalViewHolder holder, int position) { // Set a listener to handle changes in the CheckBox holder.goalCompleted.setOnCheckedChangeListener((buttonView, isChecked) -> { goal.setGoalCompleted(isChecked); - // Notify the model or database of the change - // You might need to update the data source or notify the database here - //this code is malformed and needs to be fixed, it duplicates the all the goals in the list - /*try { - goal.markGoalCompleted(goal, holder.itemView.getContext()); + // Save the state to the CSV file + try { + Goal.updateGoalInCSV(goal, context); // Added call to update CSV when checkbox is changed } catch (IOException e) { - throw new RuntimeException(e); - }*/ + e.printStackTrace(); + } }); holder.deleteGoal.setOnClickListener(v -> { @@ -79,8 +79,6 @@ private void deleteGoal(Goal goal, Context context, int position) { } } - - @Override public int getItemCount() { return goals.size(); @@ -106,4 +104,4 @@ public GoalViewHolder(@NonNull View itemView) { deleteGoal = itemView.findViewById(R.id.deleteGoal); } } -} \ No newline at end of file +} From fc94758a14ee64d252c32ff0eaf6d2868134d558 Mon Sep 17 00:00:00 2001 From: fernandezian13 Date: Tue, 30 Jul 2024 17:26:24 -0500 Subject: [PATCH 4/5] Update set_Goal_Activity.java Adapter now has access to the context of the activity. --- .../cs3443/habitquest/set_Goal_Activity.java | 185 ++++++------------ 1 file changed, 65 insertions(+), 120 deletions(-) diff --git a/app/src/main/java/edu/usta/cs3443/habitquest/set_Goal_Activity.java b/app/src/main/java/edu/usta/cs3443/habitquest/set_Goal_Activity.java index 54994a2..c6c954f 100644 --- a/app/src/main/java/edu/usta/cs3443/habitquest/set_Goal_Activity.java +++ b/app/src/main/java/edu/usta/cs3443/habitquest/set_Goal_Activity.java @@ -2,144 +2,89 @@ import android.content.Intent; import android.os.Bundle; -import android.os.Environment; -import android.util.Log; -import android.view.View; -import android.widget.ArrayAdapter; import android.widget.Button; -import android.widget.CheckBox; -import android.widget.EditText; -import android.widget.Spinner; -import android.widget.TextView; -import android.widget.Toast; +import android.view.View; +import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; +import androidx.core.graphics.Insets; +import androidx.core.view.ViewCompat; +import androidx.core.view.WindowInsetsCompat; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; +import java.util.List; import edu.usta.cs3443.habitquest.model.Goal; +import edu.usta.cs3443.habitquest.model.GoalAdapter; -public class set_Goal_Activity extends AppCompatActivity { - - private static final String TAG = "set_Goal_Activity"; - - private EditText goalNameEditText, goalDescriptionEditText, goalStartDateEditText, goalEndDateEditText; - private CheckBox checkbox1, checkbox2, checkbox3, checkbox4, checkbox5, checkbox6, checkbox7; - private Spinner goalTypeSpinner; - private Button addHabitButton, goBackButton; +public class todays_goal_Activity extends AppCompatActivity { + private static final String TAG = "todays_goal_Activity"; + private RecyclerView recyclerView; + private GoalAdapter goalAdapter; + private List goalList; + private Button addHabitButton; + private Button homeButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.activity_set_goal); - - goalNameEditText = findViewById(R.id.goalNameEditText); - goalDescriptionEditText = findViewById(R.id.goalDescriptionEditText); - goalStartDateEditText = findViewById(R.id.goalStartDateEditText); - goalEndDateEditText = findViewById(R.id.goalEndDateEditText); - goalTypeSpinner = findViewById(R.id.goalTypeSpinner); - - // Find the TextView by its ID - TextView goalRecurrenceLabel = findViewById(R.id.GoalRecurrenceLabel); - - // Set the visibility to GONE or INVISIBLE - goalRecurrenceLabel.setVisibility(View.GONE); - - // Find all TextView views by their IDs - TextView label1 = findViewById(R.id.label1); - TextView label2 = findViewById(R.id.label2); - TextView label3 = findViewById(R.id.label3); - TextView label4 = findViewById(R.id.label4); - TextView label5 = findViewById(R.id.label5); - TextView label6 = findViewById(R.id.label6); - TextView label7 = findViewById(R.id.label7); - - // Set visibility to GONE or INVISIBLE - label1.setVisibility(View.GONE); - label2.setVisibility(View.GONE); - label3.setVisibility(View.GONE); - label4.setVisibility(View.GONE); - label5.setVisibility(View.GONE); - label6.setVisibility(View.GONE); - label7.setVisibility(View.GONE); - - // Find all CheckBox views by their IDs - CheckBox checkbox1 = findViewById(R.id.checkbox1); - CheckBox checkbox2 = findViewById(R.id.checkbox2); - CheckBox checkbox3 = findViewById(R.id.checkbox3); - CheckBox checkbox4 = findViewById(R.id.checkbox4); - CheckBox checkbox5 = findViewById(R.id.checkbox5); - CheckBox checkbox6 = findViewById(R.id.checkbox6); - CheckBox checkbox7 = findViewById(R.id.checkbox7); - - // Set visibility to GONE or INVISIBLE - checkbox1.setVisibility(View.GONE); - checkbox2.setVisibility(View.GONE); - checkbox3.setVisibility(View.GONE); - checkbox4.setVisibility(View.GONE); - checkbox5.setVisibility(View.GONE); - checkbox6.setVisibility(View.GONE); - checkbox7.setVisibility(View.GONE); - - checkbox1 = findViewById(R.id.checkbox1); - checkbox2 = findViewById(R.id.checkbox2); - checkbox3 = findViewById(R.id.checkbox3); - checkbox4 = findViewById(R.id.checkbox4); - checkbox5 = findViewById(R.id.checkbox5); - checkbox6 = findViewById(R.id.checkbox6); - checkbox7 = findViewById(R.id.checkbox7); - - ArrayAdapter adapter = ArrayAdapter.createFromResource(this, - R.array.goal_type_array, android.R.layout.simple_spinner_item); - adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - goalTypeSpinner.setAdapter(adapter); + EdgeToEdge.enable(this); + setContentView(R.layout.activity_todays_goal); - addHabitButton = findViewById(R.id.addHabitButton); - addHabitButton.setOnClickListener(v -> addAndReadHabit()); - - goBackButton = findViewById(R.id.goBackButton); - goBackButton.setOnClickListener(v -> { - Intent intent = new Intent(set_Goal_Activity.this, MainActivity.class); - startActivity(intent); - } - ); - } + ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { + Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); + v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); + return insets; + }); - private void addAndReadHabit() { - String goalName = goalNameEditText.getText().toString(); - String goalDescription = goalDescriptionEditText.getText().toString(); - String goalStartDate = goalStartDateEditText.getText().toString(); - String goalEndDate = goalEndDateEditText.getText().toString(); - String goalType = (String) goalTypeSpinner.getSelectedItem(); + // Initialize RecyclerView + recyclerView = findViewById(R.id.recyclerViewGoals); + recyclerView.setLayoutManager(new LinearLayoutManager(this)); - if (goalName.isEmpty() || goalDescription.isEmpty() || goalStartDate.isEmpty() || goalEndDate.isEmpty() || goalType.isEmpty()) { - Toast.makeText(this, "Please fill in all fields", Toast.LENGTH_SHORT).show(); - return; - } + // Load goals from CSV and set adapter + loadGoals(); - // Create a new Goal object - Goal newGoal = new Goal(goalName, goalType, goalDescription, goalStartDate, goalEndDate); - - // Path to the external storage file - File file = new File(getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), "goals.csv"); - Log.d(TAG, "File path: " + file.getAbsolutePath()); + homeButton = findViewById(R.id.homeButton); + addHabitButton = findViewById(R.id.addHabitButton); - // Add the habit to the CSV file - try (BufferedWriter writer = new BufferedWriter(new FileWriter(file, true))) { - writer.write(newGoal.getGoalName() + "," + newGoal.getGoalType() + "," + newGoal.getGoalDescription() + "," + newGoal.getGoalStart() + "," + newGoal.getGoalEnd() + "," + "false" + "\n"); - Log.d(TAG, "Goal added: " + newGoal.getGoalName()); + // Home button + homeButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent intent = new Intent(todays_goal_Activity.this, MainActivity.class); + startActivity(intent); + } + }); + + // Add habit button + addHabitButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent intent = new Intent(todays_goal_Activity.this, set_Goal_Activity.class); + startActivity(intent); + } + }); - Toast.makeText(this, "Goal added successfully!", Toast.LENGTH_SHORT).show(); + } - // Redirect to today's goals activity - Intent intent = new Intent(set_Goal_Activity.this, todays_goal_Activity.class); - startActivity(intent); - } catch (IOException e) { - Log.e(TAG, "Error writing to CSV", e); - Toast.makeText(this, "Error adding goal", Toast.LENGTH_SHORT).show(); + private void loadGoals() { + // Load goals from goals.csv file + goalList = Goal.loadGoalsFromCSV(this); + + // Initialize or update adapter + if (goalAdapter == null) { + // Added context parameter to GoalAdapter constructor + goalAdapter = new GoalAdapter(goalList, this); + recyclerView.setAdapter(goalAdapter); + } else { + goalAdapter.notifyDataSetChanged(); // Refresh the adapter if it already exists } } + + @Override + protected void onResume() { + super.onResume(); + loadGoals(); + } } From 3cc6906a6487b109aea94ed4d5fb6015bc4d1e18 Mon Sep 17 00:00:00 2001 From: fernandezian13 Date: Tue, 30 Jul 2024 17:39:51 -0500 Subject: [PATCH 5/5] Update set_Goal_Activity.java --- .../cs3443/habitquest/set_Goal_Activity.java | 177 +++++++++++------- 1 file changed, 112 insertions(+), 65 deletions(-) diff --git a/app/src/main/java/edu/usta/cs3443/habitquest/set_Goal_Activity.java b/app/src/main/java/edu/usta/cs3443/habitquest/set_Goal_Activity.java index c6c954f..adefdfd 100644 --- a/app/src/main/java/edu/usta/cs3443/habitquest/set_Goal_Activity.java +++ b/app/src/main/java/edu/usta/cs3443/habitquest/set_Goal_Activity.java @@ -2,89 +2,136 @@ import android.content.Intent; import android.os.Bundle; -import android.widget.Button; +import android.os.Environment; +import android.util.Log; import android.view.View; +import android.widget.ArrayAdapter; +import android.widget.Button; +import android.widget.CheckBox; +import android.widget.EditText; +import android.widget.Spinner; +import android.widget.TextView; +import android.widget.Toast; -import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; -import androidx.core.graphics.Insets; -import androidx.core.view.ViewCompat; -import androidx.core.view.WindowInsetsCompat; -import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; -import java.util.List; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; import edu.usta.cs3443.habitquest.model.Goal; -import edu.usta.cs3443.habitquest.model.GoalAdapter; -public class todays_goal_Activity extends AppCompatActivity { - private static final String TAG = "todays_goal_Activity"; - private RecyclerView recyclerView; - private GoalAdapter goalAdapter; - private List goalList; - private Button addHabitButton; - private Button homeButton; +public class set_Goal_Activity extends AppCompatActivity { + + private static final String TAG = "set_Goal_Activity"; + + private EditText goalNameEditText, goalDescriptionEditText, goalStartDateEditText, goalEndDateEditText; + private CheckBox checkbox1, checkbox2, checkbox3, checkbox4, checkbox5, checkbox6, checkbox7; + private Spinner goalTypeSpinner; + private Button addHabitButton, goBackButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - EdgeToEdge.enable(this); - setContentView(R.layout.activity_todays_goal); + setContentView(R.layout.activity_set_goal); + + goalNameEditText = findViewById(R.id.goalNameEditText); + goalDescriptionEditText = findViewById(R.id.goalDescriptionEditText); + goalStartDateEditText = findViewById(R.id.goalStartDateEditText); + goalEndDateEditText = findViewById(R.id.goalEndDateEditText); + goalTypeSpinner = findViewById(R.id.goalTypeSpinner); + + // Find the TextView by its ID + TextView goalRecurrenceLabel = findViewById(R.id.GoalRecurrenceLabel); + + // Set the visibility to GONE or INVISIBLE + goalRecurrenceLabel.setVisibility(View.GONE); + + // Find all TextView views by their IDs + TextView label1 = findViewById(R.id.label1); + TextView label2 = findViewById(R.id.label2); + TextView label3 = findViewById(R.id.label3); + TextView label4 = findViewById(R.id.label4); + TextView label5 = findViewById(R.id.label5); + TextView label6 = findViewById(R.id.label6); + TextView label7 = findViewById(R.id.label7); + + // Set visibility to GONE or INVISIBLE + label1.setVisibility(View.GONE); + label2.setVisibility(View.GONE); + label3.setVisibility(View.GONE); + label4.setVisibility(View.GONE); + label5.setVisibility(View.GONE); + label6.setVisibility(View.GONE); + label7.setVisibility(View.GONE); + + // Find all CheckBox views by their IDs + checkbox1 = findViewById(R.id.checkbox1); + checkbox2 = findViewById(R.id.checkbox2); + checkbox3 = findViewById(R.id.checkbox3); + checkbox4 = findViewById(R.id.checkbox4); + checkbox5 = findViewById(R.id.checkbox5); + checkbox6 = findViewById(R.id.checkbox6); + checkbox7 = findViewById(R.id.checkbox7); + + // Set visibility to GONE or INVISIBLE + checkbox1.setVisibility(View.GONE); + checkbox2.setVisibility(View.GONE); + checkbox3.setVisibility(View.GONE); + checkbox4.setVisibility(View.GONE); + checkbox5.setVisibility(View.GONE); + checkbox6.setVisibility(View.GONE); + checkbox7.setVisibility(View.GONE); + + ArrayAdapter adapter = ArrayAdapter.createFromResource(this, + R.array.goal_type_array, android.R.layout.simple_spinner_item); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + goalTypeSpinner.setAdapter(adapter); - ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { - Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); - v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); - return insets; - }); + addHabitButton = findViewById(R.id.addHabitButton); + addHabitButton.setOnClickListener(v -> addAndReadHabit()); + + goBackButton = findViewById(R.id.goBackButton); + goBackButton.setOnClickListener(v -> { + Intent intent = new Intent(set_Goal_Activity.this, MainActivity.class); + startActivity(intent); + } + ); + } - // Initialize RecyclerView - recyclerView = findViewById(R.id.recyclerViewGoals); - recyclerView.setLayoutManager(new LinearLayoutManager(this)); + private void addAndReadHabit() { + String goalName = goalNameEditText.getText().toString(); + String goalDescription = goalDescriptionEditText.getText().toString(); + String goalStartDate = goalStartDateEditText.getText().toString(); + String goalEndDate = goalEndDateEditText.getText().toString(); + String goalType = (String) goalTypeSpinner.getSelectedItem(); - // Load goals from CSV and set adapter - loadGoals(); + if (goalName.isEmpty() || goalDescription.isEmpty() || goalStartDate.isEmpty() || goalEndDate.isEmpty() || goalType.isEmpty()) { + Toast.makeText(this, "Please fill in all fields", Toast.LENGTH_SHORT).show(); + return; + } - homeButton = findViewById(R.id.homeButton); - addHabitButton = findViewById(R.id.addHabitButton); + // Create a new Goal object + Goal newGoal = new Goal(goalName, goalType, goalDescription, goalStartDate, goalEndDate); - // Home button - homeButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - Intent intent = new Intent(todays_goal_Activity.this, MainActivity.class); - startActivity(intent); - } - }); - - // Add habit button - addHabitButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - Intent intent = new Intent(todays_goal_Activity.this, set_Goal_Activity.class); - startActivity(intent); - } - }); + // Path to the external storage file + File file = new File(getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), "goals.csv"); + Log.d(TAG, "File path: " + file.getAbsolutePath()); - } + // Add the habit to the CSV file + try (BufferedWriter writer = new BufferedWriter(new FileWriter(file, true))) { + writer.write(newGoal.getGoalName() + "," + newGoal.getGoalType() + "," + newGoal.getGoalDescription() + "," + newGoal.getGoalStart() + "," + newGoal.getGoalEnd() + "," + "false" + "\n"); + Log.d(TAG, "Goal added: " + newGoal.getGoalName()); - private void loadGoals() { - // Load goals from goals.csv file - goalList = Goal.loadGoalsFromCSV(this); - - // Initialize or update adapter - if (goalAdapter == null) { - // Added context parameter to GoalAdapter constructor - goalAdapter = new GoalAdapter(goalList, this); - recyclerView.setAdapter(goalAdapter); - } else { - goalAdapter.notifyDataSetChanged(); // Refresh the adapter if it already exists - } - } + Toast.makeText(this, "Goal added successfully!", Toast.LENGTH_SHORT).show(); - @Override - protected void onResume() { - super.onResume(); - loadGoals(); + // Redirect to today's goals activity + Intent intent = new Intent(set_Goal_Activity.this, todays_goal_Activity.class); + startActivity(intent); + } catch (IOException e) { + Log.e(TAG, "Error writing to CSV", e); + Toast.makeText(this, "Error adding goal", Toast.LENGTH_SHORT).show(); + } } }