App.vue 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <template>
  2. <router-view />
  3. </template>
  4. <script setup>
  5. import { useQuasar } from "quasar";
  6. import { StatusBar } from "@capacitor/status-bar";
  7. import { Capacitor } from "@capacitor/core";
  8. import { watch, onBeforeMount } from "vue";
  9. import { useI18n } from "vue-i18n";
  10. import { Preferences } from "@capacitor/preferences";
  11. defineOptions({
  12. name: "App",
  13. });
  14. const { locale } = useI18n();
  15. const $q = useQuasar();
  16. const systemTheme = window.matchMedia("(prefers-color-scheme: dark)").matches
  17. ? "dark"
  18. : "light";
  19. watch(
  20. () => $q.dark.isActive,
  21. async (value) => {
  22. await Preferences.set({ key: "theme", value: value });
  23. },
  24. );
  25. watch(
  26. () => locale.value,
  27. async (value) => {
  28. await Preferences.set({ key: "locale", value: value });
  29. },
  30. );
  31. onBeforeMount(async () => {
  32. const isNative = Capacitor.isNativePlatform();
  33. if (isNative) await StatusBar.show();
  34. const { value: themePref } = await Preferences.get({ key: "theme" });
  35. const theme = themePref || systemTheme;
  36. if (isNative) await StatusBar.setStyle({ style: theme.toUpperCase() });
  37. const { value: localePref } = await Preferences.get({ key: "locale" });
  38. const userLocale = localePref || window.navigator.language;
  39. console.log(theme, userLocale);
  40. $q.dark.set(theme == "dark");
  41. });
  42. </script>