Skip to content

A full-featured download manager.

refactor: add task type constant

Dr_rOot651f28e7e8ae
diff --git a/src/main/core/ProtocolManager.js b/src/main/core/ProtocolManager.js
index 58a9a44c..89654042 100644
--- a/src/main/core/ProtocolManager.js
+++ b/src/main/core/ProtocolManager.js
@@ -3,6 +3,7 @@ import { EventEmitter } from 'events'
import { app } from 'electron'
import logger from './Logger'
import protocolMap from '../configs/protocol'
+import { ADD_TASK_TYPE } from '@shared/constants'
export default class ProtocolManager extends EventEmitter {
constructor (options = {}) {
@@ -61,7 +62,7 @@ export default class ProtocolManager extends EventEmitter {
return
}
- global.application.sendCommandToAll('application:new-task', 'uri', url)
+ global.application.sendCommandToAll('application:new-task', ADD_TASK_TYPE.URI, url)
}
handleMoProtocol (url) {
diff --git a/src/renderer/components/Aside/Index.vue b/src/renderer/components/Aside/Index.vue
index cb3eca0e..0b7cb6cc 100644
--- a/src/renderer/components/Aside/Index.vue
+++ b/src/renderer/components/Aside/Index.vue
@@ -25,6 +25,7 @@
<script>
import is from 'electron-is'
import { mapState } from 'vuex'
+ import { ADD_TASK_TYPE } from '@shared/constants'
import LogoMini from '@/components/Logo/LogoMini'
import '@/components/Icons/menu-task'
import '@/components/Icons/menu-add'
@@ -45,7 +46,7 @@
}
},
methods: {
- showAddTask (taskType = 'uri') {
+ showAddTask (taskType = ADD_TASK_TYPE.URI) {
this.$store.dispatch('app/showAddTaskDialog', taskType)
},
showAboutPanel () {
diff --git a/src/renderer/components/Command/index.js b/src/renderer/components/Command/index.js
index 1bfbe2b1..1a18bc2d 100644
--- a/src/renderer/components/Command/index.js
+++ b/src/renderer/components/Command/index.js
@@ -5,6 +5,7 @@ import { Message } from 'element-ui'
import { getLocaleManager } from '@/components/Locale'
import { base64StringToBlob } from 'blob-util'
import { buildFileList } from '@shared/utils'
+import { ADD_TASK_TYPE } from '@shared/constants'
const commands = new CommandManager()
const i18n = getLocaleManager().getI18n()
@@ -21,22 +22,22 @@ function showAboutPanel () {
store.dispatch('app/showAboutPanel')
}
-function showAddTask (taskType = 'uri', task = '') {
- if (taskType === 'uri' && task) {
+function showAddTask (taskType = ADD_TASK_TYPE.URI, task = '') {
+ if (taskType === ADD_TASK_TYPE.URI && task) {
store.dispatch('app/updateAddTaskUrl', task)
}
store.dispatch('app/showAddTaskDialog', taskType)
}
function showAddBtTask () {
- store.dispatch('app/showAddTaskDialog', 'torrent')
+ store.dispatch('app/showAddTaskDialog', ADD_TASK_TYPE.TORRENT)
}
function showAddBtTaskWithFile (fileName, base64Data = '') {
const blob = base64StringToBlob(base64Data, 'application/x-bittorrent')
const file = new File([blob], fileName, { type: 'application/x-bittorrent' })
const fileList = buildFileList(file)
- store.dispatch('app/showAddTaskDialog', 'torrent')
+ store.dispatch('app/showAddTaskDialog', ADD_TASK_TYPE.TORRENT)
setTimeout(() => {
store.dispatch('app/addTaskAddTorrents', { fileList })
}, 200)
diff --git a/src/renderer/components/Dragger/Index.vue b/src/renderer/components/Dragger/Index.vue
index 88dd7692..58e67623 100644
--- a/src/renderer/components/Dragger/Index.vue
+++ b/src/renderer/components/Dragger/Index.vue
@@ -3,6 +3,8 @@
</template>
<script>
+import { ADD_TASK_TYPE } from '@shared/constants'
+
export default {
name: 'mo-dragger',
mounted () {
@@ -10,7 +12,7 @@ export default {
let count = 0
this.onDragEnter = (ev) => {
if (count === 0) {
- this.$store.dispatch('app/showAddTaskDialog', 'torrent')
+ this.$store.dispatch('app/showAddTaskDialog', ADD_TASK_TYPE.TORRENT)
}
count++
}
diff --git a/src/renderer/components/Task/AddTask.vue b/src/renderer/components/Task/AddTask.vue
index 1b737336..635957ef 100644
--- a/src/renderer/components/Task/AddTask.vue
+++ b/src/renderer/components/Task/AddTask.vue
@@ -176,7 +176,7 @@ import { isEmpty } from 'lodash'
import SelectDirectory from '@/components/Native/SelectDirectory'
import SelectTorrent from '@/components/Task/SelectTorrent'
import { prettifyDir } from '@/components/Native/utils'
-import { NONE_SELECTED_FILES, SELECTED_ALL_FILES } from '@shared/constants'
+import { ADD_TASK_TYPE, NONE_SELECTED_FILES, SELECTED_ALL_FILES } from '@shared/constants'
import { detectResource, splitTaskLinks } from '@shared/utils'
import { buildOuts } from '@shared/utils/rename'
import '@/components/Icons/inbox'
@@ -214,7 +214,7 @@ export default {
},
type: {
type: String,
- default: 'uri'
+ default: ADD_TASK_TYPE.URI
}
},
data () {
@@ -241,11 +241,11 @@ export default {
},
watch: {
taskType: function (current, previous) {
- if (this.visible && previous === 'uri') {
+ if (this.visible && previous === ADD_TASK_TYPE.URI) {
return
}
- if (current === 'uri') {
+ if (current === ADD_TASK_TYPE.URI) {
setTimeout(() => {
this.$refs.uri && this.$refs.uri.focus()
}, 50)
@@ -257,7 +257,7 @@ export default {
isMas: is.mas,
handleOpen () {
this.form = initialForm(this.$store.state)
- if (this.taskType === 'uri') {
+ if (this.taskType === ADD_TASK_TYPE.URI) {
this.autofillResourceLink()
setTimeout(() => {
this.$refs.uri && this.$refs.uri.focus()
@@ -353,7 +353,7 @@ export default {
result.out = out
}
- if (type === 'torrent') {
+ if (type === ADD_TASK_TYPE.TORRENT) {
if (
selectFile !== SELECTED_ALL_FILES &&
selectFile !== NONE_SELECTED_FILES
@@ -380,7 +380,7 @@ export default {
uris = splitTaskLinks(uris)
const outs = buildOuts(uris, out)
- const options = this.buildOption('uri', form)
+ const options = this.buildOption(ADD_TASK_TYPE.URI, form)
const result = {
uris,
outs,
@@ -393,7 +393,7 @@ export default {
if (isEmpty(torrent)) {
throw new Error(this.$t('task.new-task-torrent-required'))
}
- const options = this.buildOption('torrent', form)
+ const options = this.buildOption(ADD_TASK_TYPE.TORRENT, form)
const result = {
torrent,
options
@@ -402,12 +402,12 @@ export default {
},
addTask (type, form) {
let payload = null
- if (type === 'uri') {
+ if (type === ADD_TASK_TYPE.URI) {
payload = this.buildUriPayload(form)
this.$store.dispatch('task/addUri', payload).catch(err => {
this.$msg.error(err.message)
})
- } else if (type === 'torrent') {
+ } else if (type === ADD_TASK_TYPE.TORRENT) {
payload = this.buildTorrentPayload(form)
this.$store.dispatch('task/addTorrent', payload).catch(err => {
this.$msg.error(err.message)
diff --git a/src/renderer/components/Task/TaskActions.vue b/src/renderer/components/Task/TaskActions.vue
index 25758eb7..7799e054 100644
--- a/src/renderer/components/Task/TaskActions.vue
+++ b/src/renderer/components/Task/TaskActions.vue
@@ -41,7 +41,8 @@
<script>
import { mapState } from 'vuex'
- import TaskProgress from './TaskProgress'
+ import { ADD_TASK_TYPE } from '@shared/constants'
+ import { bytesToSize, timeFormat } from '@shared/utils'
import '@/components/Icons/menu-add'
import '@/components/Icons/refresh'
import '@/components/Icons/task-start-line'
@@ -49,15 +50,10 @@
import '@/components/Icons/delete'
import '@/components/Icons/purge'
import '@/components/Icons/more'
- import {
- bytesToSize,
- timeFormat
- } from '@shared/utils'
export default {
name: 'mo-task-actions',
components: {
- [TaskProgress.name]: TaskProgress
},
props: ['task'],
data: function () {
@@ -121,7 +117,7 @@
})
},
onAddClick: function () {
- this.$store.dispatch('app/showAddTaskDialog', 'uri')
+ this.$store.dispatch('app/showAddTaskDialog', ADD_TASK_TYPE.URI)
}
}
}
diff --git a/src/renderer/components/Task/TaskItemActions.vue b/src/renderer/components/Task/TaskItemActions.vue
index f6cdf05c..6bfc8a08 100644
--- a/src/renderer/components/Task/TaskItemActions.vue
+++ b/src/renderer/components/Task/TaskItemActions.vue
@@ -38,6 +38,7 @@
<script>
import is from 'electron-is'
import * as clipboard from 'clipboard-polyfill'
+ import { ADD_TASK_TYPE } from '@shared/constants'
import '@/components/Icons/task-start-line'
import '@/components/Icons/task-pause-line'
import '@/components/Icons/task-stop-line'
@@ -221,7 +222,7 @@
this.$store.dispatch('app/updateAddTaskUrl', uri)
this.$store.dispatch('app/updateAddTaskOptions', newOptions)
- this.$store.dispatch('app/showAddTaskDialog', 'uri')
+ this.$store.dispatch('app/showAddTaskDialog', ADD_TASK_TYPE.URI)
},
onPauseClick () {
this.pauseTask()
diff --git a/src/renderer/pages/index/App.vue b/src/renderer/pages/index/App.vue
index 4be54efb..b7b5da38 100644
--- a/src/renderer/pages/index/App.vue
+++ b/src/renderer/pages/index/App.vue
@@ -14,11 +14,12 @@
<script>
import is from 'electron-is'
+ import { mapState } from 'vuex'
+ import { getLangDirection } from '@shared/utils'
+ import { APP_THEME } from '@shared/constants'
import TitleBar from '@/components/Native/TitleBar'
import EngineClient from '@/components/Native/EngineClient'
import Ipc from '@/components/Native/Ipc'
- import { mapState } from 'vuex'
- import { getLangDirection } from '@shared/utils'
export default {
name: 'Motrix',
@@ -41,7 +42,7 @@
dir: state => getLangDirection(state.config.locale)
}),
themeClass: function () {
- if (this.theme === 'auto') {
+ if (this.theme === APP_THEME.AUTO) {
return `theme-${this.systemTheme}`
} else {
return `theme-${this.theme}`
diff --git a/src/renderer/store/modules/app.js b/src/renderer/store/modules/app.js
index 98373aaa..665b5e60 100644
--- a/src/renderer/store/modules/app.js
+++ b/src/renderer/store/modules/app.js
@@ -1,3 +1,4 @@
+import { ADD_TASK_TYPE } from '@shared/constants'
import api from '@/api'
import { getSystemTheme } from '@/components/Native/utils'
@@ -23,7 +24,7 @@ const state = {
numStopped: 0
},
addTaskVisible: false,
- addTaskType: 'uri',
+ addTaskType: ADD_TASK_TYPE.URI,
addTaskUrl: '',
addTaskTorrents: [],
addTaskOptions: {}
diff --git a/src/shared/constants.js b/src/shared/constants.js
index 416f8e32..74e6275b 100644
--- a/src/shared/constants.js
+++ b/src/shared/constants.js
@@ -11,6 +11,11 @@ export const APP_RUN_MODE = {
MENU_BAR: 2
}
+export const ADD_TASK_TYPE = {
+ URI: 'uri',
+ TORRENT: 'torrent'
+}
+
export const ENGINE_RPC_HOST = '127.0.0.1'
export const ENGINE_RPC_PORT = 16800