Skip to content

Commit

Permalink
✨ Improve updateActivity data
Browse files Browse the repository at this point in the history
  • Loading branch information
adlerluiz committed Aug 8, 2020
1 parent 6d897d6 commit 083cb2c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 55 deletions.
95 changes: 49 additions & 46 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ let mainWindow,
customCSSPageKey,
lastTrackId,
lastTrackProgress,
lastIsPaused,
lastSeekbarCurrentPosition,
doublePressPlayPause,
updateTrackInfoTimeout,
activityIsPaused,
activityLikeStatus,
windowsMediaProvider

Expand Down Expand Up @@ -366,6 +367,7 @@ function createWindow() {
var trackInfo = infoPlayerProvider.getTrackInfo()

var progress = playerInfo.statePercent
var seekbarCurrentPosition = playerInfo.seekbarCurrentPosition
var trackId = trackInfo.id
var title = trackInfo.title
var author = trackInfo.author
Expand All @@ -375,32 +377,9 @@ function createWindow() {
var nowPlaying = `${title} - ${author}`

if (title && author) {
discordRPC.setActivity(getAll())
rainmeterNowPlaying.setActivity(getAll())
mprisProvider.setActivity(getAll())

if (
playerInfo.isPaused != activityIsPaused ||
playerInfo.likeStatus != activityLikeStatus
) {
mediaControl.createThumbar(
mainWindow,
infoPlayerProvider.getAllInfo()
)

activityIsPaused = playerInfo.isPaused
activityLikeStatus = playerInfo.likeStatus

if (
isWindows() &&
os.release().startsWith('10.') &&
settingsProvider.get('settings-windows10-media-service') &&
windowsMediaProvider != undefined
) {
windowsMediaProvider.setPlaybackStatus(playerInfo.isPaused)
}
}

mediaControl.setProgress(
mainWindow,
settingsProvider.get('settings-enable-taskbar-progressbar')
Expand Down Expand Up @@ -436,22 +415,15 @@ function createWindow() {
}
}

// Experimental
/*if (isWindows() && view.webContents.getURL().indexOf('v=') != -1) {
mainWindow.setThumbnailClip({
x: 230,
y: 150,
width: 676,
height: 676,
})
} else {
mainWindow.setThumbnailClip({
x: 0,
y: 0,
width: mainWindow.getSize()[0],
height: mainWindow.getSize()[1],
})
}*/
/**
* Update only when change seekbar
*/
if (
lastSeekbarCurrentPosition - seekbarCurrentPosition > 2 ||
lastSeekbarCurrentPosition - seekbarCurrentPosition < -2
) {
discordRPC.setActivity(getAll())
}

/**
* Update only when change track
Expand Down Expand Up @@ -493,18 +465,49 @@ function createWindow() {
)
}

writeLog({ type: 'info', data: `Listen: ${title} - ${author}` })
discordRPC.setActivity(getAll())
}

/**
* Update only when change state play/pause
*/
if (lastIsPaused != playerInfo.isPaused) {
lastIsPaused = playerInfo.isPaused

discordRPC.setActivity(getAll())

if (!isMac() && !settingsProvider.get('settings-shiny-tray')) {
if (playerInfo.isPaused) {
tray.updateTrayIcon(iconPause)
} else {
tray.updateTrayIcon(iconPlay)
}
tray.updateTrayIcon(
playerInfo.isPaused ? iconPause : iconPlay
)
}

writeLog({ type: 'info', data: `Listen: ${title} - ${author}` })
mediaControl.createThumbar(
mainWindow,
infoPlayerProvider.getAllInfo()
)

if (
isWindows() &&
os.release().startsWith('10.') &&
settingsProvider.get('settings-windows10-media-service') &&
windowsMediaProvider != undefined
) {
windowsMediaProvider.setPlaybackStatus(playerInfo.isPaused)
}
}

if (activityLikeStatus != playerInfo.likeStatus) {
mediaControl.createThumbar(
mainWindow,
infoPlayerProvider.getAllInfo()
)
activityLikeStatus = playerInfo.likeStatus
}

lastTrackProgress = progress
lastSeekbarCurrentPosition = seekbarCurrentPosition
}
}

Expand Down
11 changes: 2 additions & 9 deletions src/providers/discordRpcProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const RPC = require('discord-rpc')
const settingsProvider = require('./settingsProvider')

var client
var _isStarted, _lastTrackId, _lastState
var _isStarted

function isStarted() {
return _isStarted
Expand Down Expand Up @@ -40,11 +40,7 @@ function stop() {
}

function setActivity(info) {
if (
isStarted() &&
info.track.title &&
(_lastTrackId != info.track.id || _lastState != info.player.isPaused)
) {
if (isStarted() && info.track.title) {
var now = Date.now()
var activity = {}
var discordSettings = settingsProvider.get('discord-presence-settings')
Expand Down Expand Up @@ -82,9 +78,6 @@ function setActivity(info) {
console.log(err)
})
}

_lastTrackId = info.track.id
_lastState = info.player.isPaused
}
}

Expand Down

0 comments on commit 083cb2c

Please sign in to comment.