Skip to content

Commit 8994312

Browse files
authored
Merge pull request #1092 from shatyuka/save_preference
refactor: improve preference
2 parents 559df6b + 8052aa0 commit 8994312

27 files changed

+234
-44
lines changed

src/renderer/components/Preference/Advanced.vue

+79-10
Original file line numberDiff line numberDiff line change
@@ -371,16 +371,18 @@
371371
import is from 'electron-is'
372372
import { dialog } from '@electron/remote'
373373
import { mapState } from 'vuex'
374-
import { cloneDeep } from 'lodash'
374+
import { cloneDeep, extend, isEmpty } from 'lodash'
375375
import randomize from 'randomatic'
376376
import * as clipboard from 'clipboard-polyfill'
377377
import ShowInFolder from '@/components/Native/ShowInFolder'
378378
import SubnavSwitcher from '@/components/Subnav/SubnavSwitcher'
379379
import userAgentMap from '@shared/ua'
380-
import { trackerSourceOptions } from '@shared/constants'
380+
import { APP_RUN_MODE, trackerSourceOptions } from '@shared/constants'
381381
import {
382+
backupConfig,
382383
buildRpcUrl,
383384
calcFormLabelWidth,
385+
changedConfig,
384386
checkIsNeedRestart,
385387
convertCommaToLine,
386388
convertLineToComma,
@@ -391,6 +393,8 @@
391393
import '@/components/Icons/dice'
392394
import '@/components/Icons/sync'
393395
import '@/components/Icons/refresh'
396+
import { getLanguage } from '@shared/locales'
397+
import { getLocaleManager } from '@/components/Locale'
394398
395399
const initForm = (config) => {
396400
const {
@@ -446,8 +450,9 @@
446450
},
447451
data () {
448452
const { locale } = this.$store.state.preference.config
449-
const form = initForm(this.$store.state.preference.config)
450-
const formOriginal = cloneDeep(form)
453+
const formOriginal = initForm(this.$store.state.preference.config)
454+
let form = {}
455+
form = initForm(extend(form, formOriginal, changedConfig.advanced))
451456
452457
return {
453458
form,
@@ -594,6 +599,10 @@
594599
.then((config) => {
595600
this.form = initForm(config)
596601
this.formOriginal = cloneDeep(this.form)
602+
if (changedConfig.basic.theme !== undefined) {
603+
this.$electron.ipcRenderer.send('command',
604+
'application:change-theme', changedConfig.basic.theme)
605+
}
597606
})
598607
},
599608
submitForm (formName) {
@@ -603,15 +612,19 @@
603612
return false
604613
}
605614
606-
const changed = diffConfig(this.formOriginal, this.form)
607615
const data = {
608-
...changed,
609-
protocols: {
610-
...this.form.protocols
611-
}
616+
...diffConfig(this.formOriginal, this.form),
617+
...changedConfig.basic
618+
}
619+
620+
const { btAutoDownloadContent, runMode, openAtLogin, autoHideWindow, btTracker, noProxy } = data
621+
622+
if ('btAutoDownloadContent' in data) {
623+
data.pauseMetadata = !btAutoDownloadContent
624+
data.followMetalink = btAutoDownloadContent
625+
data.followTorrent = btAutoDownloadContent
612626
}
613627
614-
const { btTracker, noProxy } = changed
615628
if (btTracker) {
616629
data.btTracker = reduceTrackerString(convertLineToComma(btTracker))
617630
}
@@ -632,7 +645,28 @@
632645
this.$msg.success(this.$t('preferences.save-fail-message'))
633646
})
634647
648+
changedConfig.basic = {}
649+
changedConfig.advanced = {}
650+
635651
if (this.isRenderer) {
652+
this.$electron.ipcRenderer.send('command',
653+
'application:open-at-login', openAtLogin)
654+
655+
if ('runMode' in data) {
656+
this.$electron.ipcRenderer.send('command',
657+
'application:toggle-dock', runMode === APP_RUN_MODE.STANDARD)
658+
}
659+
660+
if ('autoHideWindow' in data) {
661+
this.$electron.ipcRenderer.send('command',
662+
'application:auto-hide-window', autoHideWindow)
663+
}
664+
665+
if (checkIsNeedRestart(data)) {
666+
this.$electron.ipcRenderer.send('command',
667+
'application:relaunch')
668+
}
669+
636670
this.$electron.ipcRenderer.send('command',
637671
'application:setup-protocols-client', data.protocols)
638672
@@ -645,6 +679,41 @@
645679
resetForm (formName) {
646680
this.syncFormConfig()
647681
}
682+
},
683+
beforeRouteLeave (to, from, next) {
684+
changedConfig.advanced = diffConfig(this.formOriginal, this.form)
685+
if (to.path === '/preference/basic') {
686+
next()
687+
} else {
688+
if (isEmpty(changedConfig.basic) && isEmpty(changedConfig.advanced)) {
689+
next()
690+
} else {
691+
dialog.showMessageBox({
692+
type: 'warning',
693+
title: this.$t('preferences.not-saved'),
694+
message: this.$t('preferences.not-saved-confirm'),
695+
buttons: [this.$t('app.yes'), this.$t('app.no')],
696+
cancelId: 1
697+
}).then(({ response }) => {
698+
if (response === 0) {
699+
if (changedConfig.basic.theme !== undefined) {
700+
this.$electron.ipcRenderer.send('command',
701+
'application:change-theme', backupConfig.theme)
702+
}
703+
if (changedConfig.basic.locale !== undefined) {
704+
const lng = getLanguage(backupConfig.locale)
705+
getLocaleManager().changeLanguage(lng)
706+
this.$electron.ipcRenderer.send('command',
707+
'application:change-locale', lng)
708+
}
709+
changedConfig.basic = {}
710+
changedConfig.advanced = {}
711+
backupConfig.theme = undefined
712+
next()
713+
}
714+
})
715+
}
716+
}
648717
}
649718
}
650719
</script>

src/renderer/components/Preference/Basic.vue

+78-9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<mo-theme-switcher
2626
v-model="form.theme"
2727
@change="handleThemeChange"
28+
ref="themeSwitcher"
2829
/>
2930
</el-col>
3031
<el-col v-if="showHideAppMenuOption" class="form-item-sub" :span="16">
@@ -256,19 +257,24 @@
256257

257258
<script>
258259
import is from 'electron-is'
260+
import { dialog } from '@electron/remote'
259261
import { mapState } from 'vuex'
260-
import { cloneDeep } from 'lodash'
262+
import { cloneDeep, extend, isEmpty } from 'lodash'
261263
import SubnavSwitcher from '@/components/Subnav/SubnavSwitcher'
262264
import SelectDirectory from '@/components/Native/SelectDirectory'
263265
import ThemeSwitcher from '@/components/Preference/ThemeSwitcher'
264266
import { availableLanguages, getLanguage } from '@shared/locales'
265267
import { getLocaleManager } from '@/components/Locale'
266268
import {
269+
backupConfig,
267270
calcFormLabelWidth,
271+
changedConfig,
268272
checkIsNeedRestart,
273+
convertLineToComma,
269274
diffConfig
270275
} from '@shared/utils'
271276
import { APP_RUN_MODE } from '@shared/constants'
277+
import { reduceTrackerString } from '@shared/utils/tracker'
272278
273279
const initForm = (config) => {
274280
const {
@@ -338,8 +344,16 @@
338344
},
339345
data () {
340346
const { locale } = this.$store.state.preference.config
341-
const form = initForm(this.$store.state.preference.config)
342-
const formOriginal = cloneDeep(form)
347+
const formOriginal = initForm(this.$store.state.preference.config)
348+
let form = {}
349+
form = initForm(extend(form, formOriginal, changedConfig.basic))
350+
351+
if (backupConfig.theme === undefined) {
352+
backupConfig.theme = formOriginal.theme
353+
} else {
354+
formOriginal.theme = backupConfig.theme
355+
}
356+
backupConfig.locale = formOriginal.locale
343357
344358
return {
345359
form,
@@ -480,16 +494,27 @@
480494
return false
481495
}
482496
483-
const { btAutoDownloadContent, runMode, openAtLogin, autoHideWindow } = this.form
484-
const changed = diffConfig(this.formOriginal, this.form)
485497
const data = {
486-
...changed
498+
...diffConfig(this.formOriginal, this.form),
499+
...changedConfig.advanced
487500
}
488-
if ('btAutoDownloadContent' in changed) {
501+
502+
const { btAutoDownloadContent, runMode, openAtLogin, autoHideWindow, btTracker, noProxy } = data
503+
504+
if ('btAutoDownloadContent' in data) {
489505
data.pauseMetadata = !btAutoDownloadContent
490506
data.followMetalink = btAutoDownloadContent
491507
data.followTorrent = btAutoDownloadContent
492508
}
509+
510+
if (btTracker) {
511+
data.btTracker = reduceTrackerString(convertLineToComma(btTracker))
512+
}
513+
514+
if (noProxy) {
515+
data.noProxy = convertLineToComma(noProxy)
516+
}
517+
493518
console.log('[Motrix] preference changed data:', data)
494519
495520
this.$store.dispatch('preference/save', data)
@@ -502,16 +527,19 @@
502527
this.$msg.success(this.$t('preferences.save-fail-message'))
503528
})
504529
530+
changedConfig.basic = {}
531+
changedConfig.advanced = {}
532+
505533
if (this.isRenderer) {
506534
this.$electron.ipcRenderer.send('command',
507535
'application:open-at-login', openAtLogin)
508536
509-
if ('runMode' in changed) {
537+
if ('runMode' in data) {
510538
this.$electron.ipcRenderer.send('command',
511539
'application:toggle-dock', runMode === APP_RUN_MODE.STANDARD)
512540
}
513541
514-
if ('autoHideWindow' in changed) {
542+
if ('autoHideWindow' in data) {
515543
this.$electron.ipcRenderer.send('command',
516544
'application:auto-hide-window', autoHideWindow)
517545
}
@@ -520,12 +548,53 @@
520548
this.$electron.ipcRenderer.send('command',
521549
'application:relaunch')
522550
}
551+
552+
this.$electron.ipcRenderer.send('command',
553+
'application:setup-protocols-client', data.protocols)
554+
555+
if (checkIsNeedRestart(data)) {
556+
this.$electron.ipcRenderer.send('command', 'application:relaunch')
557+
}
523558
}
524559
})
525560
},
526561
resetForm (formName) {
562+
this.$refs.themeSwitcher.currentValue = backupConfig.theme
563+
this.handleLocaleChange(this.formOriginal.locale)
527564
this.syncFormConfig()
528565
}
566+
},
567+
beforeRouteLeave (to, from, next) {
568+
changedConfig.basic = diffConfig(this.formOriginal, this.form)
569+
if (to.path === '/preference/advanced') {
570+
next()
571+
} else {
572+
if (isEmpty(changedConfig.basic) && isEmpty(changedConfig.advanced)) {
573+
next()
574+
} else {
575+
dialog.showMessageBox({
576+
type: 'warning',
577+
title: this.$t('preferences.not-saved'),
578+
message: this.$t('preferences.not-saved-confirm'),
579+
buttons: [this.$t('app.yes'), this.$t('app.no')],
580+
cancelId: 1
581+
}).then(({ response }) => {
582+
if (response === 0) {
583+
if (changedConfig.basic.theme !== undefined) {
584+
this.$electron.ipcRenderer.send('command',
585+
'application:change-theme', backupConfig.theme)
586+
}
587+
if (changedConfig.basic.locale !== undefined) {
588+
this.handleLocaleChange(this.formOriginal.locale)
589+
}
590+
changedConfig.basic = {}
591+
changedConfig.advanced = {}
592+
backupConfig.theme = undefined
593+
next()
594+
}
595+
})
596+
}
597+
}
529598
}
530599
}
531600
</script>

