From 2323aa04dc4fc357913210d4d63f6887abcc1cfa Mon Sep 17 00:00:00 2001 From: ynhhoJ <22500212+ynhhoJ@users.noreply.github.com> Date: Mon, 6 Jan 2025 13:24:01 +0200 Subject: [PATCH] feat(ReportWeekly): Implement change month by using L2 or R2 buttons --- src/containers/ReportWeekly.tsx | 59 +++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/src/containers/ReportWeekly.tsx b/src/containers/ReportWeekly.tsx index 2ae7594..f5e51cb 100644 --- a/src/containers/ReportWeekly.tsx +++ b/src/containers/ReportWeekly.tsx @@ -1,5 +1,7 @@ import { PanelSection } from "@decky/ui"; -import { type VFC, useEffect, useState } from "react"; +import { Button } from "@src/steam/enums/Button"; +import { registerForInputEvent } from "@src/steam/registerForInputEvent"; +import { useEffect, useState } from "react"; import { formatWeekInterval } from "../app/formatters"; import { type DailyStatistics, @@ -14,8 +16,14 @@ import { PieView } from "../components/statistics/PieView"; import { WeekView } from "../components/statistics/WeekView"; import { useLocator } from "../locator"; -export const ReportWeekly: VFC = () => { +interface ReportWeeklyProperties { + slim?: boolean; +} + +export const ReportWeekly = ({ slim = false }: ReportWeeklyProperties) => { const { reports, currentSettings: settings } = useLocator(); + const [lastChangedPageTimeStamp, setLastChangedPageTimeStamp] = + useState(0); const [isLoading, setLoading] = useState(false); const [currentPage, setCurrentPage] = useState>( empty(), @@ -23,21 +31,60 @@ export const ReportWeekly: VFC = () => { useEffect(() => { setLoading(true); + reports.weeklyStatistics().then((it) => { setCurrentPage(it); setLoading(false); }); }, []); + useEffect(() => { + if (slim) { + return; + } + + const { unregister } = registerForInputEvent((buttons) => { + if (buttons.length !== 1) { + return; + } + + if (new Date().getTime() - lastChangedPageTimeStamp <= 500) { + return; + } + + if (buttons.includes(Button.L2) && currentPage.hasPrev()) { + setLastChangedPageTimeStamp(new Date().getTime()); + + onPrevWeek(); + } + + if (buttons.includes(Button.R2) && currentPage.hasNext()) { + setLastChangedPageTimeStamp(new Date().getTime()); + + onNextWeek(); + } + }); + + return () => { + unregister(); + }; + }, [ + currentPage.current().interval.start.getTime(), + currentPage.current().interval.end.getTime(), + ]); + const onNextWeek = () => { setLoading(true); + currentPage?.next().then((it) => { setCurrentPage(it); setLoading(false); }); }; + const onPrevWeek = () => { setLoading(true); + currentPage?.prev().then((it) => { setCurrentPage(it); setLoading(false); @@ -60,20 +107,28 @@ export const ReportWeekly: VFC = () => { currentText={formatWeekInterval(currentPage.current().interval)} hasNext={currentPage.hasNext()} hasPrev={currentPage.hasPrev()} + prevKey={slim ? undefined : "l2"} + nextKey={slim ? undefined : "r2"} /> + {isLoading &&
Loading...
} + {!isLoading && !currentPage &&
Error while loading data
} + {!isLoading && currentPage && (
+ + {isAnyGames && ( + {settings.gameChartStyle === ChartStyle.PIE_AND_BARS && ( )}