migrate settings store to composition API and typescript

This commit is contained in:
MaysWind
2025-01-05 19:11:09 +08:00
parent 4f51480af9
commit 0e422b5a8f
62 changed files with 376 additions and 488 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
import { defineStore } from 'pinia';
import { useSettingsStore } from './setting.js';
import { useSettingsStore } from './setting.ts';
import { useUserStore } from './user.js';
import { useAccountsStore } from './account.js';
import { useTransactionCategoriesStore } from './transactionCategory.js';
+1 -1
View File
@@ -1,6 +1,6 @@
import { defineStore } from 'pinia';
import { useSettingsStore } from '@/stores/setting.js';
import { useSettingsStore } from '@/stores/setting.ts';
import { useUserStore } from './user.js';
import { useExchangeRatesStore } from './exchangeRates.js';
-156
View File
@@ -1,156 +0,0 @@
import { defineStore } from 'pinia';
import { WeekDay } from '@/core/datetime.ts';
import { DEFAULT_CURRENCY_CODE } from '@/consts/currency.ts';
import * as settings from '@/lib/settings.ts';
export const useSettingsStore = defineStore('settings', {
state: () => ({
appSettings: {
theme: settings.getTheme(),
fontSize: settings.getFontSize(),
timeZone: settings.getTimeZone(),
applicationLock: settings.isEnableApplicationLock(),
applicationLockWebAuthn: settings.isEnableApplicationLockWebAuthn(),
autoUpdateExchangeRatesData: settings.isAutoUpdateExchangeRatesData(),
autoSaveTransactionDraft: settings.getAutoSaveTransactionDraft(),
autoGetCurrentGeoLocation: settings.isAutoGetCurrentGeoLocation(),
showAmountInHomePage: settings.isShowAmountInHomePage(),
timezoneUsedForStatisticsInHomePage: settings.getTimezoneUsedForStatisticsInHomePage(),
itemsCountInTransactionListPage: settings.getItemsCountInTransactionListPage(),
showTotalAmountInTransactionListPage: settings.isShowTotalAmountInTransactionListPage(),
showTagInTransactionListPage: settings.isShowTagInTransactionListPage(),
showAccountBalance: settings.isShowAccountBalance(),
currencySortByInExchangeRatesPage: settings.getCurrencySortByInExchangeRatesPage(),
statistics: {
defaultChartDataType: settings.getStatisticsDefaultChartDataType(),
defaultTimezoneType: settings.getStatisticsDefaultTimezoneType(),
defaultAccountFilter: settings.getStatisticsDefaultAccountFilter(),
defaultTransactionCategoryFilter: settings.getStatisticsDefaultTransactionCategoryFilter(),
defaultSortingType: settings.getStatisticsSortingType(),
defaultCategoricalChartType: settings.getStatisticsDefaultCategoricalChartType(),
defaultCategoricalChartDataRangeType: settings.getStatisticsDefaultCategoricalChartDataRange(),
defaultTrendChartType: settings.getStatisticsDefaultTrendChartType(),
defaultTrendChartDataRangeType: settings.getStatisticsDefaultTrendChartDataRange(),
},
animate: settings.isEnableAnimate()
},
localeDefaultSettings: {
currency: DEFAULT_CURRENCY_CODE,
firstDayOfWeek: WeekDay.DefaultFirstDay.type
}
}),
actions: {
setTheme(value) {
settings.setTheme(value);
this.appSettings.theme = value;
},
setFontSize(value) {
settings.setFontSize(value);
this.appSettings.fontSize = value;
},
setTimeZone(value) {
settings.setTimeZone(value);
this.appSettings.timeZone = value;
},
setEnableApplicationLock(value) {
settings.setEnableApplicationLock(value);
this.appSettings.applicationLock = value;
},
setEnableApplicationLockWebAuthn(value) {
settings.setEnableApplicationLockWebAuthn(value);
this.appSettings.applicationLockWebAuthn = value;
},
setAutoUpdateExchangeRatesData(value) {
settings.setAutoUpdateExchangeRatesData(value);
this.appSettings.autoUpdateExchangeRatesData = value;
},
setAutoSaveTransactionDraft(value) {
settings.setAutoSaveTransactionDraft(value);
this.appSettings.autoSaveTransactionDraft = value;
},
setAutoGetCurrentGeoLocation(value) {
settings.setAutoGetCurrentGeoLocation(value);
this.appSettings.autoGetCurrentGeoLocation = value;
},
setShowAmountInHomePage(value) {
settings.setShowAmountInHomePage(value);
this.appSettings.showAmountInHomePage = value;
},
setTimezoneUsedForStatisticsInHomePage(value) {
settings.setTimezoneUsedForStatisticsInHomePage(value);
this.appSettings.timezoneUsedForStatisticsInHomePage = value;
},
setItemsCountInTransactionListPage(value) {
settings.setItemsCountInTransactionListPage(value);
this.appSettings.itemsCountInTransactionListPage = value;
},
setShowTotalAmountInTransactionListPage(value) {
settings.setShowTotalAmountInTransactionListPage(value);
this.appSettings.showTotalAmountInTransactionListPage = value;
},
setShowTagInTransactionListPage(value) {
settings.setShowTagInTransactionListPage(value);
this.appSettings.showTagInTransactionListPage = value;
},
setShowAccountBalance(value) {
settings.setShowAccountBalance(value);
this.appSettings.showAccountBalance = value;
},
setCurrencySortByInExchangeRatesPage(value) {
settings.setCurrencySortByInExchangeRatesPage(value);
this.appSettings.currencySortByInExchangeRatesPage = value;
},
setStatisticsDefaultChartDataType(value) {
settings.setStatisticsDefaultChartDataType(value);
this.appSettings.statistics.defaultChartDataType = value;
},
setStatisticsDefaultTimezoneType(value) {
settings.setStatisticsDefaultTimezoneType(value);
this.appSettings.statistics.defaultTimezoneType = value;
},
setStatisticsDefaultAccountFilter(value) {
settings.setStatisticsDefaultAccountFilter(value);
this.appSettings.statistics.defaultAccountFilter = value;
},
setStatisticsDefaultTransactionCategoryFilter(value) {
settings.setStatisticsDefaultTransactionCategoryFilter(value);
this.appSettings.statistics.defaultTransactionCategoryFilter = value;
},
setStatisticsSortingType(value) {
settings.setStatisticsSortingType(value);
this.appSettings.statistics.defaultSortingType = value;
},
setStatisticsDefaultCategoricalChartType(value) {
settings.setStatisticsDefaultCategoricalChartType(value);
this.appSettings.statistics.defaultCategoricalChartType = value;
},
setStatisticsDefaultCategoricalChartDateRange(value) {
settings.setStatisticsDefaultCategoricalChartDataRange(value);
this.appSettings.statistics.defaultCategoricalChartDataRangeType = value;
},
setStatisticsDefaultTrendChartType(value) {
settings.setStatisticsDefaultTrendChartType(value);
this.appSettings.statistics.defaultTrendChartType = value;
},
setStatisticsDefaultTrendChartDateRange(value) {
settings.setStatisticsDefaultTrendChartDataRange(value);
this.appSettings.statistics.defaultTrendChartDataRangeType = value;
},
setEnableAnimate(value) {
settings.setEnableAnimate(value);
this.appSettings.animate = value;
},
clearAppSettings() {
settings.clearSettings();
},
updateLocalizedDefaultSettings(localeDefaultSettings) {
if (!localeDefaultSettings) {
return;
}
this.localeDefaultSettings.currency = localeDefaultSettings.defaultCurrency;
this.localeDefaultSettings.firstDayOfWeek = localeDefaultSettings.defaultFirstDayOfWeek;
}
}
});
+187
View File
@@ -0,0 +1,187 @@
import { type Ref, ref } from 'vue';
import { defineStore } from 'pinia';
import type { ApplicationSettings, LocaleDefaultSettings } from '@/core/setting.ts';
import {
getApplicationSettings,
getLocaleDefaultSettings,
updateApplicationSettingsValue,
updateApplicationSettingsSubValue,
clearSettings
} from '@/lib/settings.ts';
export const useSettingsStore = defineStore('settings', () => {
const appSettings: Ref<ApplicationSettings> = ref(getApplicationSettings());
const localeDefaultSettings: Ref<LocaleDefaultSettings> = ref(getLocaleDefaultSettings());
function setTheme(value: string): void {
updateApplicationSettingsValue('theme', value);
appSettings.value.theme = value;
}
function setFontSize(value: number): void {
updateApplicationSettingsValue('fontSize', value);
appSettings.value.fontSize = value;
}
function setTimeZone(value: string): void {
updateApplicationSettingsValue('timeZone', value);
appSettings.value.timeZone = value;
}
function setEnableApplicationLock(value: boolean): void {
updateApplicationSettingsValue('applicationLock', value);
appSettings.value.applicationLock = value;
}
function setEnableApplicationLockWebAuthn(value: boolean): void {
updateApplicationSettingsValue('applicationLockWebAuthn', value);
appSettings.value.applicationLockWebAuthn = value;
}
function setAutoUpdateExchangeRatesData(value: boolean): void {
updateApplicationSettingsValue('autoUpdateExchangeRatesData', value);
appSettings.value.autoUpdateExchangeRatesData = value;
}
function setAutoSaveTransactionDraft(value: string): void {
updateApplicationSettingsValue('autoSaveTransactionDraft', value);
appSettings.value.autoSaveTransactionDraft = value;
}
function setAutoGetCurrentGeoLocation(value: boolean): void {
updateApplicationSettingsValue('autoGetCurrentGeoLocation', value);
appSettings.value.autoGetCurrentGeoLocation = value;
}
function setShowAmountInHomePage(value: boolean): void {
updateApplicationSettingsValue('showAmountInHomePage', value);
appSettings.value.showAmountInHomePage = value;
}
function setTimezoneUsedForStatisticsInHomePage(value: number): void {
updateApplicationSettingsValue('timezoneUsedForStatisticsInHomePage', value);
appSettings.value.timezoneUsedForStatisticsInHomePage = value;
}
function setItemsCountInTransactionListPage(value: number): void {
updateApplicationSettingsValue('itemsCountInTransactionListPage', value);
appSettings.value.itemsCountInTransactionListPage = value;
}
function setShowTotalAmountInTransactionListPage(value: boolean): void {
updateApplicationSettingsValue('showTotalAmountInTransactionListPage', value);
appSettings.value.showTotalAmountInTransactionListPage = value;
}
function setShowTagInTransactionListPage(value: boolean): void {
updateApplicationSettingsValue('showTagInTransactionListPage', value);
appSettings.value.showTagInTransactionListPage = value;
}
function setShowAccountBalance(value: boolean): void {
updateApplicationSettingsValue('showAccountBalance', value);
appSettings.value.showAccountBalance = value;
}
function setCurrencySortByInExchangeRatesPage(value: number): void {
updateApplicationSettingsValue('currencySortByInExchangeRatesPage', value);
appSettings.value.currencySortByInExchangeRatesPage = value;
}
function setStatisticsDefaultChartDataType(value: number): void {
updateApplicationSettingsSubValue('statistics', 'defaultChartDataType', value);
appSettings.value.statistics.defaultChartDataType = value;
}
function setStatisticsDefaultTimezoneType(value: number): void {
updateApplicationSettingsSubValue('statistics', 'defaultTimezoneType', value);
appSettings.value.statistics.defaultTimezoneType = value;
}
function setStatisticsDefaultAccountFilter(value: Record<string, boolean>): void {
updateApplicationSettingsSubValue('statistics', 'defaultAccountFilter', value);
appSettings.value.statistics.defaultAccountFilter = value;
}
function setStatisticsDefaultTransactionCategoryFilter(value: Record<string, boolean>): void {
updateApplicationSettingsSubValue('statistics', 'defaultTransactionCategoryFilter', value);
appSettings.value.statistics.defaultTransactionCategoryFilter = value;
}
function setStatisticsSortingType(value: number): void {
updateApplicationSettingsSubValue('statistics', 'defaultSortingType', value);
appSettings.value.statistics.defaultSortingType = value;
}
function setStatisticsDefaultCategoricalChartType(value: number): void {
updateApplicationSettingsSubValue('statistics', 'defaultCategoricalChartType', value);
appSettings.value.statistics.defaultCategoricalChartType = value;
}
function setStatisticsDefaultCategoricalChartDateRange(value: number): void {
updateApplicationSettingsSubValue('statistics', 'defaultCategoricalChartDataRangeType', value);
appSettings.value.statistics.defaultCategoricalChartDataRangeType = value;
}
function setStatisticsDefaultTrendChartType(value: number): void {
updateApplicationSettingsSubValue('statistics', 'defaultTrendChartType', value);
appSettings.value.statistics.defaultTrendChartType = value;
}
function setStatisticsDefaultTrendChartDateRange(value: number): void {
updateApplicationSettingsSubValue('statistics', 'defaultTrendChartDataRangeType', value);
appSettings.value.statistics.defaultTrendChartDataRangeType = value;
}
function setEnableAnimate(value: boolean): void {
updateApplicationSettingsValue('animate', value);
appSettings.value.animate = value;
}
function clearAppSettings(): void {
clearSettings();
appSettings.value = getApplicationSettings();
}
function updateLocalizedDefaultSettings(newLocaleDefaultSettings: LocaleDefaultSettings) {
if (!newLocaleDefaultSettings) {
return;
}
localeDefaultSettings.value.currency = newLocaleDefaultSettings.currency;
localeDefaultSettings.value.firstDayOfWeek = newLocaleDefaultSettings.firstDayOfWeek;
}
return {
appSettings,
localeDefaultSettings,
setTheme,
setFontSize,
setTimeZone,
setEnableApplicationLock,
setEnableApplicationLockWebAuthn,
setAutoUpdateExchangeRatesData,
setAutoSaveTransactionDraft,
setAutoGetCurrentGeoLocation,
setShowAmountInHomePage,
setTimezoneUsedForStatisticsInHomePage,
setItemsCountInTransactionListPage,
setShowTotalAmountInTransactionListPage,
setShowTagInTransactionListPage,
setShowAccountBalance,
setCurrencySortByInExchangeRatesPage,
setStatisticsDefaultChartDataType,
setStatisticsDefaultTimezoneType,
setStatisticsDefaultAccountFilter,
setStatisticsDefaultTransactionCategoryFilter,
setStatisticsSortingType,
setStatisticsDefaultCategoricalChartType,
setStatisticsDefaultCategoricalChartDateRange,
setStatisticsDefaultTrendChartType,
setStatisticsDefaultTrendChartDateRange,
setEnableAnimate,
clearAppSettings,
updateLocalizedDefaultSettings
};
});
+1 -1
View File
@@ -1,6 +1,6 @@
import { defineStore } from 'pinia';
import { useSettingsStore } from './setting.js';
import { useSettingsStore } from './setting.ts';
import { useUserStore } from './user.js';
import { useAccountsStore } from './account.js';
import { useTransactionCategoriesStore } from './transactionCategory.js';
+1 -1
View File
@@ -1,6 +1,6 @@
import { defineStore } from 'pinia';
import { useSettingsStore } from './setting.js';
import { useSettingsStore } from './setting.ts';
import { useUserStore } from './user.js';
import { useAccountsStore } from './account.js';
import { useTransactionCategoriesStore } from './transactionCategory.js';
+1 -1
View File
@@ -1,6 +1,6 @@
import { defineStore } from 'pinia';
import { useSettingsStore } from './setting.js';
import { useSettingsStore } from './setting.ts';
import userState from '@/lib/userstate.ts';
import services from '@/lib/services.ts';