|
| 1 | +package com.btn.pronotes; |
| 2 | + |
| 3 | +import android.content.Intent; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.view.View; |
| 6 | +import android.widget.EditText; |
| 7 | + |
| 8 | +import androidx.appcompat.app.AppCompatActivity; |
| 9 | +import androidx.recyclerview.widget.LinearLayoutManager; |
| 10 | +import androidx.recyclerview.widget.RecyclerView; |
| 11 | + |
| 12 | +import com.btn.pronotes.Models.Notes; |
| 13 | +import com.google.android.material.floatingactionbutton.FloatingActionButton; |
| 14 | + |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | + |
| 19 | +public class ChecklistNotesActivity extends AppCompatActivity { |
| 20 | + |
| 21 | + private EditText editTextChecklistItem; |
| 22 | + private RecyclerView recyclerViewChecklist; |
| 23 | + private FloatingActionButton fabAddChecklistItem; |
| 24 | + |
| 25 | + private List<String> checklistItems; |
| 26 | + private ChecklistAdapter checklistAdapter; |
| 27 | + |
| 28 | + @Override |
| 29 | + protected void onCreate(Bundle savedInstanceState) { |
| 30 | + super.onCreate(savedInstanceState); |
| 31 | + |
| 32 | + setContentView(R.layout.activity_checklist_notes); |
| 33 | + editTextChecklistItem = findViewById(R.id.editText_checklist_item); |
| 34 | + recyclerViewChecklist = findViewById(R.id.rv_checklist); |
| 35 | + fabAddChecklistItem = findViewById(R.id.fab_add_checklist_item); |
| 36 | + checklistItems = new ArrayList<>(); |
| 37 | + checklistItems.add("Option 1"); |
| 38 | + checklistItems.add("Option 2"); |
| 39 | + checklistItems.add("Option 3"); |
| 40 | + |
| 41 | + checklistAdapter = new ChecklistAdapter(); |
| 42 | + recyclerViewChecklist.setLayoutManager(new LinearLayoutManager(this)); |
| 43 | + recyclerViewChecklist.setAdapter(checklistAdapter); |
| 44 | + fabAddChecklistItem.setOnClickListener(new View.OnClickListener() { |
| 45 | + |
| 46 | + @Override |
| 47 | + public void onClick(View v) { |
| 48 | + addChecklistItem(); |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + // Get the checklist items passed from the previous activity |
| 53 | + Intent intent = getIntent(); |
| 54 | + if (intent != null) { |
| 55 | + List<String> items = intent.getStringArrayListExtra("checklistItems"); |
| 56 | + if (items != null) { |
| 57 | + checklistItems.addAll(items); |
| 58 | + } |
| 59 | + } |
| 60 | + checklistAdapter.setChecklistItems(checklistItems); |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + private void addChecklistItem() { |
| 66 | + String item = editTextChecklistItem.getText().toString().trim(); |
| 67 | + if (!item.isEmpty()) { |
| 68 | + checklistItems.add(item); |
| 69 | + checklistAdapter.setChecklistItems(checklistItems); |
| 70 | + editTextChecklistItem.setText(""); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private void saveChecklistAsNote() { |
| 75 | + // Create a new note object |
| 76 | + String title = "Checklist Note"; |
| 77 | + StringBuilder content = new StringBuilder(); |
| 78 | + for (String item : checklistItems) { |
| 79 | + content.append("- ").append(item).append("\n"); |
| 80 | + } |
| 81 | + |
| 82 | + // Finish the activity and return to the previous activity |
| 83 | + setResult(RESULT_OK); |
| 84 | + finish(); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void onBackPressed() { |
| 89 | + saveChecklistAsNote(); |
| 90 | + super.onBackPressed(); |
| 91 | + } |
| 92 | + |
| 93 | + private void saveNote() { |
| 94 | + int index = 1; // Set the desired index value |
| 95 | + |
| 96 | + // Create a new Notes object with the index |
| 97 | + Notes note = new Notes(); |
| 98 | + |
| 99 | + // Save the note to your data storage or database |
| 100 | + // Example: |
| 101 | + // NoteDatabase.save(note); |
| 102 | + |
| 103 | + // Finish the activity and return to the previous activity |
| 104 | + setResult(RESULT_OK); |
| 105 | + finish(); |
| 106 | +} |
| 107 | + |
| 108 | + @Override |
| 109 | + protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 110 | + super.onActivityResult(requestCode, resultCode, data); |
| 111 | + if (requestCode == 101 && resultCode == RESULT_OK) { |
| 112 | + // Update the checklist items if any changes were made in the ChecklistActivity |
| 113 | + List<String> updatedItems = data.getStringArrayListExtra("checklistItems"); |
| 114 | + if (updatedItems != null) { |
| 115 | + checklistItems.clear(); |
| 116 | + checklistItems.addAll(updatedItems); |
| 117 | + checklistAdapter.setChecklistItems(checklistItems); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments