Skip to content

Commit

Permalink
✨feat(button): add a new inoperable state for the like button and sho…
Browse files Browse the repository at this point in the history
…w operation result information #330
  • Loading branch information
YXL76 committed Mar 6, 2021
1 parent d2eded3 commit 9470c5d
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 42 deletions.
1 change: 1 addition & 0 deletions i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"network": "Network error"
},
"fail": {
"addToPlaylist": "Failed to add to playlist",
"signIn": "Sign in failed"
},
"hint": {
Expand Down
1 change: 1 addition & 0 deletions i18n/zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"network": "网络错误"
},
"fail": {
"addToPlaylist": "添加到歌单失败",
"signIn": "登录失败"
},
"hint": {
Expand Down
1 change: 1 addition & 0 deletions i18n/zh-tw.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"network": "網絡錯誤"
},
"fail": {
"addToPlaylist": "添加到歌單失敗",
"signIn": "登入失敗"
},
"hint": {
Expand Down
30 changes: 16 additions & 14 deletions src/activate/command.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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);
})
);

Expand Down
4 changes: 2 additions & 2 deletions src/api/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
7 changes: 1 addition & 6 deletions src/api/playlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default __non_webpack_require__(
network: string;
};
fail: {
addToPlaylist: string;
signIn: string;
};
hint: {
Expand Down
29 changes: 21 additions & 8 deletions src/manager/button.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -46,7 +46,7 @@ export class ButtonManager {
"$(play)",
"$(chevron-right)",
"$(sync-ignored)",
"$(star)",
"$(stop)",
"$(unmute)",
"$(flame)",
this.showLyric ? "$(text-size)" : i18n.word.disabled,
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 16 additions & 4 deletions src/util/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,28 @@ 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;

private static login_ = false;

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);
Expand All @@ -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;
}
}
}
Expand Down
23 changes: 15 additions & 8 deletions src/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
} from "../constant";
import {
ButtonAction,
LikeState,
MusicCache,
PersonalFm,
Player,
Expand Down Expand Up @@ -86,6 +87,16 @@ export async function downloadMusic(
return;
}

export async function likeMusic(id: number, like: boolean): Promise<void> {
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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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<UserDetail[]>,
func: (_: number, __: number, ___: number) => Promise<UserDetail[]>,
offset: number,
id: number
): Promise<InputStep> {
Expand Down

0 comments on commit 9470c5d

Please sign in to comment.