HomeActivity.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package View;
  2. import static android.util.Log.println;
  3. import android.content.Intent;
  4. import android.graphics.Color;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.widget.Button;
  8. import android.widget.CheckBox;
  9. import android.widget.ImageButton;
  10. import android.widget.LinearLayout;
  11. import android.widget.TextView;
  12. import androidx.activity.EdgeToEdge;
  13. import androidx.annotation.NonNull;
  14. import androidx.appcompat.app.AppCompatActivity;
  15. import androidx.core.graphics.Insets;
  16. import androidx.core.view.ViewCompat;
  17. import androidx.core.view.WindowInsetsCompat;
  18. import com.example.habittracker.R;
  19. import java.util.List;
  20. import Database.DatabaseHelper;
  21. import Model.Habit;
  22. public class HomeActivity extends AppCompatActivity {
  23. String username;
  24. String password;
  25. public static String userId;
  26. TextView user_greeting_tv;
  27. Button addHabitButton;
  28. DatabaseHelper db;
  29. List<Habit> habitList;
  30. LinearLayout parentLayout;
  31. ImageButton refreshBtn;
  32. @Override
  33. protected void onCreate(Bundle savedInstanceState) {
  34. super.onCreate(savedInstanceState);
  35. EdgeToEdge.enable(this);
  36. setContentView(R.layout.activity_home);
  37. ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
  38. Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
  39. v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
  40. return insets;
  41. });
  42. user_greeting_tv = findViewById(R.id.user_greating_tv);
  43. addHabitButton = findViewById(R.id.add_habit);
  44. refreshBtn = findViewById(R.id.refresh_btn);
  45. userId = getIntent().getStringExtra("UserId");
  46. username = getIntent().getStringExtra("Username");
  47. password = getIntent().getStringExtra("Password");
  48. db = new DatabaseHelper(HomeActivity.this);
  49. habitList = db.getAllHabits(userId);
  50. parentLayout = findViewById(R.id.habits_list);
  51. user_greeting_tv.setText(String.format("Hello, %s", username));
  52. addHabitButton.setOnClickListener(v -> {
  53. startActivity(new Intent(this, AddHabit.class));
  54. });
  55. refreshBtn.setOnClickListener(v -> {
  56. habitList = db.getAllHabits(userId);
  57. parentLayout.removeAllViews();
  58. displayHabits();
  59. });
  60. displayHabits();
  61. }
  62. private void displayHabits() {
  63. for (Habit habit : habitList) {
  64. println(Log.DEBUG, "Habit: ", habit.getHabitName());
  65. LinearLayout linearLayout = getLinearLayout(habit);
  66. parentLayout.addView(linearLayout);
  67. }
  68. }
  69. private @NonNull LinearLayout getLinearLayout(Habit habit) {
  70. LinearLayout linearLayout = new LinearLayout(this);
  71. // Create LayoutParams with MATCH_PARENT for width and WRAP_CONTENT for height
  72. LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
  73. LinearLayout.LayoutParams.MATCH_PARENT,
  74. LinearLayout.LayoutParams.WRAP_CONTENT
  75. );
  76. linearLayout.setLayoutParams(params);
  77. linearLayout.setOrientation(LinearLayout.HORIZONTAL);
  78. linearLayout.setPadding(25, 25, 25, 25);
  79. CheckBox checkBox = new CheckBox(this);
  80. checkBox.setPadding(40, 40, 40, 40);
  81. TextView habitTime = new TextView(this);
  82. habitTime.setPadding(40, 40, 40, 40);
  83. habitTime.setTextSize(20);
  84. habitTime.setText(habit.getTime());
  85. habitTime.setTextColor(Color.parseColor("#e2e2e2"));
  86. TextView habitName = new TextView(this);
  87. habitName.setPadding(40, 40, 40, 40);
  88. habitName.setTextSize(20);
  89. habitName.setText(habit.getHabitName());
  90. habitName.setTextColor(Color.parseColor("#e2e2e2"));
  91. linearLayout.addView(checkBox);
  92. linearLayout.addView(habitTime);
  93. linearLayout.addView(habitName);
  94. return linearLayout;
  95. }
  96. }