feat: Remember screen lock preference (#7609)

This commit is contained in:
Michael Genson
2026-05-11 14:12:50 -05:00
committed by GitHub
parent 2dbfc7f72b
commit 8a054b1be8
2 changed files with 47 additions and 44 deletions

View File

@@ -14,17 +14,25 @@
<script setup lang="ts"> <script setup lang="ts">
import { useWakeLock } from "@vueuse/core"; import { useWakeLock } from "@vueuse/core";
import { useUserExperiencePreferences } from "~/composables/use-users/preferences";
const { isSupported: wakeIsSupported, isActive, request, release } = useWakeLock(); const { isSupported: wakeIsSupported, isActive, request, release } = useWakeLock();
const userExperiencePreferences = useUserExperiencePreferences();
function handleLock() {
if (userExperiencePreferences.value.lockScreen) {
lockScreen();
}
else {
unlockScreen();
}
}
const wakeLock = computed({ const wakeLock = computed({
get: () => isActive.value, get: () => userExperiencePreferences.value.lockScreen,
set: () => { set: () => {
if (isActive.value) { userExperiencePreferences.value.lockScreen = !userExperiencePreferences.value.lockScreen;
unlockScreen(); handleLock();
}
else {
lockScreen();
}
}, },
}); });
async function lockScreen() { async function lockScreen() {
@@ -34,11 +42,11 @@ async function lockScreen() {
} }
} }
async function unlockScreen() { async function unlockScreen() {
if (wakeIsSupported || isActive) { if (wakeIsSupported || isActive.value) {
console.debug("Wake Lock Released"); console.debug("Wake Lock Released");
await release(); await release();
} }
} }
onMounted(() => lockScreen()); onMounted(() => handleLock());
onUnmounted(() => unlockScreen()); onUnmounted(() => unlockScreen());
</script> </script>

View File

