2023-05-08 03:02:14 +00:00
|
|
|
import type { IDay, IPhoto } from './types';
|
2023-05-04 02:59:23 +00:00
|
|
|
|
2023-05-08 20:25:13 +00:00
|
|
|
const BASE_URL = 'http://127.0.0.1';
|
|
|
|
|
|
|
|
export const API = {
|
|
|
|
DAYS: () => `${BASE_URL}/api/days`,
|
|
|
|
DAY: (dayId: number) => `${BASE_URL}/api/days/${dayId}`,
|
2023-05-08 21:46:15 +00:00
|
|
|
|
2023-05-08 20:25:13 +00:00
|
|
|
IMAGE_PREVIEW: (fileId: number) => `${BASE_URL}/image/preview/${fileId}`,
|
|
|
|
IMAGE_FULL: (fileId: number) => `${BASE_URL}/image/full/${fileId}`,
|
2023-05-08 21:46:15 +00:00
|
|
|
IMAGE_INFO: (fileId: number) => `${BASE_URL}/image/info/${fileId}`,
|
2023-05-08 20:25:13 +00:00
|
|
|
};
|
2023-05-04 02:59:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Native interface for the Android app.
|
|
|
|
*/
|
|
|
|
export type NativeX = {
|
|
|
|
isNative: () => boolean;
|
2023-05-08 04:06:30 +00:00
|
|
|
setThemeColor: (color: string, isDark: boolean) => void;
|
2023-05-09 02:50:33 +00:00
|
|
|
downloadFromUrl: (url: string) => void;
|
2023-05-04 02:59:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** The native interface is a global object that is injected by the native app. */
|
|
|
|
const nativex: NativeX = globalThis.nativex;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns Whether the native interface is available.
|
|
|
|
*/
|
|
|
|
export const has = () => !!nativex;
|
|
|
|
|
2023-05-08 04:06:30 +00:00
|
|
|
/**
|
2023-05-08 04:18:33 +00:00
|
|
|
* Change the theme color of the app to default.
|
2023-05-08 04:06:30 +00:00
|
|
|
*/
|
2023-05-08 04:18:33 +00:00
|
|
|
export const setTheme = (color?: string, dark?: boolean) => {
|
|
|
|
if (!has()) return;
|
|
|
|
|
|
|
|
color ??= getComputedStyle(document.body).getPropertyValue('--color-main-background');
|
|
|
|
dark ??=
|
|
|
|
window.matchMedia('(prefers-color-scheme: dark)').matches ||
|
|
|
|
document.body.hasAttribute('data-theme-dark') ||
|
|
|
|
document.body.hasAttribute('data-theme-dark-highcontrast');
|
|
|
|
nativex?.setThemeColor?.(color, dark);
|
|
|
|
};
|
2023-05-08 04:06:30 +00:00
|
|
|
|
2023-05-09 02:50:33 +00:00
|
|
|
/**
|
|
|
|
* Download a file from the given URL.
|
|
|
|
*/
|
|
|
|
export const downloadFromUrl = (url: string) => nativex?.downloadFromUrl?.(url);
|
|
|
|
|
2023-05-08 03:02:14 +00:00
|
|
|
/**
|
|
|
|
* Extend a list of days with local days.
|
|
|
|
* Fetches the local days from the native interface.
|
|
|
|
*/
|
|
|
|
export async function extendDaysWithLocal(days: IDay[]) {
|
|
|
|
if (!has()) return;
|
|
|
|
|
|
|
|
// Query native part
|
2023-05-08 20:25:13 +00:00
|
|
|
const res = await fetch(API.DAYS());
|
|
|
|
if (!res.ok) return;
|
|
|
|
const local: IDay[] = await res.json();
|
2023-05-08 03:02:14 +00:00
|
|
|
const remoteMap = new Map(days.map((d) => [d.dayid, d]));
|
|
|
|
|
|
|
|
// Merge local days into remote days
|
|
|
|
for (const day of local) {
|
|
|
|
const remote = remoteMap.get(day.dayid);
|
|
|
|
if (remote) {
|
|
|
|
remote.count = Math.max(remote.count, day.count);
|
|
|
|
} else {
|
|
|
|
days.push(day);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: sort depends on view
|
|
|
|
// (but we show it for only timeline anyway for now)
|
|
|
|
days.sort((a, b) => b.dayid - a.dayid);
|
|
|
|
}
|
|
|
|
|
2023-05-04 02:59:23 +00:00
|
|
|
/**
|
|
|
|
* Extend a list of photos with local photos.
|
|
|
|
* Fetches the local photos from the native interface and filters out duplicates.
|
|
|
|
*
|
|
|
|
* @param dayId Day ID to append local photos to
|
|
|
|
* @param photos List of photos to append to (duplicates will not be added)
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
export async function extendDayWithLocal(dayId: number, photos: IPhoto[]) {
|
|
|
|
if (!has()) return;
|
|
|
|
|
2023-05-08 20:25:13 +00:00
|
|
|
// Query native part
|
|
|
|
const res = await fetch(API.DAY(dayId));
|
|
|
|
if (!res.ok) return;
|
|
|
|
|
|
|
|
// Merge local photos into remote photos
|
|
|
|
const localPhotos: IPhoto[] = await res.json();
|
2023-05-04 02:59:23 +00:00
|
|
|
const photosSet = new Set(photos.map((p) => p.basename));
|
|
|
|
const localOnly = localPhotos.filter((p) => !photosSet.has(p.basename));
|
|
|
|
localOnly.forEach((p) => (p.islocal = true));
|
|
|
|
photos.push(...localOnly);
|
|
|
|
}
|