src/shared/locales/ar/preferences.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,7 @@ export default {
7878
'baidu-exporter-help': 'اضغط هنا لبدء الاستخدام',
7979
'auto-update': 'التحديث التلقائي',
8080
'auto-check-update': 'تحقق تلقائيًا من التحديث',
81-
'last-check-update-time': 'آخر مرة تم التحقق من وجود تحديثات'
81+
'last-check-update-time': 'آخر مرة تم التحقق من وجود تحديثات',
82+
'not-saved': 'التفضيلات غير محفوظة',
83+
'not-saved-confirm': 'ستفقد التفضيلات التي تم تغييرها ، هل أنت متأكد من المغادرة؟'
8284
}

src/shared/locales/bg/preferences.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,7 @@ export default {
7676
'baidu-exporter-help': 'Кликнете тук, за да използвате',
7777
'auto-update':'автоматично обновяване',
7878
'auto-check-update':'автоматична проверка на актуализациите',
79-
'last-check-update-time': 'последната актуализация е проверена'
79+
'last-check-update-time': 'последната актуализация е проверена',
80+
'not-saved': 'Предпочитанията не са запазени',
81+
'not-saved-confirm': 'Променените предпочитания ще бъдат загубени, сигурни ли сте, че ще напуснете?'
8082
}

