Skip to content

Commit

Permalink
add: like tracks in playlist page
Browse files Browse the repository at this point in the history
  • Loading branch information
levovix0 committed Feb 7, 2023
1 parent 41cfede commit 232645c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
6 changes: 6 additions & 0 deletions qml/pages/PlaylistPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@ DPage {
title: objTitle
author: objAuthor
duration: objDuration
liked: objLiked

onPlay: {
if (PlaylistView.ownerId != 0) AudioPlayer.playYmPlaylist(PlaylistView.id, PlaylistView.ownerId, objI)
else AudioPlayer.playDmPlaylist(PlaylistView.id, objI)
}

onToggleLiked: {
AudioPlayer.setTrackLiked(objKind, objId, !liked)
liked = !liked
}
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ proc `liked=`*(this: Track, v: bool) {.async.} =
if v: currentUser().await.like(t.yandex).await
else: currentUser().await.unlike(t.yandex).await

of TrackKind.yandexIdOnly:
if v: currentUser().await.like(this.yandexIdOnly).await
else: currentUser().await.unlike(this.yandexIdOnly).await

of TrackKind.user:
this.user.metadata.liked = v
writeTrackMetadata(this.user.file, this.user.metadata, writeCover=false)
Expand Down Expand Up @@ -371,6 +375,19 @@ proc tracks*(playlist: Playlist): ptr seq[Track] =
of PlaylistKind.temporary:
playlist.temporary.tracks.addr

proc liked*(playlist: Playlist): Future[seq[bool]] {.async.} =
case playlist.kind
of yandex:
let liked = currentUser().await.likedTracks.await
for x in playlist.yandex.tracks:
result.add x.id in liked
of user:
for x in playlist.user.tracks:
result.add x.liked.await
of temporary:
for x in playlist.temporary.tracks:
result.add x.liked.await


# todo: refactor all above to do not specifing enum type, like as in proc below
proc toRadio*(track: Track): Future[Radio] {.async.} =
Expand Down
7 changes: 7 additions & 0 deletions src/gui/audio.nim
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,13 @@ qobject AudioPlayer:
proc playRadioFromYmTrack(id: int) =
asyncCheck: doAsync:
await play id.yandexTrack.toRadio.await

proc setTrackLiked(kind: string, id: int, v: bool) =
case kind
of "yandex", "yandexFromFile", "yandexIdOnly":
asyncCheck: doAsync:
(id.yandexTrack.liked = v).await
else: sendError(tr"Unimplemented", tr"toglleLiked() unknown track kind: {kind}")

proc `=new` =
notifyStateChanged &= proc() = this.stateChanged
Expand Down
19 changes: 19 additions & 0 deletions src/gui/playlist.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@ import ../utils, ../api, ../async
import ../yandexMusic except Track
import qt, configuration

{.experimental: "overloadableEnums".}

type PlaylistView = object
playlist: api.Playlist
covers: seq[tuple[data: string, fetched: bool]]
liked: tuple[data: seq[bool]; fetched: bool]


qmodel PlaylistView:
rows: self.playlist.tracks[].len
elem objTitle: self.playlist.tracks[i].title
elem objComment: self.playlist.tracks[i].comment
elem objAuthor: self.playlist.tracks[i].artists
elem objI: i
elem objId: self.playlist.tracks[i].id
elem objKind: $self.playlist.tracks[i].kind

elem objCover:
var instant = true
Expand All @@ -32,6 +38,19 @@ qmodel PlaylistView:
if ms.inHours != 0: ms.format("h:m:ss")
else: ms.format("m:ss")

elem objLiked:
if self.liked.fetched:
if i in 0..self.liked.data.high:
self.liked.data[i]
else:
false
else:
self.liked.fetched = true
asyncCheck: doAsync:
self.liked.data = self.playlist.liked.await
this.layoutChanged
false


property int id:
get:
Expand Down
8 changes: 8 additions & 0 deletions translations/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
"": "Исключение во время асинхронной операции"
}
},
"src/gui/audio.nim": {
"Unimplemented": {
"": "Нереализовано"
},
"toglleLiked() unknown track kind: {kind}": {
"": "toglleLiked() неизвестный тип трека: {kind}"
}
},
"src/gui/yandexMusicQmlModule.nim": {
"Downloads": {
"": "Загружено"
Expand Down

0 comments on commit 232645c

Please sign in to comment.