123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- package View;
- import static android.util.Log.println;
- import android.content.Intent;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.ImageButton;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import androidx.activity.EdgeToEdge;
- import androidx.annotation.NonNull;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.core.graphics.Insets;
- import androidx.core.view.ViewCompat;
- import androidx.core.view.WindowInsetsCompat;
- import com.example.habittracker.R;
- import java.util.List;
- import Database.DatabaseHelper;
- import Model.Habit;
- public class HomeActivity extends AppCompatActivity {
- String username;
- String password;
- public static String userId;
- TextView user_greeting_tv;
- Button addHabitButton;
- DatabaseHelper db;
- List<Habit> habitList;
- LinearLayout parentLayout;
- ImageButton refreshBtn;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- EdgeToEdge.enable(this);
- setContentView(R.layout.activity_home);
- 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;
- });
- user_greeting_tv = findViewById(R.id.user_greating_tv);
- addHabitButton = findViewById(R.id.add_habit);
- refreshBtn = findViewById(R.id.refresh_btn);
- userId = getIntent().getStringExtra("UserId");
- username = getIntent().getStringExtra("Username");
- password = getIntent().getStringExtra("Password");
- db = new DatabaseHelper(HomeActivity.this);
- habitList = db.getAllHabits(userId);
- parentLayout = findViewById(R.id.habits_list);
- user_greeting_tv.setText(String.format("Hello, %s", username));
- addHabitButton.setOnClickListener(v -> {
- startActivity(new Intent(this, AddHabit.class));
- });
- refreshBtn.setOnClickListener(v -> {
- habitList = db.getAllHabits(userId);
- parentLayout.removeAllViews();
- displayHabits();
- });
- displayHabits();
- }
- private void displayHabits() {
- for (Habit habit : habitList) {
- println(Log.DEBUG, "Habit: ", habit.getHabitName());
- LinearLayout linearLayout = getLinearLayout(habit);
- parentLayout.addView(linearLayout);
- }
- }
- private @NonNull LinearLayout getLinearLayout(Habit habit) {
- LinearLayout linearLayout = new LinearLayout(this);
- // Create LayoutParams with MATCH_PARENT for width and WRAP_CONTENT for height
- LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.MATCH_PARENT,
- LinearLayout.LayoutParams.WRAP_CONTENT
- );
- linearLayout.setLayoutParams(params);
- linearLayout.setOrientation(LinearLayout.HORIZONTAL);
- linearLayout.setPadding(25, 25, 25, 25);
- CheckBox checkBox = new CheckBox(this);
- checkBox.setPadding(40, 40, 40, 40);
- TextView habitTime = new TextView(this);
- habitTime.setPadding(40, 40, 40, 40);
- habitTime.setTextSize(20);
- habitTime.setText(habit.getTime());
- habitTime.setTextColor(Color.parseColor("#e2e2e2"));
- TextView habitName = new TextView(this);
- habitName.setPadding(40, 40, 40, 40);
- habitName.setTextSize(20);
- habitName.setText(habit.getHabitName());
- habitName.setTextColor(Color.parseColor("#e2e2e2"));
- linearLayout.addView(checkBox);
- linearLayout.addView(habitTime);
- linearLayout.addView(habitName);
- return linearLayout;
- }
- }
|