Skip to content

Commit

Permalink
feat: download management supports opening download directories
Browse files Browse the repository at this point in the history
  • Loading branch information
Linkontoask committed Dec 26, 2020
1 parent 7250d05 commit f24a734
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 9 deletions.
14 changes: 12 additions & 2 deletions src/electron/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { App } from '../main'
import { userInfo } from 'os'
import { shell } from 'electron'
export { normalize, join } from 'path'

export const isElectron = () => {
// Renderer process
Expand Down Expand Up @@ -32,5 +34,13 @@ export const isElectron = () => {
}

export const getAppPath = () => {
return App.getAppPath() || __dirname || process.env.PORTABLE_EXECUTABLE_DIR
return __dirname || process.env.PORTABLE_EXECUTABLE_DIR
}

export const getUserOS = () => {
return userInfo()
}

export const openExplorer = (path: string) => {
shell.openExternal(path)
}
3 changes: 3 additions & 0 deletions src/pages/download/children/song.less
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
&-head {
display: flex;
align-items: center;
&--dir {
font-size: 12px;
}
> div {
margin-left: 10px;
}
Expand Down
35 changes: 31 additions & 4 deletions src/pages/download/children/song.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { defineComponent } from 'vue'
import { defineComponent, onMounted } from 'vue'
import { PlayAll } from '@/components-business/button'
import { Platform } from '@/config/build'
import { Table } from '@/components-business/table'
import { useDownloadModule } from '@/modules'
import { SongsDetail } from '@/interface'
import { SongsDetail, DownloadMutations } from '@/interface'
import { playMusic } from '@/shared/music-shared'
import './song.less'

Expand All @@ -12,7 +12,7 @@ const { VUE_APP_PLATFORM } = process.env
export const DownloadSong = defineComponent({
name: 'DownloadSong',
setup() {
const { useState } = useDownloadModule()
const { useState, useMutations } = useDownloadModule()
const state = useState()
const play = playMusic()
const handlePlayAll = () => {
Expand All @@ -23,11 +23,38 @@ export const DownloadSong = defineComponent({
play(song.id)
}

const handleOpenExplorer = () => {
import('@/electron/utils/index').then(v => {
v.openExplorer(state.downloadPath)
})
}

onMounted(() => {
if (VUE_APP_PLATFORM === Platform.ELECTRON) {
if (!state.downloadPath) {
import('@/electron/utils/index').then(v => {
const os = v.getUserOS()
useMutations(
DownloadMutations.SET_DOWNLOAD_PATH,
v.join(os.homedir + '/Music')
)
})
}
}
})

return () => (
<div class="download-song">
<div class="download-song-head">
<PlayAll onClick={handlePlayAll} />
{VUE_APP_PLATFORM === Platform.ELECTRON && <div>存储目录:</div>}
{VUE_APP_PLATFORM === Platform.ELECTRON && (
<div class="download-song-head--dir">
存储目录:{state.downloadPath}{' '}
<ve-button type="text" onClick={handleOpenExplorer}>
打开目录
</ve-button>
</div>
)}
</div>
<div class="download-song-body">
<Table
Expand Down
4 changes: 3 additions & 1 deletion src/pages/download/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ export interface Downloaded extends SongsDetail {

export interface DownloadState {
downloaded: Downloaded[]
downloadPath: string
}

export const enum DownloadActions {
DOWNLOAD_MUSIC = 'DOWNLOAD_MUSIC'
}

export const enum DownloadMutations {
SET_DOWNLOAD_MUSIC = 'SET_DOWNLOAD_MUSIC'
SET_DOWNLOAD_MUSIC = 'SET_DOWNLOAD_MUSIC',
SET_DOWNLOAD_PATH = 'SET_DOWNLOAD_PATH'
}
3 changes: 3 additions & 0 deletions src/pages/download/sage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@ export const mutations: MutationTree<DownloadState> = {
[DownloadMutations.SET_DOWNLOAD_MUSIC](state, song: Downloaded) {
song.dlt = Date.now()
state.downloaded.push(song)
},
[DownloadMutations.SET_DOWNLOAD_PATH](state, path: string) {
state.downloadPath = path
}
}
3 changes: 2 additions & 1 deletion src/pages/download/state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DownloadState } from '@/interface'

export const state: DownloadState = {
downloaded: []
downloaded: [],
downloadPath: ''
}
2 changes: 1 addition & 1 deletion src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ plugins.push(
'Footer.musciHistory',
'Footer.volume',
'Footer.music',
'Download.downloaded'
'Download'
]
})
)
Expand Down

0 comments on commit f24a734

Please sign in to comment.