Improve backgrounds
This commit is contained in:
@@ -5,6 +5,7 @@ import { sample } from '~/lib/array';
|
||||
import { createLocalStore } from '~/lib/create-local-store';
|
||||
import { Background, BackgroundStorage } from '~/types';
|
||||
import { Settings } from './settings-provider';
|
||||
import { random } from '~/lib/math';
|
||||
|
||||
export type BackgroundsState = {
|
||||
currentBackground: Accessor<Background | undefined>;
|
||||
@@ -26,6 +27,11 @@ export function createBackgrounds(appSettings: Settings): BackgroundsState {
|
||||
[]
|
||||
);
|
||||
|
||||
const [unsplashBackgroundIndex, setUnsplashBackgroundIndex] = createSignal(random(19));
|
||||
const [favoritedBackgroundIndex, setFavoritedBackgroundIndex] = createSignal(
|
||||
random(favoriteBackgrounds.length - 1)
|
||||
);
|
||||
|
||||
const shouldFetch =
|
||||
!backgrounds.backgrounds ||
|
||||
backgrounds.fetched_at < new Date().getTime() - 1000 * 60 * BACKGROUND_FETCH_MINS;
|
||||
@@ -39,42 +45,22 @@ export function createBackgrounds(appSettings: Settings): BackgroundsState {
|
||||
);
|
||||
}
|
||||
|
||||
const currentRandomBackground = createMemo<Background | undefined>(() =>
|
||||
sample(backgrounds.backgrounds ?? [])
|
||||
);
|
||||
|
||||
const [currentBackground, setCurrentBackground] = createSignal(
|
||||
appSettings.useSavedBackgrounds ? sample(favoriteBackgrounds) : currentRandomBackground()
|
||||
);
|
||||
|
||||
createEffect(() =>
|
||||
// Change background when appSettings.useSavedBackgrounds changes
|
||||
setCurrentBackground(
|
||||
appSettings.useSavedBackgrounds ? sample(favoriteBackgrounds) : currentRandomBackground()
|
||||
)
|
||||
const currentBackground = createMemo<Background | undefined>(() =>
|
||||
appSettings.useSavedBackgrounds
|
||||
? favoriteBackgrounds[favoritedBackgroundIndex()]
|
||||
: backgrounds?.backgrounds?.[unsplashBackgroundIndex()]
|
||||
);
|
||||
|
||||
const onNextBackground = () => {
|
||||
if (appSettings.useSavedBackgrounds && favoriteBackgrounds.length < 2) {
|
||||
return;
|
||||
if (appSettings.useSavedBackgrounds) {
|
||||
setFavoritedBackgroundIndex((prev) => (prev + 1) % favoriteBackgrounds.length);
|
||||
} else {
|
||||
setUnsplashBackgroundIndex((prev) => (prev + 1) % 20);
|
||||
}
|
||||
|
||||
let background = currentBackground();
|
||||
while (background === currentBackground()) {
|
||||
background = appSettings.useSavedBackgrounds
|
||||
? sample(favoriteBackgrounds)
|
||||
: sample(backgrounds.backgrounds ?? []);
|
||||
}
|
||||
setCurrentBackground(background);
|
||||
};
|
||||
|
||||
const isCurrentBackgroundFavorite = () => {
|
||||
if (!currentBackground()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Boolean(favoriteBackgrounds.find((b) => b.id === currentBackground()!.id));
|
||||
};
|
||||
const isCurrentBackgroundFavorite = () =>
|
||||
Boolean(favoriteBackgrounds.find((b) => b.id === currentBackground()?.id));
|
||||
|
||||
const onFavoriteBackground = () => {
|
||||
if (!currentBackground()) {
|
||||
@@ -82,7 +68,7 @@ export function createBackgrounds(appSettings: Settings): BackgroundsState {
|
||||
}
|
||||
|
||||
setFavroiteBackgrounds((prev) => {
|
||||
if (prev.find((background) => background.id === currentBackground()!.id)) {
|
||||
if (prev.find((background) => background.id === currentBackground()?.id)) {
|
||||
return prev;
|
||||
}
|
||||
return [...prev, currentBackground()!];
|
||||
@@ -90,10 +76,8 @@ export function createBackgrounds(appSettings: Settings): BackgroundsState {
|
||||
};
|
||||
|
||||
const onUnfavoriteCurrentBackground = () => {
|
||||
if (!currentBackground()) {
|
||||
return;
|
||||
}
|
||||
setFavroiteBackgrounds((prev) => prev.filter((b) => b.id !== currentBackground()!.id));
|
||||
setFavroiteBackgrounds((prev) => prev.filter((b) => b.id !== currentBackground()?.id));
|
||||
onNextBackground();
|
||||
};
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user