App.vue 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <template>
  2. <router-view />
  3. </template>
  4. <script setup>
  5. import { useQuasar } from "quasar";
  6. import { StatusBar, Style } 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. const updateStatusBar = async (isDark) => {
  20. if (!Capacitor.isNativePlatform()) return;
  21. await StatusBar.show();
  22. await StatusBar.setStyle({
  23. style: isDark ? Style.Dark : Style.Light,
  24. });
  25. };
  26. watch(
  27. () => $q.dark.isActive,
  28. async (value) => {
  29. await Preferences.set({ key: "theme", value: value ? "dark" : "light" });
  30. await updateStatusBar(value);
  31. },
  32. );
  33. watch(
  34. () => locale.value,
  35. async (value) => {
  36. await Preferences.set({ key: "locale", value });
  37. },
  38. );
  39. onBeforeMount(async () => {
  40. const { value: themePref } = await Preferences.get({ key: "theme" });
  41. const theme = themePref || systemTheme;
  42. const isDark = theme === "dark";
  43. $q.dark.set(isDark);
  44. await updateStatusBar(isDark);
  45. const { value: localePref } = await Preferences.get({ key: "locale" });
  46. const userLocale = localePref || window.navigator.language;
  47. console.log(theme, userLocale);
  48. });
  49. </script>