fix: change to mmkv and fix downloads with VLC

This commit is contained in:
Fredrik Burmester
2024-11-24 19:34:49 +01:00
parent 335765993d
commit 93d117640a
12 changed files with 191 additions and 172 deletions

View File

@@ -1,6 +1,6 @@
import AsyncStorage from "@react-native-async-storage/async-storage";
import { atom } from "jotai";
import { atomWithStorage, createJSONStorage } from "jotai/utils";
import { atomWithStorage } from "jotai/utils";
import { storage } from "../mmkv";
export enum SortByOption {
Default = "Default",
@@ -68,9 +68,6 @@ export const sortOrderAtom = atom<SortOrderOption[]>([
SortOrderOption.Ascending,
]);
/**
* Sort preferences with persistence
*/
export interface SortPreference {
[libraryId: string]: SortByOption;
}
@@ -86,15 +83,15 @@ export const sortByPreferenceAtom = atomWithStorage<SortPreference>(
"sortByPreference",
defaultSortPreference,
{
getItem: async (key) => {
const value = await AsyncStorage.getItem(key);
getItem: (key) => {
const value = storage.getString(key);
return value ? JSON.parse(value) : null;
},
setItem: async (key, value) => {
await AsyncStorage.setItem(key, JSON.stringify(value));
setItem: (key, value) => {
storage.set(key, JSON.stringify(value));
},
removeItem: async (key) => {
await AsyncStorage.removeItem(key);
removeItem: (key) => {
storage.delete(key);
},
}
);
@@ -103,20 +100,19 @@ export const sortOrderPreferenceAtom = atomWithStorage<SortOrderPreference>(
"sortOrderPreference",
defaultSortOrderPreference,
{
getItem: async (key) => {
const value = await AsyncStorage.getItem(key);
getItem: (key) => {
const value = storage.getString(key);
return value ? JSON.parse(value) : null;
},
setItem: async (key, value) => {
await AsyncStorage.setItem(key, JSON.stringify(value));
setItem: (key, value) => {
storage.set(key, JSON.stringify(value));
},
removeItem: async (key) => {
await AsyncStorage.removeItem(key);
removeItem: (key) => {
storage.delete(key);
},
}
);
// Helper functions to get and set sort preferences
export const getSortByPreference = (
libraryId: string,
preferences: SortPreference
@@ -130,4 +126,3 @@ export const getSortOrderPreference = (
) => {
return preferences?.[libraryId] || null;
};

View File

@@ -1,7 +1,5 @@
import { BaseItemDto } from "@jellyfin/sdk/lib/generated-client/models";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { atom, useAtom } from "jotai";
import { atomWithStorage } from "jotai/utils";
import { useEffect } from "react";
export interface Job {

View File

@@ -1,7 +1,7 @@
import { atom, useAtom } from "jotai";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { useEffect } from "react";
import * as ScreenOrientation from "expo-screen-orientation";
import { storage } from "../mmkv";
export type DownloadQuality = "original" | "high" | "low";
@@ -75,15 +75,8 @@ export type Settings = {
downloadMethod: "optimized" | "remux";
autoDownload: boolean;
};
/**
*
* The settings atom is a Jotai atom that stores the user's settings.
* It is initialized with a default value of null, which indicates that the settings have not been loaded yet.
* The settings are loaded from AsyncStorage when the atom is read for the first time.
*
*/
const loadSettings = async (): Promise<Settings> => {
const loadSettings = (): Settings => {
const defaultValues: Settings = {
autoRotate: true,
forceLandscapeInVideoPlayer: false,
@@ -113,7 +106,7 @@ const loadSettings = async (): Promise<Settings> => {
};
try {
const jsonValue = await AsyncStorage.getItem("settings");
const jsonValue = storage.getString("settings");
const loadedValues: Partial<Settings> =
jsonValue != null ? JSON.parse(jsonValue) : {};
@@ -124,30 +117,28 @@ const loadSettings = async (): Promise<Settings> => {
}
};
// Utility function to save settings to AsyncStorage
const saveSettings = async (settings: Settings) => {
const saveSettings = (settings: Settings) => {
const jsonValue = JSON.stringify(settings);
await AsyncStorage.setItem("settings", jsonValue);
storage.set("settings", jsonValue);
};
// Create an atom to store the settings in memory
export const settingsAtom = atom<Settings | null>(null);
// A hook to manage settings, loading them on initial mount and providing a way to update them
export const useSettings = () => {
const [settings, setSettings] = useAtom(settingsAtom);
useEffect(() => {
if (settings === null) {
loadSettings().then(setSettings);
const loadedSettings = loadSettings();
setSettings(loadedSettings);
}
}, [settings, setSettings]);
const updateSettings = async (update: Partial<Settings>) => {
const updateSettings = (update: Partial<Settings>) => {
if (settings) {
const newSettings = { ...settings, ...update };
setSettings(newSettings);
await saveSettings(newSettings);
saveSettings(newSettings);
}
};