forked from Blendman974/faceclaw
Add calendar app and accelerometer demo
This commit is contained in:
1 parent
3cdc5ba262
commit
ff447d5161
18 files changed
+911
-5
No files matched your search
@@ -0,0 +1,94 @@
|
||||
import { Utils } from "@nativescript/core";
|
||||
import { hasCalendarPermission } from "../g2/android-permissions";
|
||||
import { spanCurrent } from "./frame-timings";
|
||||
|
||||
declare const com: any;
|
||||
|
||||
/**
|
||||
* Reads upcoming calendar events through the Android Calendar provider
|
||||
* (FaceclawCalendarProvider). Results are cached briefly so repeated paints of
|
||||
* the Calendar app don't re-run the content-provider query every frame.
|
||||
*/
|
||||
|
||||
export type CalendarEvent = {
|
||||
id: number;
|
||||
title: string;
|
||||
startMs: number;
|
||||
endMs: number;
|
||||
allDay: boolean;
|
||||
location: string;
|
||||
calendarName: string;
|
||||
};
|
||||
|
||||
const DEFAULT_MAX_EVENTS = 50;
|
||||
const DEFAULT_WINDOW_MS = 14 * 24 * 60 * 60 * 1000;
|
||||
const CACHE_MS = 30_000;
|
||||
|
||||
let cache: { events: CalendarEvent[]; atMs: number; maxEvents: number; windowMs: number } | null = null;
|
||||
|
||||
/**
|
||||
* Upcoming events from now through the given window, ordered by start time.
|
||||
* Returns [] when calendar permission is absent. Cached for CACHE_MS; pass
|
||||
* forceRefresh to bypass the cache (e.g. right after the permission grant).
|
||||
*/
|
||||
export function readUpcomingEvents(
|
||||
maxEvents = DEFAULT_MAX_EVENTS,
|
||||
windowMs = DEFAULT_WINDOW_MS,
|
||||
forceRefresh = false,
|
||||
): CalendarEvent[] {
|
||||
if (!global.isAndroid || !hasCalendarPermission()) return [];
|
||||
|
||||
const now = Date.now();
|
||||
if (
|
||||
!forceRefresh &&
|
||||
cache &&
|
||||
cache.maxEvents === maxEvents &&
|
||||
cache.windowMs === windowMs &&
|
||||
now - cache.atMs < CACHE_MS
|
||||
) {
|
||||
return cache.events;
|
||||
}
|
||||
|
||||
const context = Utils.android.getApplicationContext();
|
||||
if (!context) return [];
|
||||
|
||||
try {
|
||||
const json = spanCurrent("fetch-calendar-events", () =>
|
||||
String(
|
||||
com.faceclaw.app.FaceclawCalendarProvider.getUpcomingEventsJson(
|
||||
context,
|
||||
Math.max(0, Math.round(maxEvents)),
|
||||
Math.max(0, Math.round(windowMs)),
|
||||
),
|
||||
),
|
||||
);
|
||||
const parsed = JSON.parse(json);
|
||||
const events = Array.isArray(parsed)
|
||||
? parsed.map(normalizeEvent).filter((event): event is CalendarEvent => Boolean(event))
|
||||
: [];
|
||||
cache = { events, atMs: now, maxEvents, windowMs };
|
||||
return events;
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/** Drop the cached events so the next read re-queries the provider. */
|
||||
export function invalidateCalendarCache(): void {
|
||||
cache = null;
|
||||
}
|
||||
|
||||
function normalizeEvent(value: any): CalendarEvent | null {
|
||||
if (!value || typeof value !== "object") return null;
|
||||
const startMs = Number(value.startMs);
|
||||
if (!Number.isFinite(startMs)) return null;
|
||||
return {
|
||||
id: Number(value.id) || 0,
|
||||
title: String(value.title ?? ""),
|
||||
startMs,
|
||||
endMs: Number(value.endMs) || startMs,
|
||||
allDay: Boolean(value.allDay),
|
||||
location: String(value.location ?? ""),
|
||||
calendarName: String(value.calendarName ?? ""),
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user