From 9470c5dd5fca0d306e8ab38e090d8a865a71a1f0 Mon Sep 17 00:00:00 2001 From: YXL Date: Sat, 6 Mar 2021 19:27:40 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feat(button):=20add=20a=20new=20inoper?= =?UTF-8?q?able=20state=20for=20the=20like=20button=20and=20show=20operati?= =?UTF-8?q?on=20result=20information=20#330?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- i18n/en.json | 1 + i18n/zh-cn.json | 1 + i18n/zh-tw.json | 1 + src/activate/command.ts | 30 ++++++++++++++++-------------- src/api/helper.ts | 4 ++-- src/api/playlist.ts | 7 +------ src/i18n.ts | 1 + src/manager/button.ts | 29 +++++++++++++++++++++-------- src/util/state.ts | 20 ++++++++++++++++---- src/util/util.ts | 23 +++++++++++++++-------- 10 files changed, 75 insertions(+), 42 deletions(-) diff --git a/i18n/en.json b/i18n/en.json index 62756af51..0ce593051 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -5,6 +5,7 @@ "network": "Network error" }, "fail": { + "addToPlaylist": "Failed to add to playlist", "signIn": "Sign in failed" }, "hint": { diff --git a/i18n/zh-cn.json b/i18n/zh-cn.json index de9c9bbe2..59f9d10f0 100644 --- a/i18n/zh-cn.json +++ b/i18n/zh-cn.json @@ -5,6 +5,7 @@ "network": "网络错误" }, "fail": { + "addToPlaylist": "添加到歌单失败", "signIn": "登录失败" }, "hint": { diff --git a/i18n/zh-tw.json b/i18n/zh-tw.json index cb81b7935..4b7241ea8 100644 --- a/i18n/zh-tw.json +++ b/i18n/zh-tw.json @@ -5,6 +5,7 @@ "network": "網絡錯誤" }, "fail": { + "addToPlaylist": "添加到歌單失敗", "signIn": "登入失敗" }, "hint": { diff --git a/src/activate/command.ts b/src/activate/command.ts index 575ffda09..f8e8093ff 100644 --- a/src/activate/command.ts +++ b/src/activate/command.ts @@ -1,9 +1,16 @@ -import { AccountManager, ButtonManager } from "../manager"; -import { MultiStepInput, PersonalFm, Player, State, load } from "../util"; +import { + LikeState, + MultiStepInput, + PersonalFm, + Player, + State, + likeMusic, + load, +} from "../util"; import { QueueItemTreeItem, QueueProvider } from "../treeview"; +import { ButtonManager } from "../manager"; import type { ExtensionContext } from "vscode"; import { VOLUME_KEY } from "../constant"; -import { apiLike } from "../api"; import { commands } from "vscode"; import i18n from "../i18n"; @@ -44,17 +51,12 @@ export function initCommand(context: ExtensionContext): void { ); context.subscriptions.push( - commands.registerCommand("cloudmusic.like", async () => { - const islike = !State.like; - if (Player.treeitem instanceof QueueItemTreeItem) { - const { id } = Player.treeitem.item; - if (id && (await apiLike(id, islike))) { - State.like = islike; - islike - ? AccountManager.likelist.add(id) - : AccountManager.likelist.delete(id); - } - } + commands.registerCommand("cloudmusic.like", () => { + if ( + Player.treeitem instanceof QueueItemTreeItem && + State.like !== LikeState.none + ) + void likeMusic(Player.treeitem.valueOf, !State.like); }) ); diff --git a/src/api/helper.ts b/src/api/helper.ts index b0b9c6051..593c90449 100644 --- a/src/api/helper.ts +++ b/src/api/helper.ts @@ -193,7 +193,7 @@ export const resolveSongItem = (item: SongsItem): SongsItem => { export const resolveSongItemSt = (item: SongsItemSt): SongsItem => { const { name, id, dt, alia, ar, al, privilege } = item; - if (privilege.st < 0) unplayable.add(id); + if (privilege && privilege?.st < 0) unplayable.add(id); return { name, id, @@ -206,7 +206,7 @@ export const resolveSongItemSt = (item: SongsItemSt): SongsItem => { export const resolveAnotherSongItem = (item: AnotherSongItem): SongsItem => { const { name, id, duration, alias, artists, album, privilege } = item; - if (privilege.st < 0) unplayable.add(id); + if (privilege && privilege?.st < 0) unplayable.add(id); return { name, id, diff --git a/src/api/playlist.ts b/src/api/playlist.ts index 784ca897d..40bbc9259 100644 --- a/src/api/playlist.ts +++ b/src/api/playlist.ts @@ -180,12 +180,7 @@ export async function apiPlaylistTracks( try { await weapiRequest( "https://music.163.com/api/playlist/manipulate/tracks", - { - op, - pid, - trackIds: JSON.stringify(tracks), - imme: "true", - }, + { op, pid, trackIds: JSON.stringify(tracks), imme: "true" }, { os: "pc" } ); return true; diff --git a/src/i18n.ts b/src/i18n.ts index 7f5560d49..8300ed9cb 100644 --- a/src/i18n.ts +++ b/src/i18n.ts @@ -24,6 +24,7 @@ export default __non_webpack_require__( network: string; }; fail: { + addToPlaylist: string; signIn: string; }; hint: { diff --git a/src/manager/button.ts b/src/manager/button.ts index e71f6958b..b0732544b 100644 --- a/src/manager/button.ts +++ b/src/manager/button.ts @@ -1,6 +1,6 @@ import { BUTTON_KEY, LYRIC_KEY } from "../constant"; import type { ExtensionContext, StatusBarItem } from "vscode"; -import { MultiStepInput, State } from "../util"; +import { LikeState, MultiStepInput, State } from "../util"; import { StatusBarAlignment, window } from "vscode"; import i18n from "../i18n"; @@ -46,7 +46,7 @@ export class ButtonManager { "$(play)", "$(chevron-right)", "$(sync-ignored)", - "$(star)", + "$(stop)", "$(unmute)", "$(flame)", this.showLyric ? "$(text-size)" : i18n.word.disabled, @@ -58,7 +58,7 @@ export class ButtonManager { i18n.word.play, i18n.word.nextTrack, i18n.word.repeat, - i18n.word.like, + "", i18n.word.volume, i18n.word.song, i18n.word.lyric, @@ -149,11 +149,24 @@ export class ButtonManager { : "$(sync-ignored)"; } - static buttonLike(islike: boolean): void { - this.buttons[ButtonLabel.like].text = islike ? "$(star-full)" : "$(star)"; - this.buttons[ButtonLabel.like].tooltip = islike - ? i18n.word.dislike - : i18n.word.like; + static buttonLike(islike: LikeState): void { + let text!: string; + let tooltip!: string; + switch (islike) { + case LikeState.none: + text = "$(stop)"; + tooltip = ""; + break; + case LikeState.like: + text = "$(star-full)"; + tooltip = i18n.word.dislike; + break; + case LikeState.dislike: + text = "$(star)"; + tooltip = i18n.word.like; + } + this.buttons[ButtonLabel.like].text = text; + this.buttons[ButtonLabel.like].tooltip = tooltip; } static buttonVolume(level: number): void { diff --git a/src/util/state.ts b/src/util/state.ts index 873f29c90..5c1c1a61e 100644 --- a/src/util/state.ts +++ b/src/util/state.ts @@ -9,9 +9,16 @@ import { import { apiPersonalFm, apiRecommendSongs, apiUserLevel } from "../api"; import { commands } from "vscode"; import i18n from "../i18n"; +import { unplayable } from "../constant"; + +export const enum LikeState { + none = -1, + like = 1, + dislike = 0, +} export class State { - private static like_ = false; + private static like_ = LikeState.none; private static loading_ = false; @@ -19,11 +26,11 @@ export class State { private static playing_ = false; - static get like(): boolean { + static get like(): LikeState { return this.like_; } - static set like(newValue: boolean) { + static set like(newValue: LikeState) { if (newValue !== this.like_) { this.like_ = newValue; ButtonManager.buttonLike(newValue); @@ -44,7 +51,12 @@ export class State { else if (Player.treeitem) { const { name, ar, id } = Player.treeitem.item; ButtonManager.buttonSong(name, ar.map(({ name }) => name).join("/")); - this.like = AccountManager.likelist.has(id); + this.like = + Player.treeitem instanceof QueueItemTreeItem && !unplayable.has(id) + ? AccountManager.likelist.has(id) + ? LikeState.like + : LikeState.dislike + : LikeState.none; } } } diff --git a/src/util/util.ts b/src/util/util.ts index 7129318b3..3de8136bf 100644 --- a/src/util/util.ts +++ b/src/util/util.ts @@ -10,6 +10,7 @@ import type { } from "../constant"; import { ButtonAction, + LikeState, MusicCache, PersonalFm, Player, @@ -86,6 +87,16 @@ export async function downloadMusic( return; } +export async function likeMusic(id: number, like: boolean): Promise { + const word = like ? i18n.word.like : i18n.word.dislike; + if (await apiLike(id, like)) { + if (id === Player.treeitem?.valueOf) + State.like = like ? LikeState.like : LikeState.dislike; + like ? AccountManager.likelist.add(id) : AccountManager.likelist.delete(id); + void window.showInformationMessage(word); + } else void window.showErrorMessage(word); +} + export function stop(): void { Player.stop(); Player.treeitem = undefined; @@ -335,10 +346,7 @@ export async function pickSong( await Webview.comment(CommentType.song, id, name); break; case PickType.like: - if (await apiLike(id, true)) { - AccountManager.likelist.add(id); - if (id === Player.treeitem?.valueOf) State.like = true; - } + await likeMusic(id, true); break; case PickType.add: void commands.executeCommand( @@ -1043,9 +1051,9 @@ export async function pickAddToPlaylist( id, })), }); - if (await apiPlaylistTracks("add", pick.id, [id])) { + if (await apiPlaylistTracks("add", pick.id, [id])) PlaylistProvider.refresh(PlaylistProvider.playlists.get(pick.id)); - } + else void window.showErrorMessage(i18n.sentence.fail.addToPlaylist); return input.stay(); } @@ -1115,8 +1123,7 @@ const limit = 50; export async function pickUsers( input: MultiStepInput, step: number, - // eslint-disable-next-line @typescript-eslint/no-explicit-any - func: (...args: any[]) => Promise, + func: (_: number, __: number, ___: number) => Promise, offset: number, id: number ): Promise {