Skip to content

Commit

Permalink
fix: daily page astronomy fixes for daylight remaining on different t…
Browse files Browse the repository at this point in the history
…imezones. Astronomy update on daily page swipe
  • Loading branch information
farfromrefug committed Jan 8, 2025
1 parent d014d9e commit 02f114f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 26 deletions.
21 changes: 13 additions & 8 deletions app/components/DailyPage.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import dayjs, { Dayjs } from 'dayjs';
import { NativeViewElementNode } from 'svelte-native/dom';
import AstronomyView from '~/components/astronomy/AstronomyView.svelte';
import { DAILY_PAGE_HOURLY_CHART, SETTINGS_DAILY_PAGE_HOURLY_CHART } from '~/helpers/constants';
import { DAILY_PAGE_HOURLY_CHART, SETTINGS_DAILY_PAGE_HOURLY_CHART, SETTINGS_SHOW_CURRENT_DAY_DAILY, SHOW_CURRENT_DAY_DAILY } from '~/helpers/constants';
import { formatDate, lc } from '~/helpers/locale';
import { POLLENS_POLLUTANTS_TITLES } from '~/services/airQualityData';
import { WeatherLocation } from '~/services/api';
Expand Down Expand Up @@ -37,8 +37,6 @@
</script>

<script lang="ts">
import { SwipeGestureEventData } from '@akylas/nativescript';
const showHourlyChart = ApplicationSettings.getBoolean(SETTINGS_DAILY_PAGE_HOURLY_CHART, DAILY_PAGE_HOURLY_CHART);
export let getDailyPageProps: Function;
Expand All @@ -51,9 +49,11 @@
export let timezoneOffset;
export let startTime: Dayjs;
const isCurrentDay = dayjs(startTime).isSame(dayjs(), 'days');
let itemsCount = items.length;
DEV_LOG && console.log('startTime', startTime, dayjs(startTime).isSame(dayjs(), 'd'));
// DEV_LOG && console.log('startTime', startTime, dayjs(), startTime.valueOf(), dayjs().valueOf(), isCurrentDay);
let page: NativeViewElementNode<Page>;
let animated = iconService.animated;
$: ({ colorOnSurface, colorOnSurfaceVariant, colorOutline } = $colors);
Expand Down Expand Up @@ -303,16 +303,21 @@
drawPollOnCanvas(item.pollens, lc('pollens'), canvas);
}
const showDayDataInCurrent = ApplicationSettings.getBoolean(SETTINGS_SHOW_CURRENT_DAY_DAILY, SHOW_CURRENT_DAY_DAILY);
const minIndex = showDayDataInCurrent ? 1 : 0;
function onSwipe(e) {
if (e.direction === 1 && itemIndex > 0) {
if (e.direction === 1 && itemIndex > minIndex) {
const data = getDailyPageProps(items[itemIndex - 1]);
startTime = data.startTime;
item = data.item;
itemIndex = data.itemIndex;
items = data.items;
itemsCount = items.length;
redraw();
} else if (e.direction === 2 && itemIndex < itemsCount - 1) {
const data = getDailyPageProps(items[itemIndex + 1]);
const delta = showDayDataInCurrent && itemIndex === 0 ? 2 : 1;
const data = getDailyPageProps(items[itemIndex + delta]);
startTime = data.startTime;
item = data.item;
itemIndex = data.itemIndex;
items = data.items;
Expand Down Expand Up @@ -350,7 +355,7 @@
rightAxisSuggestedMaximum={8}
row={1}
showCurrentTimeLimitLine={false}
startTime={dayjs(startTime).isSame(dayjs(), 'd') ? startTime.valueOf() : undefined}
startTime={isCurrentDay ? Date.now() : undefined}
temperatureLineWidth={3}
visibility={item.hourly.length > 0 ? 'visible' : 'collapsed'} />
{:else}
Expand All @@ -364,7 +369,7 @@
{#if item.pollens}
<canvasview height={Math.ceil(Object.keys(item.pollens).length / 2) * 40 * $fontScale + 55 * $fontScale} row={3} on:draw={drawPollens} />
{/if}
<AstronomyView {location} row={4} selectableDate={false} {startTime} {timezoneOffset} />
<AstronomyView {isCurrentDay} {location} row={4} selectableDate={false} startTime={isCurrentDay ? startTime : undefined} {timezoneOffset} />
</gridlayout>
</scrollview>
<CActionBar title={weatherLocation && weatherLocation.name} on:swipe={onSwipe} />
Expand Down
12 changes: 8 additions & 4 deletions app/components/WeatherPage.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
SETTINGS_SHOW_CURRENT_DAY_DAILY,
SETTINGS_SHOW_DAILY_IN_CURRENTLY,
SETTINGS_SWIPE_ACTION_BAR_PROVIDER,
SHOW_CURRENT_DAY_DAILY,
SWIPE_ACTION_BAR_PROVIDER
} from '~/helpers/constants';
import { FavoriteLocation, favoriteIcon, favoriteIconColor, favorites, getFavoriteKey, queryTimezone, toggleFavorite } from '~/helpers/favorites';
Expand Down Expand Up @@ -452,24 +453,27 @@
if (endIndex === -1) {
endIndex = hourly.length;
}
DEV_LOG && console.log('getDailyPageProps', item.time, startOfDay, endOfDay, startIndex, endIndex);
// DEV_LOG && console.log('getDailyPageProps', item.time, dayjs(item.time), getLocalTime(item.time, item.timezoneOffset), startOfDay, endOfDay, startIndex, endIndex);
return {
getDailyPageProps,
itemIndex: items.indexOf(item),
items,
item: { ...item, hourly: startIndex >= 0 && endIndex - startIndex >= 2 ? hourly.slice(startIndex, endIndex) : [] },
location: weatherLocation,
startTime: dayjs(item.time).isSame(Date.now(), 'day') ? getLocalTime(undefined, item.timezoneOffset) : dayjs(item.time).set('h', dayjs().get('h')).set('m', dayjs().get('m')),
startTime: dayjs(item.time).add(-item.timezoneOffset, 'h'),
weatherLocation,
timezoneOffset: item.timezoneOffset
};
}
const onTap = throttle(async function (event) {
try {
const item = event as DailyData;
let item = event as DailyData;
const component = (await import('~/components/DailyPage.svelte')).default;
DEV_LOG && console.log('onTap', item.time, item.timezoneOffset);
const showDayDataInCurrent = ApplicationSettings.getBoolean(SETTINGS_SHOW_CURRENT_DAY_DAILY, SHOW_CURRENT_DAY_DAILY);
if (showDayDataInCurrent && items.indexOf(item) === 0) {
item = items[1];
}
navigate({
page: component,
props: getDailyPageProps(item)
Expand Down
43 changes: 29 additions & 14 deletions app/components/astronomy/AstronomyView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
export let selectableDate = true;
export let timezoneOffset;
export let startTime = getLocalTime(undefined, timezoneOffset);
export let isCurrentDay = false;
// let limitLine: LimitLine;
let moonPhase: number; // MoonPhase;
let sunTimes: GetTimesResult; // SunTimes;
Expand All @@ -164,6 +165,8 @@
let sunPoses: any[]; // SunPosition[];
let moonPoses: any[]; // MoonPosition[];
let selectedTime;
const moonPaint = new Paint();
moonPaint.strokeWidth = 1.5;
Expand Down Expand Up @@ -206,7 +209,7 @@
return;
}
const computeStartTime = getStartOfDay(startTime, timezoneOffset);
DEV_LOG && console.log('updateChartData', startTime, timezoneOffset, computeStartTime);
// DEV_LOG && console.log('updateChartData', startTime, timezoneOffset, computeStartTime);
const chart = chartView.nativeView;
const sets = [];
sunPoses = [];
Expand Down Expand Up @@ -236,11 +239,11 @@
const hours = Math.min(Math.floor(h.x / 6), 23);
const minutes = (h.x * 10) % 60;
startTime = startTime.set('h', hours).set('m', minutes);
selectedTime = startTime.set('h', hours).set('m', minutes);
c.drawLine(h.drawX, 0, h.drawX, c.getHeight(), highlightPaint);
highlightPaint.setTextAlign(Align.LEFT);
let x = h.drawX + 4;
const text = formatTime(startTime);
const text = formatTime(selectedTime);
const size = Utils.calcTextSize(highlightPaint, text);
if (x > c.getWidth() - size.width) {
x = h.drawX - 4;
Expand Down Expand Up @@ -301,9 +304,11 @@
const lineData = new LineData(sets);
chart.data = lineData;
const nowMinutes = dayjs(startTime).diff(computeStartTime, 'minutes');
const h = chart.getHighlightByXValue(nowMinutes / 10);
chart.highlight(h[0]);
if (isCurrentDay) {
const nowMinutes = dayjs(startTime).diff(computeStartTime, 'minutes');
const h = chart.getHighlightByXValue(nowMinutes / 10);
chart.highlight(h[0]);
}
} else {
chartData.getDataSetByIndex(1).values = sunPoses;
chartData.getDataSetByIndex(1).notifyDataSetChanged();
Expand All @@ -317,8 +322,8 @@
async function selectDate() {
try {
const date = await pickDate(dayjs(startTime));
if (date && startTime.valueOf() !== date) {
const date = await pickDate(dayjs(selectedTime));
if (date && selectedTime.valueOf() !== date) {
updateStartTime(getLocalTime(date, timezoneOffset));
}
} catch (error) {
Expand All @@ -338,27 +343,37 @@
function updateStartTime(time: Dayjs) {
startTime = time;
updateChartData();
}
let isCurrentDay = false;
$: if (selectedTime) {
selectedTime = startTime.set('h', selectedTime.get('h')).set('m', selectedTime.get('m'));
} else {
selectedTime = startTime;
}
$: {
try {
const date = startTime.toDate();
// const date = getStartOfDay(startTime, 0).toDate();
moonPhase = getMoonPhase(date);
moonAzimuth = getCompassInfo(getMoonPosition(date, location.coord.lat, location.coord.lon).azimuth * TO_DEG + 180);
sunTimes = getTimes(date, location.coord.lat, location.coord.lon);
sunriseEnd = dayjs.utc(sunTimes.sunriseEnd.valueOf()).valueOf();
sunsetStart = dayjs.utc(sunTimes.sunsetStart.valueOf()).valueOf();
sunAzimuth = getCompassInfo(getPosition(date, location.coord.lat, location.coord.lon).azimuth * TO_DEG + 180);
isCurrentDay = dayjs(sunsetStart).isSame(dayjs(), 'd');
updateChartData();
// sunriseEndAzimuth = getCompassInfo(getPosition(sunTimes.sunriseEnd, location.coord.lat, location.coord.lon).azimuth * TO_DEG + 180);
// sunsetStartAzimuth = getCompassInfo(getPosition(sunTimes.sunsetStart, location.coord.lat, location.coord.lon).azimuth * TO_DEG + 180);
} catch (err) {
console.error(err);
}
}
$: {
try {
const date = selectedTime.toDate();
sunAzimuth = getCompassInfo(getPosition(date, location.coord.lat, location.coord.lon).azimuth * TO_DEG + 180);
moonAzimuth = getCompassInfo(getMoonPosition(date, location.coord.lat, location.coord.lon).azimuth * TO_DEG + 180);
} catch (err) {
console.error(err);
}
}
// async function setDateTime() {
// try {
Expand Down

0 comments on commit 02f114f

Please sign in to comment.