refs #850 Replace Preferences with typescript

This commit is contained in:
AkiraFukushima 2019-04-06 22:08:12 +09:00
parent 0edac701ae
commit ca6fc13975
11 changed files with 623 additions and 475 deletions

View File

@ -3,8 +3,14 @@ import Account from './Preferences/Account'
import Language from './Preferences/Language'
import Notification from './Preferences/Notification'
import Appearance from './Preferences/Appearance'
import { Module } from 'vuex'
const Preferences = {
export interface PreferencesState {}
const state = (): PreferencesState => ({})
// TODO: use type of rootState
const Preferences: Module<PreferencesState, any> = {
namespaced: true,
modules: {
General,
@ -13,8 +19,7 @@ const Preferences = {
Notification,
Appearance
},
state: {},
mutations: {}
state: state
}
export default Preferences

View File

@ -1,89 +0,0 @@
import { ipcRenderer } from 'electron'
const Account = {
namespaced: true,
state: {
accounts: [],
accountLoading: false
},
mutations: {
updateAccounts (state, accounts) {
state.accounts = accounts
},
updateAccountLoading (state, value) {
state.accountLoading = value
}
},
actions: {
loadAccounts ({ commit }) {
return new Promise((resolve, reject) => {
ipcRenderer.send('list-accounts', 'list')
ipcRenderer.once('error-list-accounts', (event, err) => {
ipcRenderer.removeAllListeners('response-list-accounts')
reject(err)
})
ipcRenderer.once('response-list-accounts', (event, accounts) => {
ipcRenderer.removeAllListeners('error-list-accounts')
commit('updateAccounts', accounts)
resolve(accounts)
})
})
},
removeAccount (_, account) {
return new Promise((resolve, reject) => {
ipcRenderer.send('remove-account', account._id)
ipcRenderer.once('error-remove-account', (_, err) => {
ipcRenderer.removeAllListeners('response-remove-account')
reject(err)
})
ipcRenderer.once('response-remove-account', () => {
ipcRenderer.removeAllListeners('error-remove-account')
resolve()
})
})
},
forwardAccount (_, account) {
console.log(account)
return new Promise((resolve, reject) => {
ipcRenderer.send('forward-account', account)
ipcRenderer.once('error-forward-account', (_, err) => {
ipcRenderer.removeAllListeners('response-forward-account')
reject(err)
})
ipcRenderer.once('response-forward-account', () => {
ipcRenderer.removeAllListeners('error-forward-account')
resolve()
})
})
},
backwardAccount (_, account) {
console.log(account)
return new Promise((resolve, reject) => {
ipcRenderer.send('backward-account', account)
ipcRenderer.once('error-backward-account', (event, err) => {
ipcRenderer.removeAllListeners('response-forward-account')
reject(err)
})
ipcRenderer.once('response-backward-account', () => {
ipcRenderer.removeAllListeners('error-backward-account')
resolve()
})
})
},
removeAllAccounts () {
return new Promise((resolve, reject) => {
ipcRenderer.send('remove-all-accounts')
ipcRenderer.once('error-remove-all-accounts', (_, err) => {
ipcRenderer.removeAllListeners('response-remove-all-accounts')
reject(err)
})
ipcRenderer.once('response-remove-all-accounts', () => {
ipcRenderer.removeAllListeners('error-remove-all-accounts')
resolve()
})
})
}
}
}
export default Account

View File

@ -0,0 +1,107 @@
import { ipcRenderer } from 'electron'
import { Module, MutationTree, ActionTree } from 'vuex'
import Account from '~/src/types/account'
export interface AccountState {
accounts: Array<Account>,
accountLoading: boolean
}
const state = (): AccountState => ({
accounts: [],
accountLoading: false
})
export const MUTATION_TYPES = {
UPDATE_ACCOUNTS: 'updateAccounts',
UPDATE_ACCOUNT_LOADING: 'updateAccountLoading'
}
const mutations: MutationTree<AccountState> = {
[MUTATION_TYPES.UPDATE_ACCOUNTS]: (state, accounts: Array<Account>) => {
state.accounts = accounts
},
[MUTATION_TYPES.UPDATE_ACCOUNT_LOADING]: (state, value: boolean) => {
state.accountLoading = value
}
}
// TODO: use type of rootState
const actions: ActionTree<AccountState, any> = {
loadAccounts: ({ commit }): Promise<Array<Account>> => {
return new Promise((resolve, reject) => {
ipcRenderer.send('list-accounts', 'list')
ipcRenderer.once('error-list-accounts', (_, err: Error) => {
ipcRenderer.removeAllListeners('response-list-accounts')
reject(err)
})
ipcRenderer.once('response-list-accounts', (_, accounts: Array<Account>) => {
ipcRenderer.removeAllListeners('error-list-accounts')
commit(MUTATION_TYPES.UPDATE_ACCOUNTS, accounts)
resolve(accounts)
})
})
},
removeAccount: (_, account: Account) => {
return new Promise((resolve, reject) => {
ipcRenderer.send('remove-account', account._id)
ipcRenderer.once('error-remove-account', (_, err: Error) => {
ipcRenderer.removeAllListeners('response-remove-account')
reject(err)
})
ipcRenderer.once('response-remove-account', () => {
ipcRenderer.removeAllListeners('error-remove-account')
resolve()
})
})
},
forwardAccount: (_, account: Account) => {
return new Promise((resolve, reject) => {
ipcRenderer.send('forward-account', account)
ipcRenderer.once('error-forward-account', (_, err: Error) => {
ipcRenderer.removeAllListeners('response-forward-account')
reject(err)
})
ipcRenderer.once('response-forward-account', () => {
ipcRenderer.removeAllListeners('error-forward-account')
resolve()
})
})
},
backwardAccount: (_, account: Account) => {
return new Promise((resolve, reject) => {
ipcRenderer.send('backward-account', account)
ipcRenderer.once('error-backward-account', (_, err: Error) => {
ipcRenderer.removeAllListeners('response-forward-account')
reject(err)
})
ipcRenderer.once('response-backward-account', () => {
ipcRenderer.removeAllListeners('error-backward-account')
resolve()
})
})
},
removeAllAccounts: () => {
return new Promise((resolve, reject) => {
ipcRenderer.send('remove-all-accounts')
ipcRenderer.once('error-remove-all-accounts', (_, err: Error) => {
ipcRenderer.removeAllListeners('response-remove-all-accounts')
reject(err)
})
ipcRenderer.once('response-remove-all-accounts', () => {
ipcRenderer.removeAllListeners('error-remove-all-accounts')
resolve()
})
})
}
}
// TODO: use type of rootState
const account: Module<AccountState, any> = {
namespaced: true,
state: state,
mutations: mutations,
actions: actions
}
export default account

View File

@ -1,186 +0,0 @@
import { ipcRenderer } from 'electron'
import DisplayStyle from '~/src/constants/displayStyle'
import Theme from '~/src/constants/theme'
import TimeFormat from '~/src/constants/timeFormat'
import { LightTheme } from '~/src/renderer/utils/theme'
import DefaultFonts from '../../utils/fonts'
export default {
namespaced: true,
state: {
appearance: {
theme: Theme.Light.key,
fontSize: 14,
displayNameStyle: DisplayStyle.DisplayNameAndUsername.value,
timeFormat: TimeFormat.Absolute.value,
customThemeColor: LightTheme,
font: DefaultFonts[0]
},
fonts: []
},
mutations: {
updateAppearance (state, conf) {
state.appearance = conf
},
updateFonts (state, fonts) {
state.fonts = fonts
}
},
actions: {
loadAppearance ({ commit }) {
return new Promise((resolve, reject) => {
ipcRenderer.send('get-preferences')
ipcRenderer.once('error-get-preferences', (event, err) => {
ipcRenderer.removeAllListeners('response-get-preferences')
reject(err)
})
ipcRenderer.once('response-get-preferences', (event, conf) => {
ipcRenderer.removeAllListeners('error-get-preferences')
commit('updateAppearance', conf.appearance)
resolve(conf)
})
})
},
loadFonts ({ commit }) {
return new Promise((resolve, reject) => {
ipcRenderer.send('list-fonts')
ipcRenderer.once('error-list-fonts', (event, err) => {
ipcRenderer.removeAllListeners('response-list-fonts')
reject(err)
})
ipcRenderer.once('response-list-fonts', (event, fonts) => {
ipcRenderer.removeAllListeners('error-list-fonts')
commit('updateFonts', [DefaultFonts[0]].concat(fonts))
resolve(fonts)
})
})
},
updateTheme ({ dispatch, commit, state }, theme) {
const newAppearance = Object.assign({}, state.appearance, {
theme: theme
})
const config = {
appearance: newAppearance
}
return new Promise((resolve, reject) => {
ipcRenderer.send('update-preferences', config)
ipcRenderer.once('error-update-preferences', (event, err) => {
ipcRenderer.removeAllListeners('response-update-preferences', err)
reject(err)
})
ipcRenderer.once('response-update-preferences', (event, conf) => {
ipcRenderer.removeAllListeners('error-update-preferences')
commit('updateAppearance', conf.appearance)
dispatch('App/loadPreferences', null, { root: true })
resolve(conf.appearance)
})
})
},
updateFontSize ({ dispatch, commit, state }, fontSize) {
const newAppearance = Object.assign({}, state.appearance, {
fontSize: fontSize
})
const config = {
appearance: newAppearance
}
return new Promise((resolve, reject) => {
ipcRenderer.send('update-preferences', config)
ipcRenderer.once('error-update-preferences', (event, err) => {
ipcRenderer.removeAllListeners('response-update-preferences')
reject(err)
})
ipcRenderer.once('response-update-preferences', (event, conf) => {
ipcRenderer.removeAllListeners('error-update-preferences')
commit('updateAppearance', conf.appearance)
dispatch('App/loadPreferences', null, { root: true })
resolve(conf.appearance)
})
})
},
updateDisplayNameStyle ({ dispatch, commit, state }, value) {
const newAppearance = Object.assign({}, state.appearance, {
displayNameStyle: value
})
const config = {
appearance: newAppearance
}
return new Promise((resolve, reject) => {
ipcRenderer.send('update-preferences', config)
ipcRenderer.once('error-update-preferences', (event, err) => {
ipcRenderer.removeAllListeners('response-update-preferences')
reject(err)
})
ipcRenderer.once('response-update-preferences', (event, conf) => {
ipcRenderer.removeAllListeners('error-update-preferences')
dispatch('App/loadPreferences', null, { root: true })
commit('updateAppearance', conf.appearance)
resolve(conf.appearance)
})
})
},
updateTimeFormat ({ dispatch, commit, state }, value) {
const newAppearance = Object.assign({}, state.appearance, {
timeFormat: value
})
const config = {
appearance: newAppearance
}
return new Promise((resolve, reject) => {
ipcRenderer.send('update-preferences', config)
ipcRenderer.once('error-update-preferences', (event, err) => {
ipcRenderer.removeAllListeners('response-update-preferences')
reject(err)
})
ipcRenderer.once('response-update-preferences', (event, conf) => {
ipcRenderer.removeAllListeners('error-update-preferences')
dispatch('App/loadPreferences', null, { root: true })
commit('updateAppearance', conf.appearance)
resolve(conf.appearance)
})
})
},
updateCustomThemeColor ({ dispatch, state, commit }, value) {
const newCustom = Object.assign({}, state.appearance.customThemeColor, value)
const newAppearance = Object.assign({}, state.appearance, {
customThemeColor: newCustom
})
const config = {
appearance: newAppearance
}
return new Promise((resolve, reject) => {
ipcRenderer.send('update-preferences', config)
ipcRenderer.once('error-update-preferences', (event, err) => {
ipcRenderer.removeAllListeners('response-update-preferences')
reject(err)
})
ipcRenderer.once('response-update-preferences', (event, conf) => {
ipcRenderer.removeAllListeners('error-update-preferences')
commit('updateAppearance', conf.appearance)
dispatch('App/loadPreferences', null, { root: true })
resolve(conf.appearance)
})
})
},
updateFont ({ dispatch, state, commit }, value) {
const newAppearance = Object.assign({}, state.appearance, {
font: value
})
const config = {
appearance: newAppearance
}
return new Promise((resolve, reject) => {
ipcRenderer.send('update-preferences', config)
ipcRenderer.once('error-update-preferences', (event, err) => {
ipcRenderer.removeAllListeners('response-update-preferences')
reject(err)
})
ipcRenderer.once('response-update-preferences', (event, conf) => {
ipcRenderer.removeAllListeners('error-update-preferences')
commit('updateAppearance', conf.appearance)
dispatch('App/loadPreferences', null, { root: true })
resolve(conf.appearance)
})
})
}
}
}

View File

@ -0,0 +1,229 @@
import { ipcRenderer } from 'electron'
import DisplayStyle from '~/src/constants/displayStyle'
import Theme from '~/src/constants/theme'
import TimeFormat from '~/src/constants/timeFormat'
import { LightTheme } from '@/utils/theme'
import DefaultFonts from '@/utils/fonts'
import { Module, MutationTree, ActionTree } from 'vuex'
interface ColorThemeSet {
background_color: string,
selected_background_color: string,
global_header_color: string,
side_menu_color: string,
primary_color: string,
regular_color: string,
secondary_color: string,
border_color: string,
header_menu_color: string,
wrapper_mask_color: string
}
interface AppearanceSet {
theme: string,
fontSize: number,
displayNameStyle: number,
timeFormat: number,
customThemeColor: ColorThemeSet,
font: string
}
export interface AppearanceState {
appearance: AppearanceSet,
fonts: Array<string>
}
const state = (): AppearanceState => ({
appearance: {
theme: Theme.Light.key,
fontSize: 14,
displayNameStyle: DisplayStyle.DisplayNameAndUsername.value,
timeFormat: TimeFormat.Absolute.value,
customThemeColor: LightTheme,
font: DefaultFonts[0]
},
fonts: []
})
export const MUTATION_TYPES = {
UPDATE_APPEARANCE: 'updateAppearance',
UPDATE_FONTS: 'updateFonts'
}
const mutations: MutationTree<AppearanceState> = {
[MUTATION_TYPES.UPDATE_APPEARANCE]: (state, conf: AppearanceSet) => {
state.appearance = conf
},
[MUTATION_TYPES.UPDATE_FONTS]: (state, fonts: Array<string>) => {
state.fonts = fonts
}
}
// TODO: use type of rootState
const actions: ActionTree<AppearanceState, any> = {
loadAppearance: ({ commit }) => {
return new Promise((resolve, reject) => {
ipcRenderer.send('get-preferences')
ipcRenderer.once('error-get-preferences', (_, err: Error) => {
ipcRenderer.removeAllListeners('response-get-preferences')
reject(err)
})
ipcRenderer.once('response-get-preferences', (_, conf: any) => {
ipcRenderer.removeAllListeners('error-get-preferences')
commit(MUTATION_TYPES.UPDATE_APPEARANCE, conf.appearance as AppearanceSet)
resolve(conf)
})
})
},
loadFonts: ({ commit }) => {
return new Promise((resolve, reject) => {
ipcRenderer.send('list-fonts')
ipcRenderer.once('error-list-fonts', (_, err: Error) => {
ipcRenderer.removeAllListeners('response-list-fonts')
reject(err)
})
ipcRenderer.once('response-list-fonts', (_, fonts: Array<string>) => {
ipcRenderer.removeAllListeners('error-list-fonts')
commit(MUTATION_TYPES.UPDATE_FONTS, [DefaultFonts[0]].concat(fonts))
resolve(fonts)
})
})
},
updateTheme: ({ dispatch, commit, state }, themeKey: string) => {
const newAppearance: AppearanceSet = Object.assign({}, state.appearance, {
theme: themeKey
})
const config = {
appearance: newAppearance
}
return new Promise((resolve, reject) => {
ipcRenderer.send('update-preferences', config)
ipcRenderer.once('error-update-preferences', (_, err: Error) => {
ipcRenderer.removeAllListeners('response-update-preferences')
reject(err)
})
ipcRenderer.once('response-update-preferences', (_, conf: any) => {
ipcRenderer.removeAllListeners('error-update-preferences')
commit(MUTATION_TYPES.UPDATE_APPEARANCE, conf.appearance as AppearanceSet)
dispatch('App/loadPreferences', null, { root: true })
resolve(conf.appearance)
})
})
},
updateFontSize: ({ dispatch, commit, state }, fontSize: number) => {
const newAppearance: AppearanceSet = Object.assign({}, state.appearance, {
fontSize: fontSize
})
const config = {
appearance: newAppearance
}
return new Promise((resolve, reject) => {
ipcRenderer.send('update-preferences', config)
ipcRenderer.once('error-update-preferences', (_, err: Error) => {
ipcRenderer.removeAllListeners('response-update-preferences')
reject(err)
})
ipcRenderer.once('response-update-preferences', (_, conf: any) => {
ipcRenderer.removeAllListeners('error-update-preferences')
commit(MUTATION_TYPES.UPDATE_APPEARANCE, conf.appearance as AppearanceSet)
dispatch('App/loadPreferences', null, { root: true })
resolve(conf.appearance)
})
})
},
updateDisplayNameStyle: ({ dispatch, commit, state }, value: number) => {
const newAppearance: AppearanceSet = Object.assign({}, state.appearance, {
displayNameStyle: value
})
const config = {
appearance: newAppearance
}
return new Promise((resolve, reject) => {
ipcRenderer.send('update-preferences', config)
ipcRenderer.once('error-update-preferences', (_, err: Error) => {
ipcRenderer.removeAllListeners('response-update-preferences')
reject(err)
})
ipcRenderer.once('response-update-preferences', (_, conf: any) => {
ipcRenderer.removeAllListeners('error-update-preferences')
dispatch('App/loadPreferences', null, { root: true })
commit(MUTATION_TYPES.UPDATE_APPEARANCE, conf.appearance as AppearanceSet)
resolve(conf.appearance)
})
})
},
updateTimeFormat: ({ dispatch, commit, state }, value: number) => {
const newAppearance: AppearanceSet = Object.assign({}, state.appearance, {
timeFormat: value
})
const config = {
appearance: newAppearance
}
return new Promise((resolve, reject) => {
ipcRenderer.send('update-preferences', config)
ipcRenderer.once('error-update-preferences', (_, err: Error) => {
ipcRenderer.removeAllListeners('response-update-preferences')
reject(err)
})
ipcRenderer.once('response-update-preferences', (_, conf: any) => {
ipcRenderer.removeAllListeners('error-update-preferences')
dispatch('App/loadPreferences', null, { root: true })
commit(MUTATION_TYPES.UPDATE_APPEARANCE, conf.appearance as AppearanceSet)
resolve(conf.appearance)
})
})
},
updateCustomThemeColor: ({ dispatch, state, commit }, value: object) => {
const newCustom: ColorThemeSet = Object.assign({}, state.appearance.customThemeColor, value)
const newAppearance: AppearanceSet = Object.assign({}, state.appearance, {
customThemeColor: newCustom
})
const config = {
appearance: newAppearance
}
return new Promise((resolve, reject) => {
ipcRenderer.send('update-preferences', config)
ipcRenderer.once('error-update-preferences', (_, err: Error) => {
ipcRenderer.removeAllListeners('response-update-preferences')
reject(err)
})
ipcRenderer.once('response-update-preferences', (_, conf: any) => {
ipcRenderer.removeAllListeners('error-update-preferences')
commit(MUTATION_TYPES.UPDATE_APPEARANCE, conf.appearance as AppearanceSet)
dispatch('App/loadPreferences', null, { root: true })
resolve(conf.appearance)
})
})
},
updateFont: ({ dispatch, state, commit }, value: string) => {
const newAppearance: AppearanceSet = Object.assign({}, state.appearance, {
font: value
})
const config = {
appearance: newAppearance
}
return new Promise((resolve, reject) => {
ipcRenderer.send('update-preferences', config)
ipcRenderer.once('error-update-preferences', (_, err: Error) => {
ipcRenderer.removeAllListeners('response-update-preferences')
reject(err)
})
ipcRenderer.once('response-update-preferences', (_, conf: any) => {
ipcRenderer.removeAllListeners('error-update-preferences')
commit(MUTATION_TYPES.UPDATE_APPEARANCE, conf.appearance as AppearanceSet)
dispatch('App/loadPreferences', null, { root: true })
resolve(conf.appearance)
})
})
}
}
// TODO: use type of rootState
const Appearance: Module<AppearanceState, any> = {
namespaced: true,
state: state,
mutations: mutations,
actions: actions
}
export default Appearance

View File

@ -1,97 +0,0 @@
import { ipcRenderer } from 'electron'
const General = {
namespaced: true,
state: {
general: {
sound: {
fav_rb: true,
toot: true
},
timeline: {
cw: false,
nfsw: false,
hideAllAttachments: false
}
},
loading: false
},
mutations: {
updateGeneral (state, conf) {
state.general = conf
},
changeLoading (state, value) {
state.loading = value
}
},
actions: {
loadGeneral ({ commit }) {
return new Promise((resolve, reject) => {
commit('changeLoading', true)
ipcRenderer.send('get-preferences')
ipcRenderer.once('error-get-preferences', (event, err) => {
ipcRenderer.removeAllListeners('response-get-preferences')
commit('changeLoading', false)
reject(err)
})
ipcRenderer.once('response-get-preferences', (event, conf) => {
ipcRenderer.removeAllListeners('error-get-preferences')
commit('updateGeneral', conf.general)
commit('changeLoading', false)
resolve(conf)
})
})
},
updateSound ({ commit, state }, sound) {
commit('changeLoading', true)
const newSound = Object.assign({}, state.general.sound, sound)
const newGeneral = Object.assign({}, state.general, {
sound: newSound
})
const config = {
general: newGeneral
}
return new Promise((resolve, reject) => {
ipcRenderer.send('update-preferences', config)
ipcRenderer.once('error-update-preferences', (event, err) => {
ipcRenderer.removeAllListeners('response-update-preferences')
commit('changeLoading', false)
reject(err)
})
ipcRenderer.once('response-update-preferences', (event, conf) => {
ipcRenderer.removeAllListeners('error-update-preferences')
commit('updateGeneral', conf.general)
commit('changeLoading', false)
resolve(conf)
})
})
},
updateTimeline ({ commit, state, dispatch }, timeline) {
commit('changeLoading', true)
const newTimeline = Object.assign({}, state.general.timeline, timeline)
const newGeneral = Object.assign({}, state.general, {
timeline: newTimeline
})
const config = {
general: newGeneral
}
return new Promise((resolve, reject) => {
ipcRenderer.once('error-update-preferences', (event, err) => {
ipcRenderer.removeAllListeners('response-update-preferences')
commit('changeLoading', false)
reject(err)
})
ipcRenderer.once('response-update-preferences', (event, conf) => {
ipcRenderer.removeAllListeners('error-update-preferences')
commit('updateGeneral', conf.general)
commit('changeLoading', false)
dispatch('App/loadPreferences', null, { root: true })
resolve(conf)
})
ipcRenderer.send('update-preferences', config)
})
}
}
}
export default General

View File

@ -0,0 +1,129 @@
import { ipcRenderer } from 'electron'
import { Module, MutationTree, ActionTree } from 'vuex'
interface Sound {
fav_rb: boolean,
toot: boolean
}
interface Timeline {
cw: boolean,
nfsw: boolean,
hideAllAttachments: boolean
}
interface GeneralSet {
sound: Sound,
timeline: Timeline
}
export interface GeneralState {
general: GeneralSet,
loading: boolean
}
const state = (): GeneralState => ({
general: {
sound: {
fav_rb: true,
toot: true
},
timeline: {
cw: false,
nfsw: false,
hideAllAttachments: false
}
},
loading: false
})
export const MUTATION_TYPES = {
UPDATE_GENERAL: 'updateGeneral',
CHANGE_LOADING: 'changeLoading'
}
const mutations: MutationTree<GeneralState> = {
[MUTATION_TYPES.UPDATE_GENERAL]: (state, conf: GeneralSet) => {
state.general = conf
},
[MUTATION_TYPES.CHANGE_LOADING]: (state, value: boolean) => {
state.loading = value
}
}
// TODO: use type of rootState
const actions: ActionTree<GeneralState, any> = {
loadGeneral: ({ commit }) => {
return new Promise((resolve, reject) => {
commit(MUTATION_TYPES.CHANGE_LOADING, true)
ipcRenderer.send('get-preferences')
ipcRenderer.once('error-get-preferences', (_, err: Error) => {
ipcRenderer.removeAllListeners('response-get-preferences')
commit(MUTATION_TYPES.CHANGE_LOADING, false)
reject(err)
})
ipcRenderer.once('response-get-preferences', (_, conf: any) => {
ipcRenderer.removeAllListeners('error-get-preferences')
commit(MUTATION_TYPES.UPDATE_GENERAL, conf.general as GeneralSet)
commit(MUTATION_TYPES.CHANGE_LOADING, false)
resolve(conf)
})
})
},
updateSound: ({ commit, state }, sound: object) => {
commit(MUTATION_TYPES.CHANGE_LOADING, true)
const newSound: Sound = Object.assign({}, state.general.sound, sound)
const newGeneral: GeneralSet = Object.assign({}, state.general, {
sound: newSound
})
const config = {
general: newGeneral
}
return new Promise((resolve, reject) => {
ipcRenderer.send('update-preferences', config)
ipcRenderer.once('error-update-preferences', (_, err: Error) => {
ipcRenderer.removeAllListeners('response-update-preferences')
commit(MUTATION_TYPES.CHANGE_LOADING, false)
reject(err)
})
ipcRenderer.once('response-update-preferences', (_, conf: any) => {
ipcRenderer.removeAllListeners('error-update-preferences')
commit(MUTATION_TYPES.UPDATE_GENERAL, conf.general as GeneralSet)
commit(MUTATION_TYPES.CHANGE_LOADING, false)
resolve(conf)
})
})
},
updateTimeline: ({ commit, state, dispatch }, timeline: object) => {
commit(MUTATION_TYPES.CHANGE_LOADING, true)
const newTimeline: Timeline = Object.assign({}, state.general.timeline, timeline)
const newGeneral: GeneralSet = Object.assign({}, state.general, {
timeline: newTimeline
})
const config = {
general: newGeneral
}
return new Promise((resolve, reject) => {
ipcRenderer.once('error-update-preferences', (_, err: Error) => {
ipcRenderer.removeAllListeners('response-update-preferences')
commit('changeLoading', false)
reject(err)
})
ipcRenderer.once('response-update-preferences', (_, conf: any) => {
ipcRenderer.removeAllListeners('error-update-preferences')
commit(MUTATION_TYPES.UPDATE_GENERAL, conf.general as GeneralSet)
commit(MUTATION_TYPES.CHANGE_LOADING, false)
dispatch('App/loadPreferences', null, { root: true })
resolve(conf)
})
ipcRenderer.send('update-preferences', config)
})
}
}
const General: Module<GeneralState, any> = {
namespaced: true,
state: state,
mutations: mutations,
actions: actions
}
export default General

View File

@ -1,47 +0,0 @@
import { ipcRenderer } from 'electron'
import Language from '~/src/constants/language'
export default {
namespaced: true,
state: {
language: {
language: Language.en.key
}
},
mutations: {
updateLanguage (state, conf) {
state.language = conf
},
changeLanguage (state, key) {
state.language.language = key
}
},
actions: {
loadLanguage ({ commit }) {
return new Promise((resolve, reject) => {
ipcRenderer.send('get-preferences')
ipcRenderer.once('error-get-preferences', (event, err) => {
ipcRenderer.removeAllListeners('response-get-preferences')
reject(err)
})
ipcRenderer.once('response-get-preferences', (event, conf) => {
ipcRenderer.removeAllListeners('error-get-preferences')
commit('updateLanguage', conf.language)
resolve(conf)
})
})
},
changeLanguage ({ commit }, key) {
return new Promise(resolve => {
ipcRenderer.send('change-language', key)
ipcRenderer.once('response-change-language', (event, value) => {
commit('changeLanguage', value)
resolve(value)
})
})
},
relaunch () {
ipcRenderer.send('relaunch')
}
}
}

View File

@ -0,0 +1,69 @@
import { ipcRenderer } from 'electron'
import Language from '~/src/constants/language'
import { Module, MutationTree, ActionTree } from 'vuex'
interface LanguageSet {
language: string
}
export interface LanguageState {
language: LanguageSet
}
const state: LanguageState = {
language: {
language: Language.en.key
}
}
export const MUTATION_TYPES = {
UPDATE_LANGUAGE: 'updateLanguage',
CHANGE_LANGUAGE: 'changeLanguage'
}
const mutations: MutationTree<LanguageState> = {
[MUTATION_TYPES.UPDATE_LANGUAGE]: (state, conf: LanguageSet) => {
state.language = conf
},
[MUTATION_TYPES.CHANGE_LANGUAGE]: (state, key: string) => {
state.language.language = key
}
}
// TODO: use type of rootState
const actions: ActionTree<LanguageState, any> = {
loadLanguage: ({ commit }) => {
return new Promise((resolve, reject) => {
ipcRenderer.send('get-preferences')
ipcRenderer.once('error-get-preferences', (_, err: Error) => {
ipcRenderer.removeAllListeners('response-get-preferences')
reject(err)
})
ipcRenderer.once('response-get-preferences', (_, conf: any) => {
ipcRenderer.removeAllListeners('error-get-preferences')
commit(MUTATION_TYPES.UPDATE_LANGUAGE, conf.language as LanguageSet)
resolve(conf)
})
})
},
changeLanguage: ({ commit }, key: string) => {
return new Promise(resolve => {
ipcRenderer.send('change-language', key)
ipcRenderer.once('response-change-language', (_, value: string) => {
commit(MUTATION_TYPES.CHANGE_LANGUAGE, value)
resolve(value)
})
})
},
relaunch: () => {
ipcRenderer.send('relaunch')
}
}
// TODO: use type of rootState
export default {
namespaced: true,
state: state,
mutations: mutations,
actions: actions
} as Module<LanguageState, any>

View File

@ -1,53 +0,0 @@
import { ipcRenderer } from 'electron'
export default {
namespaced: true,
state: {
notification: {
notify: {
reply: true,
reblog: true,
favourite: true,
follow: true
}
}
},
mutations: {
updateNotification (state, notification) {
state.notification = notification
}
},
actions: {
loadNotification ({ commit }) {
return new Promise((resolve, reject) => {
ipcRenderer.send('get-preferences')
ipcRenderer.once('error-get-preferences', (event, err) => {
ipcRenderer.removeAllListeners('response-get-preferences')
reject(err)
})
ipcRenderer.once('response-get-preferences', (event, conf) => {
ipcRenderer.removeAllListeners('error-get-preferences')
commit('updateNotification', conf.notification)
resolve(conf)
})
})
},
updateNotify ({ commit, state, dispatch }, notify) {
const newNotify = Object.assign({}, state.notification.notify, notify)
const newNotification = Object.assign({}, state.notification, {
notify: newNotify
})
const config = {
notification: newNotification
}
return new Promise(resolve => {
ipcRenderer.send('update-preferences', config)
ipcRenderer.once('response-update-preferences', (event, conf) => {
commit('updateNotification', conf.notification)
dispatch('App/loadPreferences', null, { root: true })
resolve(conf.notification)
})
})
}
}
}

View File

@ -0,0 +1,81 @@
import { ipcRenderer } from 'electron'
import { Module, MutationTree, ActionTree } from 'vuex'
interface Notify {
reply: boolean,
reblog: boolean,
favourite: boolean,
follow: boolean
}
interface Notification {
notify: Notify
}
export interface NotificationState {
notification: Notification
}
const state: NotificationState = {
notification: {
notify: {
reply: true,
reblog: true,
favourite: true,
follow: true
}
}
}
export const MUTATION_TYPES = {
UPDATE_NOTIFICATION: 'updateNotification'
}
const mutations: MutationTree<NotificationState> = {
[MUTATION_TYPES.UPDATE_NOTIFICATION]: (state, notification: Notification) => {
state.notification = notification
}
}
// TODO: use type of rootState
const actions: ActionTree<NotificationState, any> = {
loadNotification: ({ commit }) => {
return new Promise((resolve, reject) => {
ipcRenderer.send('get-preferences')
ipcRenderer.once('error-get-preferences', (_, err: Error) => {
ipcRenderer.removeAllListeners('response-get-preferences')
reject(err)
})
ipcRenderer.once('response-get-preferences', (_, conf: any) => {
ipcRenderer.removeAllListeners('error-get-preferences')
commit(MUTATION_TYPES.UPDATE_NOTIFICATION, conf.notification as Notification)
resolve(conf)
})
})
},
updateNotify: ({ commit, state, dispatch }, notify: object) => {
const newNotify: Notify = Object.assign({}, state.notification.notify, notify)
const newNotification: Notification = Object.assign({}, state.notification, {
notify: newNotify
})
const config = {
notification: newNotification
}
return new Promise(resolve => {
ipcRenderer.send('update-preferences', config)
ipcRenderer.once('response-update-preferences', (_, conf: any) => {
commit(MUTATION_TYPES.UPDATE_NOTIFICATION, conf.notification as Notification)
dispatch('App/loadPreferences', null, { root: true })
resolve(conf.notification)
})
})
}
}
// TODO: use type of rootState
export default {
namespaced: true,
state: state,
mutations: mutations,
actions: actions
} as Module<NotificationState, any>