@@ -73,6 +73,10 @@ export interface UserActivityPreferences {
defaultActivity: ActivityKey; defaultActivity: ActivityKey;
} }
export interface UserExperiencePreferences {
lockScreen: boolean;
}
export function useUserMealPlanPreferences(): Ref<UserMealPlanPreferences> { export function useUserMealPlanPreferences(): Ref<UserMealPlanPreferences> {
const fromStorage = useLocalStorage( const fromStorage = useLocalStorage(
"meal-planner-preferences", "meal-planner-preferences",
@@ -81,9 +85,7 @@ export function useUserMealPlanPreferences(): Ref<UserMealPlanPreferences> {
numberOfDays: 7, numberOfDays: 7,
}, },
{ mergeDefaults: true }, { mergeDefaults: true },
// we cast to a Ref because by default it will return an optional type ref );
// but since we pass defaults we know all properties are set.
) as unknown as Ref<UserMealPlanPreferences>;
return fromStorage; return fromStorage;
} }
@@ -92,15 +94,14 @@ export function useUserPrintPreferences(): Ref<UserPrintPreferences> {
const fromStorage = useLocalStorage( const fromStorage = useLocalStorage(
"recipe-print-preferences", "recipe-print-preferences",
{ {
imagePosition: "left", imagePosition: "left" as ImagePosition,
showDescription: true, showDescription: true,
showNotes: true, showNotes: true,
showNutrition: false,
expandChildRecipes: false, expandChildRecipes: false,
}, },
{ mergeDefaults: true }, { mergeDefaults: true },
// we cast to a Ref because by default it will return an optional type ref );
// but since we pass defaults we know all properties are set.
) as unknown as Ref<UserPrintPreferences>;
return fromStorage; return fromStorage;
} }
@@ -118,9 +119,7 @@ export function useUserSortPreferences(): Ref<UserRecipePreferences> {
useMobileCards: false, useMobileCards: false,
}, },
{ mergeDefaults: true }, { mergeDefaults: true },
// we cast to a Ref because by default it will return an optional type ref );
// but since we pass defaults we know all properties are set.
) as unknown as Ref<UserRecipePreferences>;
return fromStorage; return fromStorage;
} }
@@ -132,9 +131,7 @@ export function useUserActivityPreferences(): Ref<UserActivityPreferences> {
defaultActivity: ActivityKey.RECIPES, defaultActivity: ActivityKey.RECIPES,
}, },
{ mergeDefaults: true }, { mergeDefaults: true },
// we cast to a Ref because by default it will return an optional type ref );
// but since we pass defaults we know all properties are set.
) as Ref<UserActivityPreferences>;
return fromStorage; return fromStorage;
} }
@@ -146,9 +143,7 @@ export function useUserSearchQuerySession(): Ref<UserSearchQuery> {
recipe: "", recipe: "",
}, },
{ mergeDefaults: true }, { mergeDefaults: true },
// we cast to a Ref because by default it will return an optional type ref );
// but since we pass defaults we know all properties are set.
) as unknown as Ref<UserSearchQuery>;
return fromStorage; return fromStorage;
} }
@@ -160,9 +155,7 @@ export function useShoppingListPreferences(): Ref<UserShoppingListPreferences> {
viewAllLists: false, viewAllLists: false,
}, },
{ mergeDefaults: true }, { mergeDefaults: true },
// we cast to a Ref because by default it will return an optional type ref );
// but since we pass defaults we know all properties are set.
) as unknown as Ref<UserShoppingListPreferences>;
return fromStorage; return fromStorage;
} }
@@ -175,9 +168,7 @@ export function useTimelinePreferences(): Ref<UserTimelinePreferences> {
types: ["info", "system", "comment"] as TimelineEventType[], types: ["info", "system", "comment"] as TimelineEventType[],
}, },
{ mergeDefaults: true }, { mergeDefaults: true },
// we cast to a Ref because by default it will return an optional type ref );
// but since we pass defaults we know all properties are set.
) as unknown as Ref<UserTimelinePreferences>;
return fromStorage; return fromStorage;
} }
@@ -186,12 +177,10 @@ export function useParsingPreferences(): Ref<UserParsingPreferences> {
const fromStorage = useLocalStorage( const fromStorage = useLocalStorage(
"parsing-preferences", "parsing-preferences",
{ {
parser: "nlp", parser: "nlp" as RegisteredParser,
}, },
{ mergeDefaults: true }, { mergeDefaults: true },
// we cast to a Ref because by default it will return an optional type ref );
// but since we pass defaults we know all properties are set.
) as unknown as Ref<UserParsingPreferences>;
return fromStorage; return fromStorage;
} }
@@ -203,9 +192,7 @@ export function useCookbookPreferences(): Ref<UserCookbooksPreferences> {
hideOtherHouseholds: false, hideOtherHouseholds: false,
}, },
{ mergeDefaults: true }, { mergeDefaults: true },
// we cast to a Ref because by default it will return an optional type ref );
// but since we pass defaults we know all properties are set.
) as unknown as Ref<UserCookbooksPreferences>;
return fromStorage; return fromStorage;
} }
@@ -224,9 +211,7 @@ export function useRecipeFinderPreferences(): Ref<UserRecipeFinderPreferences> {
includeToolsOnHand: true, includeToolsOnHand: true,
}, },
{ mergeDefaults: true }, { mergeDefaults: true },
// we cast to a Ref because by default it will return an optional type ref );
// but since we pass defaults we know all properties are set.
) as unknown as Ref<UserRecipeFinderPreferences>;
return fromStorage; return fromStorage;
} }
@@ -241,9 +226,19 @@ export function useRecipeCreatePreferences(): Ref<UserRecipeCreatePreferences> {
parseRecipe: true, parseRecipe: true,
}, },
{ mergeDefaults: true }, { mergeDefaults: true },
// we cast to a Ref because by default it will return an optional type ref );
// but since we pass defaults we know all properties are set.
) as unknown as Ref<UserRecipeCreatePreferences>; return fromStorage;
}
export function useUserExperiencePreferences(): Ref<UserExperiencePreferences> {
const fromStorage = useLocalStorage(
"user-experience-preferences",
{
lockScreen: true,
},
{ mergeDefaults: true },
);
return fromStorage; return fromStorage;
} }