Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fernandezian13 checkbox bugfix #22

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 47 additions & 10 deletions app/src/main/java/edu/usta/cs3443/habitquest/model/Goal.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,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");
Expand Down Expand Up @@ -189,6 +187,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);
Expand Down Expand Up @@ -228,8 +227,52 @@ 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<String> 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 {
// Method to mark a goal as completed and update the CSV file
List<Goal> goals = loadGoalsFromCSV(context);

// Find the goal to update
Expand All @@ -248,8 +291,6 @@ private void writeGoalsToCSV(List<Goal> goals, Context context) throws IOExcepti
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");
Expand All @@ -265,8 +306,4 @@ private void writeGoalsToCSV(List<Goal> goals, Context context) throws IOExcepti
}
}
}




}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

public class GoalAdapter extends RecyclerView.Adapter<GoalAdapter.GoalViewHolder> {
private List<Goal> goals;
private Context context;

public GoalAdapter(List<Goal> goals) {
public GoalAdapter(List<Goal> goals, Context context) {
this.goals = goals;
this.context = context; // Added context to the constructor to be able to save the state to CSV.
}

@NonNull
Expand All @@ -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 -> {
Expand All @@ -79,8 +79,6 @@ private void deleteGoal(Goal goal, Context context, int position) {
}
}



@Override
public int getItemCount() {
return goals.size();
Expand All @@ -106,4 +104,4 @@ public GoalViewHolder(@NonNull View itemView) {
deleteGoal = itemView.findViewById(R.id.deleteGoal);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ protected void onCreate(Bundle savedInstanceState) {
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);
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);
Expand All @@ -84,14 +84,6 @@ protected void onCreate(Bundle savedInstanceState) {
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<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.goal_type_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Expand Down