From 2c02773ee5bbb5e6d445d047b78c3c2ea9a0eb76 Mon Sep 17 00:00:00 2001 From: Forrest Guice Date: Tue, 19 Nov 2024 17:07:14 -0700 Subject: [PATCH] MoonPhaseView fixes bug where moon dialog displays the wrong phase label for a given date/time (#843) --- .../suntimeswidget/calculator/SuntimesMoonData.java | 12 ++++++++++++ .../suntimeswidget/moon/MoonPhaseView.java | 4 +++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/forrestguice/suntimeswidget/calculator/SuntimesMoonData.java b/app/src/main/java/com/forrestguice/suntimeswidget/calculator/SuntimesMoonData.java index 55e4949c8..5c11082f2 100644 --- a/app/src/main/java/com/forrestguice/suntimeswidget/calculator/SuntimesMoonData.java +++ b/app/src/main/java/com/forrestguice/suntimeswidget/calculator/SuntimesMoonData.java @@ -413,6 +413,18 @@ protected MoonPhaseDisplay findPhaseOf(Calendar calendar, boolean updateNext) return (nextPhaseIsToday ? toPhase(nextPhase) : prevMinorPhase(nextPhase)); } + public static MoonPhaseDisplay findCurrentPhaseOf(Calendar calendar, SuntimesMoonData data) + { + SuntimesMoonData data1 = new SuntimesMoonData(data); + data1.setTodayIs(calendar); + data1.calculate(); + + SuntimesCalculator.MoonPhase nextPhase = data1.nextPhase(calendar); + Calendar nextPhaseDate = data1.moonPhases.get(nextPhase); + boolean nextPhaseIsNow = (nextPhaseDate != null) && (Math.abs(nextPhaseDate.getTimeInMillis() - calendar.getTimeInMillis()) <= (2000 * 60)); + return (nextPhaseIsNow ? toPhase(nextPhase) : prevMinorPhase(nextPhase)); + } + public CharSequence getMoonPhaseLabel(Context context, SuntimesCalculator.MoonPhase majorPhase) { return getMoonPhaseLabel(context, majorPhase, moonPhaseCalendar(majorPhase)); } diff --git a/app/src/main/java/com/forrestguice/suntimeswidget/moon/MoonPhaseView.java b/app/src/main/java/com/forrestguice/suntimeswidget/moon/MoonPhaseView.java index 153d2d98a..506a8c161 100644 --- a/app/src/main/java/com/forrestguice/suntimeswidget/moon/MoonPhaseView.java +++ b/app/src/main/java/com/forrestguice/suntimeswidget/moon/MoonPhaseView.java @@ -251,7 +251,9 @@ public void updateViews(Context context, SuntimesMoonData data, @Nullable Calend themeIcons(context, themeOverride); hideIcons(); - MoonPhaseDisplay phase = (tomorrowMode ? data.getMoonPhaseTomorrow() : data.getMoonPhaseToday()); + MoonPhaseDisplay phase = (dateTime != null) ? SuntimesMoonData.findCurrentPhaseOf(dateTime, data) + : (tomorrowMode ? data.getMoonPhaseTomorrow() : data.getMoonPhaseToday()); + if (phase != null) { if (phase == MoonPhaseDisplay.FULL || phase == MoonPhaseDisplay.NEW) {