src/shared/locales/ca/preferences.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,7 @@ export default {
7676
'baidu-exporter-help': 'Fes click aquí per veure l\'ús',
7777
'auto-update': 'Actualitzar automàticament',
7878
'auto-check-update': 'Revisar actualitzacions automàticament',
79-
'last-check-update-time': 'Última revisió d\'actualitzacions'
79+
'last-check-update-time': 'Última revisió d\'actualitzacions',
80+
'not-saved': 'Preferències no desades',
81+
'not-saved-confirm': 'Les preferències modificades es perdran, esteu segur que marxareu?'
8082
}

src/shared/locales/de/preferences.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,7 @@ export default {
7676
'baidu-exporter-help': 'mehr über die Verwendung zu erfahren',
7777
'auto-update': 'Auto-Update',
7878
'auto-check-update': 'Automatische Updates überprüfen',
79-
'last-check-update-time': 'letzte kontrolle update - zeit'
79+
'last-check-update-time': 'letzte kontrolle update - zeit',
80+
'not-saved': 'Einstellungen nicht gespeichert',
81+
'not-saved-confirm': 'Die geänderten Einstellungen gehen verloren. Möchten Sie wirklich gehen?'
8082
}

src/shared/locales/el/preferences.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,7 @@ export default {
7676
'baidu-exporter-help': 'Κάντε κλικ εδώ για χρήση',
7777
'auto-update': 'Αυτόματη ενημέρωση',
7878
'auto-check-update': 'Αυτόματος έλεγχος για ενημερώσεις',
79-
'last-check-update-time': 'Τελευταία φορά που έγινε έλεγχος για Ενημερώσεις'
79+
'last-check-update-time': 'Τελευταία φορά που έγινε έλεγχος για Ενημερώσεις',
80+
'not-saved': 'Οι προτιμήσεις δεν αποθηκεύτηκαν',
81+
'not-saved-confirm': 'Οι αλλαγμένες προτιμήσεις θα χαθούν, είστε σίγουροι ότι θα φύγετε;'
8082
}

src/shared/locales/en-US/preferences.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,7 @@ export default {
7878
'baidu-exporter-help': 'Click here for usage',
7979
'auto-update': 'Auto Update',
8080
'auto-check-update': 'Automatically check for update',
81-
'last-check-update-time': 'Last Time Checking for Update'
81+
'last-check-update-time': 'Last Time Checking for Update',
82+
'not-saved': 'Preferences not saved',
83+
'not-saved-confirm': 'The modified preferences will be lost, are you sure to leave?'
8284
}

src/shared/locales/es/preferences.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,7 @@ export default {
7676
'baidu-exporter-help': 'Presiona aqui para ver el uso',
7777
'auto-update': 'Auto-actualizar',
7878
'auto-check-update': 'Revisar automáticamente por actualizaciones',
79-
'last-check-update-time': 'Última revisión de actualizaciones'
79+
'last-check-update-time': 'Última revisión de actualizaciones',
80+
'not-saved': 'Preferencias no guardadas',
81+
'not-saved-confirm': 'Las preferencias cambiadas se perderán, ¿está seguro de irse?'
8082
}

src/shared/locales/fa/preferences.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,7 @@ export default {
7878
'baidu-exporter-help': 'برای استفاده اینجا کلیک کنید',
7979
'auto-update': 'به‌روز رسانی خودکار',
8080
'auto-check-update': 'به صورت خودکار برای به‌روز رسانی بررسی کن',
81-
'last-check-update-time': 'آخرین زمان بررسی برای به‌روز رسانی'
81+
'last-check-update-time': 'آخرین زمان بررسی برای به‌روز رسانی',
82+
'not-saved': 'تنظیمات برگزیده ذخیره نشد',
83+
'not-saved-confirm': 'تنظیمات برگزیده تغییر یافته از بین خواهند رفت، آیا مطمئن هستید که می روید؟'
8284
}

src/shared/locales/fr/preferences.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,7 @@ export default {
7676
'baidu-exporter-help': 'Cliquez ici pour voir l\'utilisation',
7777
'auto-update': 'Mettre à jour',
7878
'auto-check-update': 'Mise à jour automatique',
79-
'last-check-update-time': 'dernier contrôle la mise à jour du temps'
79+
'last-check-update-time': 'dernier contrôle la mise à jour du temps',
80+
'not-saved': 'Préférences non enregistrées',
81+
'not-saved-confirm': 'Les préférences modifiées seront perdues, êtes-vous sûr de partir ?'
8082
}

0 commit comments

Comments
 (0)