Skip to content

Commit

Permalink
Feature: View current day's Habits (#39)
Browse files Browse the repository at this point in the history
View current day's Habits

- Make Habit.isInDay() in Habit.java
- Implement the checkBox feature in TodayHabitsFragment.java

Closes #7
  • Loading branch information
mosaicthej authored Oct 25, 2021
1 parent 3bc6295 commit 7869fba
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@
import java.util.List;

public interface Habit {
static final int MONDAY = 0;
static final int TUESDAY = 1;
static final int WEDNESDAY = 2;
static final int THURSDAY = 3;
static final int FRIDAY = 4;
static final int SATURDAY = 5;
static final int SUNDAY = 6;

String getId();

Expand All @@ -21,5 +14,20 @@ public interface Habit {
Instant getDateStarted();

List<HabitEvent> getEvents();

boolean[] getDaysOfWeek();

static boolean isInDay(Instant today, boolean[] dayOfWeek) {
// dayOfWeek goes from 1-7 (Monday-Sunday)
int dayOfWeek = today.atZone(ZoneId.systemDefault()).getDayOfWeek().getValue();

// daysOfWeek is an array that matches the checkBox for viewing.
// in case today is Monday:
// [true, true ,true, true, true, true, true] = every day
// [true, false , false, false, false, false, false] = only on monday
// [true, false , false, false, false, false, true] = for monday and sunday
return daysOfWeek[dayOfWeek-1]
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,17 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
@Override
public void onStart() {
super.onStart();
mHabitViewModel.getAllHabits().observe(this, list -> {
// need to cast List<HabitEntity> to List<Habit>
List<Habit> habits = new ArrayList<>(list);
adapter.submitList(habits);
mHabitViewModel.getAllHabits().observe(this, habitList -> {
List<Habit> todaysHabits = new ArrayList<>();
Instant today = Instant.now();
// traverse all h in habitList, and only masks in those who matches the checkBox
// checkBox implementation can be seen in isInDay() from Habit.java
for (Habit h : habitList) {
if (Habit.isInDay(today, h.getDaysOfWeek())) {
todaysHabits.add(h);
}
}
adapter.submitList(todaysHabits);
});
}

Expand Down

0 comments on commit 7869fba

Please sign in to comment.