Skip to content

Commit

Permalink
Fixed bugs with dates and date differences
Browse files Browse the repository at this point in the history
  • Loading branch information
dgudim committed Jan 10, 2024
1 parent 844874a commit b23c93c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,11 @@ public boolean isNotGlobalEntryVisibleOnDay(@NonNull TodoEntry entry, long targe

public void notifyEntryVisibilityChanged(@NonNull TodoEntry entry,
boolean coreDaysChanged,
boolean extendedDaysChanged,
@NonNull Set<Long> invalidatedDaySet,
boolean onlyAddDifference) {
// if the entry is currently marked as global in the list and is global now
if (globalEntries.contains(entry) && entry.isGlobal() && !coreDaysChanged) {
if (globalEntries.contains(entry) && entry.isGlobal()) {
Logger.warning(NAME, "Trying to change visibility range of a global entry");
return;
}
Expand Down Expand Up @@ -350,12 +351,20 @@ public void notifyEntryVisibilityChanged(@NonNull TodoEntry entry,
TodoEntry.FullDaySet newDaySet = entry.getFullDaySet(firstLoadedDay, lastLoadedDay);

if (onlyAddDifference) {
Logger.debug(NAME, "Added difference to invalidatedDaySet");
invalidatedDaySet.addAll(displayUpcomingExpired ?
Sets.symmetricDifference(prevExpiredUpcomingDays, newDaySet.getUpcomingExpiredDaySet()) :
Sets.symmetricDifference(prevCoreDays, newDaySet.getCoreDaySet()));
if (displayUpcomingExpired && extendedDaysChanged) {
invalidatedDaySet.addAll(Sets.symmetricDifference(prevExpiredUpcomingDays, newDaySet.getUpcomingExpiredDaySet()));
}
// Sometimes the extended days may change in a way, that a new core day is on an old extended day, like this
// Before: _ _ _ _ E - - - -
// After: _ _ _ _ E E - - - -
// In this case the only changed extended day will be the last one, so we also need to add the difference from the core days
if (coreDaysChanged) {
invalidatedDaySet.addAll(Sets.symmetricDifference(prevCoreDays, newDaySet.getCoreDaySet()));
}

Logger.debug(NAME, "Added difference to invalidatedDaySet");
} else {
// Add both new and old sets
invalidatedDaySet.addAll(displayUpcomingExpired ? prevExpiredUpcomingDays : prevCoreDays);
invalidatedDaySet.addAll(displayUpcomingExpired ? newDaySet.getUpcomingExpiredDaySet() : newDaySet.getCoreDaySet());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package prototype.xd.scheduler.fragments;

import static prototype.xd.scheduler.utilities.DateManager.currentlySelectedTimestampUTC;
import static prototype.xd.scheduler.utilities.DateManager.dateStringUTCFromMsUTC;
import static prototype.xd.scheduler.utilities.DateManager.getCurrentlySelectedDate;
import static prototype.xd.scheduler.utilities.DateManager.getEndOfMonthDayUTC;
import static prototype.xd.scheduler.utilities.DateManager.getStartOfMonthDayUTC;
import static prototype.xd.scheduler.utilities.DateManager.selectDate;
Expand Down Expand Up @@ -155,7 +154,7 @@ private void setupListeners(@NonNull ContentWrapperBinding contentBnd,
todoListViewAdapter.notifyEntryListChanged();
// update the status text with entry count
int events = todoListViewAdapter.getItemCount();
binding.contentWrapper.toolbar.setTitle(getString(R.string.status, dateStringUTCFromMsUTC(currentlySelectedTimestampUTC), events));
binding.contentWrapper.toolbar.setTitle(getString(R.string.status, getCurrentlySelectedDate(), events));
binding.contentWrapper.content.noEventsText.setVisibility(events == 0 ? View.VISIBLE : View.GONE);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ private DateManager() throws InstantiationException {
private static LocalDate currentDate = LocalDate.now();

public static long currentlySelectedDayUTC = DAY_FLAG_GLOBAL;
public static long currentlySelectedTimestampUTC = DAY_FLAG_GLOBAL;

@NonNull
public static final Locale systemLocale = Objects.requireNonNull(LocaleList.getDefault().get(0));
Expand Down Expand Up @@ -121,7 +120,6 @@ public static synchronized void updateDate() {
public static synchronized void selectDate(@NonNull LocalDate date) {
updateDate();
currentlySelectedDayUTC = date.toEpochDay();
currentlySelectedTimestampUTC = daysToMs(currentlySelectedDayUTC);
}

public static long getStartOfMonthDayUTC(@NonNull YearMonth month) {
Expand Down Expand Up @@ -182,13 +180,13 @@ public static long msToDays(long msUTC) {
}

public static long daysToMs(long daysUTC) {
return TimeUnit.MILLISECONDS.convert(daysUTC, TimeUnit.DAYS) + ONE_MINUTE_MS;
return TimeUnit.MILLISECONDS.convert(daysUTC, TimeUnit.DAYS);
}

// return date given a UTC timestamp
// return currently selected date date
@NonNull
public static String dateStringUTCFromMsUTC(long msUTC) {
return dateFormat.format(msUTCtoLocalDate(msUTC, ZoneOffset.UTC));
public static String getCurrentlySelectedDate() {
return dateFormat.format(msUTCtoLocalDate(daysToMs(currentlySelectedDayUTC), ZoneOffset.UTC));
}

// return date (months are 3 letters instead of numbers) and time given a UTC timestamp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,12 @@ public void accept(@NonNull TodoEntry entry, @NonNull Set<String> parameters) {
&& todoEntries.displayUpcomingExpired;

// parameters that change event range
if (extendedDaysChanged || coreDaysChanged) {
if (coreDaysChanged || extendedDaysChanged) {

todoEntries.notifyEntryVisibilityChanged(
entry,
coreDaysChanged,
extendedDaysChanged,
daysToRebind,
// include all days if BG_COLOR changed, else include just the difference
!parameters.contains(BG_COLOR.CURRENT.key));
Expand Down Expand Up @@ -351,6 +352,7 @@ private void notifyDatasetChanged(boolean timezoneChanged) {
todoEntries.notifyEntryVisibilityChanged(
todoEntry,
true,
true,
daysToRebind,
true);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package prototype.xd.scheduler.views;

import static prototype.xd.scheduler.utilities.DateManager.currentlySelectedDayUTC;
import static prototype.xd.scheduler.utilities.DateManager.dateStringMonthNamesUTCFromMsUTC;
import static prototype.xd.scheduler.utilities.DateManager.msToDays;
import static prototype.xd.scheduler.utilities.DateManager.daysToMs;
import static prototype.xd.scheduler.utilities.DateManager.msToDays;

import android.content.Context;
import android.util.AttributeSet;
Expand All @@ -21,8 +20,8 @@

public class DateSelectButton extends MaterialButton {

private Long selectedDayUTC;
private Long selectedMsUTC;
private volatile Long selectedDayUTC;
private volatile Long selectedMsUTC;

private MaterialDatePicker.Builder<Long> datePickerBuilder;

Expand All @@ -49,13 +48,10 @@ public DateSelectButton(@NonNull Context context, @Nullable AttributeSet attrs)
}

public void setup(@NonNull FragmentManager fragmentManager, long initialDay) {
if(initialDay == -1) {
initialDay = currentlySelectedDayUTC;
}
long initialMsUTC = daysToMs(initialDay);
selectedDayUTC = initialDay;
selectedMsUTC = initialMsUTC;

datePickerBuilder.setSelection(initialMsUTC);

MaterialDatePicker<Long> datePicker = datePickerBuilder.build();
Expand Down

0 comments on commit b23c93c

Please sign in to comment.